mozilla / rhino

Rhino is an open-source implementation of JavaScript written entirely in Java
https://rhino.github.io
Other
4.12k stars 848 forks source link

Unable to execute a Javascript code with simple function to increment the counter upto 1 million #1546

Closed Saloni1232 closed 3 weeks ago

Saloni1232 commented 1 month ago

I have a JS function as below :

`function runMillionTimes() { var counter = 0; for (let i = 0; i < 1000000; i++) { counter++; } return counter.toString(); }

runMillionTimes();`

I am using rhino to execute this JS. But getting exception as : org.mozilla.javascript.EvaluatorException: missing ; after for-loop initializer (JavaScript#21).

Rhino code as below ->

private fun executeJSViaRhino(jsCode: String){ val rhino = Context.enter() rhino.optimizationLevel = -1 // Required to run with Android try { val scope: Scriptable = rhino.initStandardObjects() val result = rhino.evaluateString(scope, jsCode, "JavaScript", 1, null) Log.d("Rhino", "print output", $result) } finally { Context.exit() } }

Kindly help to fix this exception.

nmondal commented 1 month ago

Hello @Saloni1232

This code :

function runMillionTimes() {
    var counter = 0;
    for (let i = 0; i < 1000000; i++) {
        counter++;
    }
    return counter.toString();
}
runMillionTimes(); // return

Perfectly runs in JSR-223 mode. I guess you are trying to do something Android Specific, is that correct?

gbrail commented 1 month ago

I bet that setting the language version to VERSION_ES6 would fix this, since the let keyword isn't recognized otherwise.

And this begs the question of when we should consider making that the default language version...

On Tue, Jul 30, 2024 at 5:34 AM Saloni Garg @.***> wrote:

I have a JS function as below :

`function runMillionTimes() { var counter = 0; for (let i = 0; i < 1000000; i++) { counter++; } return counter.toString(); }

runMillionTimes();`

I am using rhino to execute this JS. But getting exception as : org.mozilla.javascript.EvaluatorException: missing ; after for-loop initializer (JavaScript#21).

Rhino code as below ->

private fun executeJSViaRhino(jsCode: String){ val rhino = Context.enter() rhino.optimizationLevel = -1 // Required to run with Android try { val scope: Scriptable = rhino.initStandardObjects() val result = rhino.evaluateString(scope, jsCode, "JavaScript", 1, null) Log.d("Rhino", "print output", $result) } finally { Context.exit() } }

Kindly help to fix this exception.

— Reply to this email directly, view it on GitHub https://github.com/mozilla/rhino/issues/1546, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD7I2ZUPRUJT7QEAEVNDSDZO6B43AVCNFSM6AAAAABLWI4ZXGVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQZTONZTGE2TMNY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

p-bakker commented 1 month ago

And this begs the question of when we should consider making that the default language version...

I'd say we can do this today, as long as we clearly state this in the release notes of the next version

1279

nmondal commented 1 month ago

I bet that setting the language version to VERSION_ES6 would fix this,

Hi @gbrail , then how exactly it is already running in JSR-223 mode? Is there a discrepancy somewhere?

Never mind, my mistake. The latest Engine has this comment:

javax. script. language_version: The version of the JavaScript language supported, which is an integer defined in the Context class. The default is the latest "ES6" version, defined as 200.

Thanks!

Saloni1232 commented 1 month ago

This issue is resolved by adding language version. Thank you all.

p-bakker commented 1 month ago

So, it didn't default to ES6/200 by default, as per the comment you quoted, but you had to explicitly set it to 200 yourself for things to work?

p-bakker commented 3 weeks ago

@Saloni1232 @nmondal any chance you could let us know how you used Rhino, so that it ran with an older language_versions?

Because unless you explicitly set it to an older language version, I think it should've defaulted to ES6/200, so if it didn't, I think it's a bug that we should fix

nmondal commented 3 weeks ago

@p-bakker I ran it with JSR-223 mode. Therefore, as I can see the code, it defaults to ES6 :-). That is why my code could run, so no issue here!

p-bakker commented 3 weeks ago

@nmondal it worked for you, as you used the JSR-232 script engine, while @Saloni1232 used the shell?

For the later we already have #1279, so closing this issue them