rhino

How can you run Javascript using Rhino for Java in a sandbox?

你说的曾经没有我的故事 提交于 2019-11-27 17:13:11
Part of our java application needs to run javascript that is written by non-developers. These non-developers are using javascript for data formatting. (Simple logic and string concatenation mostly). My question is how can I setup the execution of these scripts to make sure scripting errors don't have a major negative impact on the rest of the application. Need to guard against infinite loops Guard against spawning new threads. Limit access to services and environment File system (Example: If a disgruntled script writer decided to delete files) Database (Same thing delete database records)

Has anyone used or written an Ant task to compile (Rhino) JavaScript to Java bytecode?

末鹿安然 提交于 2019-11-27 12:31:31
I'd like to use the Rhino JavaScript compiler to compile some JavaScript to .class bytecode files for use in a project. It seems like this should already exist, since there are groovyc, netrexxc, and jythonc tasks for Groovy, NetREXX(!) and Jython, respectively. Has anyone used or written such an Ant task, or can anyone provide some tips on how to write one? Ideally it would have some way to resolve dependencies among JavaScript or Java classes. Why not simply use java task? <java fork="yes" classpathref="build.path" classname="org.mozilla.javascript.tools.jsc.Main" failonerror="true"> <arg

What is the lifecycle and concurrency semantics of Rhino Script Engine

孤者浪人 提交于 2019-11-27 11:43:45
问题 I am interested in the lifecycle and concurrency semantics of (Rhino) Script Engine and associated classes. Specifically: Is Bindings supposed to be thread safe? Should multiple threads be allowed to share a single ScriptEngine instance? ... or should each thread construct a short-lived instance? ... or keep them in a pool? What happens if multiple threads concurrently call ScriptEngine.eval(...) ? Same questions for CompiledScript instances Same questions for interface implementations

Switching from Rhino to Nashorn

…衆ロ難τιáo~ 提交于 2019-11-27 11:10:01
问题 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?

Javascript Engines Advantages

限于喜欢 提交于 2019-11-27 09:28:19
问题 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

XMLHttpRequest in Rhino?

泪湿孤枕 提交于 2019-11-27 07:43:55
问题 Lately I have been playing around with Java's ScriptEngine API, namely the javascript engine, which uses Rhino for everything.. I sumbled upon the fact that Rhino has no XMLHttpRequest. I was wondering if anyone knew of a possible way around this? 回答1: You practically need to define XMLHttpRequest in Java. And it is already done - check this out. 来源: https://stackoverflow.com/questions/13985560/xmlhttprequest-in-rhino

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

北战南征 提交于 2019-11-27 06:23:21
问题 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

Problems using Rhino on Android

久未见 提交于 2019-11-27 04:27:54
问题 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

Call external javascript functions from java code

烈酒焚心 提交于 2019-11-26 21:56:01
By using Java Scripting API, I am able to execute JavaScript within Java. However, can someone please explain what I would need to add to this code in order to be able to call on functions that are in C:/Scripts/Jsfunctions.js import javax.script.*; public class InvokeScriptFunction { public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); // JavaScript code in a String String script1 = (String)"function hello(name) {print ('Hello, ' + name);}"; String script2 = (String)

How to convert Rhino-JavaScript arrays to Java-Arrays

泪湿孤枕 提交于 2019-11-26 20:54:14
问题 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