rhino

Switching from Rhino to Nashorn

被刻印的时光 ゝ 提交于 2019-11-28 18:16:00
I have a Java 7 project which makes a lot of use of Javascript for scripting various features. Until now I was using Rhino as script engine. I would now like to move to Java 8, which also means that I will replace Rhino by Nashorn. How compatible is Nashorn to Rhino? Can I use it as a drop-in replacement, or can I expect that some of my scripts will not work anymore and will need to be ported to the new engine? Are there any commonly-used features of Rhino which are not supported by Nashorn? One problem is that Nashorn can no longer by default import whole Java packages into the global scope

Javascript Engines Advantages

≡放荡痞女 提交于 2019-11-28 15:57:46
I am confused about JavaScript engines right now. I know that V8 was a big deal because it compiled JavaScript to native code. Then I started reading about Mozilla SpiderMonkey , which from what I understand is written in C and can compile JavaScript. So how is this different from V8 and if this is true, why does Firefox not do this? Finally, does Rhino literally compile the JavaScript to Java byte code so you would get all the speed advantages of Java? If not, why do people not run V8 when writing scripts on their desktops? There are various approaches to JavaScript execution, even when doing

testing an internal class

有些话、适合烂在心里 提交于 2019-11-28 11:33:26
how to write unit tests to internal classes ??? You write tests which specify the behaviour of the top-level class' external interface. Whether that class uses internal classes to implement that behaviour or not, is an implementation detail of the class, and the tests don't need to know anything about it. If the internal class cannot be adequately tested through the top-level class' interface, then it's usually best to move the internal class out and test it directly as a new top-level class. Wanting to test internal classes is a code smell that the internal class might be significant enough

How to invoke Rhino compiled JavaScript methods (class files) in the Java program?

女生的网名这么多〃 提交于 2019-11-28 11:30:00
I compiled following JavaScript file, "test.js", into the "test.class" : var test = (function () { var that = {}; that.addNumbers = function (a, b) { return a+b; }; return that; }()); I would like to call the compiled JavaScript function, "test.addNumbers(1,2)", in the simple Java program "run.java" as follows : public class run { public static void main(String[] args) throws Exception { Context cx = Context.enter(); try { Scriptable scope = cx.initStandardObjects(); // HOW TO CALL THE METHOD, Test.addNumbers(1,2)? Please help me! } finally { Context.exit(); } } } I tried many ways, but failed

ReferenceError: “alert” is not defined

梦想的初衷 提交于 2019-11-28 10:54:31
I am trying to call a java script function from java code. Here is my Java code public static void main(String[] args) throws FileNotFoundException { try { /** * To call a anonymous function from java script file */ ScriptEngine engine = new ScriptEngineManager() .getEngineByName("javascript"); FileReader fr = new FileReader("src/js/MySpec.js"); engine.eval(fr); } catch (ScriptException scrEx) { scrEx.printStackTrace(); } } Here is my java script file: (function() { alert("Hello World !!!"); })(); But when I run main method of driver class it is giving me error as below: Exception in thread

Rhino: How to call JS function from Java

China☆狼群 提交于 2019-11-28 04:35:34
I'm using Mozilla Rhino 1.7r2 (not the JDK version), and I want to call a JS function from Java. My JS function is like this: function abc(x,y) { return x+y } How do I do this? Edit: (The JS function is in a separate file) String script = "function abc(x,y) {return x+y;}"; Context context = Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); Scriptable that = context.newObject(scope); Function fct = context.compileFunction(scope, script, "script", 1, null); Object result = fct.call( context, scope, that, new Object[] {2, 3}); System.out.println(Context.jsToJava

Can Nashorn startup slowness be overcome?

强颜欢笑 提交于 2019-11-27 23:54:09
问题 I've used Rhino for a scripting component inside graphics. In the project there are about 200 small scripts running independantly. Immediately when starting the application the scripts should be at full speed. Rhino's performance was sufficient, but since Oracle advices to migrate to Nashorn, i'm facing a dilema. Below a picture showing the load difference between Rhino and Nashorn at approximayely 15,000 invocations of the scripts. The Startup slowness of Nashorn is my biggest issue. Note,

How to convert Rhino-JavaScript arrays to Java-Arrays

旧街凉风 提交于 2019-11-27 22:04:51
I have the following: ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); jsEngine.eval("function getArray() {return [1,2,3,4,5];};"); Object result = jsEngine.eval("getArray();"); How can i convert the result object which is of type sun.org.mozilla.javascript.internal.NativeArray to a corresponding java array? Can somone show me a working code sample where this is done? It should work for String and Integer arrays. Plus, it would be great to know where to look for other data type conversions between the rhino engine and java. Btw, i

Problems using Rhino on Android

 ̄綄美尐妖づ 提交于 2019-11-27 20:37:14
I'm trying to use Mozilla Rhino in my Java application for Android to evaluate some JavaScript. I am using Eclipse + ADT plugin. First I tried simply downloading the Rhino .jar file from Mozilla's website and adding it to the project as a library in Eclipse. Eclipse recognised it fine and compiled the application. However, when running it I get an exception when calling Context.evaluateReader() (see below for stack trace). Then I tried adding the Rhino source code as a separate Android project in Eclipse, marking it as a library and referencing it in my project, which was enough to get Eclipse

ANT script to compile all (css) LESS files in a dir and subdirs with RHINO

谁都会走 提交于 2019-11-27 19:54:33
I want do compile all *.less scripts in a specific folder and it subdirs with less-rhino-1.1.3.js . There is an example on github for doing this for a specific file, which works perfect. But I want to do the same for a complete folder. I tried a lot, here is my last try. It doesn't work, propertyregex seems not to be standard ANT, I don't want to use such things. I am not even sure if this code would work. <project name="test" default="main" basedir="../../"> <property name="css.dir" location="public/css"/> <property name="tool.less" location="bin/less/less-rhino-1.1.3.js"/> <property name=