diff --git a/src/org/mozilla/javascript/NativeArray.java b/src/org/mozilla/javascript/NativeArray.java index 9096dec92e..3f501eeda5 100644 --- a/src/org/mozilla/javascript/NativeArray.java +++ b/src/org/mozilla/javascript/NativeArray.java @@ -154,6 +154,7 @@ protected void fillConstructorProperties(IdFunctionObject ctor) { addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_isArray, "isArray", 1); addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_of, "of", 0); addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_from, "from", 1); + addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_toReversed, "toReversed", 0); super.fillConstructorProperties(ctor); } @@ -303,6 +304,10 @@ protected void initPrototypeId(int id) { arity = 1; s = "flatMap"; break; + case Id_toReversed: + arity = 0; + s = "toReversed"; + break; default: throw new IllegalArgumentException(String.valueOf(id)); } @@ -341,6 +346,7 @@ public Object execIdCall( case ConstructorId_findIndex: case ConstructorId_reduce: case ConstructorId_reduceRight: + case ConstructorId_toReversed: { // this is a small trick; we will handle all the ConstructorId_xxx calls // the same way the object calls are processed @@ -448,6 +454,9 @@ public Object execIdCall( case Id_flatMap: return js_flatMap(cx, scope, thisObj, args); + case Id_toReversed: + return js_toReversed(cx, scope, thisObj); + case Id_every: case Id_filter: case Id_forEach: @@ -2252,6 +2261,17 @@ private static boolean js_isArray(Object o) { return "Array".equals(((Scriptable) o).getClassName()); } + private static Scriptable js_toReversed(Context cx, Scriptable scope, Scriptable thisObj) { + Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj); + long len = getLengthProperty(cx, o); + + Scriptable result = cx.newArray(scope, (int) len); + for (long k = len - 1; k >= 0; k--) { + Object temp1 = getRawElem(o, k); + defineElemOrThrow(cx, result, len - k - 1, temp1); + } + return result; + } // methods to implement java.util.List @Override @@ -2688,6 +2708,9 @@ protected int findPrototypeId(String s) { case "flatMap": id = Id_flatMap; break; + case "toReversed": + id = Id_toReversed; + break; default: id = 0; break; @@ -2729,7 +2752,8 @@ protected int findPrototypeId(String s) { Id_at = 32, Id_flat = 33, Id_flatMap = 34, - SymbolId_iterator = 35, + Id_toReversed = 35, + SymbolId_iterator = 36, MAX_PROTOTYPE_ID = SymbolId_iterator; private static final int ConstructorId_join = -Id_join, ConstructorId_reverse = -Id_reverse, @@ -2752,6 +2776,7 @@ protected int findPrototypeId(String s) { ConstructorId_findIndex = -Id_findIndex, ConstructorId_reduce = -Id_reduce, ConstructorId_reduceRight = -Id_reduceRight, + ConstructorId_toReversed = -Id_toReversed, ConstructorId_isArray = -26, ConstructorId_of = -27, ConstructorId_from = -28; diff --git a/testsrc/jstests/es2023/array-toReversed.js b/testsrc/jstests/es2023/array-toReversed.js new file mode 100644 index 0000000000..f164aea489 --- /dev/null +++ b/testsrc/jstests/es2023/array-toReversed.js @@ -0,0 +1,31 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +load("testsrc/assert.js"); + +const array1=[1,2,3]; +assertArrayEquals([3,2,1], array1.toReversed()); +assertFalse(arrayEquals(array1,array1.toReversed())); + +const array2 = ["321","abc", "def","fgh"]; +assertArrayEquals(["fgh", "def","abc","321"], array2.toReversed()); +assertFalse(arrayEquals(array2,array2.toReversed())); + +const array3 = ["321","", "","o"]; +assertArrayEquals(["o", "","","321"], array3.toReversed()); +assertFalse(arrayEquals(array3,array3.toReversed())); + +const array4 = ["1",,,"d"]; +assertArrayEquals(["d",,,"1"], array4.toReversed()); +assertFalse(arrayEquals(array4,array4.toReversed())); + +function arrayEquals(a, b) { + return Array.isArray(a) && + Array.isArray(b) && + a.length === b.length && + a.every((val, index) => val === b[index]); +} + + +"success" \ No newline at end of file diff --git a/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java b/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java new file mode 100644 index 0000000000..119bfadc32 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java @@ -0,0 +1,10 @@ +package org.mozilla.javascript.tests.es2023; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.drivers.LanguageVersion; +import org.mozilla.javascript.drivers.RhinoTest; +import org.mozilla.javascript.drivers.ScriptTestsBase; + +@RhinoTest("testsrc/jstests/es2023/array-toReversed.js") +@LanguageVersion(Context.VERSION_ES6) +public class ArrayToReversedTest extends ScriptTestsBase {}