Skip to content

Commit

Permalink
Fix up shared scope usage to match how it's documented.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrail committed Dec 24, 2024
1 parent 23f3f63 commit 122fae0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,25 @@ public void importInSameContext() {

@Test
public void importMultipleTimes() {
Utils.runWithAllOptimizationLevels(cx -> {
ScriptableObject sharedScope = cx.initStandardObjects();
//sharedScope.sealObject(); code below will try to modify sealed object

Script script = cx.compileString("importClass(java.util.UUID);true", "TestScript", 1, null);

for (int i = 0; i < 3; i++) {
Scriptable scope = new ImporterTopLevel(cx);
scope.setParentScope(sharedScope);
script.exec(cx, scope);
assertEquals(UniqueTag.NOT_FOUND, sharedScope.get("UUID", sharedScope));
assertTrue(scope.get("UUID", scope) instanceof NativeJavaClass);
}
return null;
});
Utils.runWithAllModes(
cx -> {
ScriptableObject sharedScope = cx.initStandardObjects();
// sharedScope.sealObject(); // code below will try to modify sealed object

Script script =
cx.compileString(
"importClass(java.util.UUID);true", "TestScript", 1, null);

for (int i = 0; i < 3; i++) {
Scriptable scope = new ImporterTopLevel(cx);
scope.setPrototype(sharedScope);
scope.setParentScope(null);
script.exec(cx, scope);
assertEquals(UniqueTag.NOT_FOUND, sharedScope.get("UUID", sharedScope));
assertTrue(scope.get("UUID", scope) instanceof NativeJavaClass);
}
return null;
});
}

private Object runScript(final String scriptSourceText) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.IdFunctionObject;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Wrapper;

Expand All @@ -42,10 +41,12 @@ public void setUp() throws Exception {

ctx = Context.enter();
ctx.setLanguageVersion(Context.VERSION_DEFAULT);
scope1 = new NativeObject();
scope1 = ctx.newObject(sharedScope);
scope1.setPrototype(sharedScope);
scope2 = new NativeObject();
scope1.setParentScope(null);
scope2 = ctx.newObject(sharedScope);
scope2.setPrototype(sharedScope);
scope2.setParentScope(null);
}

@After
Expand Down

0 comments on commit 122fae0

Please sign in to comment.