nashorn

Coercion of Enums in Nashorn works differently than in Rhino

主宰稳场 提交于 2019-12-10 17:34:32
问题 It seems that Java enums are no longer coerced into their string value: java.math.RoundingMode.UP == "UP" evaluates to true in Java 7 but to false in Java 8 when executed in the JavaScript engine. Does anybody know if this is actually a bug or just something in the specification? Thanks! 回答1: This is a bug: https://bugs.openjdk.java.net/browse/JDK-8072426 ..................................................................... 来源: https://stackoverflow.com/questions/28152990/coercion-of-enums-in

Nashorn: Call function inside of a namespace

六月ゝ 毕业季﹏ 提交于 2019-12-10 16:48:18
问题 I have evaluated the following script using the NashornScriptEngine : var Namespace = { test: function() { return "It works"; } } Now I want to call the function test . When using the method invokeFunction of the nashorn engine, the following exception is thrown: java.lang.NoSuchMethodException: No such function Namespace.test How is it possible to call this function? 回答1: You are trying to access a global function called window["Namespace.test"] , not window.Namespace.Test . You first need

Reading Nashorn JO4 and NativeArray

时光总嘲笑我的痴心妄想 提交于 2019-12-10 14:57:43
问题 Java calling code: import jdk.nashorn.api.scripting.*; .... myCustomHashMap dataStore = new myCustomHashMap(); ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine engine = sem.getEngineByName("nashorn"); engine.put("dataStore",dataStore); engine.eval(new java.io.FileReader("test.js")); ((Invocable)engine).invokeFunction("jsTestFunc", "testStr" ); Javascript: function jsTestFunc (testParam) { dataStore.a = [1,2,3]; dataStore.b = {First:"John",Last:"Doe",age:37}; } Goal: I need to

sun.org.mozilla.javascript.internal.NativeObject vs org.mozilla.javascript.NativeObject

杀马特。学长 韩版系。学妹 提交于 2019-12-10 11:19:22
问题 I am really stuck at this now.. Essentially I have a Java Map, which I would like to pass it to a Javascript Code, so that in my JS code I can use dot notation to refer the keys in this Map. ( I know I can serialize the map into JSON and deserialize it back and pass it into JS, but I don't like that ) I have this piece of the unit code @Test public void mapToJsTest() throws Exception{ Map m = Maps.newHashMap(); m.put("name", "john"); NativeObject nobj = new NativeObject(); for (Object k : m

Add Java-implemented function into Nashorn's global scope

微笑、不失礼 提交于 2019-12-10 10:16:34
问题 I'm trying to migrate/update my project to use Nashorn from Rhino. I have some global utility functions implemented in Java and added into global scope of the target script engine, typical example is log(message) . In Rhino it is implemented via public static class LogFunction extends org.mozilla.javascript.BaseFunction { @Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { ... } } whose instance added into target scope. What has to be done in case

Java 脚本引擎入门

天大地大妈咪最大 提交于 2019-12-10 05:04:43
Java Script Engine Java 脚本引擎可以将脚本嵌入Java代码中,可以自定义和扩展Java应用程序,自JDK1.6被引入,基于Rhino引擎,JDK1.8后使用Nashorn引擎,支持ECMAScript 5,但后期还可能会换。 脚本引擎包位于javax.script中,各个类名及描述如下 接口 Bindings 键值对映射,所有key都为String Compilable 由具体的脚本引擎实现,用于将脚本进行编译,可重复使用。 Invocable 由具体的脚本引擎实现,其允许调用先前已执行的脚本 ScriptContext 脚本引擎上下文,用于将应用程序与脚本引擎进行绑定 ScriptEngine 由具体的脚本引擎实现,定义了执行脚本的方法、键值对映射关系、脚本引擎上下文 ScriptEngineFactory 脚本引擎工厂,每一个ScriptEngine都有一个对应的工厂。ScriptEngineManager会从ClassLoader中获取所有的ScriptEngineFactories实例 类 AbstractScriptEngine ScriptEngine的抽象实现类,提供了ScriptEngine的标准实现 CompiledScript 由存储编译结果的类扩展。可以以Java类、Java类文件或者脚本操作码的形式存储,可以重复执行无需重新解析

Nashorn bug JDK8u40 “Method Code Too Large”

吃可爱长大的小学妹 提交于 2019-12-08 08:21:25
问题 The Nashorn eval of a js script on a method invoked on an object that is actually implemented by its superclass, delegates to a dynamic linker. This iterates through the entire hierarchy of the class and builds a classWriter on all its methods. If the superclasses contain few 1000's of methods it still emits the "Method Code Too Large" error exceeding the 64K limit JDK defines. var obj = SomeInitCode.getObjectOfCustomType(); var xyz = obj.doSomeOperation() // this is method implemented on obj

Nashorn bug JDK8u40 “Method Code Too Large”

假装没事ソ 提交于 2019-12-08 03:54:40
The Nashorn eval of a js script on a method invoked on an object that is actually implemented by its superclass, delegates to a dynamic linker. This iterates through the entire hierarchy of the class and builds a classWriter on all its methods. If the superclasses contain few 1000's of methods it still emits the "Method Code Too Large" error exceeding the 64K limit JDK defines. var obj = SomeInitCode.getObjectOfCustomType(); var xyz = obj.doSomeOperation() // this is method implemented on obj's superclass which is Abstract . The Nashorn engine then tries to create an Adaptor which thereafter

How to convert Java Pojo to Nashorn Json?

心不动则不痛 提交于 2019-12-08 02:43:14
问题 I have a Java object that I want to turn into a json object and pass to the Nashorn javascript engine. It is surprisingly difficult to google an answer for this! Can someone tell me how to do it? I tried this: ObjectMapper mapper = new ObjectMapper(); String inputModelAsString = mapper.writeValueAsString(inputModel); And then passing the string json to the function: result = invocable.invokeFunction(PROGRAM_FUNCTION, moduleName, inputModelAsString); But it was passed as a string, not as a

Nashorn access non-static Java method

China☆狼群 提交于 2019-12-07 20:11:08
问题 In Java 7 (1.7), I could access a Java method from JavaScript by running this: ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript"); jse.eval("importClass(net.apocalypselabs.symat.Functions);"); jse.eval("SyMAT_Functions = new net.apocalypselabs.symat.Functions();"); String input = "notify(\"Foo\");"; // This is user input jse.eval("with(SyMAT_Functions){ "+input+" }"); Which would run the notify() function from the Functions java class: public class Functions { private