有没有办法在 GraalJS 中使用 java 对象作为函数的参数和 return 所述对象的值之一?
Is there a way to use a java object as an argument for a function and return one of said objects's values in GraalJS?
我想使用 Java 对象 new Train()
作为参数传递给 Java 脚本函数,这里是 Java 代码
public void execute() {
File script = new File("/Test.js");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
try {
engine.eval(new FileReader(script));
Invocable invocable = (Invocable)engine;
Object result = invocable.invokeFunction("fun1", new Train());
System.out.println(result);
} catch(Exception e) {
e.printStackTrace();
}
}
public class Train {
private double speed = 0.0D;
public Train() {
this.speed = 10.5D;
}
public double getSpeed() {
return this.speed;
}
}
Java脚本代码
var fun1 = function test(train) {
print(train.getSpeed());
return train.getSpeed();
}
截至目前,它会在控制台中显示此错误
[16:56:42] [INFO]: [STDERR]: javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (getSpeed) on ScriptExecutor$Train@429b2a7b failed due to: Unknown identifier: getSpeed [16:56:42] [INFO]: [STDERR]: at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.toScriptException(GraalJSScriptEngine.java:483) [16:56:42] [INFO]: [STDERR]: at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.invokeFunction(GraalJSScriptEngine.java:558)
有什么方法可以实现吗?
GraalJS(和一般的 GraalVM)默认有严格的 security/access 控制。 GraalJS 没有将 Train
实例上的 getSpeed()
(或任何其他字段或方法)暴露给 JavaScript。
您可以使用配置设置打开对所有主机 fields/methods 的访问权限:
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.allowHostAccess", true);
或者通过在 Train
:
中修饰 getSpeed()
来在更细粒度的级别上启用它
import org.graalvm.polyglot.HostAccess;
// ...
@HostAccess.Export
public double getSpeed() {
return this.speed;
}
我想使用 Java 对象 new Train()
作为参数传递给 Java 脚本函数,这里是 Java 代码
public void execute() {
File script = new File("/Test.js");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
try {
engine.eval(new FileReader(script));
Invocable invocable = (Invocable)engine;
Object result = invocable.invokeFunction("fun1", new Train());
System.out.println(result);
} catch(Exception e) {
e.printStackTrace();
}
}
public class Train {
private double speed = 0.0D;
public Train() {
this.speed = 10.5D;
}
public double getSpeed() {
return this.speed;
}
}
Java脚本代码
var fun1 = function test(train) {
print(train.getSpeed());
return train.getSpeed();
}
截至目前,它会在控制台中显示此错误
[16:56:42] [INFO]: [STDERR]: javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (getSpeed) on ScriptExecutor$Train@429b2a7b failed due to: Unknown identifier: getSpeed [16:56:42] [INFO]: [STDERR]: at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.toScriptException(GraalJSScriptEngine.java:483) [16:56:42] [INFO]: [STDERR]: at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.invokeFunction(GraalJSScriptEngine.java:558)
有什么方法可以实现吗?
GraalJS(和一般的 GraalVM)默认有严格的 security/access 控制。 GraalJS 没有将 Train
实例上的 getSpeed()
(或任何其他字段或方法)暴露给 JavaScript。
您可以使用配置设置打开对所有主机 fields/methods 的访问权限:
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.allowHostAccess", true);
或者通过在 Train
:
getSpeed()
来在更细粒度的级别上启用它
import org.graalvm.polyglot.HostAccess;
// ...
@HostAccess.Export
public double getSpeed() {
return this.speed;
}