rhino

Rhino: How to pass Java object to script, where can be referenced as “this”

安稳与你 提交于 2019-12-05 04:51:19
问题 I am new to JSR-223 Java Scripting, actually I'm switching from MVEL to standard Mozilla Rhino JS. I have read all documentation, but get stuck. I have tried to reference some Java objects from script by bindings just like in tutorial: // my object public class MyBean { public String getStringValue() { return "abc" }; } // initialization ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); // add bindings engine.put("bean", new

reasonable handling of ScriptException thrown by JSR223 Rhino

醉酒当歌 提交于 2019-12-05 04:51:16
I'm starting to run into the dirty little secrets of what is an otherwise very useful JSR223 scripting environment. I'm using the builtin version of Rhino shipped with Java 6 SE, accessing it through JSR223's ScriptingEngine et al. When I get an exception caused by a Java object I've exported into the Javascript environment, it is a ScriptingException that wraps a sun.org.mozilla.javascript.internal.WrappedException that wraps my real exception (e.g. UnsupportedOperationException or whatever) The ScriptingException returns null for getFileName() and -1 for getLineNumber(). But when I look at

Rhino load() function available in JavaScript provided by javax.script?

别等时光非礼了梦想. 提交于 2019-12-05 04:48:37
Some JavaScript files that have been developed for Rhino's shell use load() to load additional JavaScript files. I'm attempting to embed functionality from one of these Rhino JavaScript files using javax.script. Unfortunately, the load() function is not implemented by javax.script's JavaScript. When attempting to eval() a script containing load(), the following error occurs: com.sun.script.javascript.RhinoScriptEngine:-1:in `eval': javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "load" is not defined. Does javax.script provide a for the load()

example using rhino's JavaAdapter

懵懂的女人 提交于 2019-12-05 01:11:52
问题 Can someone provide me an example on how to extend a java class in java script using Rhino's java adapter ? 回答1: For anyone else who might come across this, there's a decent example here (the author uses it to extend awt.Canvas ). var smileyCanvas = new JavaAdapter(awt.Canvas, { paint: function (g) { var size = this.getSize(); var d = Math.min(size.width, size.height); var ed = d / 20; var x = (size.width - d) / 2; var y = (size.height - d) / 2; // draw head (color already set to foreground)

How can I add an object property to the global object in rhino javascript

末鹿安然 提交于 2019-12-04 21:16:33
问题 I have some properties in an object that I would like to add to the global namespace. In javascript on the browser I could just add it to the window object like so: var myObject = { foo : function() { alert("hi"); } // and many more properties }; for (property in myObject) { window[property] = myObject[property]; } // now I can just call foo() foo(); But since rhino doesn't have the global window object I can't do that. Is there an equivalent object or some other way to accomplish this? 回答1:

Anchor element's pathname returns undefined in Rhino with env.js

心不动则不痛 提交于 2019-12-04 17:16:31
I have run into an issue that I believe is rooted in the implementation of anchor tags in Rhino. Although I am utilizing env.js , I suspect perhaps I am not configuring something correctly. In particular, my issue occurs while I am attempting to write unit tests against code written for an angularjs application. When I include angular.js (versions 1.2.1 to present), I get the following error: TypeError: Cannot call method "charAt" of undefined I am convinced the error is the result of this call to urlParsingNode.pathname since a console.log call reveals that the pathname object is undefined. I

android Rhino error can't load this type of class file

北战南征 提交于 2019-12-04 11:35:20
问题 I'm using Rhino in android to execute JavaScript function code in Android(java) and get the result from this code, but I got this exception 01-22 11:33:57.063: E/AndroidRuntime(29175): Caused by: java.lang.UnsupportedOperationException: can't load this type of class file This is my code String test = "function abc(x,y) {return x+y;}"; org.mozilla.javascript.Context context = org.mozilla.javascript.Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); Scriptable that =

Using Rhino's Javascript parser, how to get the comments?

不想你离开。 提交于 2019-12-04 11:08:50
I have some javascript files and parse it using Rhino's javascript parser. but I can't get the comments. How can I get the comments? here's a part of my code. run this code, "comment" variable has null. also, while running "astRoot.toSource();", it shows only javascript code. no comment included. it disappeared! [java code] public void parser() { AstRoot astRoot = new Parser().parse(this.jsString, this.uri, 1); List<AstNode> statList = astRoot.getStatements(); for(Iterator<AstNode> iter = statList.iterator(); iter.hasNext();) { FunctionNode fNode = (FunctionNode)iter.next(); System.out.println

How can I pass a javaScript function to a Java Method to act as a callback (Rhino)

这一生的挚爱 提交于 2019-12-04 02:37:20
Basically I'm trying to pass a javaScript function to a Java method to act as a callback to the script. I can do it - sort of - but the object I receive is a sun.org.mozilla.javascript.internal.InterpretedFunction and I don't see a way to invoke it. Any ideas? Here's what I have so far: var someNumber = 0; function start() { // log is just an log4j instance added to the Bindings log.info("started...."); someNumber = 20; // Test is a unit test object with this method on it (taking Object as a param). test.callFromRhino(junk); } function junk() { log.info("called back " + someNumber); }

Rhino and concurrent access to javax.script.ScriptEngine

↘锁芯ラ 提交于 2019-12-03 22:59:06
I'm using Rhino 1.6r2 through the javax.script API. I know that the Rhino engine claims to be MULTITHREADED: "The engine implementation is internally thread-safe and scripts may execute concurrently although effects of script execution on one thread may be visible to scripts on other threads." What I'd like to know is, under what exact conditions would the effects of one script execution be visible to another? In my code, I sometimes re-use a ScriptEngine object, but for every execution I create a new SimpleBindings and pass it to eval(String, Bindings) . With this arrangement, is there any