Use Context.get/putThreadLocal() to register per-thread scopes for DynamicGlobalObject.

This is to fix some very obscure behaviour that was likely caused by the WeakHashMap code.
This commit is contained in:
hns 2005-07-07 15:28:09 +00:00
parent 38b4f265ce
commit ab5694092e
3 changed files with 15 additions and 25 deletions

View file

@ -18,9 +18,8 @@ package helma.scripting.rhino;
import helma.framework.core.Application;
import org.mozilla.javascript.Scriptable;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import org.mozilla.javascript.Context;
/**
* This class implements a global scope object that is a dynamic proxy
@ -28,14 +27,13 @@ import java.util.WeakHashMap;
*/
public class DynamicGlobalObject extends GlobalObject {
Map map = Collections.synchronizedMap(new WeakHashMap());
public DynamicGlobalObject(RhinoCore core, Application app) {
super(core, app);
}
public Object get(String s, Scriptable scriptable) {
Scriptable scope = getScope();
Context cx = Context.getCurrentContext();
Scriptable scope = (Scriptable) cx.getThreadLocal("threadscope");
if (scope != null) {
Object obj = scope.get(s, scope);
if (obj != null && obj != NOT_FOUND) {
@ -45,16 +43,4 @@ public class DynamicGlobalObject extends GlobalObject {
return super.get(s, scriptable);
}
public void registerScope(Scriptable scope) {
map.put(Thread.currentThread(), scope);
}
public Scriptable unregisterScope() {
return (Scriptable) map.remove(Thread.currentThread());
}
public final Scriptable getScope() {
return (Scriptable) map.get(Thread.currentThread());
}
}

View file

@ -785,14 +785,16 @@ public final class RhinoCore implements ScopeProvider {
////////////////////////////////////////////////
private synchronized void evaluate (TypeInfo type, Resource code) {
Scriptable threadScope = global.unregisterScope();
// get the current context
Context cx = Context.getCurrentContext();
// unregister the per-thread scope while evaluating
Object threadScope = cx.getThreadLocal("threadscope");
cx.removeThreadLocal("threadscope");
String sourceName = code.getName();
Reader reader = null;
try {
// get the current context
Context cx = Context.getCurrentContext();
Scriptable op = type.objProto;
// do the update, evaluating the file
@ -832,7 +834,7 @@ public final class RhinoCore implements ScopeProvider {
}
}
if (threadScope != null) {
global.registerScope(threadScope);
cx.putThreadLocal("threadscope", threadScope);
}
}
}

View file

@ -174,7 +174,8 @@ public class RhinoEngine implements ScriptingEngine {
context.setOptimizationLevel(optLevel);
// register the per-thread scope with the dynamic scope
core.global.registerScope(global);
// core.global.registerScope(global);
context.putThreadLocal("threadscope", global);
context.putThreadLocal("reval", reval);
context.putThreadLocal("engine", this);
// update prototypes
@ -225,8 +226,9 @@ public class RhinoEngine implements ScriptingEngine {
public void exitContext() {
context.removeThreadLocal("reval");
context.removeThreadLocal("engine");
context.removeThreadLocal("threadscope");
Context.exit();
core.global.unregisterScope();
// core.global.unregisterScope();
thread = null;
// if visual debugger is on let it know we're exiting a context