From ab5694092eeb247fed4271ff2906c3936ced08e8 Mon Sep 17 00:00:00 2001 From: hns Date: Thu, 7 Jul 2005 15:28:09 +0000 Subject: [PATCH] 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. --- .../scripting/rhino/DynamicGlobalObject.java | 22 ++++--------------- src/helma/scripting/rhino/RhinoCore.java | 12 +++++----- src/helma/scripting/rhino/RhinoEngine.java | 6 +++-- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/helma/scripting/rhino/DynamicGlobalObject.java b/src/helma/scripting/rhino/DynamicGlobalObject.java index 29e715d9..4fadf3e1 100644 --- a/src/helma/scripting/rhino/DynamicGlobalObject.java +++ b/src/helma/scripting/rhino/DynamicGlobalObject.java @@ -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()); - } - } diff --git a/src/helma/scripting/rhino/RhinoCore.java b/src/helma/scripting/rhino/RhinoCore.java index 8e6c0848..aecaf02b 100644 --- a/src/helma/scripting/rhino/RhinoCore.java +++ b/src/helma/scripting/rhino/RhinoCore.java @@ -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); } } } diff --git a/src/helma/scripting/rhino/RhinoEngine.java b/src/helma/scripting/rhino/RhinoEngine.java index c0440c49..64978c88 100644 --- a/src/helma/scripting/rhino/RhinoEngine.java +++ b/src/helma/scripting/rhino/RhinoEngine.java @@ -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