Skip to content

Commit 9d06be8

Browse files
committed
Update NativeArray for toReversed()
1 parent dd9c638 commit 9d06be8

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/org/mozilla/javascript/NativeArray.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ protected void fillConstructorProperties(IdFunctionObject ctor) {
154154
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_isArray, "isArray", 1);
155155
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_of, "of", 0);
156156
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_from, "from", 1);
157+
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_toReversed, "toReversed", 0);
157158
super.fillConstructorProperties(ctor);
158159
}
159160

@@ -303,6 +304,10 @@ protected void initPrototypeId(int id) {
303304
arity = 1;
304305
s = "flatMap";
305306
break;
307+
case Id_toReversed:
308+
arity = 0;
309+
s = "toReversed";
310+
break;
306311
default:
307312
throw new IllegalArgumentException(String.valueOf(id));
308313
}
@@ -341,7 +346,8 @@ public Object execIdCall(
341346
case ConstructorId_findIndex:
342347
case ConstructorId_reduce:
343348
case ConstructorId_reduceRight:
344-
{
349+
case ConstructorId_toReversed:
350+
{
345351
// this is a small trick; we will handle all the ConstructorId_xxx calls
346352
// the same way the object calls are processed
347353
// so we adjust the args, inverting the id and
@@ -448,6 +454,9 @@ public Object execIdCall(
448454
case Id_flatMap:
449455
return js_flatMap(cx, scope, thisObj, args);
450456

457+
case Id_toReversed:
458+
return js_toReversed(cx, scope, thisObj, args);
459+
451460
case Id_every:
452461
case Id_filter:
453462
case Id_forEach:
@@ -2252,6 +2261,19 @@ private static boolean js_isArray(Object o) {
22522261
return "Array".equals(((Scriptable) o).getClassName());
22532262
}
22542263

2264+
private static Scriptable js_toReversed(
2265+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
2266+
Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj);
2267+
int len = (getLengthProperty(cx, o) > Integer.MAX_VALUE) ? 0 :(int) getLengthProperty(cx, o);
2268+
2269+
Scriptable result = cx.newArray(scope, len);
2270+
2271+
for(int k = len-1; k >= 0; k--){
2272+
Object temp1 = getRawElem(o, k);
2273+
setRawElem(cx, result, k, temp1);
2274+
}
2275+
return result;
2276+
}
22552277
// methods to implement java.util.List
22562278

22572279
@Override
@@ -2688,6 +2710,9 @@ protected int findPrototypeId(String s) {
26882710
case "flatMap":
26892711
id = Id_flatMap;
26902712
break;
2713+
case "toReversed":
2714+
id = Id_toReversed;
2715+
break;
26912716
default:
26922717
id = 0;
26932718
break;
@@ -2730,6 +2755,7 @@ protected int findPrototypeId(String s) {
27302755
Id_flat = 33,
27312756
Id_flatMap = 34,
27322757
SymbolId_iterator = 35,
2758+
Id_toReversed =36,
27332759
MAX_PROTOTYPE_ID = SymbolId_iterator;
27342760
private static final int ConstructorId_join = -Id_join,
27352761
ConstructorId_reverse = -Id_reverse,
@@ -2752,6 +2778,7 @@ protected int findPrototypeId(String s) {
27522778
ConstructorId_findIndex = -Id_findIndex,
27532779
ConstructorId_reduce = -Id_reduce,
27542780
ConstructorId_reduceRight = -Id_reduceRight,
2781+
ConstructorId_toReversed = -Id_toReversed,
27552782
ConstructorId_isArray = -26,
27562783
ConstructorId_of = -27,
27572784
ConstructorId_from = -28;

0 commit comments

Comments
 (0)