Skip to content

Commit

Permalink
Re-add ability of getCurrentWorker() to get worker from scope argument.
Browse files Browse the repository at this point in the history
This also makes getCurrentWorker stricter by throwing an exception if no worker
is associated with the current thread or the argument object, or if different
workers are associated with both.
  • Loading branch information
hns committed Apr 13, 2013
1 parent 1333095 commit 1a753a8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 18 deletions.
4 changes: 2 additions & 2 deletions modules/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Object.defineProperty(this, "global", { value: this });
this.setTimeout = function(callback, delay) {
var args = Array.slice(arguments, 2);
delay = parseInt(delay, 10) || 0;
var worker = engine.getCurrentWorker();
var worker = engine.getCurrentWorker(callback);
return worker.schedule(delay, this, callback, args);
};

Expand Down Expand Up @@ -121,7 +121,7 @@ Object.defineProperty(this, "global", { value: this });
global.setInterval = function(callback, delay) {
var args = Array.slice(arguments, 2);
delay = Math.max(parseInt(delay, 10) || 0, 1);
var worker = engine.getCurrentWorker();
var worker = engine.getCurrentWorker(callback);
return worker.scheduleInterval(delay, this, callback, args);
};

Expand Down
9 changes: 5 additions & 4 deletions modules/ringo/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ function getWorker() {
}

/**
* Get the current worker instance.
* @return {org.ringojs.engine.RingoWorker} the current RingoWorker instance
* Get the worker instance associated with the current thread or the given scope or function object.
* @param obj {Object} optional scope or function to get the worker from.
* @return {org.ringojs.engine.RingoWorker} the current worker
*/
function getCurrentWorker() {
return engine.getCurrentWorker();
function getCurrentWorker(obj) {
return engine.getCurrentWorker(obj || null);
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/org/ringojs/engine/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public Callback(Scriptable function, RhinoEngine engine, boolean sync) {
if (function instanceof Function) {
this.module = scope;
this.function = function;
worker = engine.getCurrentWorker();
if (worker == null) {
worker = engine.getWorker();
}
this.worker = engine.getCurrentWorker(function);
} else {
this.module = ScriptableObject.getProperty(function, "module");
this.function = ScriptableObject.getProperty(function, "name");
Expand Down
6 changes: 6 additions & 0 deletions src/org/ringojs/engine/ModuleScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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;
Expand All @@ -50,6 +51,7 @@ public ModuleScope(String moduleId, Trackable source,
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 @@ -69,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
5 changes: 1 addition & 4 deletions src/org/ringojs/engine/Require.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
ModuleScope moduleScope = thisObj instanceof ModuleScope ?
(ModuleScope) thisObj : null;
try {
RingoWorker worker = engine.getCurrentWorker();
if (worker == null) {
worker = engine.getMainWorker();
}
RingoWorker worker = engine.getCurrentWorker(scope);
String arg = args[0].toString();
Scriptable module = worker.loadModule(cx, arg, moduleScope);
return module instanceof ModuleScope ?
Expand Down
32 changes: 28 additions & 4 deletions src/org/ringojs/engine/RhinoEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,35 @@ protected RingoWorker setCurrentWorker(RingoWorker worker) {
}

/**
* Get the worker associated with the given scope, or null.
* @return the worker associated with the current thread, or null.
* Get the worker associated with the current thread, or the given scope or function argument if provided.
* An {@code IllegalStateException} is thrown if no worker could be found or if different workers are
* associated with the current thread and the argument object.
*
* @param obj a scope or function object
* @throws IllegalStateException if no worker could be found, or if different workers are associates with
* the current thread and the argument object
* @return the current worker
*/
public RingoWorker getCurrentWorker() {
return currentWorker.get();
public RingoWorker getCurrentWorker(Scriptable obj) {
RingoWorker worker = currentWorker.get(); // Get worker associated with current thread
Scriptable scriptable = obj;

while (scriptable != null) {
if (scriptable instanceof ModuleScope) {
RingoWorker scopeWorker = ((ModuleScope) scriptable).getWorker();
if (worker == null) {
worker = scopeWorker;
} else if (worker != scopeWorker) {
throw new IllegalStateException("Current thread worker differs from scope worker");
}
break;
}
scriptable = scriptable.getParentScope();
}
if (worker == null) {
throw new IllegalStateException("No worker associated with current thread or scope");
}
return worker;
}

/**
Expand Down

0 comments on commit 1a753a8

Please sign in to comment.