Skip to content

Commit 10c5f88

Browse files
committed
Introduce createIsolate to make use of shared sealed scopes easier.
1 parent 24cf02e commit 10c5f88

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

rhino/src/main/java/org/mozilla/javascript/TopLevel.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ public TopLevel(ScriptableObject customGlobal) {
118118
globalThis = customGlobal;
119119
}
120120

121+
public TopLevel createIsolate() {
122+
var newGlobal = new NativeObject();
123+
newGlobal.setPrototype(getGlobalThis());
124+
newGlobal.setParentScope(null);
125+
var isolate = new TopLevel(newGlobal);
126+
isolate.cacheBuiltins(newGlobal, false);
127+
return isolate;
128+
}
129+
130+
public TopLevel createIsolate(ScriptableObject customGlobal) {
131+
customGlobal.setParentScope(null);
132+
customGlobal.setPrototype(getGlobalThis());
133+
var isolate = new TopLevel(customGlobal);
134+
isolate.cacheBuiltins(customGlobal, false);
135+
return isolate;
136+
}
137+
121138
@Override
122139
public String getClassName() {
123140
return "topLevel";

tests/src/test/java/org/mozilla/javascript/drivers/ScriptTestsBase.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ private Object executeRhinoScript(boolean interpretedMode) {
7171
Global global = new Global(cx);
7272
loadNatives(global);
7373

74-
Scriptable scope = cx.newObject(global);
75-
scope.setPrototype(global);
76-
scope.setParentScope(null);
74+
var scope = global.createIsolate();
7775

7876
return cx.evaluateReader(scope, script, suiteName, 1, null);
7977
} catch (JavaScriptException ex) {

tests/src/test/java/org/mozilla/javascript/tests/SealedSharedScopeTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import org.mozilla.javascript.EvaluatorException;
2222
import org.mozilla.javascript.IdFunctionObject;
2323
import org.mozilla.javascript.ImporterTopLevel;
24-
import org.mozilla.javascript.NativeObject;
2524
import org.mozilla.javascript.Scriptable;
26-
import org.mozilla.javascript.TopLevel;
2725
import org.mozilla.javascript.Wrapper;
2826

2927
@RunWith(BlockJUnit4ClassRunner.class)
@@ -43,12 +41,8 @@ public void setUp() throws Exception {
4341

4442
ctx = Context.enter();
4543
ctx.setLanguageVersion(Context.VERSION_DEFAULT);
46-
var scope1Global = new NativeObject();
47-
scope1Global.setPrototype(sharedScope.getGlobalThis());
48-
scope1 = new TopLevel(scope1Global);
49-
var scope2Global = new NativeObject();
50-
scope2Global.setPrototype(sharedScope.getGlobalThis());
51-
scope2 = new TopLevel(scope2Global);
44+
scope1 = sharedScope.createIsolate();
45+
scope2 = sharedScope.createIsolate();
5246
}
5347

5448
@After

0 commit comments

Comments
 (0)