想要从Nashorn迁移到GraalVM。已安装Graal VM CE
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-jvmci-19.3-b05-LTS)
OpenJDK 64-Bit GraalVM CE 19.3.0 (build 11.0.5+10-jvmci-19.3-b05-LTS, mixed mode, sharing)
测试应用
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
public class Main {
public static void main(String[] args) {
Context context = Context.newBuilder("js").allowHostAccess(HostAccess.ALL).build();
context.eval("js", "var FileClass = Java.type(\"java.io.File\");");
}
}
例外:
Exception in thread "main" ReferenceError: Java is not defined
at <js> :program(Unnamed:1:16-19)
at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:370)
at task.Main.main(Main.java:9)
怎么了?
答案 0 :(得分:1)
据我了解,这是正确的选择:
Context context = Context.newBuilder("js")
.allowHostClassLookup(s -> true)
.allowHostAccess(HostAccess.ALL)
.build();
HostAccess.ALL –预定义的主机访问策略,该策略允许完全不受限制地访问公共方法或公共主机的字段 课程。
...但是我们还需要更改默认过滤谓词org.graalvm.polyglot.Context#UNSET_HOST_LOOKUP
,该谓词始终返回false
:
allowHostClassLookup
–默认情况下,并且如果所有访问均为false,则禁用主机类查找。
因此,无需过滤allowHostClassLookup(s -> true)
。