From bff550c6d9f79ed00819526d4716686b3c5192cc Mon Sep 17 00:00:00 2001 From: hns Date: Fri, 17 Oct 2008 14:05:23 +0000 Subject: [PATCH] Activate instruction count based thread termination. Replace deprecated context enter()/exit() calls. --- src/helma/scripting/rhino/RhinoCore.java | 16 +++++++++------- src/helma/scripting/rhino/RhinoEngine.java | 18 +++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/helma/scripting/rhino/RhinoCore.java b/src/helma/scripting/rhino/RhinoCore.java index ce358177..41ecb8ab 100644 --- a/src/helma/scripting/rhino/RhinoCore.java +++ b/src/helma/scripting/rhino/RhinoCore.java @@ -131,7 +131,7 @@ public final class RhinoCore implements ScopeProvider { wrapper = new WrapMaker(); wrapper.setJavaPrimitiveWrap(false); - Context context = contextFactory.enter(); + Context context = contextFactory.enterContext(); try { // create global object @@ -182,7 +182,7 @@ public final class RhinoCore implements ScopeProvider { app.logError("Cannot initialize interpreter", e); throw new RuntimeException(e.getMessage(), e); } finally { - contextFactory.exit(); + Context.exit(); isInitialized = true; } } @@ -1124,7 +1124,7 @@ public final class RhinoCore implements ScopeProvider { protected void onContextCreated(Context cx) { cx.setWrapFactory(wrapper); cx.setOptimizationLevel(optLevel); - // cx.setInstructionObserverThreshold(5000); + cx.setInstructionObserverThreshold(10000); if (cx.isValidLanguageVersion(languageVersion)) { cx.setLanguageVersion(languageVersion); } else { @@ -1155,9 +1155,11 @@ public final class RhinoCore implements ScopeProvider { * This can be used to customize {@link Context} without introducing * additional subclasses. */ - /* protected void observeInstructionCount(Context cx, int instructionCount) { - if (instructionCount >= 0xfffffff) - throw new EvaluatorException("Exceeded instruction count, interrupting"); - } */ + protected void observeInstructionCount(Context cx, int instructionCount) { + RhinoEngine engine = RhinoEngine.getRhinoEngine(); + if (engine != null && engine.thread != Thread.currentThread()) { + throw new EvaluatorException("Request timed out"); + } + } } } diff --git a/src/helma/scripting/rhino/RhinoEngine.java b/src/helma/scripting/rhino/RhinoEngine.java index 7d90d990..8c376162 100644 --- a/src/helma/scripting/rhino/RhinoEngine.java +++ b/src/helma/scripting/rhino/RhinoEngine.java @@ -86,7 +86,7 @@ public class RhinoEngine implements ScriptingEngine { this.reval = reval; initRhinoCore(app); - context = core.contextFactory.enter(); + context = core.contextFactory.enterContext(); try { extensionGlobals = new HashMap(); @@ -113,7 +113,7 @@ public class RhinoEngine implements ScriptingEngine { app.logError("Cannot initialize interpreter", e); throw new RuntimeException(e.getMessage(), e); } finally { - core.contextFactory.exit (); + Context.exit(); } } @@ -162,7 +162,7 @@ public class RhinoEngine implements ScriptingEngine { // (chicken and egg problem, kind of) thread = Thread.currentThread(); global = new GlobalObject(core, app, true); - context = core.contextFactory.enter(); + context = core.contextFactory.enterContext(); if (core.hasTracer) { context.setDebugger(new Tracer(getResponse()), null); @@ -214,7 +214,7 @@ public class RhinoEngine implements ScriptingEngine { public synchronized void exitContext() { // unregister the engine threadlocal engines.set(null); - core.contextFactory.exit(); + Context.exit(); thread = null; global = null; } @@ -345,7 +345,7 @@ public class RhinoEngine implements ScriptingEngine { * Let the evaluator know that the current evaluation has been * aborted. */ - public void abort() { + public void abort() { // current request has been aborted. Thread t = thread; // set thread to null @@ -528,7 +528,7 @@ public class RhinoEngine implements ScriptingEngine { * @throws java.io.IOException */ public void serialize(Object obj, OutputStream out) throws IOException { - core.contextFactory.enter(); + core.contextFactory.enterContext(); engines.set(this); try { // use a special ScriptableOutputStream that unwraps Wrappers @@ -557,7 +557,7 @@ public class RhinoEngine implements ScriptingEngine { sout.writeObject(obj); sout.flush(); } finally { - core.contextFactory.exit(); + Context.exit(); } } @@ -571,7 +571,7 @@ public class RhinoEngine implements ScriptingEngine { * @throws java.io.IOException */ public Object deserialize(InputStream in) throws IOException, ClassNotFoundException { - core.contextFactory.enter(); + core.contextFactory.enterContext(); engines.set(this); try { ObjectInputStream sin = new ScriptableInputStream(in, core.global) { @@ -584,7 +584,7 @@ public class RhinoEngine implements ScriptingEngine { }; return sin.readObject(); } finally { - core.contextFactory.exit(); + Context.exit(); } }