From 4e27a0cf1ac889d346fffaa38fa52a8029d56062 Mon Sep 17 00:00:00 2001 From: Gregory Brail Date: Thu, 11 Jun 2020 14:30:02 -0700 Subject: [PATCH] Make Context Closeable. This makes it possible to write: try (Context cx = Context.enter()) { ... } instead of using try...finally. --- src/org/mozilla/javascript/Context.java | 7 ++++ .../javascript/engine/RhinoScriptEngine.java | 40 ++++--------------- .../engine/RhinoScriptEngineFactory.java | 5 +-- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/org/mozilla/javascript/Context.java b/src/org/mozilla/javascript/Context.java index 96e57f1666..8a376d43dc 100644 --- a/src/org/mozilla/javascript/Context.java +++ b/src/org/mozilla/javascript/Context.java @@ -10,6 +10,7 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.io.Closeable; import java.io.IOException; import java.io.PrintWriter; import java.io.Reader; @@ -54,6 +55,7 @@ */ public class Context + implements Closeable { /** * Language versions. @@ -533,6 +535,11 @@ public static void exit() } } + @Override + public void close() { + exit(); + } + /** * Call {@link ContextAction#run(Context cx)} * using the Context instance associated with the current thread. diff --git a/src/org/mozilla/javascript/engine/RhinoScriptEngine.java b/src/org/mozilla/javascript/engine/RhinoScriptEngine.java index 70e276d178..64052abc43 100644 --- a/src/org/mozilla/javascript/engine/RhinoScriptEngine.java +++ b/src/org/mozilla/javascript/engine/RhinoScriptEngine.java @@ -113,23 +113,19 @@ private Scriptable initScope(Context cx, ScriptContext sc) throws ScriptExceptio @Override public Object eval(String script, ScriptContext context) throws ScriptException { - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { Scriptable scope = initScope(cx, context); Object ret = cx.evaluateString(scope, script, getFilename(), 0, null); return Context.jsToJava(ret, Object.class); } catch (RhinoException re) { throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), re.columnNumber()); - } finally { - Context.exit(); } } @Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { Scriptable scope = initScope(cx, context); Object ret = cx.evaluateReader(scope, reader, getFilename(), 0, null); return Context.jsToJava(ret, Object.class); @@ -138,15 +134,12 @@ public Object eval(Reader reader, ScriptContext context) throws ScriptException re.columnNumber()); } catch (IOException ioe) { throw new ScriptException(ioe); - } finally { - Context.exit(); } } @Override public CompiledScript compile(String script) throws ScriptException { - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { configureContext(cx); Script s = cx.compileString(script, getFilename(), 1, null); @@ -154,15 +147,12 @@ public CompiledScript compile(String script) throws ScriptException { } catch (RhinoException re) { throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), re.columnNumber()); - } finally { - Context.exit(); } } @Override public CompiledScript compile(Reader script) throws ScriptException { - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { configureContext(cx); Script s = cx.compileReader(script, getFilename(), 1, null); @@ -172,22 +162,17 @@ public CompiledScript compile(Reader script) throws ScriptException { re.columnNumber()); } catch (IOException ioe) { throw new ScriptException(ioe); - } finally { - Context.exit(); } } Object eval(Script script, ScriptContext sc) throws ScriptException { - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { Scriptable scope = initScope(cx, sc); Object ret = script.exec(cx, scope); return Context.jsToJava(ret, Object.class); } catch (RhinoException re) { throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), re.columnNumber()); - } finally { - Context.exit(); } } @@ -205,8 +190,7 @@ public Object invokeMethod(Object thiz, String name, Object... args) Object invokeMethodRaw(Object thiz, String name, Class returnType, Object... args) throws ScriptException, NoSuchMethodException { - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { Scriptable scope = initScope(cx, context); Scriptable localThis; @@ -240,8 +224,6 @@ Object invokeMethodRaw(Object thiz, String name, Class returnType, Object... } catch (RhinoException re) { throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), re.columnNumber()); - } finally { - Context.exit(); } } @@ -251,16 +233,13 @@ public T getInterface(Class clasz) { if ((clasz == null) || !clasz.isInterface()) { throw new IllegalArgumentException("Not an interface"); } - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { Scriptable scope = initScope(cx, context); if (methodsMissing(scope, clasz)) { return null; } } catch (ScriptException se) { return null; - } finally { - Context.exit(); } return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{clasz}, new RhinoInvocationHandler(this, null)); @@ -272,8 +251,7 @@ public T getInterface(Object thiz, Class clasz) { if ((clasz == null) || !clasz.isInterface()) { throw new IllegalArgumentException("Not an interface"); } - Context cx = ctxFactory.enterContext(); - try { + try (Context cx = ctxFactory.enterContext()) { Scriptable scope = initScope(cx, context); Scriptable thisObj = Context.toObject(thiz, scope); if (methodsMissing(thisObj, clasz)) { @@ -281,8 +259,6 @@ public T getInterface(Object thiz, Class clasz) { } } catch (ScriptException se) { return null; - } finally { - Context.exit(); } return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{clasz}, new RhinoInvocationHandler(this, thiz)); diff --git a/src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java b/src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java index f9e1c61c07..cb1f6369c7 100644 --- a/src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java +++ b/src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java @@ -50,12 +50,9 @@ public String getEngineName() { @Override public String getEngineVersion() { - Context cx = Context.enter(); - try { + try (Context cx = Context.enter()) { String v = cx.getImplementationVersion(); return (v == null ? "unknown" : v); - } finally { - Context.exit(); } }