Rhino 1.7R4 Support CommonJS
Here are some testing about whether Rhino 1.7R4 support CommonJS. The answer is YES!
Tips: Two APIs provided by Rhino could load the JavaScript file, compileReader and evaluateReader.
Differences: compileReader will load JavaScript file without executed and return a Script object. You have to executed the Script object before you get the result.
evaluateReader will automatically execute JavaScript file after loading.
library.js
var require = (function() {
var exportsObjects = {};
var require = function(name) {
if (exportsObjects.hasOwnProperty(name)) {
return exportsObjects[name];
}
var exports = {};
exportsObjects[name] = exports;
modules[name](require, exports);
return exports;
};
return require;
})();
var run = function(name) {
require(name);
};
var modules = {};
math.js
modules["math"] = function(require, exports) {
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
};
increment.js
modules["increment"] = function(require, exports) {
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
};
program.js
modules["program"] = function(require, exports) {
var inc = require('increment').increment;
exports.pro = function() {
return inc(1998);
}
};
Compile Test
/**
* compileReader compiles the source in the given reader and returns a
* script that may later be executed. Will consume all the source in the reader.
* @throws FileNotFoundException
* @throws IOException
*/
public void compileTest() throws FileNotFoundException, IOException {
Context cx = Context.enter();
final ScriptableObject scope = cx.initStandardObjects();
Script scLibrary = cx.compileReader(new FileReader("library.js"), "library", 1, null);
Script scMath = cx.compileReader(new FileReader("math.js"), "math", 1, null);
Script scIncrement = cx.compileReader(new FileReader("increment.js"), "increment", 1, null);
Script scProgram = cx.compileReader(new FileReader("program.js"), "program", 1, null);
String js = "var pro = require('program').pro; pro();";
Script sc = cx.compileString(js, "js", 1, null);
scLibrary.exec(cx, scope);
scMath.exec(cx, scope);
scIncrement.exec(cx, scope);
scProgram.exec(cx, scope);
Object result = sc.exec(cx, scope);
System.out.println(Context.toString(result));
Context.exit();
}
Evaluate Test
/**
* Evaluate a reader as JavaScript source. All characters of the reader are consumed.
* @throws FileNotFoundException
* @throws IOException
*/
public void evaluateTest() throws FileNotFoundException, IOException {
Context cx = Context.enter();
final ScriptableObject scope = cx.initStandardObjects();
cx.evaluateReader(scope, new FileReader("library.js"), "library", 1, null);
cx.evaluateReader(scope, new FileReader("math.js"), "math", 1, null);
cx.evaluateReader(scope, new FileReader("increment.js"), "increment", 1, null);
cx.evaluateReader(scope, new FileReader("program.js"), "program", 1, null);
String js = "var pro = require('program').pro; pro();";
Object result = cx.evaluateString(scope, js, "js", 1, null);
System.out.println(Context.toString(result));
Context.exit();
}
Comments
Post a Comment