Skip to content

Commit 6689d9d

Browse files
committed
scope handling (wip)
1 parent 2b91d05 commit 6689d9d

File tree

6 files changed

+16
-10
lines changed

6 files changed

+16
-10
lines changed

src/main/java/org/htmlunit/html/HtmlPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final
10281028

10291029
@SuppressWarnings("unchecked")
10301030
final AbstractJavaScriptEngine<Object> engine = (AbstractJavaScriptEngine<Object>) client.getJavaScriptEngine();
1031-
engine.execute(this, getEnclosingWindow().getScriptableObject(), script);
1031+
engine.execute(this, getEnclosingWindow().getTopLevelScope(), script);
10321032
return JavaScriptLoadResult.SUCCESS;
10331033
}
10341034

src/main/java/org/htmlunit/javascript/host/Window.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ public Selection getSelection() {
17661766
public Selection getSelectionImpl() {
17671767
if (selection_ == null) {
17681768
selection_ = new Selection();
1769-
selection_.setParentScope(this);
1769+
selection_.setParentScope(getWebWindow().getTopLevelScope());
17701770
selection_.setPrototype(getPrototype(selection_.getClass()));
17711771
}
17721772
return selection_;
@@ -2075,7 +2075,7 @@ public static int getPort(final URL url) {
20752075
public Scriptable getPerformance() {
20762076
if (performance_ == null) {
20772077
final Performance performance = new Performance();
2078-
performance.setParentScope(this);
2078+
performance.setParentScope(getWebWindow().getTopLevelScope());
20792079
performance.setPrototype(getPrototype(performance.getClass()));
20802080
performance_ = performance;
20812081
}

src/main/java/org/htmlunit/javascript/host/canvas/ImageData.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.htmlunit.corejs.javascript.Function;
1919
import org.htmlunit.corejs.javascript.Scriptable;
2020
import org.htmlunit.corejs.javascript.ScriptableObject;
21+
import org.htmlunit.corejs.javascript.TopLevel;
2122
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
2223
import org.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray;
2324
import org.htmlunit.javascript.HtmlUnitScriptable;
@@ -72,7 +73,7 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
7273
data = array;
7374
if (data.getArrayLength() % 4 != 0) {
7475
throw JavaScriptEngine.asJavaScriptException(
75-
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
76+
(HtmlUnitScriptable) ((TopLevel) JavaScriptEngine.getTopCallScope()).getGlobalThis(),
7677
"ImageData ctor - data length mod 4 not zero",
7778
DOMException.INVALID_STATE_ERR);
7879
}
@@ -83,7 +84,7 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
8384

8485
if (data.getArrayLength() != 4 * width * height) {
8586
throw JavaScriptEngine.asJavaScriptException(
86-
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
87+
(HtmlUnitScriptable) ((TopLevel) JavaScriptEngine.getTopCallScope()).getGlobalThis(),
8788
"ImageData ctor - width not correct",
8889
DOMException.INDEX_SIZE_ERR);
8990
}
@@ -94,7 +95,7 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
9495

9596
if (data.getArrayLength() != 4 * width * height) {
9697
throw JavaScriptEngine.asJavaScriptException(
97-
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
98+
(HtmlUnitScriptable) ((TopLevel) JavaScriptEngine.getTopCallScope()).getGlobalThis(),
9899
"ImageData ctor - width/height not correct",
99100
DOMException.INDEX_SIZE_ERR);
100101
}
@@ -106,13 +107,13 @@ public static ImageData jsConstructor(final Context cx, final Scriptable scope,
106107

107108
if (width < 0) {
108109
throw JavaScriptEngine.asJavaScriptException(
109-
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
110+
(HtmlUnitScriptable) ((TopLevel) JavaScriptEngine.getTopCallScope()).getGlobalThis(),
110111
"ImageData ctor - width negative",
111112
DOMException.INDEX_SIZE_ERR);
112113
}
113114
if (height < 0) {
114115
throw JavaScriptEngine.asJavaScriptException(
115-
(HtmlUnitScriptable) JavaScriptEngine.getTopCallScope(),
116+
(HtmlUnitScriptable) ((TopLevel) JavaScriptEngine.getTopCallScope()).getGlobalThis(),
116117
"ImageData ctor - height negative",
117118
DOMException.INDEX_SIZE_ERR);
118119
}

src/main/java/org/htmlunit/javascript/host/crypto/Crypto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void jsConstructor() {
6060
*/
6161
public Crypto(final Window window) {
6262
this();
63-
setParentScope(window);
63+
setParentScope(window.getWebWindow().getTopLevelScope());
6464
setPrototype(window.getPrototype(Crypto.class));
6565
}
6666

src/main/java/org/htmlunit/javascript/host/event/EventListenersContainer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.htmlunit.corejs.javascript.NativeObject;
3030
import org.htmlunit.corejs.javascript.Scriptable;
3131
import org.htmlunit.corejs.javascript.ScriptableObject;
32+
import org.htmlunit.corejs.javascript.TopLevel;
3233
import org.htmlunit.html.DomNode;
3334
import org.htmlunit.html.HtmlPage;
3435
import org.htmlunit.javascript.JavaScriptEngine;
@@ -281,7 +282,10 @@ private void executeEventListeners(final int eventPhase, final Event event, fina
281282
page = (HtmlPage) jsNode_.getDomNodeOrDie();
282283
}
283284
else {
284-
final Scriptable parentScope = jsNode_.getParentScope();
285+
Scriptable parentScope = jsNode_.getParentScope();
286+
if (parentScope instanceof TopLevel topLevel) {
287+
parentScope = topLevel.getGlobalThis();
288+
}
285289
if (parentScope instanceof Window window) {
286290
page = (HtmlPage) window.getDomNodeOrDie();
287291
}

src/main/java/org/htmlunit/javascript/host/worker/DedicatedWorkerGlobalScope.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public void jsConstructor() {
128128

129129
final TopLevel scope = context.initSafeStandardObjects(new TopLevel(this));
130130
this.setParentScope(scope);
131+
131132
JavaScriptEngine.configureRhino(webClient, browserVersion, scope, this);
132133

133134
final WorkerJavaScriptConfiguration jsConfig = WorkerJavaScriptConfiguration.getInstance(browserVersion);

0 commit comments

Comments
 (0)