diff --git a/src/org/mozilla/javascript/Context.java b/src/org/mozilla/javascript/Context.java index dfbce475a9..c5b86cbcc4 100644 --- a/src/org/mozilla/javascript/Context.java +++ b/src/org/mozilla/javascript/Context.java @@ -493,7 +493,12 @@ public static void exit() { @Override public void close() { - exit(); + if (enterCount < 1) Kit.codeBug(); + if (--enterCount == 0) { + Object helper = VMBridge.instance.getThreadContextHelper(); + VMBridge.instance.setContext(helper, null); + factory.onContextReleased(this); + } } /** @@ -539,11 +544,8 @@ public static Object call( /** The method implements {@link ContextFactory#call(ContextAction)} logic. */ static T call(ContextFactory factory, ContextAction action) { - Context cx = enter(null, factory); - try { + try (Context cx = enter(null, factory)) { return action.run(cx); - } finally { - exit(); } }