现在,我正在尝试通过GraalJS在Java应用程序中执行Javascript。下面是我的代码:
import javax.script.*;
import java.util.List;
import java.util.Map;
public class ScriptExec {
public static void main(String[] args) throws ScriptException {
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Bindings bindings = engine.createBindings();
// variables
int num = 9;
String hello = "bye";
String[] strings = new String[]{"a", "string", "array"};
List<String> stringList = List.of("a", "string", "list");
Map<String, Object> kvs = Map.of("a", "b", "object", "...", "map", "container");
bindings.put("num", num);
bindings.put("hello", hello);
bindings.put("strings", strings);
bindings.put("stringList", stringList);
bindings.put("kvs", kvs);
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
String js = """
console.log(num)
console.log(hello)
console.log(JSON.stringify(strings))
console.log(JSON.stringify(stringList))
console.log(JSON.stringify(kvs))
for(let str in strings) {
console.log('str: ' + str)
}
for(let k in kvs) {
console.log('key: ' + k)
}
""";
engine.eval(js);
}
}
- 控制台输出为:
9
bye
{}
{}
{}
如您所见,只有整型和String变量绑定成功。
- JDK版本:17
- GraalJS依赖项:
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>22.3.2</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>22.3.2</version>
</dependency>
1.数组、列表和Map类型变量是怎么回事?2.如何解决这个问题?
1条答案
按热度按时间luaexgnf1#
好的,我从here找到答案
上面代码,没有显式创建ScriptEngine。Graal的多语言环境起了作用。
也可以先创建ScriptEngine,然后将特定的多语言上下文传递给它。
special
表示主机资源访问控制配置(allowXXX...)。如果有新内容,请稍后发布