Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ConsString is not converted to Java String when nested JS execution #1728

Open
821938089 opened this issue Nov 24, 2024 · 3 comments
Open

ConsString is not converted to Java String when nested JS execution #1728

821938089 opened this issue Nov 24, 2024 · 3 comments
Labels
Community input needed Issues requiring community input Java Interop Issues related to the interaction between Java and JavaScript

Comments

@821938089
Copy link

When nested JS execution, ScriptRuntime.hasTopCall(cx) is true and the ConsString returned by the else branch is not converted to a Java String.

public Object exec(Context cx, Scriptable scope) {
if (!isScript()) {
// Can only be applied to scripts
throw new IllegalStateException();
}
Object ret;
if (!ScriptRuntime.hasTopCall(cx)) {
// It will go through "call" path. but they are equivalent
ret =
ScriptRuntime.doTopCall(
this, cx, scope, scope, ScriptRuntime.emptyArgs, idata.isStrict);
} else {
ret = Interpreter.interpret(this, cx, scope, scope, ScriptRuntime.emptyArgs);
}
cx.processMicrotasks();
return ret;
}

protected Object doTopCall(
Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object result = callable.call(cx, scope, thisObj, args);
return result instanceof ConsString ? result.toString() : result;
}

Reproduce code:

public class Main {

  public static void main(String[] args) {
    var obj = new MyObject();

    obj.evalJS("myObj.method1()");
    System.out.println(obj.evalJS("var a = 'a'; a + 'b'").getClass().getName());

  }

}
public class MyObject {

  public void method1() {
    System.out.println(evalJS("var a = 'a'; a + 'b'").getClass().getName());
  }

  public Object evalJS(String jsStr) {
    try (var cx = Context.enter()) {
      cx.setOptimizationLevel(-1);
      var scope = cx.initStandardObjects();
      scope.put("myObj", scope, Context.javaToJS(this, scope));
      return cx.evaluateString(scope, jsStr, "unknown source", 0, null);
    }
  }

}
@p-bakker
Copy link
Collaborator

You should not expect calls from Java into JavaScript that return a JavaScript string to be a Java String.

Instead expect CharSequence

@p-bakker
Copy link
Collaborator

@gbrail @rbri agree this isn't a bug, but working as expected?

@p-bakker
Copy link
Collaborator

Also see #247

@p-bakker p-bakker added Community input needed Issues requiring community input Java Interop Issues related to the interaction between Java and JavaScript labels Dec 25, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Community input needed Issues requiring community input Java Interop Issues related to the interaction between Java and JavaScript
Projects
None yet
Development

No branches or pull requests

2 participants