Skip to content
Advertisement

Is there a way to use a java object as an argument for a function and return one of said objects’s values in GraalJS?

I want to use a Java object new Train() as an argument to pass into a JavaScript function, here is the Java code

JavaScript
JavaScript

JavaScript code

JavaScript

As of right now it puts this error in the console

[16:56:42] [INFO]: [STDERR]: javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (getSpeed) on ScriptExecutor$Train@429b2a7b failed due to: Unknown identifier: getSpeed [16:56:42] [INFO]: [STDERR]: at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.toScriptException(GraalJSScriptEngine.java:483) [16:56:42] [INFO]: [STDERR]: at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.invokeFunction(GraalJSScriptEngine.java:558)

Is there any way to make this work?

Advertisement

Answer

GraalJS (and GraalVM in general) has tight security/access controls by default. GraalJS is not exposing getSpeed() (or any other field or method) on the Train instance to JavaScript.

You can open up access to all host fields/methods with a configuration setting:

JavaScript

or instead enable it on a more granular level by decorating getSpeed() in Train:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement