rhino

Using Rhino instead of ScriptEngine to run Javascript code in Java

牧云@^-^@ 提交于 2019-12-30 17:22:30
问题 Based on the discussion converting string representation of unknown date-format to Date in java, I want to use the JavaScript Date function in my App-Engine project. However, ScriptEngine does not work on App-Engine. So I need a little help converting to Rhino. Here is the ScriptEngine code I need to convert: ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine engine = scriptEngineManager.getEngineByName("JavaScript"); String script = "var date = new Date('" +

Interpreting JavaScript in Java with Rhino: pausing/resuming scripts

微笑、不失礼 提交于 2019-12-30 01:53:08
问题 I'm using the javax.script.* package of the JDK. Specifically, I'm using the JavaScript engine, which, from what I've read, seems to be based on a Mozilla-developed JavaScript-in-Java interpreter called Rhino. What I'm hoping to accomplish is to basically have my JavaScript able to "pause" itself at a certain point in the code (say, halfway through a function call) and only resume itself later when Java allows it to do so. To illustrate what I mean, imagine this JavaScript code: function

Get last evaluated expression inside function

耗尽温柔 提交于 2019-12-25 07:08:12
问题 This is related to this other question: Last evaluated expression in Javascript But I wanted to provide more details about what I wanted to do and show how I finally solved the problem as some users requested in the comments. I have snippets of Javascript that are written by users of my app. This snippets need to go to a kind of template like this: var foo1 = function(data, options) { <snippet of code written by user> } var foo2 = function(data, options) { <snippet of code written by user> }

How can I load a local file from embedded Rhino?

眉间皱痕 提交于 2019-12-25 05:22:36
问题 I'm using Rhino's context.evaluateString() to run some simple JavaScript from inside of Java. It's textbook right out of the Embedding Javascript guide: String script = // simple logic Context c = new ContextFactory().enterContext(); ScriptableObject scope = context.initStandardObjects(); Object o = context.evaluateString(scope, script, "myScript", 1, null); ScriptableObject result = Context.jsToJava(o, ScriptableObject.class); I'm not sure this is the current best-practice, because the main

Using Google channel api on the client side by embedding javascript in java using rhino or standard inbuilt javascript support?

懵懂的女人 提交于 2019-12-25 03:54:54
问题 I am just experimenting about and I want to know whether it will be possible (and if possible how should I go about it?) to create a desktop client which will use javascript embedded in java using rhino or simply the inbuilt libraries(which are rhino again anyway) to successfully open and maintain an instance of the Channel API for google app engine? 回答1: It's probably possible, but only with a great deal of difficulty. The javascript library expects to be run in a browser, so it expects

TypeError in Rhino: migration from Java 6 to Java 7

余生颓废 提交于 2019-12-24 13:26:18
问题 My project perfectly works on Java 6 (different updates and OS). It uses Rhino as script engine. Now we need to migrate to Java 7 and I found one issue that prevent us to do it. We have code like this: Context.enter(); try { ScriptEngine js = new ScriptEngineManager().getEngineByName("js"); final ScriptContext context = new SimpleScriptContext(); context.setAttribute("console", new Object() { public void log(String out) { logger.info(out); } }, ScriptContext.ENGINE_SCOPE); js.setContext

Java calling JS with Rhino (Uint8Array is not defined)

喜你入骨 提交于 2019-12-24 06:41:09
问题 I'm trying to use the Rhino lib to call some javascript from java code. But it seems that it is choking on a typed array. Here is my simple js file function decrypt(version, iv, encryptedBuffer) { var output8; output8 = new Uint8Array(encryptedBuffer); var outputBuffer = output8.buffer; var output32 = new Int32Array(outputBuffer); ... more funny code } But when calling jsFunction.call(rhino, scope, scope, params); I get this sun.org.mozilla.javascript.internal.EcmaError: ReferenceError:

Getting the path of a script in Rhino

断了今生、忘了曾经 提交于 2019-12-24 00:59:41
问题 I'm trying to get the path to a script executing in Rhino. I would prefer to not have to pass in the directory as the first argument. I don't even have a lead on how to get it. I'm currently calling Rhino via java -jar /some/path/to/js.jar -modules org.mozilla.javascript.commonjs.module /path/to/myscript.js and would like myscript.js to recognize /path/to as it's dirname, regardless of where I run this script from. The only other related question & suggestion here on StackOverflow is to pass

js - 'this' is undefined when calling method 'indirectly'

落花浮王杯 提交于 2019-12-23 20:06:27
问题 My goal is to call a function from a table of functions to generalize command handling (i.e indirectly). Unfortunately, this is undefined when called as such. function Server() { this.sessions = {}; server = this; this.handlers = { "dummy" : server.dummyCommandHandler, }; } Server.prototype.dummyCommandHandler = function() { print (this.sessions); } Server.prototype.run = function ( command ) { print (this.sessions); // [Object object] this.handlers[command.name](); // prints 'undefined' this

Is it possible to add Function.caller support to Rhino?

与世无争的帅哥 提交于 2019-12-23 19:46:40
问题 Looking at it, Rhino doesn't support the caller property for functions - does anyone know if there is a branch that allows this, even if just in interpreter mode? If not, does anyone have any general ideas about how this might be added? 回答1: Never heard about Function.prototype.caller in javascript, but there's arguments.callee.caller , which is really unsupported in Rhino according to the Internet; If you need to get stack traces, there's an idea for solution: http://groups.google.com/group