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

[21.3.0][JDK11][javax.script] TypeError with scoped Bindings #3937

Closed
ctabin opened this issue Oct 21, 2021 · 1 comment
Closed

[21.3.0][JDK11][javax.script] TypeError with scoped Bindings #3937

ctabin opened this issue Oct 21, 2021 · 1 comment
Labels

Comments

@ctabin
Copy link

ctabin commented Oct 21, 2021

Hello,

We are migrating to GraalJS as scripting engine behind the javax.script API in replacement of Nashorn. We were waiting for #631 and happy to see it finally implemented in 21.3.0.

In our case we need to execute multiple script blocks in isolation, but all have some common data/tools used. By doing this, we are hitting some strange issues. Find below the detailed data to reproduce the case.

Initialization script (INIT). We want BigDecimal and MathContext to be available in all the lines that will be executed. This script is executed in the ENGINE_SCOPE that will be the 'super-scope' for all our lines.

var BigDecimal = Java.type("java.math.BigDecimal");
var MathContext = Java.type("java.math.MathContext");

The ScriptEngine is initialized like this:

ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("graal.js");
        
//init GraalJS
Bindings defaultBindings = engine.createBindings();
defaultBindings.put("polyglot.js.allowHostAccess", true);
defaultBindings.put("polyglot.js.allowHostClassLookup", true);
engine.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
        
engine.eval(INIT); //provides BigDecimal and MathContext

Here is the sample script used (SCRIPT):

var a = new BigDecimal(15);
var b = new BigDecimal(3);
a.divide(b, MathContext.DECIMAL64)

When the script is executed in the ENGINE_SCOPE, there is no problem:

engine.put(ScriptEngine.FILENAME, "script");
Compilable compiler = (Compilable)engine;
CompiledScript cs = compiler.compile(SCRIPT);
return (BigDecimal)cs.eval(); //returns a BigDecimal(5)

However, when an isolated scope is created, then an error is thrown

Bindings defaults = engine.getBindings(ScriptContext.ENGINE_SCOPE);
SimpleBindings localBindings = new SimpleBindings(new HashMap<>(defaults));

engine.put(ScriptEngine.FILENAME, "script");
Compilable compiler = (Compilable)engine;
CompiledScript cs = compiler.compile(SCRIPT);
return (BigDecimal)cs.eval(localBindings); //error
javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (divide) on java.math.BigDecimal@12bfd80d failed due to: Multiple applicable overloads found for method name divide (candidates: [Method[public java.math.BigDecimal java.math.BigDecimal.divide(java.math.BigDecimal,java.math.MathContext)], Method[public java.math.BigDecimal java.math.BigDecimal.divide(java.math.BigDecimal,java.math.RoundingMode)]], arguments: [JavaObject[3 (java.math.BigDecimal)] (HostObject), DynamicObject<undefined>@b2c5e07 (Nullish)])
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.toScriptException(GraalJSScriptEngine.java:483)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:460)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.access$400(GraalJSScriptEngine.java:83)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine$10.eval(GraalJSScriptEngine.java:628)
        at java.scripting/javax.script.CompiledScript.eval(CompiledScript.java:89)
        at ch.astorm.graaljs.ScopeTest.evaluate(ScopeTest.java:53)
        at ch.astorm.graaljs.ScopeTest.testFailureDivision(ScopeTest.java:45)
Caused by: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (divide) on java.math.BigDecimal@12bfd80d failed due to: Multiple applicable overloads found for method name divide (candidates: [Method[public java.math.BigDecimal java.math.BigDecimal.divide(java.math.BigDecimal,java.math.MathContext)], Method[public java.math.BigDecimal java.math.BigDecimal.divide(java.math.BigDecimal,java.math.RoundingMode)]], arguments: [JavaObject[3 (java.math.BigDecimal)] (HostObject), DynamicObject<undefined>@b2c5e07 (Nullish)])
        at <js>.:program(script:3)
        at org.graalvm.polyglot.Context.eval(Context.java:379)
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458)
        ... 23 more

Here is a small project that reproduces the case. Just type mvn clean package in graaljs-bug dir to see it.

@ctabin ctabin added the truffle label Oct 21, 2021
@ctabin ctabin changed the title [JDK11][javax.script] TypeError with scoped Bindings [21.3.0][JDK11][javax.script] TypeError with scoped Bindings Oct 21, 2021
@ctabin
Copy link
Author

ctabin commented Oct 22, 2021

I'm closing this issue, because it seems to be at a better place in graaljs.

@ctabin ctabin closed this as completed Oct 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant