Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Context Closeable #864

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,7 @@
*/

public class Context
implements Closeable
{
/**
* Language versions.
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 8 additions & 32 deletions src/org/mozilla/javascript/engine/RhinoScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -138,31 +134,25 @@ 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);
return new RhinoCompiledScript(this, s);
} 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);
Expand All @@ -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();
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -251,16 +233,13 @@ public <T> T getInterface(Class<T> 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));
Expand All @@ -272,17 +251,14 @@ public <T> T getInterface(Object thiz, Class<T> 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)) {
return null;
}
} catch (ScriptException se) {
return null;
} finally {
Context.exit();
}
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class<?>[]{clasz}, new RhinoInvocationHandler(this, thiz));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down