Make Context Closeable.
This makes it possible to write:
try (Context cx = Context.enter()) { ... }
instead of using try...finally.
diff --git a/src/org/mozilla/javascript/Context.java b/src/org/mozilla/javascript/Context.java
index 96e57f1..8a376d4 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 @@
}
}
+ @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 70e276d..64052ab 100644
--- a/src/org/mozilla/javascript/engine/RhinoScriptEngine.java
+++ b/src/org/mozilla/javascript/engine/RhinoScriptEngine.java
@@ -113,23 +113,19 @@
@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 @@
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 @@
} 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 @@
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 @@
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 @@
} catch (RhinoException re) {
throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(),
re.columnNumber());
- } finally {
- Context.exit();
}
}
@@ -251,16 +233,13 @@
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 @@
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 @@
}
} 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 f9e1c61..cb1f636 100644
--- a/src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java
+++ b/src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java
@@ -50,12 +50,9 @@
@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();
}
}