Skip to content

Commit 46d9b4f

Browse files
committed
Support Iterables in general in array_get
1 parent 17cdf6f commit 46d9b4f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/main/java/com/laytonsmith/core/functions/ArrayHandling.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,13 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
263263
throw e;
264264
}
265265
}
266-
} else if(args[0].isInstanceOf(ArrayAccess.TYPE)) {
266+
} else if(args[0].isInstanceOf(com.laytonsmith.core.natives.interfaces.Iterable.TYPE)) {
267267
com.laytonsmith.core.natives.interfaces.Iterable aa
268268
= (com.laytonsmith.core.natives.interfaces.Iterable) args[0];
269-
if(index instanceof CSlice) {
269+
if(index instanceof CSlice cSlice) {
270270
//It's a range
271-
int start = (int) ((CSlice) index).getStart();
272-
int finish = (int) ((CSlice) index).getFinish();
271+
int start = (int) cSlice.getStart();
272+
int finish = (int) cSlice.getFinish();
273273
try {
274274
//Convert negative indexes
275275
if(start < 0) {
@@ -288,7 +288,7 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
288288
return aa.get(index, t);
289289
}
290290
} else {
291-
throw new CRECastException("Argument 1 of " + this.getName() + " must be an array", t);
291+
throw new CRECastException("Argument 1 of " + this.getName() + " must be an Iterable object, such as an array.", t);
292292
}
293293
}
294294

src/main/java/com/laytonsmith/core/natives/interfaces/ArrayAccess.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ public interface ArrayAccess extends Booleanish {
7878

7979
/**
8080
* Returns a slice at the specified location. Should throw an exception if an element in the range doesn't exist.
81+
* The range is inclusive.
82+
*
83+
* @implNote In general, MethodScript supports undefined begin and end slices, as well as reverse slices. For
84+
* CArray type only, this is fully covered, but for other slice types, only negative slice numbers are converted,
85+
* and subclasses must handle both forward and reverse slices if applicable. A good way to handle that is as such:
86+
* <pre><code>
87+
* int start = Math.min(begin, end);
88+
* int stop = Math.max(begin, end);
89+
* int step = (begin &lt;= end) ? 1 : -1;
90+
* for (int i = start; i != stop; i += step) { ... }
91+
* </code></pre>
8192
*
8293
* @param begin
8394
* @param end

0 commit comments

Comments
 (0)