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

Conversation

pkkht
Copy link

@pkkht pkkht commented Nov 3, 2023

Hi,
I get the below error when testing the method manually from the console. I don't see NativeArray invoked in the exception stack trace. What's going wrong in the code? Any ideas please

js> const items = [1, 2, 3];
js> items
1,2,3
js> items.toReversed()
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 70 out of bounds for length 70
at org.mozilla.javascript.IdScriptableObject$PrototypeValues.ensureId(IdScriptableObject.java:284)
at org.mozilla.javascript.IdScriptableObject$PrototypeValues.get(IdScriptableObject.java:162)
at org.mozilla.javascript.IdScriptableObject.get(IdScriptableObject.java:380)
at org.mozilla.javascript.ScriptableObject.getProperty(ScriptableObject.java:2037)
at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2584)
at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2575)
at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:71)
at org.mozilla.javascript.gen._stdin__3._c_script_0(:4)
at org.mozilla.javascript.gen._stdin__3.call()
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:383)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3876)
at org.mozilla.javascript.gen._stdin__3.call()
at org.mozilla.javascript.gen._stdin__3.exec()
at org.mozilla.javascript.tools.shell.Main.processSource(Main.java:482)
at org.mozilla.javascript.tools.shell.Main.processFiles(Main.java:180)
at org.mozilla.javascript.tools.shell.Main$IProxy.run(Main.java:98)
at org.mozilla.javascript.Context.call(Context.java:544)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:475)
at org.mozilla.javascript.tools.shell.Main.exec(Main.java:164)
at org.mozilla.javascript.tools.shell.Main.main(Main.java:142)

@pkkht pkkht marked this pull request as draft November 3, 2023 11:51
@p-bakker
Copy link
Collaborator

p-bakker commented Nov 4, 2023

I'd set a breakpoint early in the stacktrace you're getting and then see what is the difference between a call to [1,2,3].reverse() and [1,2,3].toReversed().

If you do, I think you'll find that you forgot to update MAX_PROTOTYPE_ID to the new value

@pkkht
Copy link
Author

pkkht commented Nov 4, 2023

I'd set a breakpoint early in the stacktrace you're getting and then see what is the difference between a call to [1,2,3].reverse() and [1,2,3].toReversed().

If you do, I think you'll find that you forgot to update MAX_PROTOTYPE_ID to the new value

cool, thanks. I know it is a basic question, but how to debug from the shell? Is it this one:

https://rhino.github.io/tools/debugger/

Says there is no breakpoint available?

@p-bakker
Copy link
Collaborator

p-bakker commented Nov 5, 2023

That's the JavaScript Debugger. In your case as you're developing Rhino itself, in Java, you'll need a Java debugger.

I personally use Eclipse and run the rhino shell from source inside Eclipse, but you can use whatever Java development environment you like

@pkkht
Copy link
Author

pkkht commented Nov 5, 2023

That's the JavaScript Debugger. In your case as you're developing Rhino itself, in Java, you'll need a Java debugger.

I personally use Eclipse and run the rhino shell from source inside Eclipse, but you can use whatever Java development environment you like

Ta, able to debug now and no more the error! Appreciate it

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

Copy link
Collaborator

@rbri rbri left a comment

Choose a reason for hiding this comment

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

Test cases are missing

@@ -2729,7 +2753,8 @@ protected int findPrototypeId(String s) {
Id_at = 32,
Id_flat = 33,
Id_flatMap = 34,
SymbolId_iterator = 35,
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

@@ -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

@pkkht
Copy link
Author

pkkht commented Nov 8, 2023

Test cases are missing

Hi there, I am looking the array-at.js file. Probably, a similar file for this new method? How do I run just the test js file?

@rbri
Copy link
Collaborator

rbri commented Nov 8, 2023

@pkkht you can have a look at the packages org.mozilla.javascript.tests.es2019, org.mozilla.javascript.tests.es2020, org.mozilla.javascript.tests.es2022. Check out e.g. org.mozilla.javascript.tests.es2020.TypedArrayAtTest for a sample.

@pkkht pkkht marked this pull request as ready for review November 11, 2023 10:07
@p-bakker
Copy link
Collaborator

@pkkht I see you mentioned this PR being a draft, but it's not marked as such and it's referencing just the .toReversed() method

What is your idea? Are you going to implement all the new methods of the Change Array by Copy proposal on NativeArray in this PR? Or are you planning to provide individual PRs for each method?

@p-bakker p-bakker linked an issue Nov 12, 2023 that may be closed by this pull request
@p-bakker
Copy link
Collaborator

As for the tests: the test262 testsuite for Array.prototype.toReversed() is (obviously) way more extensive than the tests you've added.

This is not particularly an issue with the tests you've added, but it does make me wonder what we might be missing. One thing in particular with our NativeArray implementation is that it internally switches to another implementation if the length of an Array is over NativeArray.maximumInitialCapacity (also see the NativeArray.denseOnly flag. I wonder if we should have explicit tests with Arrays that go beyond this size

I will see if I can expedite the work on #958 which hopefully would allow us to move to the latest or at least way more recent version of Test262

@pkkht
Copy link
Author

pkkht commented Nov 12, 2023

Okay, on a second thought, I will use the same PR for all the methods. I see there is more work in the tests.. so we can do altogether

@pkkht
Copy link
Author

pkkht commented Nov 16, 2023

Why did the tests fail in the pipeline?

MAX_PROTOTYPE_ID = SymbolId_iterator;
Id_toReversed = 36,
SymbolId_iterator = 37,
MAX_PROTOTYPE_ID = Id_toReversed;
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

@p-bakker
Copy link
Collaborator

@pkkht hi, wondering where you're at with this PR. Are you still working on it?

@pkkht
Copy link
Author

pkkht commented Jun 23, 2024

Not as of now

@p-bakker
Copy link
Collaborator

p-bakker commented Aug 9, 2024

@pkkht note that Rhino now runs the Test262 test suite against a very recent version of test262, which includes all the tests for newer Array methods like .at() and .toReversed()

So likely you can remove the testcases you added, because those would already be covered by test262 now

@pkkht
Copy link
Author

pkkht commented Aug 9, 2024

Okay

@pkkht pkkht closed this Aug 10, 2024
@rbri
Copy link
Collaborator

rbri commented Aug 19, 2024

@pkkht why did you close this? Looks like the implementation of toReversed is still missing in Rhino and i think your PR will at least help to get this done.

@pkkht
Copy link
Author

pkkht commented Aug 19, 2024

Sorry I got a bit confused with the recent comments.
Regardless, I am unable to take forward the code change at this stage.

@andreabergia
Copy link
Contributor

@rbri I am working on a PR for this (and the other new toXXX methods: toSorted, toSpliced, with) by the way. I plan to open it this week, unless there's some emergency at work. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support ES2023 Change Array by copy
4 participants