* No more need for per-thread-scope workaround, works out of the box with Rhino 1.6.
This commit is contained in:
parent
b210d5cd11
commit
7e00def51c
3 changed files with 12 additions and 45 deletions
|
@ -78,7 +78,7 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
|
||||||
"wrapJavaMap", "unwrapJavaMap"
|
"wrapJavaMap", "unwrapJavaMap"
|
||||||
};
|
};
|
||||||
|
|
||||||
defineFunctionProperties(globalFuncs, GlobalObject.class, 0);
|
defineFunctionProperties(globalFuncs, GlobalObject.class, DONTENUM | READONLY | PERMANENT);
|
||||||
put("app", this, Context.toObject(new ApplicationBean(app), this));
|
put("app", this, Context.toObject(new ApplicationBean(app), this));
|
||||||
put("Xml", this, Context.toObject(new XmlObject(core), this));
|
put("Xml", this, Context.toObject(new XmlObject(core), this));
|
||||||
put("global", this, this);
|
put("global", this, this);
|
||||||
|
@ -127,48 +127,19 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
|
||||||
if (isRecording) {
|
if (isRecording) {
|
||||||
changedProperties.add(name);
|
changedProperties.add(name);
|
||||||
}
|
}
|
||||||
Context cx = Context.getCurrentContext();
|
// check if this is a per-thread scope instance
|
||||||
GlobalObject scope = (GlobalObject) cx.getThreadLocal("threadscope");
|
if (sharedGlobal != null) {
|
||||||
if (scope != null) {
|
|
||||||
// make thread scope accessible as "global"
|
// make thread scope accessible as "global"
|
||||||
if ("global".equals(name)) {
|
if ("global".equals(name)) {
|
||||||
return scope;
|
return this;
|
||||||
}
|
}
|
||||||
// use synchronized get on fast changing per-thread scopes just to be sure
|
// use synchronized get on fast changing per-thread scopes just to be sure
|
||||||
Object obj = scope.getSynchronized(name);
|
synchronized(this) {
|
||||||
if (obj != null && obj != NOT_FOUND) {
|
return super.get(name, start);
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sharedGlobal != null) {
|
// we're the shared scope
|
||||||
// we're a per-thread scope
|
return super.get(name, start);
|
||||||
return sharedGlobal.getInternal(name);
|
|
||||||
} else {
|
|
||||||
// we are the shared scope
|
|
||||||
return super.get(name, start);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Directly get a property, bypassing the extra stuff in get(String, Scriptable).
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
* @return the property for the given name
|
|
||||||
*/
|
|
||||||
protected Object getInternal(String name) {
|
|
||||||
return super.get(name, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Directly get a property, bypassing the extra stuff in get(String, Scriptable),
|
|
||||||
* and synchronizing in order to prevent cache read errors on multiprocessor systems.
|
|
||||||
* TODO: we need extensive testing in order to tell whether this is really necessary.
|
|
||||||
*
|
|
||||||
* @param name
|
|
||||||
* @return the property for the given name
|
|
||||||
*/
|
|
||||||
protected synchronized Object getSynchronized(String name) {
|
|
||||||
return super.get(name, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -750,4 +721,8 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
|
||||||
public void clearChangeSet() {
|
public void clearChangeSet() {
|
||||||
changedProperties = null;
|
changedProperties = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return sharedGlobal == null ? "[Shared Scope]" : "[Thread Scope]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -780,9 +780,6 @@ public final class RhinoCore implements ScopeProvider {
|
||||||
private synchronized void evaluate (TypeInfo type, Resource code) {
|
private synchronized void evaluate (TypeInfo type, Resource code) {
|
||||||
// get the current context
|
// get the current context
|
||||||
Context cx = Context.getCurrentContext();
|
Context cx = Context.getCurrentContext();
|
||||||
// unregister the per-thread scope while evaluating
|
|
||||||
Object threadScope = cx.getThreadLocal("threadscope");
|
|
||||||
cx.removeThreadLocal("threadscope");
|
|
||||||
|
|
||||||
String sourceName = code.getName();
|
String sourceName = code.getName();
|
||||||
Reader reader = null;
|
Reader reader = null;
|
||||||
|
@ -830,9 +827,6 @@ public final class RhinoCore implements ScopeProvider {
|
||||||
// shouldn't happen
|
// shouldn't happen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (threadScope != null) {
|
|
||||||
cx.putThreadLocal("threadscope", threadScope);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,7 +176,6 @@ public class RhinoEngine implements ScriptingEngine {
|
||||||
|
|
||||||
context.setOptimizationLevel(optLevel);
|
context.setOptimizationLevel(optLevel);
|
||||||
// register the per-thread scope with the dynamic scope
|
// register the per-thread scope with the dynamic scope
|
||||||
context.putThreadLocal("threadscope", global);
|
|
||||||
context.putThreadLocal("reval", reval);
|
context.putThreadLocal("reval", reval);
|
||||||
context.putThreadLocal("engine", this);
|
context.putThreadLocal("engine", this);
|
||||||
// update prototypes
|
// update prototypes
|
||||||
|
@ -227,7 +226,6 @@ public class RhinoEngine implements ScriptingEngine {
|
||||||
public synchronized void exitContext() {
|
public synchronized void exitContext() {
|
||||||
context.removeThreadLocal("reval");
|
context.removeThreadLocal("reval");
|
||||||
context.removeThreadLocal("engine");
|
context.removeThreadLocal("engine");
|
||||||
context.removeThreadLocal("threadscope");
|
|
||||||
Context.exit();
|
Context.exit();
|
||||||
// core.global.unregisterScope();
|
// core.global.unregisterScope();
|
||||||
thread = null;
|
thread = null;
|
||||||
|
|
Loading…
Add table
Reference in a new issue