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
Changes from 2 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
30 changes: 28 additions & 2 deletions 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,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
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,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);
Copy link
Collaborator

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


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 +2709,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 +2753,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 =36,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
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 +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;
Expand Down
Loading