Skip to content

Commit

Permalink
use try-with-resources for the context at more places
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Mar 29, 2024
1 parent be0b804 commit 36a3809
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
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

0 comments on commit 36a3809

Please sign in to comment.