Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript (Rhino) use library or include other scripts

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"?

like image 857
Ayman Avatar asked Mar 16 '09 13:03

Ayman


People also ask

How do you use rhino in JavaScript?

// Embedding-specific globals: Type help() at the rhino prompt for more print ( x ); // Global print function prints to the console version ( 170 ); // Tell Rhino we want JS 1.7 language features load ( filename ,...); // Load and execute one or more files of JavaScript code readFile ( file ); // Read a text file and ...

What is JavaScript Rhino 1. 7?

JavaScript 1.7 is the default version in the Rhino shell. The JavaScript language is controlled by the ECMA-262 ECMAScript standard, which is a general purpose, cross-platform programming language. Rhino 1.3 and greater conform to edition 3 of the standard.

What is Rhino programming?

Rhino is a JavaScript engine written fully in Java and managed by the Mozilla Foundation as open source software. It is separate from the SpiderMonkey engine, which is also developed by Mozilla, but written in C++ and used in Mozilla Firefox. Rhino.


3 Answers

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 shell, which is the only context I know (your question mentioned the Java SDK, which is outside my area of experience)

like image 159
Alan Storm Avatar answered Oct 21 '22 18:10

Alan Storm


if you happen to be trying to do this within ant, you might see this error:

<script language="javascript">
    load('foo.js');
</script>
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function load.

but you can sidestep it:

<script language="javascript">
    eval(''+new String(org.apache.tools.ant.util.FileUtils.readFully(new java.io.FileReader('foo.js'))));
</script>
like image 10
Chris Plock Avatar answered Oct 21 '22 16:10

Chris Plock


A real-life example this time, i.e. running the esprima parser with Rhino 1.7R4.

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
...

Context context = Context.enter();
Scriptable globalScope = context.initStandardObjects();
Reader esprimaLibReader = new InputStreamReader(getClass().getResourceAsStream("/esprima.js"));
context.evaluateReader(globalScope, esprimaLibReader, "esprima.js", 1, null);

// Add a global variable out that is a JavaScript reflection of the System.out variable:
Object wrappedOut = Context.javaToJS(System.out, globalScope);
ScriptableObject.putProperty(globalScope, "out", wrappedOut);

String code = "var syntax = esprima.parse('42');" +
    "out.print(JSON.stringify(syntax, null, 2));";

// The module esprima is available as a global object due to the same
// scope object passed for evaluation:
context.evaluateString(globalScope, code, "<mem>", 1, null);
Context.exit();

After running this code, you should see the output as follows:

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "Literal",
        "value": 42,
        "raw": "42"
      }
    }
  ]
}

So indeed, the trick is in reusing the globalScope object.

like image 10
Daniel Pacak Avatar answered Oct 21 '22 17:10

Daniel Pacak