简体   繁体   English

从GraalVM本机图像中的Java代码运行JS脚本

[英]Running JS script from Java code in GraalVM native-image

I am experimenting with running JS scripts from Java code within a GraalVM native-image. 我正在尝试从GraalVM本机映像中的Java代码运行JS脚本。

The Java code looks like this: Java代码如下所示:

try (Context context = Context.create("js")) {
    Value bindings = context.getBindings("js");
    bindings.putMember("response", response);
    bindings.putMember("UTF8", StandardCharsets.UTF_8);
    context.eval("js", script);
} catch (PolyglotException e) {
    error("Error: " + e, 10);
}

The JS code just tries to use the response object by calling a method on it, for example: JS代码只是通过调用response对象来尝试使用response对象,例如:

 print("Status code: " + response.getStatusCode());

This works when running in GraalVM, but when creating a native-image, it fails with this error: 在GraalVM中运行时可以使用,但是在创建本机映像时会失败,并显示以下错误:

INVOKE on JavaObject[...] failed due to: Message not supported: INVOKE

If I just print the object as in print("Response: " + response); 如果我只是像print("Response: " + response);一样打印对象print("Response: " + response); , it does not fail. ,它不会失败。 But if I attempt to call any method on response , I get this error (even toString() or hashCode() ). 但是,如果尝试在response上调用任何方法,则会收到此错误(甚至是toString()hashCode() )。

Is there something else I need to do or is this just a bug in SubstractVM native-image, currently? 目前,我还需要做其他事情吗?或者这仅仅是SubstractVM本机映像中的错误?

My GraalVM version: 我的GraalVM版本:

java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
GraalVM 1.0.0-rc4 (build 25.71-b01-internal-jvmci-0.45, mixed mode)

native-image command I am using: 我正在使用的native-image命令:

native-image --language:js --report-unsupported-elements-at-runtime -jar my.jar

Update: as of RC 13 reflective access with native image is now supported. 更新:从RC 13开始,现在支持使用本机映像进行反射访问。 You need to provide a reflection config to native image. 您需要为本地映像提供一个反射配置。

Unfortunately GraalVM, as of RC5, doesn't yet support reflective access of Java objects when compiled using native-image. 不幸的是,从RC5开始,GraalVM在使用本机映像编译时还不支持Java对象的反射访问。 We plan to support this in one of the next release candidates. 我们计划在下一个发行候选版本中对此提供支持。

As a temporary workaround you may use the Proxy API like this: 作为临时的解决方法,您可以使用Proxy API,如下所示:

try (Context context = Context.create("js")) {
    Map<String, Object> myObject = new HashMap<>();
    myObject.put("foo", "bar");
    context.getBindings("js").putMember("hostObject", ProxyObject.fromMap(myObject));
    assert "bar".equals(context.eval("js", "hostObject.foo").asString());
    myObject.put("foo", "baz");
    assert "baz".equals(context.eval("js", "hostObject.foo").asString());
}

The Proxy API allows to mimic guest language values. 代理API允许模仿来宾语言值。

Here is another Proxy Example: http://www.graalvm.org/docs/graalvm-as-a-platform/embed/#computed-arrays-using-polyglot-proxies 这是另一个代理示例: http : //www.graalvm.org/docs/graalvm-as-a-platform/embed/#computed-arrays-using-polyglot-proxies

Proxy Javadoc: http://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/proxy/package-summary.html 代理Javadoc: http : //www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/proxy/package-summary.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM