2424import org .apache .commons .logging .Log ;
2525import org .apache .commons .logging .LogFactory ;
2626import org .htmlunit .BrowserVersion ;
27- import org .htmlunit .SgmlPage ;
2827import org .htmlunit .WebAssert ;
2928import org .htmlunit .WebWindow ;
3029import 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 ()});
0 commit comments