nashorn

How do you invoke a method in a Nashorn CompiledScript?

旧巷老猫 提交于 2020-01-05 17:18:44
问题 I have the following code which works: ScriptEngine jsEngine = ScriptEngineManager.new().getEngineByName("nashorn"); jsEngine.eval("some script"); jsEngine.invokeMethod(jsEngine.eval("foo"), "bar"); but I want to do use a pre-compiled script so I don't have to evaluate the script every time I need to run it, so I'm trying; ScriptEngine jsEngine = ScriptEngineManager.new().getEngineByName("nashorn"); CompiledScript compiledJS = jsEngine.compile("some script"); but then I'm not sure what to do

Java ScriptEngine (nashorn & rhino) : how to stack scopes / bindings?

放肆的年华 提交于 2020-01-04 17:54:47
问题 I have to port a software using Rhino1.7R4 and its org.mozilla.javascript package to use the javax.script package and its ScriptEngine ( Rhino in Java 6 & 7, Nashorn in Java 8). The main problem is to stack scopes (Bindings). Using the Rhino jar, I do: Scriptable scope ... Scriptable newScope = javascriptContext.initStandardObjects(); newScope.setParentScope(scope); So if a variable is defined without var, it's a global variable (root scope) if a variable is defined with var, it's a local

Seamlessly pass Arrays and Lists to and from Nashorn

倾然丶 夕夏残阳落幕 提交于 2020-01-01 05:00:07
问题 I know you can work with Java arrays in Nashorn and there are plenty of examples of how to do this. The problem for me with the standard approach is that it makes the javascript code explicitly aware of it's runtime environment. Currently I have a solution that makes use of Rhino and it seamlessly converts between Java type and Native javascript types. For Rhino I accomplished this by implementing org.mozilla.javascript.ContextFactory and org.mozilla.javascript.WrapFActory and setting

Java Scripting With Nashorn (JSR 223) & Pre-compilation

拜拜、爱过 提交于 2019-12-30 00:39:21
问题 I am using Nashorn via JSR 223 to execute small snippets of user entered script: public Invocable buildInvocable(String script) throws ScriptException { ScriptEngine engine = new ScriptEngineManager().getEngineByName(ENGINE); engine.eval(functions); engine.eval(script); return (Invocable) engine; } The varying user script calls JavaScript functions that are defined in a static, central library (held in the functions String in the code snippet above). Every time I want to get hold of an

How to serialize a Predicate<T> from Nashorn engine in java 8

纵饮孤独 提交于 2019-12-25 06:50:04
问题 How can i serialize a predicate obtained from java ScriptEngine nashorn? or how can i cast jdk.nashorn.javaadapters.java.util.function.Predicate to Serializable? Here is the case: I have this class import java.io.Serializable; import java.util.function.Predicate; public class Filter implements Serializable { private Predicate<Object> filter; public Predicate<Object> getFilter() { return filter; } public void setFilter(Predicate<Object> filter) { this.filter = filter; } public boolean evaluate

Nashorn TypeError: Cannot call undefined in <eval>

强颜欢笑 提交于 2019-12-25 03:35:10
问题 While running below code, I am getting error. I have no idea what is causing this error. ScriptEngine engine = engineManager.getEngineByName("nashorn"); String str = "var shape_objects = [ Java.Type(\"new Triangle()\"), Java.Type(\"new Circle()\"), Java.Type(\"new Rectangle()\"), Java.Type(\"new Shape()\")];"+ "var colors = [\"Red\", \"Green\", \"Blue\", \"Abstract\"];"+ "var j = 0;"+ "for(var i in shape_objects) {"+ " shape_objects[i].setColor(colors[j]);"+ " j = j+1;"+ "}"+ "for(var k in

Nashorn TypeError: Cannot call undefined in <eval>

时光怂恿深爱的人放手 提交于 2019-12-25 03:35:02
问题 While running below code, I am getting error. I have no idea what is causing this error. ScriptEngine engine = engineManager.getEngineByName("nashorn"); String str = "var shape_objects = [ Java.Type(\"new Triangle()\"), Java.Type(\"new Circle()\"), Java.Type(\"new Rectangle()\"), Java.Type(\"new Shape()\")];"+ "var colors = [\"Red\", \"Green\", \"Blue\", \"Abstract\"];"+ "var j = 0;"+ "for(var i in shape_objects) {"+ " shape_objects[i].setColor(colors[j]);"+ " j = j+1;"+ "}"+ "for(var k in

New Java Array of Strings in Nashorn

笑着哭i 提交于 2019-12-24 04:59:08
问题 I'm writing a high-performance nashorn application, and what I'd really like to do is find something equivalent to new String[]{"foo", "bar", "noise"} from within javascript. The cost of converting from a javascript array to a java array is prohibitively expensive, shows up on every flame graph. The best I've found up to this point is: {var StringArray = Java.type('java.lang.String[]');"); var arr = new StringArray(3)); var arr[0] = 'foo'; var arr[1] = 'bar'; var arr[2] = 'noise'; arr; } But

Multi-threaded Nashorn: o.constructor === o.constructor gives false

≡放荡痞女 提交于 2019-12-24 04:09:14
问题 I'm experimenting with multi-threaded scripts loading and evaluation in Nashorn and get kind of shocking behavior: // having some object o loaded in another thread print(o.constructor === o.constructor); // false print(o.constructor === Object); // false as well print(o.foo === o.foo); // true - OK How can this be possible within single script engine? o above is just an object created using object literal notation (in another thread). Printing o.constructor gives usual function Object() {

JavaFX development with just JavaScript (Nashorn)

戏子无情 提交于 2019-12-24 00:59:12
问题 Is there a way to write a JavaFX app with just JavaScript files now that Nashorn is available in Java 8? Where can I find more info or videos on this? How would you compile it? Or is there a minimal set of bootstrapping files required? 回答1: Yes, you can develop JavaFX programs with just JavaScript (Nashorn). How to access JavaFX from JavaScript Use the jjs -fx yourscript.js command line switch. The -fx flag on jjs will bootstrap scripts using a javafx.application.Application. There is no