rhino

How to load .js files into a Rhino context in Java

≡放荡痞女 提交于 2019-11-30 09:05:46
Here is my situation: I have access to a Rhino Context object in a Java class. I want to read in a bunch of .js files and pass them along to the Rhino context to have them evaluated. I'm not really interested in having the functions in the .js files available in the scripting context so much as I am in just having the variables that are declared in the .js files available (this is a tooling validation kind of issue). Ideally I would read in and try to evaluate each file all at once, not line by line. I noticed there is a method in Context (see Rhino API ) called evaluateReader() . My first

Access Rhino's native JSON.Stringify from Java

一个人想着一个人 提交于 2019-11-30 07:30:36
问题 Is there a cleaner way to get the JSON representation of a Javascript object than with the following kludge? System.out.println(((ScriptableObject) scope).callMethod( cx, (Scriptable) scope.get("JSON", scope), "stringify", new Object[]{jsObject})); Where jsObject is the ScriptableObject I want to stringify. 回答1: Note that Hannes has now addressed this in Rhino. So the usage simplifies to this: import org.mozilla.javascript.NativeJSON; // ... Object json = NativeJSON.stringify(cx, scope,

How can I start coding with Oracle's Nashorn JS Engine and when will it replace Rhino in the OpenJDK?

风格不统一 提交于 2019-11-30 06:13:20
I'm looking for a way to start playing around with Oracle's new Nashorn JavaScript Engine. I've DL'd the latest OpenJDK 8 (b65) and it appears that Rhino is still the only included script engine. Anyone know when (or in which build) Nashorn will replace Rhino in the OpenJDK? Or even better, where I can get a JDK with it included already? I know Netbeans has already written a debugger to use it, just not sure where they got the libraries/code to start writing it. Anyone have some links? Thanks. It looks like there is no sign of Nashorn on OpenJDK yet. The most recent comment from Jim Laskey in

Convert a date to string in Javascript

僤鯓⒐⒋嵵緔 提交于 2019-11-30 06:01:34
问题 I'm looking for a way to convert a Javascript Date object to a string. I'm converting my site from Ruby to server side Javascript, and I'm looking for something analogous to strftime in Ruby, C, and many other languages. I found plenty of simple scripts that do this kind of conversion, but I'd prefer not to include a custom implementation if there is a standard way of doing this. I'm not using a Javascript framework. I'm using Mozilla Rhino, but would prefer to stay away from using the Java

How to create a 'real' JavaScript array in Rhino

淺唱寂寞╮ 提交于 2019-11-30 04:08:44
问题 Okay, I'm a little stumped. I'm probably missing something blatantly obvious but apparently I just can't see the forest for the trees: I'm trying to call a JavaScript function that expects its parameter to be an array, i.e. it checks if (arg instanceof Array)... Unfortunately, I (or Rhino) just can't seem to create such an array: Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); String src = "function f(a) { return a instanceof Array; };"; cx.evaluateString(scope, src

JavaScript (Rhino) use library or include other scripts

对着背影说爱祢 提交于 2019-11-29 23:00:36
In JDK6, is there a way to load multiple scripts, each in a file, and have the one script reference a method of another script? Sort of like "include"? I think you're after the load() method/property of Rhino's global object/scope load("file1.js"); load("file2.js"); load("file3.js"); methodFromFileOne(); var bar = methodFromFileTwo(); var etc = dotDotDot(); This will load a javascript source file, similar to how include/require will in PHP. Once you load a file, you'll be able to call and function or use any object defined in the loaded file. This is how things work when you're using the Rhino

Clone Entire JavaScript ScriptEngine

前提是你 提交于 2019-11-29 20:14:28
问题 I need to somehow deep clone the entire set of bindings of my ScriptEngine object. What I have tried I have tried so far the Cloner library to clone the entire Bindings structure. This would be great if it worked because it would have ensured a precise copy, including private variables. But this leads to jvm heap corruption (the jvm just crashes with exit code -1073740940). Sometimes it doesn't crash but weird things happen, like the System.out.println() stops working as it should... I have

Capturing Nashorn's Global Variables

限于喜欢 提交于 2019-11-29 20:00:03
问题 I have a Java 7 program, that loads thousands of objects (components), each with many parameters (stored in a Map ), and executes various Rhino scripts on those objects to calculate other derived parameters which get stored back in the object's Map . Before each script is run, a Scope object is created, backed by the object's map, which is used as a JavaScript's scope for the duration of the script. As a simple example, the following creates a HashMap with a=10 and b=20, and executes the

ScriptEngine how to pass a String that represent JSON?

偶尔善良 提交于 2019-11-29 17:35:43
I was trying to pass a String object that represents a JSON string from java to javascript function using ScriptEngine in jdk 1.6. My code works in JSFIDDLE http://jsfiddle.net/hn4yk/13/ but when I try to load it with the ScriptEngine it doesn't work and throws an exception sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot read property "resultList" from undefined (#1164) I tested that the variable is passed correctly but the eval or the JSON.parse method doesn't work! I tried to put the library for JSON.parse and my function in the same file! This is my java code: public class

Instantiating Rhinoscript Native Objects from Java/Scala

喜欢而已 提交于 2019-11-29 16:39:53
I'm trying to improve the performance of a javascript snippet evaluator . These script snippets can reference any number of variables that exist in a string-keyed map of json-like object graphs (IE: Json AST). I'm using JDK 1.6 and the embedded Rhinoscript engine (v1.6R2). Currently, processing takes the form: Snippet is parsed to discover the names of referenced variables Variables are retrieved from the map and serialized to a json string Json string is assigned to a similarly named variable at the start of the script Evaluate augmented script I'm trying to figure out how to skip the json