5

I am embedding some javascript in a Java application using Rhino. I am following the example on the Rhino website, executing a script by calling the Context's evaluateString method and passing the actual script in as a String.

I have a whole bunch of existing javascript code that I would like to make use of. I don't want to concatenate it all into an enormous String and pass it in to evaluateString. I would rather be able to load the code in so that I can call it from the code that I do pass into evaluateString (kind of like the AddCode method works in Microsoft's scripting control). I would like to add code like I can currently add variables by using the ScriptableObject.putProperty method.

Is there a way to do this? Can someone provide a code snippet or a link to the documentation. Thanks!

2
  • Put all your javascript code in one file. Read the file into a StringWriter and use its toString method. Now use evaluateString to parse the entire javascript code and return a Scriptable object. After that you may simply use the get(String namespace,Scriptable jsObject) method of Scriptables to access any object in scope. Commented Jun 24, 2010 at 21:26
  • Just a remark: if you want to enable a continuation-passing-pattern (like you stated) you will need to have all relevant functions within scope and declare that scope in Function.call(Scriptable context,Scriptable scope,Scriptable thisObject,Object[] args). Therefore, I don't really see the benefit of splitting your code into fragments. Commented Jun 26, 2010 at 7:55

2 Answers 2

2

From the documentation and examples it looks like references to previously evaluated objects are controlled by scopes.

Context context = Context.enter();
try {
  ScriptableObject scope = context.initStandardObjects();
  Object out = Context.javaToJS(System.out, scope);
  ScriptableObject.putProperty(scope, "out", out);
  context.evaluateString(scope,
      "function foo() { out.println('Hello, World!'); }", "<1>", 1, null);
  context
      .evaluateString(scope, "function bar() { foo(); }", "<2>", 1, null);
  context.evaluateString(scope, "bar();", "<3>", 1, null);
} finally {
  Context.exit();
}

(Rhino 1.7 release 2)


I know some people use Rhino directly to get the latest version, but the Java 6 implementation can evaluate scripts like this:

ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
engine.eval("function foo() { println('Hello, World!'); }");
engine.eval("function bar() { foo(); }");
engine.eval("bar();");
Sign up to request clarification or add additional context in comments.

Comments

0

In my code I had that need (utility scripts and such), and I just simply concatenated them together in a giant StringBuilder and evaled it (Java 6). Its the only way since javascript can't do (without Java wrapper objects) otherJSScript.someUsefulFunction().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.