From 9d06be84c859f0fb9f51e298fcceb4b45f95c8d1 Mon Sep 17 00:00:00 2001 From: pkkht Date: Fri, 3 Nov 2023 22:42:42 +1100 Subject: [PATCH 1/6] Update NativeArray for toReversed() --- src/org/mozilla/javascript/NativeArray.java | 29 ++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/org/mozilla/javascript/NativeArray.java b/src/org/mozilla/javascript/NativeArray.java index 9096dec92e..f5862cd992 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,7 +346,8 @@ 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 // so we adjust the args, inverting the id and @@ -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, args); + case Id_every: case Id_filter: case Id_forEach: @@ -2252,6 +2261,19 @@ private static boolean js_isArray(Object o) { return "Array".equals(((Scriptable) o).getClassName()); } + private static Scriptable js_toReversed( + Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { + Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj); + int len = (getLengthProperty(cx, o) > Integer.MAX_VALUE) ? 0 :(int) getLengthProperty(cx, o); + + Scriptable result = cx.newArray(scope, len); + + for(int k = len-1; k >= 0; k--){ + Object temp1 = getRawElem(o, k); + setRawElem(cx, result, k, temp1); + } + return result; + } // methods to implement java.util.List @Override @@ -2688,6 +2710,9 @@ protected int findPrototypeId(String s) { case "flatMap": id = Id_flatMap; break; + case "toReversed": + id = Id_toReversed; + break; default: id = 0; break; @@ -2730,6 +2755,7 @@ protected int findPrototypeId(String s) { Id_flat = 33, Id_flatMap = 34, SymbolId_iterator = 35, + Id_toReversed =36, MAX_PROTOTYPE_ID = SymbolId_iterator; private static final int ConstructorId_join = -Id_join, ConstructorId_reverse = -Id_reverse, @@ -2752,6 +2778,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; From c6c116a63d0fad14e1f487fd38ebae71fbb6e772 Mon Sep 17 00:00:00 2001 From: pkkht Date: Mon, 6 Nov 2023 00:28:53 +1100 Subject: [PATCH 2/6] Update NativeArray.java --- src/org/mozilla/javascript/NativeArray.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/org/mozilla/javascript/NativeArray.java b/src/org/mozilla/javascript/NativeArray.java index f5862cd992..feea67c9a2 100644 --- a/src/org/mozilla/javascript/NativeArray.java +++ b/src/org/mozilla/javascript/NativeArray.java @@ -455,7 +455,7 @@ public Object execIdCall( return js_flatMap(cx, scope, thisObj, args); case Id_toReversed: - return js_toReversed(cx, scope, thisObj, args); + return js_toReversed(cx, scope, thisObj); case Id_every: case Id_filter: @@ -2262,15 +2262,14 @@ private static boolean js_isArray(Object o) { } private static Scriptable js_toReversed( - Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { + Context cx, Scriptable scope, Scriptable thisObj) { Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj); - int len = (getLengthProperty(cx, o) > Integer.MAX_VALUE) ? 0 :(int) getLengthProperty(cx, o); - - Scriptable result = cx.newArray(scope, len); + long len = (getLengthProperty(cx, o) > Integer.MAX_VALUE) ? 0 : getLengthProperty(cx, o); - for(int k = len-1; k >= 0; k--){ + Scriptable result = cx.newArray(scope, (int)len); + for(long k = len-1; k >= 0; k--){ Object temp1 = getRawElem(o, k); - setRawElem(cx, result, k, temp1); + defineElemOrThrow(cx, result, len-k-1, temp1); } return result; } @@ -2754,8 +2753,8 @@ protected int findPrototypeId(String s) { Id_at = 32, Id_flat = 33, Id_flatMap = 34, - SymbolId_iterator = 35, Id_toReversed =36, + SymbolId_iterator = 37, MAX_PROTOTYPE_ID = SymbolId_iterator; private static final int ConstructorId_join = -Id_join, ConstructorId_reverse = -Id_reverse, From 420a9655f96f3e2bc8b5bbe70cd5296dd5368f03 Mon Sep 17 00:00:00 2001 From: pkkht Date: Sat, 11 Nov 2023 21:06:05 +1100 Subject: [PATCH 3/6] Fixed formatting, max prototype variable and updated unit test --- src/org/mozilla/javascript/NativeArray.java | 17 +++++++------- testsrc/jstests/es2023/array-toReversed.js | 23 +++++++++++++++++++ .../tests/es2023/ArrayToReversedTest.java | 12 ++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 testsrc/jstests/es2023/array-toReversed.js create mode 100644 testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java diff --git a/src/org/mozilla/javascript/NativeArray.java b/src/org/mozilla/javascript/NativeArray.java index feea67c9a2..fd51f49528 100644 --- a/src/org/mozilla/javascript/NativeArray.java +++ b/src/org/mozilla/javascript/NativeArray.java @@ -347,7 +347,7 @@ public Object execIdCall( 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 // so we adjust the args, inverting the id and @@ -2261,15 +2261,14 @@ private static boolean js_isArray(Object o) { return "Array".equals(((Scriptable) o).getClassName()); } - private static Scriptable js_toReversed( - Context cx, Scriptable scope, Scriptable thisObj) { + private static Scriptable js_toReversed(Context cx, Scriptable scope, Scriptable thisObj) { Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj); - long len = (getLengthProperty(cx, o) > Integer.MAX_VALUE) ? 0 : getLengthProperty(cx, o); + long len = getLengthProperty(cx, o); - Scriptable result = cx.newArray(scope, (int)len); - for(long k = len-1; k >= 0; k--){ + 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); + defineElemOrThrow(cx, result, len - k - 1, temp1); } return result; } @@ -2753,9 +2752,9 @@ protected int findPrototypeId(String s) { Id_at = 32, Id_flat = 33, Id_flatMap = 34, - Id_toReversed =36, + Id_toReversed = 36, SymbolId_iterator = 37, - MAX_PROTOTYPE_ID = SymbolId_iterator; + MAX_PROTOTYPE_ID = Id_toReversed; private static final int ConstructorId_join = -Id_join, ConstructorId_reverse = -Id_reverse, ConstructorId_sort = -Id_sort, diff --git a/testsrc/jstests/es2023/array-toReversed.js b/testsrc/jstests/es2023/array-toReversed.js new file mode 100644 index 0000000000..49797321ea --- /dev/null +++ b/testsrc/jstests/es2023/array-toReversed.js @@ -0,0 +1,23 @@ +// 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())); + +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..733232e4f3 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java @@ -0,0 +1,12 @@ +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 { +} From a4a73c8812840daddcefba386460714c6b0b5fcb Mon Sep 17 00:00:00 2001 From: pkkht Date: Sat, 11 Nov 2023 21:35:39 +1100 Subject: [PATCH 4/6] Added 2 more tests --- testsrc/jstests/es2023/array-toReversed.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/testsrc/jstests/es2023/array-toReversed.js b/testsrc/jstests/es2023/array-toReversed.js index 49797321ea..f164aea489 100644 --- a/testsrc/jstests/es2023/array-toReversed.js +++ b/testsrc/jstests/es2023/array-toReversed.js @@ -12,6 +12,14 @@ 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) && From 20604f9bfa4f1b3a381070bb25195731963c6731 Mon Sep 17 00:00:00 2001 From: pkkht Date: Sat, 11 Nov 2023 21:45:00 +1100 Subject: [PATCH 5/6] Fixed formatting in test class --- .../mozilla/javascript/tests/es2023/ArrayToReversedTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java b/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java index 733232e4f3..119bfadc32 100644 --- a/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java +++ b/testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java @@ -1,6 +1,5 @@ package org.mozilla.javascript.tests.es2023; - import org.mozilla.javascript.Context; import org.mozilla.javascript.drivers.LanguageVersion; import org.mozilla.javascript.drivers.RhinoTest; @@ -8,5 +7,4 @@ @RhinoTest("testsrc/jstests/es2023/array-toReversed.js") @LanguageVersion(Context.VERSION_ES6) -public class ArrayToReversedTest extends ScriptTestsBase { -} +public class ArrayToReversedTest extends ScriptTestsBase {} From 921af08500b7de49a4d85f2a51230ebffc9888f7 Mon Sep 17 00:00:00 2001 From: pkkht Date: Wed, 27 Dec 2023 15:24:53 +1100 Subject: [PATCH 6/6] Updated as per review feedback for failing tests --- src/org/mozilla/javascript/NativeArray.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/org/mozilla/javascript/NativeArray.java b/src/org/mozilla/javascript/NativeArray.java index fd51f49528..3f501eeda5 100644 --- a/src/org/mozilla/javascript/NativeArray.java +++ b/src/org/mozilla/javascript/NativeArray.java @@ -2752,9 +2752,9 @@ protected int findPrototypeId(String s) { Id_at = 32, Id_flat = 33, Id_flatMap = 34, - Id_toReversed = 36, - SymbolId_iterator = 37, - MAX_PROTOTYPE_ID = Id_toReversed; + Id_toReversed = 35, + SymbolId_iterator = 36, + MAX_PROTOTYPE_ID = SymbolId_iterator; private static final int ConstructorId_join = -Id_join, ConstructorId_reverse = -Id_reverse, ConstructorId_sort = -Id_sort,