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

Rhino cannot evaluate a certain JavaScript code #1218

Closed
otsuka-kohei opened this issue May 5, 2022 · 10 comments
Closed

Rhino cannot evaluate a certain JavaScript code #1218

otsuka-kohei opened this issue May 5, 2022 · 10 comments

Comments

@otsuka-kohei
Copy link

Rhino returns a error missing ; before statement at line #20 of this JavaScript code.

Do you know the reason of this error?

@otsuka-kohei
Copy link
Author

FYI, Chrome and Firefox can execute that JavaScript code.

@p-bakker
Copy link
Collaborator

p-bakker commented May 5, 2022

Thats a large chunk of code, I think you'll have to put in the effort to pinpoint where the actual issue occurs within the code (line 20 is just a wrapper around all the code, so it happens somewhere inside the wrapper).

You haven't even specified which version of Rhino you're using, so even if someone would be willing to digg into such a broad question, they'd have no clue where to start

@otsuka-kohei
Copy link
Author

@p-bakker
Let me apologize for lack of information.

I am using Rhino v1.7.14.

Also, this line occurs a error.
I found that let occurs error, but const does not occur error.

let foo = "foo";

@p-bakker
Copy link
Collaborator

p-bakker commented May 5, 2022

There must be more to it, because just executing that bit of code i doubt throws an error

@otsuka-kohei
Copy link
Author

otsuka-kohei commented May 6, 2022

@p-bakker
I created a Java project to reproduce the error with minimum code.
This project shows same error.

Could you please try to run this Java project?

You can run with a command ./gradlew run.
I built and ran this Java project with Java 11 on Windows 11 (21H2, 22000.652).

@p-bakker
Copy link
Collaborator

p-bakker commented May 6, 2022

Mmm, strange, can reproduce this, while its quite basic JavaScript stuff

BTW: providing just the code snippet would've been sufficient:

	  Context ctx = Context.enter();
      ctx.setOptimizationLevel(-1);
      Scriptable scriptable = ctx.initStandardObjects();
      ctx.evaluateString(scriptable, "let foo = \"foo\";", "<cmd>", 0, null);

@p-bakker p-bakker added bug Issues considered a bug and removed bug Issues considered a bug labels May 6, 2022
@p-bakker
Copy link
Collaborator

p-bakker commented May 6, 2022

Figured it out: you're running Rhino without having specified the language version, so it uses the default (0), in which let isn't supported, so it sees 'let' as a identifier declaration and then indeed there's a missing semi-colon.

Setting the language version to 200 fixes it:

  Context ctx = Context.enter();
  ctx.setLanguageVersion(200);
  Scriptable scriptable = ctx.initStandardObjects();
  ctx.evaluateString(scriptable, "let foo = \"foo\";\n", "<cmd>", 0, null);

If this also solves the problem for you, please close the case

@otsuka-kohei
Copy link
Author

@p-bakker
Thank you for investigation.
Rhino can evaluate let foo = "foo"; by your solution.

But, I faced another issue.
Rhino returns error TypeError: redeclaration of const foo. when evaluate below JavaScript code.

function funcFoo() {
  const foo = "foo";
  let str = foo;
  {
    const foo = "FOO";
    str = str + foo;
  }
  return str;
}

const foo in code bloack must be different from another const foo at head of function.
Of course, Firefox can evaluate this code.

Do you know what is wrong?
Let me apologize for frequent inquiries.

Best Regards,

@p-bakker
Copy link
Collaborator

p-bakker commented May 7, 2022

Unfortunately Rhino currently doesn't support proper block scoping for const, resulting in the redeclaration error.

There are already several issues that report this. Hopefully this issue can be solved in v1.7.15 or v2.0.0.

For now the best path forward is to run your (library) code through Babel, using the 'rhino' profile.

No problem with the questions, but we have more appropriate places for them: discussions, Google Groups and Stack Overflow

@otsuka-kohei
Copy link
Author

@p-bakker
Thank you for answer.
I understand current situation.
I will consider to use Babel.
I will close this ticket.
Best Regards,

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

No branches or pull requests

2 participants