-
Notifications
You must be signed in to change notification settings - Fork 858
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
Changes from 2 commits
9d06be8
c6c116a
420a965
a4a73c8
20604f9
921af08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,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); | ||
|
||
case Id_every: | ||
case Id_filter: | ||
case Id_forEach: | ||
|
@@ -2252,6 +2261,18 @@ 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) > Integer.MAX_VALUE) ? 0 : 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 +2709,9 @@ protected int findPrototypeId(String s) { | |
case "flatMap": | ||
id = Id_flatMap; | ||
break; | ||
case "toReversed": | ||
id = Id_toReversed; | ||
break; | ||
default: | ||
id = 0; | ||
break; | ||
|
@@ -2729,7 +2753,8 @@ protected int findPrototypeId(String s) { | |
Id_at = 32, | ||
Id_flat = 33, | ||
Id_flatMap = 34, | ||
SymbolId_iterator = 35, | ||
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. Leave this, just update MAX_PROTOTYPE_ID to be Id_toReversed |
||
Id_toReversed =36, | ||
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. formatting |
||
SymbolId_iterator = 37, | ||
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 +2777,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; | ||
|
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.
i guess getLengthProperty(cx, o) is expensive, maybe calling this only once is a good idea