I am trying to run this Graalvm sample code:
package org.homi.scripting.experimental;
import org.graalvm.polyglot.*;
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
import java.io.File;
import java.io.IOException;
public class ScriptEngine {
public static class Name {
@HostAccess.Export public String name = "hello";
@HostAccess.Export
public String getName() {
return name;
}
}
public static void main(String[] args) {
Name n = new Name();
Context context = Context.newBuilder("js")
.allowAllAccess(true)
.allowIO(true)
.allowPolyglotAccess(PolyglotAccess.ALL)
.build();
context.getPolyglotBindings().putMember("name", n);
context.eval("js", "var name = Polyglot.import('name');");
context.eval("js", "console.log(name.getName())");
}
}
I am getting this exception:
Exception in thread "main" TypeError: invokeMember (getName) on org.homi.scripting.experimental.Name@2313052e failed due to: Unknown identifier: getName
at <js> :program(Unnamed:1:12-25)
at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:379)
at ScriptEngine/org.homi.scripting.experimental.ScriptEngine.tester(ScriptEngine.java:43)
at ScriptEngine/org.homi.scripting.experimental.ScriptEngine.main(ScriptEngine.java:29)
I'm new to graalvm, what am I doing wrong? I was following this demo from the documentation (see the section on host interoperability): https://www.graalvm.org/sdk/javadoc/
The code and the stack trace don't match, for example the code lacks the tester
method
at ScriptEngine/org.homi.scripting.experimental.ScriptEngine.tester(ScriptEngine.java:43)
When you specify allowAllAccess(true)
you allow all access, so the HostAccess stops being necesary.
The code in the question works for me and prints hello as expected.