javadelight / delight-rhino-sandbox

A sandbox to execute JavaScript code with Rhino in Java.
Other
38 stars 12 forks source link

Java strings not passed as native strings #23

Open lcanet opened 3 years ago

lcanet commented 3 years ago

Hello,

When injecting java.lang.String instances into sandbox, they do not seems to be evaluated as native javascript strings, as strict equality operator (===) not recognizing them. For instance the following snippet

      RhinoSandbox sandbox = RhinoSandboxes.create();

      sandbox.inject("fromJava",  "Hello");

      System.out.println(sandbox.eval("source", "fromJava === 'Hello'"));
      System.out.println(sandbox.eval("source", "fromJava == 'Hello'"));

Outputs "false", "true". Expected: "true", "true"

mxro commented 3 years ago

Thank you for your question!

I think Rhino creates string objects that are 'almost' like JS strings. An easy way to make the 'Java' strings more JS like, maybe something like:

(fromJava+"")

Would that work for you?