Skip to content

Commit adebc68

Browse files
committed
scope handling fix (wip)
1 parent d138383 commit adebc68

31 files changed

Lines changed: 54 additions & 71 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ protected Script compileString(String source, final Evaluator compiler,
198198
}
199199

200200
@Override
201-
protected Function compileFunction(final Scriptable scope, String source,
201+
protected Function compileFunction(final VarScope scope, String source,
202202
final Evaluator compiler, final ErrorReporter compilationErrorReporter,
203203
final String sourceName, final int lineno, final Object securityDomain) {
204204

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626
import org.htmlunit.BrowserVersion;
27-
import org.htmlunit.SgmlPage;
2827
import org.htmlunit.WebAssert;
2928
import org.htmlunit.WebWindow;
3029
import org.htmlunit.corejs.javascript.Context;
@@ -267,24 +266,14 @@ else if (domNode.hasFeature(HTMLIMAGE_HTMLUNKNOWNELEMENT)) {
267266
throw JavaScriptEngine.throwAsScriptRuntimeEx(e);
268267
}
269268
}
270-
initParentScope(domNode, scriptable);
271269

270+
scriptable.setParentScope(getParentScope());
272271
scriptable.setPrototype(getPrototype(javaScriptClass));
273272
scriptable.setDomNode(domNode);
274273

275274
return scriptable;
276275
}
277276

278-
/**
279-
* Initialize the parent scope of a newly created scriptable.
280-
* @param domNode the DOM node for the script object
281-
* @param scriptable the script object to initialize
282-
*/
283-
protected void initParentScope(final DomNode domNode, final HtmlUnitScriptable scriptable) {
284-
final SgmlPage page = domNode.getPage();
285-
scriptable.setParentScope(ScriptableObject.getTopLevelScope(page.getScriptableObject()));
286-
}
287-
288277
/**
289278
* Gets the prototype object for the given host class.
290279
* @param javaScriptClass the host class
@@ -333,7 +322,7 @@ protected static Window getWindow(final Scriptable s) throws RuntimeException {
333322
return window;
334323
}
335324

336-
final TopLevel topLevel = ScriptableObject.getTopLevelScope(s);
325+
final TopLevel topLevel = ScriptableObject.getTopLevelScope(s.getParentScope());
337326
if (topLevel.getGlobalThis() instanceof Window window) {
338327
return window;
339328
}
@@ -346,7 +335,7 @@ protected static WindowOrWorkerGlobalScope getWindowOrWorkerGlobalScope(
346335
return wow;
347336
}
348337

349-
final TopLevel topLevel = ScriptableObject.getTopLevelScope(s);
338+
final TopLevel topLevel = ScriptableObject.getTopLevelScope(s.getParentScope());
350339
if (topLevel.getGlobalThis() instanceof WindowOrWorkerGlobalScope wow) {
351340
return wow;
352341
}
@@ -433,7 +422,7 @@ public HtmlUnitScriptable clone() {
433422
}
434423

435424
protected NativePromise setupPromise(final FailableSupplier<Object, IOException> resolver) {
436-
final VarScope scope = ScriptableObject.getTopLevelScope(this);
425+
final VarScope scope = ScriptableObject.getTopLevelScope(getParentScope());
437426
final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
438427

439428
try {
@@ -448,7 +437,7 @@ protected NativePromise setupPromise(final FailableSupplier<Object, IOException>
448437
}
449438

450439
protected NativePromise setupRejectedPromise(final Supplier<Object> resolver) {
451-
final VarScope scope = ScriptableObject.getTopLevelScope(this);
440+
final VarScope scope = ScriptableObject.getTopLevelScope(getParentScope());
452441
final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
453442
final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
454443
return (NativePromise) reject.call(Context.getCurrentContext(), scope, ctor, new Object[] {resolver.get()});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static BroadcastChannel jsConstructor(final Context cx, final VarScope sc
6666

6767
final BroadcastChannel broadcastChannel = new BroadcastChannel();
6868
final Window window = getWindow(ctorObj);
69-
broadcastChannel.setParentScope(getTopLevelScope(ctorObj));
69+
broadcastChannel.setParentScope(getTopLevelScope(scope));
7070
broadcastChannel.setPrototype(((FunctionObject) ctorObj).getClassPrototype());
7171

7272
broadcastChannel.name_ = JavaScriptEngine.toString(args[0]);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected void createEventHandler(final String eventName, final String attrValue
150150

151151
// TODO: check that it is an "allowed" event for the browser, and take care to the case
152152
final BaseFunction eventHandler = new EventHandler(htmlElt, eventName, attrValue);
153-
eventHandler.setPrototype(ScriptableObject.getClassPrototype(htmlElt.getScriptableObject(), "Function"));
153+
eventHandler.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), "Function"));
154154

155155
setEventHandler(eventName, eventHandler);
156156
}
@@ -346,7 +346,7 @@ public void removeAttribute(final String name) {
346346
@JsxFunction
347347
public DOMRect getBoundingClientRect() {
348348
final DOMRect textRectangle = new DOMRect(1, 1, 0, 0);
349-
textRectangle.setParentScope(getTopLevelScope(this));
349+
textRectangle.setParentScope(getTopLevelScope(getParentScope()));
350350
textRectangle.setPrototype(getPrototype(textRectangle.getClass()));
351351
return textRectangle;
352352
}
@@ -662,7 +662,7 @@ public HTMLCollection getElementsByClassName(final String className) {
662662
*/
663663
@JsxFunction
664664
public DOMRectList getClientRects() {
665-
final Scriptable topScope = getTopLevelScope(this);
665+
final Scriptable topScope = getTopLevelScope(getParentScope());
666666
final DOMRectList rectList = new DOMRectList();
667667
rectList.setParentScope(topScope);
668668
rectList.setPrototype(getPrototype(rectList.getClass()));
@@ -1614,7 +1614,7 @@ public static boolean matches(final Context context, final VarScope scope,
16141614
}
16151615
catch (final CSSException e) {
16161616
throw JavaScriptEngine.asJavaScriptException(
1617-
(HtmlUnitScriptable) getTopLevelScope(thisObj).getGlobalThis(),
1617+
(HtmlUnitScriptable) getTopLevelScope(scope).getGlobalThis(),
16181618
"An invalid or illegal selector was specified (selector: '"
16191619
+ selectorString + "' error: " + e.getMessage() + ").",
16201620
DOMException.SYNTAX_ERR);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public NativeUint8Array encode(final Object toEncode) {
6060
if (JavaScriptEngine.isUndefined(toEncode)) {
6161
final NativeUint8Array result = new NativeUint8Array(0);
6262
result.setParentScope(getParentScope());
63-
result.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), result.getClassName()));
63+
result.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), result.getClassName()));
6464
return result;
6565
}
6666

@@ -79,7 +79,7 @@ public NativeUint8Array encode(final Object toEncode) {
7979

8080
final NativeUint8Array result = new NativeUint8Array(arrayBuffer, 0, bytes.length);
8181
result.setParentScope(getParentScope());
82-
result.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), result.getClassName()));
82+
result.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), result.getClassName()));
8383
return result;
8484
}
8585
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void onWebSocketBinary(final byte[] data, final int offset, final int len
173173
final NativeArrayBuffer buffer = new NativeArrayBuffer(length);
174174
System.arraycopy(data, offset, buffer.getBuffer(), 0, length);
175175
buffer.setParentScope(getParentScope());
176-
buffer.setPrototype(ScriptableObject.getClassPrototype(getWindow(), buffer.getClassName()));
176+
buffer.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), buffer.getClassName()));
177177

178178
final MessageEvent msgEvent = new MessageEvent(buffer);
179179
msgEvent.setParentScope(scope);
@@ -270,7 +270,7 @@ public static Scriptable jsConstructor(final Context cx, final VarScope scope, f
270270
throw JavaScriptEngine.reportRuntimeError(
271271
"WebSocket Error: 'url' parameter '" + urlString + "' is not a valid url.");
272272
}
273-
return new WebSocket(urlString, getTopLevelScope(ctorObj), win);
273+
return new WebSocket(urlString, getTopLevelScope(scope), win);
274274
}
275275

276276
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,8 @@ public static void postMessage(final Context context, final VarScope scope,
19881988
}
19891989
}
19901990

1991-
final Window sender = (Window) ScriptableObject.getTopLevelScope(scope).getGlobalThis();
1991+
final TopLevel topLevel = ScriptableObject.getTopLevelScope(scope);
1992+
final Window sender = (Window) topLevel.getGlobalThis();
19921993
final Window receiver = (Window) thisObj;
19931994
final URL receiverURL = receiver.getWebWindow().getEnclosedPage().getUrl();
19941995

@@ -2007,7 +2008,7 @@ public static void postMessage(final Context context, final VarScope scope,
20072008
}
20082009
catch (final Exception e) {
20092010
throw JavaScriptEngine.asJavaScriptException(
2010-
(HtmlUnitScriptable) getTopLevelScope(thisObj).getGlobalThis(),
2011+
sender,
20112012
"Failed to execute 'postMessage' on 'Window': Invalid target origin '"
20122013
+ targetOrigin + "' was specified (reason: " + e.getMessage() + ".",
20132014
DOMException.SYNTAX_ERR);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static Object queueMicrotask(final Scriptable thisObj, final Object[] arg
172172
}
173173

174174
final Function callback = (Function) args[0];
175-
final VarScope scope = ScriptableObject.getTopLevelScope(thisObj);
175+
final VarScope scope = ScriptableObject.getTopLevelScope(thisObj.getParentScope());
176176
final Context cx = Context.getCurrentContext();
177177
cx.enqueueMicrotask(() -> {
178178
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public NativeUint8ClampedArray getData() {
169169

170170
data_ = new NativeUint8ClampedArray(arrayBuffer, 0, bytes_.length);
171171
data_.setParentScope(getParentScope());
172-
data_.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), data_.getClassName()));
172+
data_.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), data_.getClassName()));
173173
}
174174

175175
return data_;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ else if (internalKey instanceof SecretKey) {
9494
key.usages_ = new LinkedHashSet<>(usages);
9595

9696
key.setParentScope(scope);
97-
key.setPrototype(getWindow(scope).getPrototype(CryptoKey.class));
97+
key.setPrototype(getWindow(key).getPrototype(CryptoKey.class));
9898
return key;
9999
}
100100

0 commit comments

Comments
 (0)