Help re. my first exercise in Rhino

74 views
Skip to first unread message

Hariharan Karuppasamy

unread,
Nov 2, 2023, 6:01:19 AM11/2/23
to mozilla-rhino
Hi all,
I am working on this task:

I am starting with the toReversed method. I understand the change should go in NativeArray.java

This is the algorithm that I got. How do I find the equivalent Java methods to implement this logic? I appreciate your inputs to how go with this implementation.

When the toReversed method is called, the following steps are taken:

  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. Let A be ? ArrayCreate(𝔽(len)).
  4. Let k be 0.
  5. Repeat, while k < len,
    1. Let from be ! ToString(𝔽(len - k - 1)).
    2. Let Pk be ! ToString(𝔽(k)).
    3. Let fromValue be ? Get(Ofrom).
    4. Perform ! CreateDataPropertyOrThrow(APkfromValue).
    5. Set k to k + 1.
  6. Return A.



P

unread,
Nov 3, 2023, 5:28:14 AM11/3/23
to mozilla-rhino
Hi,

There are no clear-cut answers here. Best approach I find is to look at prior implementations of similar stuff, like for example the implementation of the .at(idx) method, see https://github.com/mozilla/rhino/pull/1289

Hariharan Karuppasamy

unread,
Nov 3, 2023, 6:13:37 AM11/3/23
to mozilla-rhino
Yes, I did have a look around. Thanks for this. Can I submit one PR after the toReveresed() implementation before I carry on with other methods?

P

unread,
Nov 3, 2023, 7:48:21 AM11/3/23
to mozilla-rhino
Maybe create a draft PR for feedback once you have toReversed implemented, while you continue to work on the other methods?

Hariharan Karuppasamy

unread,
Nov 3, 2023, 7:48:59 AM11/3/23
to mozilla-rhino
Hi, 
I have checked in the code to this branch "hari-change-array-by-copy" . However 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(<stdin>:4)
        at org.mozilla.javascript.gen._stdin__3.call(<stdin>)
        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(<stdin>)
        at org.mozilla.javascript.gen._stdin__3.exec(<stdin>)
        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)

Hariharan Karuppasamy

unread,
Nov 3, 2023, 7:51:40 AM11/3/23
to mozilla-rhino
Done, created a draft PR with the query above. Thanks
Reply all
Reply to author
Forward
0 new messages