Skip to content

Commit 24cf02e

Browse files
committed
Associate internal values with globalThis so isolates can find them.
1 parent 40d0232 commit 24cf02e

File tree

7 files changed

+30
-0
lines changed

7 files changed

+30
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ static ES6Generator init(ScriptableObject scope, boolean sealed) {
3131
// approach instead.
3232
if (scope != null) {
3333
scope.associateValue(GENERATOR_TAG, prototype);
34+
if (scope instanceof TopLevel) {
35+
((TopLevel) scope).getGlobalThis().associateValue(GENERATOR_TAG, prototype);
36+
}
3437
}
3538

3639
return prototype;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ protected static void init(
2727
// approach instead.
2828
if (scope != null) {
2929
scope.associateValue(tag, prototype);
30+
if (scope instanceof TopLevel) {
31+
((TopLevel) scope).getGlobalThis().associateValue(tag, prototype);
32+
}
3033
}
3134
}
3235

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ static NativeGenerator init(ScriptableObject scope, boolean sealed) {
3838
// approach instead.
3939
if (scope != null) {
4040
scope.associateValue(GENERATOR_TAG, prototype);
41+
if (scope instanceof TopLevel) {
42+
((TopLevel) scope).getGlobalThis().associateValue(GENERATOR_TAG, prototype);
43+
}
4144
}
4245

4346
return prototype;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ static void init(Context cx, ScriptableObject scope, boolean sealed) {
4242
// throw StopIteration even if the property of the global
4343
// scope is replaced or deleted.
4444
scope.associateValue(ITERATOR_TAG, obj);
45+
if (scope instanceof TopLevel) {
46+
((TopLevel) scope).getGlobalThis().associateValue(ITERATOR_TAG, obj);
47+
}
4548
}
4649

4750
/** Only for constructing the prototype object. */

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ public static ScriptableObject initSafeStandardObjects(
191191
}
192192

193193
scope.associateValue(LIBRARY_SCOPE_KEY, scope);
194+
if (scope instanceof TopLevel) {
195+
((TopLevel) scope).getGlobalThis().associateValue(LIBRARY_SCOPE_KEY, scope);
196+
}
197+
194198
new ClassCache().associate(scope);
195199
new ConcurrentFactory().associate(scope);
196200

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,16 @@ public final Object getAssociatedValue(Object key) {
29452945
*/
29462946
public static Object getTopScopeValue(Scriptable scope, Object key) {
29472947
scope = ScriptableObject.getTopLevelScope(scope);
2948+
if (scope instanceof ScriptableObject) {
2949+
ScriptableObject so = (ScriptableObject) scope;
2950+
Object value = so.getAssociatedValue(key);
2951+
if (value != null) {
2952+
return value;
2953+
}
2954+
}
2955+
if (scope instanceof TopLevel) {
2956+
scope = ((TopLevel) scope).getGlobalThis();
2957+
}
29482958
for (; ; ) {
29492959
if (scope instanceof ScriptableObject) {
29502960
ScriptableObject so = (ScriptableObject) scope;

rhino/src/main/java/org/mozilla/javascript/lc/type/TypeInfoFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.function.Supplier;
2424
import org.mozilla.javascript.Scriptable;
2525
import org.mozilla.javascript.ScriptableObject;
26+
import org.mozilla.javascript.TopLevel;
2627
import org.mozilla.javascript.lc.type.impl.factory.NoCacheFactory;
2728
import org.mozilla.javascript.lc.type.impl.factory.WeakReferenceFactory;
2829

@@ -365,6 +366,9 @@ default TypeInfoFactory associate(ScriptableObject topScope) {
365366
if (topScope.getParentScope() != null) {
366367
throw new IllegalArgumentException("provided scope not top scope");
367368
}
369+
if (topScope instanceof TopLevel) {
370+
topScope = ((TopLevel) topScope).getGlobalThis();
371+
}
368372
return (TypeInfoFactory) topScope.associateValue("TypeInfoFactory", this);
369373
}
370374

0 commit comments

Comments
 (0)