Graaljs: Has Java.type changed in 1.0.0-rc15

Created on 17 Apr 2019  路  4Comments  路  Source: oracle/graaljs

I've got the following maven dependencies, with graalvm-js-version being updated to 1.0.0-rc15 (from 1.0.0-rc14)

        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>${graalvm-js-version}</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
            <version>${graalvm-js-version}</version>
        </dependency>

When using ScriptEngine, I'm running the following script:

// Allows loading functionality to be used by scripts

// Testing by loading Java types
const PrimitiveTypes = Java.type("net.officefloor.polyglot.test.PrimitiveTypes");
// rest removed for brevity

And I'm getting the following exception:

Caused by: org.graalvm.polyglot.PolyglotException: ReferenceError: Java is not defined
at .:program(:4)
at org.graalvm.polyglot.Context.eval(Context.java:341)
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:336)

Is there something I'm missing going to 1.0.0-rc15 (as this is working in 1.0.0-rc14)?

All 4 comments

I am sorry for your troubles. The root of the problem is that various security-sensitive operations (like an access to arbitrary Java classes/methods/fields) were allowed in our ScriptEngine by default in rc14. This has changed for rc15 (where all these operations are disabled by default). You can restore the behavior from rc14 by including polyglot.js.allowAllAccess binding with value true in the early stages of the creation of ScriptEngine.

In other words, if you add the following two lines

        Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
        bindings.put("polyglot.js.allowAllAccess", true);

then your test-case should work in rc15. See Graal JavaScript ScriptEngine implementation for more details.

No worries. Fixed the issue. Thanks for the quick response.

What about the org.graalvm.polyglot.Engine used in GraalVM, not the ScriptEngine. How do I make it work?

What about the org.graalvm.polyglot.Engine

Read section "Host Interoperability" in the Context doc. Provide allowAllAccess on your Context.builder. I'm a little skeptical about doing this (is it a security issue?) but it seemed to work. Perhaps there's a different subset of rules to enable what you want, but this will at least unblock you.

https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/Context.Builder.html#allowAllAccess-boolean-

Example:

    public static void main(String[] args) throws IOException {
        String someJs = "var FileClass = Java.type(\"java.io.File\"); print(FileClass);";
        Source source = Source.newBuilder("js", someJs, "foo.js")
                .build();

        try (Context context = Context.newBuilder("js")
                .allowAllAccess(true)
                .build()) {
            context.eval(source);
        }
    }

Outputs

class java.io.File
Was this page helpful?
0 / 5 - 0 ratings