我认为它应该开箱即用。如果不是,它可能是一个错误。您能否向https://github.com/oracle/graaljs提交问题。
它在最简单的测试中对我有用,我正在尝试最新版本 21.1node
和js
.
which node
~/21.1/graalvm-ce-java11-21.1.0/bin/node
[opc@ol8-demo 21.1]$ node
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> try {
... if (typeof hostObj['propertyItDoesntHave'] === "undefined") {
..... throw new Error('first');
..... }
... } catch (e) {
... throw new Error('second');
... }
Uncaught Error: second
>
>
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
>
[opc@ol8-demo 21.1]$ js
> try {
> if (typeof hostObj['propertyItDoesntHave'] === "undefined") {
> throw new Error('first');
> }
> } catch (e) {
> throw new Error('second');
> }
>
Error: second
at <js> :program(<shell>:1:6:126-144)
>
在将 JS 嵌入 Java 应用程序时:
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.Context.Builder;
import org.graalvm.polyglot.proxy.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.eval("js",
"{ " +
"try {" +
" if (typeof hostObj['propertyItDoesntHave'] === \"undefined\") {" +
" throw new Error('first');" +
" }" +
"} catch (e) {" +
" throw new Error('second'); " +
" } " +
"}");
}
}
}
我确实得到了第二个:
javac Main.java && java Main
Exception in thread "main" Error: second
at <js> :program(Unnamed:1:123-141)
at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:379)
at Main.main(Main.java:12)