Skip to content

Commit a642231

Browse files
committed
scope handling fix (wip)
1 parent f7846b1 commit a642231

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/main/java/org/htmlunit/javascript/HtmlUnitScriptable.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.htmlunit.html.DomNode;
3939
import org.htmlunit.html.HtmlImage;
4040
import org.htmlunit.javascript.host.Window;
41+
import org.htmlunit.javascript.host.WindowOrWorkerGlobalScope;
4142
import org.htmlunit.javascript.host.html.HTMLElement;
4243
import org.htmlunit.javascript.host.html.HTMLUnknownElement;
4344

@@ -339,6 +340,19 @@ protected static Window getWindow(final Scriptable s) throws RuntimeException {
339340
throw new RuntimeException("Unable to find window associated with " + s);
340341
}
341342

343+
protected static WindowOrWorkerGlobalScope getWindowOrWorkerGlobalScope(
344+
final Scriptable s) throws RuntimeException {
345+
if (s instanceof WindowOrWorkerGlobalScope wow) {
346+
return wow;
347+
}
348+
349+
final TopLevel topLevel = ScriptableObject.getTopLevelScope(s);
350+
if (topLevel.getGlobalThis() instanceof WindowOrWorkerGlobalScope wow) {
351+
return wow;
352+
}
353+
throw new RuntimeException("Unable to find WindowOrWorkerGlobalScope associated with " + s);
354+
}
355+
342356
/**
343357
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
344358
*

src/main/java/org/htmlunit/javascript/JavaScriptEngine.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@
7575
import org.htmlunit.javascript.host.ConsoleCustom;
7676
import org.htmlunit.javascript.host.URLSearchParams;
7777
import org.htmlunit.javascript.host.Window;
78+
import org.htmlunit.javascript.host.WindowOrWorkerGlobalScope;
7879
import org.htmlunit.javascript.host.dom.DOMException;
7980
import org.htmlunit.javascript.host.html.HTMLElement;
8081
import org.htmlunit.javascript.host.html.HTMLImageElement;
8182
import org.htmlunit.javascript.host.html.HTMLOptionElement;
8283
import org.htmlunit.javascript.host.intl.Intl;
84+
import org.htmlunit.javascript.host.worker.WorkerGlobalScope;
8385
import org.htmlunit.javascript.host.xml.FormData;
8486
import org.htmlunit.javascript.polyfill.Polyfill;
8587
import org.htmlunit.util.StringUtils;
@@ -1286,7 +1288,14 @@ public static EcmaError constructError(final String error, final String message)
12861288
public static RhinoException asJavaScriptException(final HtmlUnitScriptable scriptable, final String message, final int type) {
12871289
final DOMException domException = new DOMException(message, type);
12881290
domException.setParentScope(scriptable.getParentScope());
1289-
domException.setPrototype(scriptable.getWindow().getPrototype(DOMException.class));
1291+
1292+
final WindowOrWorkerGlobalScope wow = HtmlUnitScriptable.getWindowOrWorkerGlobalScope(scriptable);
1293+
if (wow instanceof Window window) {
1294+
domException.setPrototype(window.getPrototype(DOMException.class));
1295+
}
1296+
else if (wow instanceof WorkerGlobalScope w) {
1297+
domException.setPrototype(w.getPrototype(DOMException.class));
1298+
}
12901299

12911300
final EcmaError helper = ScriptRuntime.syntaxError("helper");
12921301
String fileName = helper.sourceName();

0 commit comments

Comments
 (0)