Skip to content

Commit

Permalink
Use module scope instead of thread-local to store current worker
Browse files Browse the repository at this point in the history
API changes in RhinoEngine:
  - getCurrentWorker() now requires a Scriptable argument
  - getCurrentErrors() and setCurrentWorker() have been removed

API changes in ringo/engine:
  - add getWorker() and getCurrentWorker()
  - remove evaluate() and getErrors()
  • Loading branch information
hns committed Jun 28, 2012
1 parent cb41122 commit fbc139a
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 156 deletions.
66 changes: 27 additions & 39 deletions modules/ringo/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ export( 'properties',
'asJavaString',
'asJavaObject',
'createSandbox',
'evaluate',
'getErrors',
'getRingoHome',
'getRepositories',
'getRhinoContext',
'getRhinoEngine',
'getOptimizationLevel',
'setOptimizationLevel',
'getCurrentWorker',
'getWorker',
'serialize',
'deserialize',
'version');

var rhino = org.mozilla.javascript;
var engine = org.ringojs.engine;
var {Context, ClassSHutter} = org.mozilla.javascript;
var {RhinoEngine, RingoConfiguration} = org.ringojs.engine;
var engine = RhinoEngine.getEngine(global);

/**
* An object reflecting the Java system properties.
Expand All @@ -36,14 +37,14 @@ try {
* The RingoJS version as an array-like object with the major and minor version
* number as first and second element.
*/
var version = new ScriptableList(engine.RhinoEngine.VERSION);
var version = new ScriptableList(RhinoEngine.VERSION);

/**
* Define a class as Rhino host object.
* @param {JavaClass} javaClass the class to define as host object
*/
function addHostObject(javaClass) {
getRhinoEngine().defineHostClass(javaClass);
engine.defineHostClass(javaClass);
}

/**
Expand All @@ -62,23 +63,23 @@ function addHostObject(javaClass) {
function createSandbox(modulePath, globals, options) {
options = options || {};
var systemModules = options.systemModules || null;
var config = new engine.RingoConfiguration(
var config = new RingoConfiguration(
getRingoHome(), modulePath, systemModules);
if (options.classShutter) {
var shutter = options.shutter;
config.setClassShutter(shutter instanceof rhino.ClassShutter ?
shutter : new rhino.ClassShutter(shutter));
config.setClassShutter(shutter instanceof ClassShutter ?
shutter : new ClassShutter(shutter));
}
config.setSealed(Boolean(options.sealed));
return getRhinoEngine().createSandbox(config, globals);
return engine.createSandbox(config, globals);
}

/**
* Get the RingoJS installation directory.
* @returns {Repository} a Repository representing the Ringo installation directory
*/
function getRingoHome() {
return getRhinoEngine().getRingoHome();
return engine.getRingoHome();
}

/**
Expand All @@ -87,7 +88,7 @@ function getRingoHome() {
* @returns {Object} the object wrapped as native java object
*/
function asJavaObject(object) {
return getRhinoEngine().asJavaObject(object);
return engine.asJavaObject(object);
}

/**
Expand All @@ -98,7 +99,7 @@ function asJavaObject(object) {
* @returns {Object} the object converted to a string and wrapped as native java object
*/
function asJavaString(object) {
return getRhinoEngine().asJavaString(object);
return engine.asJavaString(object);
}

/**
Expand All @@ -109,7 +110,7 @@ function asJavaString(object) {
* @returns {Number} level an integer between -1 and 9
*/
function getOptimizationLevel() {
return getRhinoEngine().getOptimizationLevel();
return engine.getOptimizationLevel();
}

/**
Expand All @@ -120,55 +121,42 @@ function getOptimizationLevel() {
* @param {Number} level an integer between -1 and 9
*/
function setOptimizationLevel(level) {
getRhinoEngine().setOptimizationLevel(level);
}

/**
* Evaluate a module script on an existing scope instead of creating a
* new module scope. This can be used to mimic traditional JavaScript
* environments such as those found in web browsers.
* @param {String} moduleName the name of the module to evaluate
* @param {Object} scope the JavaScript object to evaluate the script on
*/
function evaluate(moduleName, scope) {
var script = getRhinoEngine().getScript(moduleName);
if (!scope) {
// create a new top level scope object
scope = new engine.ModuleScope(moduleName, script.getSource(), global);
}
script.evaluate(scope, getRhinoContext());
return scope;
engine.setOptimizationLevel(level);
}

/**
* Get the org.mozilla.javascript.Context associated with the current thread.
*/
function getRhinoContext() {
return rhino.Context.getCurrentContext();
return Context.getCurrentContext();
}

/**
* Get the org.ringojs.engine.RhinoEngine associated with this application.
* @returns {org.ringojs.engine.RhinoEngine} the current RhinoEngine instance
*/
function getRhinoEngine() {
return engine.RhinoEngine.getEngine(global);
return engine;
}

function getWorker() {
return engine.getWorker();
}

/**
* Get a list containing the syntax errors encountered in the current context.
* @returns {ScriptableList} a list containing the errors encountered in the current context
* Get the current worker instance.
* @return {org.ringojs.engine.RingoWorker} the current RingoWorker instance
*/
function getErrors() {
return new ScriptableList(getRhinoEngine().getCurrentErrors());
function getCurrentWorker() {
return engine.getCurrentWorker(module);
}

/**
* Get the app's module search path as list of repositories.
* @returns {ScriptableList} a list containing the module search path repositories
*/
function getRepositories() {
return new ScriptableList(getRhinoEngine().getRepositories());
return new ScriptableList(engine.getRepositories());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/ringo/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* functions.
*/

var engine = require("ringo/engine").getRhinoEngine();
var engine = require("ringo/engine");

/**
* Executes a function after specified delay. The function will be called
Expand Down
10 changes: 5 additions & 5 deletions modules/ringo/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @fileoverview A Worker API based on the
* [W3C Web Workers](http://www.w3.org/TR/workers/).
*/
var engine = require("ringo/engine").getRhinoEngine();
var engine = require("ringo/engine");
var Deferred = require("ringo/promise").Deferred;

exports.Worker = Worker;
Expand Down Expand Up @@ -44,17 +44,17 @@ function Worker(moduleId) {
// throw an error if module can't be loaded.
worker.loadModuleInWorkerThread(moduleId).get();

var onmessage = function(e) {
function onmessage(e) {
if (typeof self.onmessage === "function") {
self.onmessage(e);
}
};
}

var onerror = function(e) {
function onerror(e) {
if (typeof self.onerror === "function") {
self.onerror(e);
}
};
}

/**
* Post a message to the worker. This enqueues the message
Expand Down
9 changes: 8 additions & 1 deletion src/org/ringojs/engine/ModuleScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public class ModuleScope extends ImporterTopLevel {
private Trackable source;
private Repository repository;
private String id;
private RingoWorker worker;
private long checksum;
private Scriptable exportsObject, moduleObject;
private static final long serialVersionUID = -2409425841990094897L;

public ModuleScope(String moduleId, Trackable source, Scriptable prototype) {
public ModuleScope(String moduleId, Trackable source,
Scriptable prototype, RingoWorker worker) {
setParentScope(null);
setPrototype(prototype);
// for activating the ImporterTopLevel import* functions
Expand All @@ -49,6 +51,7 @@ public ModuleScope(String moduleId, Trackable source, Scriptable prototype) {
this.repository = source instanceof Repository ?
(Repository) source : source.getParentRepository();
this.id = moduleId;
this.worker = worker;
// create and define module meta-object
moduleObject = new ModuleObject(this);
defineProperty("module", moduleObject, DONTENUM);
Expand All @@ -68,6 +71,10 @@ public Repository getRepository() {
return repository;
}

public RingoWorker getWorker() {
return worker;
}

public void reset() {
Scriptable exports = new ExportsObject();
defineProperty("exports", exports, DONTENUM);
Expand Down
30 changes: 15 additions & 15 deletions src/org/ringojs/engine/ReloadableScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Resource getSource() {
* @throws IOException if an error occurred reading the script file
* @return the compiled and up-to-date script
*/
public synchronized Object getScript(Context cx)
public synchronized Object getScript(Context cx, RingoWorker worker)
throws JavaScriptException, IOException {
// only use shared code cache if optlevel >= 0
int optlevel = cx.getOptimizationLevel();
Expand Down Expand Up @@ -121,8 +121,8 @@ public synchronized Object getScript(Context cx)
cache.put(resource, scriptref);
}
}
if (errors != null && !errors.isEmpty()) {
List<ScriptError> currentErrors = engine.getCurrentErrors();
if (errors != null && worker != null && !errors.isEmpty()) {
List<ScriptError> currentErrors = worker.getErrors();
if (currentErrors != null) {
currentErrors.addAll(errors);
}
Expand Down Expand Up @@ -171,17 +171,17 @@ protected synchronized Object compileScript(Context cx)
* @throws IOException if an error occurred reading the script file
*/
public Object evaluate(Scriptable scope, Context cx,
Map<Resource, Scriptable> modules)
RingoWorker worker)
throws JavaScriptException, IOException {
Object obj = getScript(cx);
Object obj = getScript(cx, worker);
if (!(obj instanceof Script)) {
return obj;
}
Script script = (Script) obj;
ModuleScope module = scope instanceof ModuleScope ?
(ModuleScope) scope : null;
if (module != null) {
modules.put(resource, module);
worker.registerModule(resource, module);
}
Object value = script.exec(cx, scope);
if (module != null) {
Expand All @@ -197,46 +197,46 @@ public Object evaluate(Scriptable scope, Context cx,
* @param prototype the prototype for the module, usually the shared top level scope
* @param cx the rhino context
* @param module the preexisting module for this resource if available
* @param modules thread-local map for registering the module scope
* @param worker the worker instance loading this module
* @return a new module scope
* @throws JavaScriptException if an error occurred evaluating the script file
* @throws IOException if an error occurred reading the script file
*/
protected Scriptable load(Scriptable prototype, Context cx,
Scriptable module, Map<Resource, Scriptable> modules)
Scriptable module, RingoWorker worker)
throws JavaScriptException, IOException {
if (module instanceof ModuleScope &&
((ModuleScope)module).getChecksum() == getChecksum()) {
// Module scope exists and is up to date
modules.put(resource, module);
worker.registerModule(resource, module);
return module;
}

return exec(cx, prototype, modules);
return exec(cx, prototype, worker);
}

private synchronized Scriptable exec(Context cx, Scriptable prototype,
Map<Resource, Scriptable> modules)
RingoWorker worker)
throws IOException {
if (log.isLoggable(Level.FINE)) {
log.fine("Loading module: " + moduleName);
}
if (engine.getConfig().isVerbose()) {
System.err.println("Loading module: " + moduleName);
}
Object obj = getScript(cx);
Object obj = getScript(cx, worker);
if (!(obj instanceof Script)) {
if (!(obj instanceof Scriptable)) {
throw Context.reportRuntimeError("Module must be an object");
}
Scriptable scriptable = (Scriptable) obj;
modules.put(resource, scriptable);
worker.registerModule(resource, scriptable);
return scriptable;
}
Script script = (Script) obj;
ModuleScope module = new ModuleScope(moduleName, resource, prototype);
ModuleScope module = new ModuleScope(moduleName, resource, prototype, worker);
// put module scope in map right away to make circular dependencies work
modules.put(resource, module);
worker.registerModule(resource, module);
// warnings are disabled in shell - enable warnings for module loading
ErrorReporter er = cx.getErrorReporter();
ToolErrorReporter reporter = er instanceof ToolErrorReporter ?
Expand Down
6 changes: 3 additions & 3 deletions src/org/ringojs/engine/Require.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
ModuleScope moduleScope = thisObj instanceof ModuleScope ?
(ModuleScope) thisObj : null;
try {
RingoWorker worker = engine.getCurrentWorker();
RingoWorker worker = engine.getCurrentWorker(thisObj);
if (worker == null) {
worker = engine.getWorker();
worker = engine.getMainWorker();
}
String arg = args[0].toString();
Scriptable module = worker.loadModuleInternal(cx, arg, moduleScope);
Scriptable module = worker.loadModule(cx, arg, moduleScope);
return module instanceof ModuleScope ?
((ModuleScope)module).getExports() : module;
} catch (FileNotFoundException notFound) {
Expand Down
Loading

0 comments on commit fbc139a

Please sign in to comment.