From 809253fea600011e45ca3e427c344f520a27fac0 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Mon, 13 Oct 2003 15:57:18 +0000 Subject: [PATCH] Run ContextListener.contextCreated from Context.enter(), not Context constructor. In this way listeners will not be invoked for Context that are never associated with a thread and contextCreated will be more symmetric with Context.contextReleased. git-svn-id: svn://10.0.0.236/trunk@147906 18797224-902f-48f8-a5cc-f745e15eee43 --- .../src/org/mozilla/javascript/Context.java | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java index 707295b3008..da75934131e 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java @@ -98,12 +98,6 @@ public class Context { public Context() { setLanguageVersion(VERSION_DEFAULT); optimizationLevel = codegenClass != null ? 0 : -1; - Object[] array = contextListeners; - if (array != null) { - for (int i = array.length; i-- != 0;) { - ((ContextListener)array[i]).contextCreated(this); - } - } } /** @@ -126,14 +120,16 @@ public class Context { * try { * ... * cx.evaluateString(...); + * } finally { + * Context.exit(); * } - * finally { Context.exit(); } * * @return a Context associated with the current thread * @see org.mozilla.javascript.Context#getCurrentContext * @see org.mozilla.javascript.Context#exit */ - public static Context enter() { + public static Context enter() + { return enter(null); } @@ -148,32 +144,30 @@ public class Context { * @param cx a Context to associate with the thread if possible * @return a Context associated with the current thread */ - public static Context enter(Context cx) { - + public static Context enter(Context cx) + { Context old = getCurrentContext(); - - if (cx == null) { - if (old != null) { - cx = old; - } else { - cx = new Context(); - setThreadContext(cx); - } - } else { - if (cx.enterCount != 0) { + if (old != null) { + if (cx != null && cx != old && cx.enterCount != 0) { // The suplied context must be the context for // the current thread if it is already entered - if (cx != old) { - throw new RuntimeException - ("Cannot enter Context active on another thread"); - } - } else { - if (old != null) { - cx = old; - } else { - setThreadContext(cx); + throw new IllegalArgumentException( + "Cannot enter Context active on another thread"); + } + cx = old; + } else { + if (cx == null) + cx = new Context(); + if (cx.enterCount != 0) Context.codeBug(); + + Object[] array = contextListeners; + if (array != null) { + for (int i = array.length; i-- != 0;) { + ((ContextListener)array[i]).contextCreated(cx); } } + + setThreadContext(cx); } ++cx.enterCount; @@ -200,12 +194,13 @@ public class Context { * * @see org.mozilla.javascript.Context#enter */ - public static void exit() { + public static void exit() + { boolean released = false; Context cx = getCurrentContext(); if (cx == null) { - throw new RuntimeException - ("Calling Context.exit without previous Context.enter"); + throw new IllegalStateException( + "Calling Context.exit without previous Context.enter"); } if (Context.check && cx.enterCount < 1) Context.codeBug(); --cx.enterCount;