Skip to content

Commit 03d7ed4

Browse files
committed
Alter isolate API to handle edge cases properly.
1 parent 19e9931 commit 03d7ed4

File tree

9 files changed

+49
-29
lines changed

9 files changed

+49
-29
lines changed

rhino-engine/src/main/java/org/mozilla/javascript/engine/RhinoScriptEngine.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ private Scriptable initScope(Context cx, ScriptContext sc) throws ScriptExceptio
9797

9898
if (sc.getBindings(ScriptContext.GLOBAL_SCOPE) != null) {
9999
var globalScope =
100-
topLevelScope.createIsolate(
100+
TopLevel.createIsolate(
101+
topLevelScope,
101102
new BindingsObject(sc.getBindings(ScriptContext.GLOBAL_SCOPE)));
102-
return globalScope.createIsolate(
103-
new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
103+
return TopLevel.createIsolate(
104+
globalScope, new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
104105
} else {
105-
return topLevelScope.createIsolate(
106-
new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
106+
return TopLevel.createIsolate(
107+
topLevelScope, new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
107108
}
108109
}
109110

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public ImporterTopLevel(Context cx, boolean sealed) {
116116
initStandardObjects(cx, sealed);
117117
}
118118

119+
private ImporterTopLevel(ScriptableObject scope) {
120+
super(scope);
121+
topScopeFlag = true;
122+
}
123+
119124
@Override
120125
public String getClassName() {
121126
return topScopeFlag ? "global" : "JavaImporter";
@@ -275,6 +280,19 @@ public ImporterGlobalThis getGlobalThis() {
275280
return (ImporterGlobalThis) super.getGlobalThis();
276281
}
277282

283+
public static TopLevel createIsolate(Context cx, TopLevel parent) {
284+
var newGlobal = new ImporterGlobalThis(true);
285+
newGlobal.setPrototype(parent.getGlobalThis());
286+
newGlobal.setParentScope(null);
287+
newGlobal.put("globalThis", newGlobal, newGlobal);
288+
newGlobal.setAttributes("globalThis", ScriptableObject.DONTENUM);
289+
var isolate = new ImporterTopLevel(newGlobal);
290+
isolate.copyAssociatedValue(parent);
291+
isolate.copyBuiltins(parent, false);
292+
ImporterTopLevel.init(cx, isolate, false, true);
293+
return isolate;
294+
}
295+
278296
private static final String AKEY = "importedPackages";
279297
private final boolean topScopeFlag;
280298
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,22 @@ public TopLevel(ScriptableObject customGlobal) {
131131
globalThis = customGlobal;
132132
}
133133

134-
public TopLevel createIsolate() {
134+
public static TopLevel createIsolate(TopLevel parent) {
135135
var newGlobal = new NativeObject();
136-
newGlobal.setPrototype(getGlobalThis());
136+
newGlobal.setPrototype(parent.getGlobalThis());
137137
newGlobal.setParentScope(null);
138138
var isolate = new TopLevel(newGlobal);
139-
isolate.copyAssociatedValue(this);
140-
isolate.copyBuiltins(this, false);
139+
isolate.copyAssociatedValue(parent);
140+
isolate.copyBuiltins(parent, false);
141141
return isolate;
142142
}
143143

144-
public TopLevel createIsolate(ScriptableObject customGlobal) {
144+
public static TopLevel createIsolate(TopLevel parent, ScriptableObject customGlobal) {
145145
customGlobal.setParentScope(null);
146-
customGlobal.setPrototype(getGlobalThis());
146+
customGlobal.setPrototype(parent.getGlobalThis());
147147
var isolate = new TopLevel(customGlobal);
148-
isolate.copyAssociatedValue(this);
149-
isolate.copyBuiltins(this, false);
148+
isolate.copyAssociatedValue(parent);
149+
isolate.copyBuiltins(parent, false);
150150
return isolate;
151151
}
152152

@@ -155,11 +155,12 @@ public TopLevel createIsolate(ScriptableObject customGlobal) {
155155
* this top level's global object. This should only be done if you know for certain that no
156156
* other use will be made of this prototype chain.
157157
*/
158-
public TopLevel createIsolateCustomPrototypeChain(ScriptableObject customGlobal) {
158+
public static TopLevel createIsolateCustomPrototypeChain(
159+
TopLevel parent, ScriptableObject customGlobal) {
159160
customGlobal.setParentScope(null);
160161
var isolate = new TopLevel(customGlobal);
161-
isolate.copyAssociatedValue(this);
162-
isolate.copyBuiltins(this, false);
162+
isolate.copyAssociatedValue(parent);
163+
isolate.copyBuiltins(parent, false);
163164
return isolate;
164165
}
165166

rhino/src/test/java/org/mozilla/javascript/tests/DynamicScopeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void standardMethodObjectCreate() {
113113
"source2",
114114
1,
115115
null);
116-
var newTopLevel = someScope.createIsolate((ScriptableObject) subScope);
116+
var newTopLevel = TopLevel.createIsolate(someScope, (ScriptableObject) subScope);
117117

118118
Scriptable subObj =
119119
(Scriptable)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.mozilla.javascript.ScriptRuntime;
2525
import org.mozilla.javascript.Scriptable;
2626
import org.mozilla.javascript.ScriptableObject;
27+
import org.mozilla.javascript.TopLevel;
2728
import org.mozilla.javascript.Undefined;
2829
import org.mozilla.javascript.tools.shell.Global;
2930

@@ -71,7 +72,7 @@ private Object executeRhinoScript(boolean interpretedMode) {
7172
Global global = new Global(cx);
7273
loadNatives(global);
7374

74-
var scope = global.createIsolate();
75+
var scope = TopLevel.createIsolate(global);
7576

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

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
import org.mozilla.javascript.ImporterTopLevel;
1818
import org.mozilla.javascript.NativeJavaClass;
1919
import org.mozilla.javascript.Script;
20-
import org.mozilla.javascript.Scriptable;
21-
import org.mozilla.javascript.ScriptableObject;
20+
import org.mozilla.javascript.TopLevel;
2221
import org.mozilla.javascript.UniqueTag;
2322
import org.mozilla.javascript.drivers.TestUtils;
2423
import org.mozilla.javascript.testutils.Utils;
@@ -80,18 +79,16 @@ public void importInSameContext() {
8079
public void importMultipleTimes() {
8180
Utils.runWithAllModes(
8281
cx -> {
83-
ScriptableObject sharedScope = cx.initStandardObjects();
82+
TopLevel sharedScope = cx.initStandardObjects();
8483
// sharedScope.sealObject(); // code below will try to modify sealed object
8584

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

9089
for (int i = 0; i < 3; i++) {
91-
Scriptable scope = new ImporterTopLevel(cx);
92-
scope.setPrototype(sharedScope);
93-
scope.setParentScope(null);
94-
script.exec(cx, scope, scope);
90+
var scope = ImporterTopLevel.createIsolate(cx, sharedScope);
91+
script.exec(cx, scope, scope.getGlobalThis());
9592
assertEquals(UniqueTag.NOT_FOUND, sharedScope.get("UUID", sharedScope));
9693
assertTrue(scope.get("UUID", scope) instanceof NativeJavaClass);
9794
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private Object runScript(String scriptSourceText) {
106106
scope = context.newObject(global);
107107
break;
108108
case SEALED:
109-
scope = global.createIsolate();
109+
scope = TopLevel.createIsolate(global);
110110
break;
111111
case SEALED_OWN_OBJECTS:
112112
scope = context.initStandardObjects(new TopLevel());

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.mozilla.javascript.IdFunctionObject;
2323
import org.mozilla.javascript.ImporterTopLevel;
2424
import org.mozilla.javascript.Scriptable;
25+
import org.mozilla.javascript.TopLevel;
2526
import org.mozilla.javascript.Wrapper;
2627

2728
@RunWith(BlockJUnit4ClassRunner.class)
@@ -41,8 +42,8 @@ public void setUp() throws Exception {
4142

4243
ctx = Context.enter();
4344
ctx.setLanguageVersion(Context.VERSION_DEFAULT);
44-
scope1 = sharedScope.createIsolate();
45-
scope2 = sharedScope.createIsolate();
45+
scope1 = TopLevel.createIsolate(sharedScope);
46+
scope2 = TopLevel.createIsolate(sharedScope);
4647
}
4748

4849
@After

tests/src/test/java/org/mozilla/javascript/tests/commonjs/module/RequireTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public void customGlobal() throws Exception {
8181

8282
var obj = cx.newObject(scope, "CustomGlobal", null);
8383
obj.getPrototype().setPrototype(scope.getGlobalThis());
84-
final TopLevel global = scope.createIsolateCustomPrototypeChain((ScriptableObject) obj);
84+
final TopLevel global =
85+
TopLevel.createIsolateCustomPrototypeChain(scope, (ScriptableObject) obj);
8586

8687
final Require require =
8788
new Require(

0 commit comments

Comments
 (0)