@@ -211,7 +211,8 @@ private void init(final WebWindow webWindow, final Page page, final Context cx)
211211 final Window jsWindow = new Window ();
212212 jsWindow .setClassName ("Window" );
213213
214- final Scriptable scope = cx .initSafeStandardObjects (jsWindow );
214+ final TopLevel scope = cx .initSafeStandardObjects (new TopLevel (jsWindow ));
215+ jsWindow .setParentScope (scope );
215216 configureRhino (webClient , browserVersion , scope , jsWindow );
216217
217218 final Map <Class <? extends Scriptable >, Scriptable > prototypes = new HashMap <>();
@@ -232,8 +233,8 @@ private void init(final WebWindow webWindow, final Page page, final Context cx)
232233 configureGlobalThis (scope , jsWindow , windowConfig , functionObject , jsConfig_ , browserVersion , prototypes , prototypesPerJSName );
233234
234235 // TODO remove the cast
235- URLSearchParams .NativeParamsIterator .init (( ScriptableObject ) scope , "URLSearchParams Iterator" );
236- FormData .FormDataIterator .init (( ScriptableObject ) scope , "FormData Iterator" );
236+ URLSearchParams .NativeParamsIterator .init (scope , "URLSearchParams Iterator" );
237+ FormData .FormDataIterator .init (scope , "FormData Iterator" );
237238
238239 // strange but this is the reality for browsers
239240 // because there will be still some sites using this for browser detection the property is
@@ -275,7 +276,7 @@ private void init(final WebWindow webWindow, final Page page, final Context cx)
275276 * @throws Exception in case of error
276277 */
277278 public static void configureGlobalThis (
278- final Scriptable scope ,
279+ final TopLevel scope ,
279280 final HtmlUnitScriptable globalThis ,
280281 final ClassConfiguration scopeConfig ,
281282 final FunctionObject scopeContructorFunctionObject ,
@@ -414,17 +415,17 @@ private static void additionalCtor(final Scriptable scope, final Window window,
414415 * @param globalThis the window or the DedicatedWorkerGlobalScope
415416 */
416417 public static void configureRhino (final WebClient webClient , final BrowserVersion browserVersion ,
417- final Scriptable scope , final HtmlUnitScriptable globalThis ) {
418+ final TopLevel scope , final HtmlUnitScriptable globalThis ) {
418419
419420 // this should be like
420421 // NativeConsole.init(scope, globalThis, false, webClient.getWebConsole());
421422 // but so far both objects are the same
422- NativeConsole .init (globalThis , false , webClient .getWebConsole ());
423+ NativeConsole .init (scope , false , webClient .getWebConsole ());
423424
424425 // https://developer.mozilla.org/en-US/docs/Web/API/console/timeStamp_static
425426 // this is not standard and therefore not in Rhino
426427 final ScriptableObject console = (ScriptableObject ) ScriptableObject .getProperty (globalThis , "console" );
427- console .defineFunctionProperties (new String [] {"timeStamp" }, ConsoleCustom .class , ScriptableObject .DONTENUM );
428+ console .defineFunctionProperties (scope , new String [] {"timeStamp" }, ConsoleCustom .class , ScriptableObject .DONTENUM );
428429
429430 // remove some objects, that Rhino defines in top scope but that we don't want
430431 deleteProperties (globalThis , "Continuation" , "StopIteration" , "uneval" , "global" );
@@ -543,7 +544,7 @@ private static void removePrototypeProperties(final Scriptable scope, final Stri
543544 * @throws Exception in case of errors
544545 */
545546 public static HtmlUnitScriptable configureClass (final ClassConfiguration config ,
546- final Scriptable scope )
547+ final TopLevel scope )
547548 throws Exception {
548549
549550 final HtmlUnitScriptable prototype = config .getHostClass ().getDeclaredConstructor ().newInstance ();
@@ -561,7 +562,7 @@ public static HtmlUnitScriptable configureClass(final ClassConfiguration config,
561562 * @param scriptable the object to configure
562563 */
563564 private static void configureConstantsStaticPropertiesAndStaticFunctions (final ClassConfiguration config ,
564- final Scriptable scope , final ScriptableObject scriptable ) {
565+ final TopLevel scope , final ScriptableObject scriptable ) {
565566 configureConstants (config , scriptable );
566567 configureStaticProperties (config , scope , scriptable );
567568 configureStaticFunctions (config , scope , scriptable );
@@ -574,7 +575,7 @@ private static void configureConstantsStaticPropertiesAndStaticFunctions(final C
574575 * @param scriptable the object to configure
575576 */
576577 private static void configureConstantsPropertiesAndFunctions (final ClassConfiguration config ,
577- final Scriptable scope , final ScriptableObject scriptable ) {
578+ final TopLevel scope , final ScriptableObject scriptable ) {
578579 configureConstants (config , scriptable );
579580 configureProperties (config , scope , scriptable );
580581 configureFunctions (config , scope , scriptable );
@@ -583,7 +584,7 @@ private static void configureConstantsPropertiesAndFunctions(final ClassConfigur
583584 }
584585
585586 private static void configureFunctions (final ClassConfiguration config ,
586- final Scriptable scope , final ScriptableObject scriptable ) {
587+ final TopLevel scope , final ScriptableObject scriptable ) {
587588 // the functions
588589 final Map <String , Method > functionMap = config .getFunctionMap ();
589590 if (functionMap != null ) {
@@ -606,20 +607,20 @@ private static void configureConstants(final ClassConfiguration config, final Sc
606607 }
607608
608609 private static void configureProperties (final ClassConfiguration config ,
609- final Scriptable scope , final ScriptableObject scriptable ) {
610+ final TopLevel scope , final ScriptableObject scriptable ) {
610611 final Map <String , PropertyInfo > propertyMap = config .getPropertyMap ();
611612 if (propertyMap != null ) {
612613 for (final Entry <String , PropertyInfo > propertyEntry : propertyMap .entrySet ()) {
613614 final PropertyInfo info = propertyEntry .getValue ();
614615 final Method readMethod = info .getReadMethod ();
615616 final Method writeMethod = info .getWriteMethod ();
616- scriptable .defineProperty (propertyEntry .getKey (), null , readMethod , writeMethod , ScriptableObject .EMPTY );
617+ scriptable .defineProperty (scope , propertyEntry .getKey (), null , readMethod , writeMethod , ScriptableObject .EMPTY );
617618 }
618619 }
619620 }
620621
621622 private static void configureStaticProperties (final ClassConfiguration config ,
622- final Scriptable scope , final ScriptableObject scriptable ) {
623+ final TopLevel scope , final ScriptableObject scriptable ) {
623624 final Map <String , PropertyInfo > staticPropertyMap = config .getStaticPropertyMap ();
624625 if (staticPropertyMap != null ) {
625626 for (final Entry <String , ClassConfiguration .PropertyInfo > propertyEntry : staticPropertyMap .entrySet ()) {
@@ -628,13 +629,13 @@ private static void configureStaticProperties(final ClassConfiguration config,
628629 final Method writeMethod = propertyEntry .getValue ().getWriteMethod ();
629630 final int flag = ScriptableObject .EMPTY ;
630631
631- scriptable .defineProperty (propertyName , null , readMethod , writeMethod , flag );
632+ scriptable .defineProperty (scope , propertyName , null , readMethod , writeMethod , flag );
632633 }
633634 }
634635 }
635636
636637 private static void configureStaticFunctions (final ClassConfiguration config ,
637- final Scriptable scope , final ScriptableObject scriptable ) {
638+ final TopLevel scope , final ScriptableObject scriptable ) {
638639 final Map <String , Method > staticFunctionMap = config .getStaticFunctionMap ();
639640 if (staticFunctionMap != null ) {
640641 for (final Entry <String , Method > staticFunctionInfo : staticFunctionMap .entrySet ()) {
@@ -656,7 +657,7 @@ private static void configureSymbolConstants(final ClassConfiguration config, fi
656657 }
657658
658659 private static void configureSymbols (final ClassConfiguration config ,
659- final Scriptable scope , final ScriptableObject scriptable ) {
660+ final TopLevel scope , final ScriptableObject scriptable ) {
660661 final Map <Symbol , Method > symbolMap = config .getSymbolMap ();
661662 if (symbolMap != null ) {
662663 for (final Entry <Symbol , Method > symbolInfo : symbolMap .entrySet ()) {
@@ -791,7 +792,7 @@ public Object execute(final HtmlPage page, final Scriptable scope, final Script
791792 final HtmlUnitContextAction action = new HtmlUnitContextAction (page ) {
792793 @ Override
793794 public Object doRun (final Context cx ) {
794- return script .exec (cx , scope , scope );
795+ return script .exec (cx , scope , (( TopLevel ) scope ). getGlobalThis () );
795796 }
796797
797798 @ Override
@@ -1259,15 +1260,15 @@ public static EcmaError constructError(final String error, final String message)
12591260 *
12601261 * Creates a {@link DOMException} and encapsulates it into a Rhino-compatible exception.
12611262 *
1262- * @param scope the parent scope
1263+ * @param scriptable the scriptable triggering this
12631264 * @param message the exception message
12641265 * @param type the exception type
12651266 * @return the created exception
12661267 */
1267- public static RhinoException asJavaScriptException (final HtmlUnitScriptable scope , final String message , final int type ) {
1268+ public static RhinoException asJavaScriptException (final HtmlUnitScriptable scriptable , final String message , final int type ) {
12681269 final DOMException domException = new DOMException (message , type );
1269- domException .setParentScope (scope );
1270- domException .setPrototype (scope .getPrototype (DOMException .class ));
1270+ domException .setParentScope (scriptable . getParentScope () );
1271+ domException .setPrototype (scriptable . getWindow () .getPrototype (DOMException .class ));
12711272
12721273 final EcmaError helper = ScriptRuntime .syntaxError ("helper" );
12731274 String fileName = helper .sourceName ();
@@ -1480,7 +1481,7 @@ public static String evaluateProxyAutoConfig(final BrowserVersion browserVersion
14801481 ProxyAutoConfigJavaScriptConfiguration .getInstance (browserVersion );
14811482
14821483 final ScriptableObject globalThis = new NativeObject ();
1483- final Scriptable scope = cx .initSafeStandardObjects (globalThis );
1484+ final TopLevel scope = cx .initSafeStandardObjects (new TopLevel ( globalThis ) );
14841485
14851486 for (final ClassConfiguration config : jsConfig .getAll ()) {
14861487 configureFunctions (config , scope , globalThis );
0 commit comments