-
Notifications
You must be signed in to change notification settings - Fork 864
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d06be8
Update NativeArray for toReversed()
pkkht c6c116a
Update NativeArray.java
pkkht 420a965
Fixed formatting, max prototype variable and updated unit test
pkkht a4a73c8
Added 2 more tests
pkkht 20604f9
Fixed formatting in test class
pkkht 921af08
Updated as per review feedback for failing tests
pkkht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ta |
||
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
10 changes: 10 additions & 0 deletions
10
testsrc/org/mozilla/javascript/tests/es2023/ArrayToReversedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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