nashorn

How to pass types and functions to a JSR-223 script?

走远了吗. 提交于 2019-12-12 04:29:44
问题 A typical JSR-223 script would begin with a series of surrogate imports like this (JavaScript+Nashorn chosen for example purposes): // "import" classes and static methods var Foo = Java.type("my.package.Foo"); // application classes require Java.type() use var bar = Foo.bar; // static method var Logger = java.util.logging.Logger; // system classes can be accessed directly var sin = java.lang.Math.sin; // the same for static methods // use them var foo = new Foo(); print(bar()); var log =

Nashorn: How to pre-set Java.type()-vars inside of Java before JavaScript execution?

耗尽温柔 提交于 2019-12-12 03:28:35
问题 I am currently executing my JavaScript-scripts with this java code: ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval(new FileReader("awesome_script.js")); I need to call Java functions from JavaScript, so I defined this at the top of my awesome_script.js file: var first = Java.type('io.github.awesomeprogram.FirstClass'); var second = Java.type('io.github.awesomeprogram.SecondClass'); var extra = Java.type('io.github.awesomeprogram.ExtraClass'); I can

Safely re-using sandboxed Nashorn containers

对着背影说爱祢 提交于 2019-12-12 03:04:25
问题 I'm implementing a sandboxed JS environment that allows users to upload their JS code and trigger it based on a set of rules. I disabled Java access from Nashorn environment and only allow accessing some utility classes for a few operations such as HTTP requests, base64 encoding etc. Currently, I create ScriptEngine (Nashorn environment) for each JS code uploaded by users but in our environment we have many pre-defined JS code which are used by most of our users. Since creating ScriptEngine

Writing a `jjs` shebanged script that accepts just arguments

偶尔善良 提交于 2019-12-12 00:36:15
问题 Supposedly jjs is the Java 8 version of nashorn, ready for shell-scripting. However when I write a file with execute permission as: #!/usr/bin/jjs arguments.forEach(function(v,i,a){print(v);}); Running it produces some not so ideal shell-scripting behavior: $./file.js $./file.js one two java.io.IOException: one is not a file $./file.js -- one two one two $./file.js file.js -- one two # what were they thinking one two one two $ So, I don't ever want this script I'm writing to allow for the

empty range in char class in <eval> when trying to run browser.js on Nashorn

橙三吉。 提交于 2019-12-11 13:09:48
问题 I'm trying to run browser.js in Nashorn, to use babel.transform in the library for some Isomorphic Webapp. public BabelTransformer() throws ScriptException, IOException{ try { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine nashorn = mgr.getEngineByName("nashorn"); nashorn.eval(getScript("com/facebook/babel/node_modules/babel-core/browser.min.js")); //The line 38 babel = (ScriptObjectMirror)nashorn.eval("babel"); invocable = (Invocable) nashorn; } catch(ScriptException |

Supply JavaScript date to Nashorn script

核能气质少年 提交于 2019-12-11 03:33:44
问题 I am working on an API in Java that allows users to write scripts and access a specific set of methods that are passed in (in the form of an API object) by the Nashorn script engine. I want to, in the JavaScript, call a function getDate(), which will return some arbitrary date (as a native JavaScript date) that's provided from the Java side. I have tried simply putting an org.java.util.Date on the API object, but that won't behave like a JS date. The goal is to make this as simple as possible

Java 8/Javascript (Nashorn) long interoperatiblity

故事扮演 提交于 2019-12-11 03:07:08
问题 The following Javascript code executed in Java 8 (Nashorn) does not behave as expected : if( a != b ) { do_sth(); } a and b are long values coming from Java object (e.g., 1023948, 1023949). For example, when a = 1023949 and b = 1023949, a != b is true. Note that the following code works fine: if( (a+0) != (b+0) ) { do_sth(); } I know about long precision issue (as Javascript numbers are 64 doubles) but I was expecting that "small" long values should work. Any input is appreciated. Thx. 回答1: I

Nashorn class not found in WildFly

百般思念 提交于 2019-12-11 01:58:36
问题 I need to access an internal class from nashorn inside a web application running in WildFly. The following code is working fine in my machine: public class NashornTest { public static void main(String[] args) throws ClassNotFoundException { Class<?> cls = Class.forName("jdk.nashorn.internal.objects.ScriptFunctionImpl"); System.out.println(cls); } } But the following servlet is throwing a ClassNotFoundException when I run inside WildFly: @WebServlet("/nashorn") public class NashornServlet

Does it make sense to load scripts concurrently in Java 8 Nashorn JavaScript engine

半腔热情 提交于 2019-12-10 20:55:38
问题 Does it make sense to load scripts concurrently in Java 8 Nashorn JavaScript engine for faster startup? Will it rize any problems even if scripts do not modify global variables? I didn't find any information in javax.script.ScriptEngine javadocs. Moreover, can Nashorn itself load scripts in parallel when engine.eval(...) is called from multiple threads at the same time? Is it safe to do so? If it doesn't, the whole idea of adding parallelism to scripts loading process is doomed. 回答1: It may

Use java class in Graal.js

*爱你&永不变心* 提交于 2019-12-10 19:47:01
问题 Using Graal.js, how can I import a java class to script within JS? The following code works with Nashorn JJS, but does not work with Graal.js because there is no Java.type() in graal, do I need to invoke truffle at some point? var ArrayList = Java.type("java.util.ArrayList"); var myList = new ArrayList(); myList.add("hello"); myList.add("world"); print(myList); EDIT: I was able to get it to import java types using the --jvm paramter, which seems to indicate that it runs it on the JVM. So does