nashorn

getEngineByName(“nashorn”) returns null

怎甘沉沦 提交于 2019-12-06 22:32:16
问题 Cant get Nashorn engine ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("print('Hello World!');"); engine returns null I am using eclipse, jdk1.8.0_11 java -version java version "1.8.0_20-ea" Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b23) 回答1: Its working when I pass null param into ScriptEngineManager constructor: ScriptEngine engine = new ScriptEngineManager(null).getEngineByName("nashorn"); engine.eval("print('Hello World!');"); from java

How can I pass a proper method reference in so Nashorn can execute it?

混江龙づ霸主 提交于 2019-12-06 12:37:14
问题 I am trying to write a library that will let me execute JSON Logic rules via the Nashorn Javascript engine. My problem right now is specifically around the JSObject wrapper I've created to handle moving data from Java/Kotlin into the scripting engine. If an array is passed in such as [true] it is wrapped and the json-logic script will receive it, see that it is an array, and attempt to run the following bit of code: if(Array.isArray(logic)) { return logic.map(function(l) { return jsonLogic

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

微笑、不失礼 提交于 2019-12-06 10:26:03
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.keySet()) { nobj.defineProperty((String)k, m.get(k), NativeObject.READONLY); } engine.eval("function

How to convert Java Pojo to Nashorn Json?

会有一股神秘感。 提交于 2019-12-06 08:53:18
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 json. You can convert json from engine by ScriptEngine engine = new ScriptEngineManager().getEngineByName(

Casting Array Result From JavaScript to Something Useful Using ScriptObjectMirror

眉间皱痕 提交于 2019-12-06 07:44:18
If I have a JS function that returns an array of strings, how should I go about casting it to a useful type (I'm thinking either a Java array or Collection class)? I've noticed that the return type is always a ScriptObjectMirror which has an interesting to(Class<?> clazz) method, but I'm uncertain on it's use. I've checked several other StackOverflow questions, but none were useful. Can you show an example? Ok this worked for me: ScriptObjectMirror result = (ScriptObjectMirror) function.invokeFunction("nameGen", 10); String[] strings = result.to(String[].class); 来源: https://stackoverflow.com

Nashorn Abstract Syntax Tree Traversal

孤街醉人 提交于 2019-12-06 06:27:21
问题 I am attempting to parse this Javascript via Nashorn: function someFunction() { return b + 1 }; and navigate to all of the statements. This including statements inside the function. The code below just prints: "function {U%}someFunction = [] function {U%}someFunction()" How do I "get inside" the function node to it's body "return b + 1"? I presume I need to traverse the tree with a visitor and get the child node? I have been following the second answer to the following question: Javascript

Passing undefined to Nashorn Javascript in Scala/Java

女生的网名这么多〃 提交于 2019-12-06 05:48:51
问题 I need to evaluate this function in Javascript from Scala/Java function hello(a, b) { return a+b; } I did this basic code: val factory = new ScriptEngineManager(null) val engine = factory.getEngineByName("JavaScript") val body = """ |function hello(a, b) { | return a+b; |} """.stripMargin engine match { case engine: Invocable => engine.eval(body) println(engine.invokeFunction("hello", null, 1: java.lang.Double)) } For the parameter a I'm passing a null and I get a 1.0 as a result. If I hack

How to access Java Enums from Javascript (Java 1.8)

孤者浪人 提交于 2019-12-06 04:54:37
问题 In Java 1.7, prior to it's removal, one could use 'Packages' to access Java Enums in the following way from Javascript on an HTML page viewed a browser: var enumvar1 = document.appletid.Packages.com.mycompany.MyClass$MyEnumYesNo.YES var enumvar2 = document.appletid.Packages.com.mycompany.MyClass$MyEnumYesNo.NO I'm upgrading these HTML pages to use Java 1.8 (which now uses the Nashorn javascript engine), and I cannot seem to figure out how to access the Enum members. I've rewritten the Java

Nashorn JSON.parse() - java.lang.OutOfMemoryError: Java heap space - JDK8u60

徘徊边缘 提交于 2019-12-06 04:18:24
Nashron Release notes claims they fixed the JSON parser bugs, but I am still able to produce a (different) bug on new patch 8u60. This time it is OutOfMemoryError. Refer the attached JSON [1] (it is typically a Category & Subcategory relation). When I try to invoke JSON.parse() it is failing. [1] http://jsfiddle.net/manivannandsekaran/rfftavkz/ I tried to increase the heap size, didn't help, instead of getting the OOM Exception quickly, it delayed bit. When I replace all the integer key with Alpahnumberic, the entire parsing time is super fast. [2] [2] https://jsfiddle.net/manivannandsekaran

Differ null and undefined values in Nashorn

放肆的年华 提交于 2019-12-06 01:28:43
问题 I'm running this code ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("var out;"); engine.eval("var out1 = null;"); Object m = engine.get("out"); Object m1 = engine.get("out1"); And getting m == null and m1 == null. How to determine if value is undefined or null? 回答1: Java doesn't have a concept of "undefined", so understanding the distinction will require expressing it in the script's language. I suggest using this expression: Boolean isUndefined =