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

an optimized version of Context#close() #1460

Merged
merged 2 commits into from
Apr 27, 2024
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
12 changes: 7 additions & 5 deletions src/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -539,11 +544,8 @@ public static Object call(

/** The method implements {@link ContextFactory#call(ContextAction)} logic. */
static <T> T call(ContextFactory factory, ContextAction<T> action) {
Context cx = enter(null, factory);
try {
try (Context cx = enter(null, factory)) {
return action.run(cx);
} finally {
exit();
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ public boolean equals(Object other) {
// one. It is required as some objects within fully initialized
// global scopes (notably, XMLLibImpl) need to have a top scope
// in order to evaluate their attributes.
final Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
if (ScriptRuntime.hasTopCall(cx)) {
return equalsInTopScope(other).booleanValue();
}
Expand All @@ -237,8 +236,6 @@ public boolean equals(Object other) {
ScriptRuntime.emptyArgs,
isStrictTopFrame()))
.booleanValue();
} finally {
Context.exit();
}
}
return false;
Expand Down
5 changes: 1 addition & 4 deletions src/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class JavaMembers {
}

JavaMembers(Scriptable scope, Class<?> cl, boolean includeProtected) {
try {
Context cx = ContextFactory.getGlobal().enterContext();
try (Context cx = ContextFactory.getGlobal().enterContext()) {
ClassShutter shutter = cx.getClassShutter();
if (shutter != null && !shutter.visibleToScripts(cl.getName())) {
throw Context.reportRuntimeErrorById("msg.access.prohibited", cl.getName());
Expand All @@ -52,8 +51,6 @@ class JavaMembers {
this.cl = cl;
boolean includePrivate = cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS);
reflect(cx, scope, includeProtected, includePrivate);
} finally {
Context.exit();
}
}

Expand Down
22 changes: 11 additions & 11 deletions testsrc/org/mozilla/javascript/tests/Bug448816Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public void setUp() {
reference.put("c", new HashMap<Object, Object>());
reference.put(Integer.valueOf(1), Integer.valueOf(42));
// get a js object as map
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
map =
(Map<Object, Object>)
context.evaluateString(
scope,
"({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});",
"testsrc",
1,
null);
Context.exit();
try (Context context = Context.enter()) {
ScriptableObject scope = context.initStandardObjects();
map =
(Map<Object, Object>)
context.evaluateString(
scope,
"({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});",
"testsrc",
1,
null);
}
}

@Test
Expand Down
6 changes: 1 addition & 5 deletions testsrc/org/mozilla/javascript/tests/DynamicScopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public void initStandardObjectsSealed() {
public void standardMethodObjectCreate() {
ContextFactory contextFactory = new DynamicScopeContextFactory();

final Context cx = contextFactory.enterContext();
try {
try (Context cx = contextFactory.enterContext()) {

// Used to fail with org.mozilla.javascript.EvaluatorException: Cannot modify a property
// of a sealed object: iterator.
Expand Down Expand Up @@ -122,9 +121,6 @@ public void standardMethodObjectCreate() {
assertSame(subScope.getPrototype(), someScope);
assertSame(someObj.getParentScope(), someScope);
assertSame(subObj.getParentScope(), subScope);

} finally {
Context.exit();
}
}
}
24 changes: 14 additions & 10 deletions testsrc/org/mozilla/javascript/tests/XMLSecureParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public void xmlSecureConfiguration() {
CALLED_BY_XML_PARSER = false;

// run with defaults for this JRE
executeXML(ContextFactory.getGlobal().enterContext());
try (Context cx = ContextFactory.getGlobal().enterContext()) {
executeXML(cx);
}
assertFalse(CALLED_BY_XML_PARSER);

// store the original setting for xml, if any
Expand All @@ -44,7 +46,9 @@ public void xmlSecureConfiguration() {
// inject our own xml parser
System.setProperty(XML_PROPERTY, DBF_CLASSNAME);
// run with our injected parser
executeXML(ContextFactory.getGlobal().enterContext());
try (Context cx = ContextFactory.getGlobal().enterContext()) {
executeXML(cx);
}
} catch (RuntimeException e) {
// Our parser immediately throws a ParserConfigurationException on creating a
// documentbuilder,
Expand Down Expand Up @@ -78,7 +82,9 @@ public void xmlInsecureConfiguration() {
CALLED_BY_XML_PARSER = false;

// run with defaults for this JRE
executeXML(new InsecureContextFactory().enterContext());
try (Context cx = new InsecureContextFactory().enterContext()) {
executeXML(cx);
}
assertFalse(CALLED_BY_XML_PARSER);

// store the original setting for xml, if any
Expand All @@ -87,7 +93,9 @@ public void xmlInsecureConfiguration() {
// inject our own xml parser
System.setProperty(XML_PROPERTY, DBF_CLASSNAME);
// run with our injected parser
executeXML(new InsecureContextFactory().enterContext());
try (Context cx = new InsecureContextFactory().enterContext()) {
executeXML(cx);
}
} catch (RuntimeException e) {
// Our parser immediately throws a ParserConfigurationException on creating a
// documentbuilder,
Expand All @@ -113,12 +121,8 @@ public void xmlInsecureConfiguration() {
}

private void executeXML(Context cx) {
try {
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, "new XML('<a></a>').toXMLString();", "source", 1, null);
} finally {
Context.exit();
}
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, "new XML('<a></a>').toXMLString();", "source", 1, null);
}

class InsecureContextFactory extends ContextFactory {
Expand Down
Loading