Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NativeArray for toReversed() #1414

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2729,7 +2752,8 @@ protected int findPrototypeId(String s) {
Id_at = 32,
Id_flat = 33,
Id_flatMap = 34,
SymbolId_iterator = 35,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave this, just update MAX_PROTOTYPE_ID to be Id_toReversed

Id_toReversed = 35,
SymbolId_iterator = 36,
MAX_PROTOTYPE_ID = SymbolId_iterator;
private static final int ConstructorId_join = -Id_join,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        Id_flatMap = 34,
        Id_toReversed = 35,
        SymbolId_iterator = 36,
        MAX_PROTOTYPE_ID = SymbolId_iterator;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ta

ConstructorId_reverse = -Id_reverse,
Expand All @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions testsrc/jstests/es2023/array-toReversed.js
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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 {}