22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
5- use crate :: dom:: bindings:: cell:: DomRefCell ;
5+ use crate :: dom:: bindings:: cell:: { DomRefCell , RefMut } ;
66use crate :: dom:: bindings:: codegen:: Bindings :: BroadcastChannelBinding :: BroadcastChannelMethods ;
77use crate :: dom:: bindings:: codegen:: Bindings :: EventSourceBinding :: EventSourceBinding :: EventSourceMethods ;
88use crate :: dom:: bindings:: codegen:: Bindings :: GPUValidationErrorBinding :: GPUError ;
@@ -55,7 +55,8 @@ use crate::dom::workerglobalscope::WorkerGlobalScope;
5555use crate :: dom:: workletglobalscope:: WorkletGlobalScope ;
5656use crate :: microtask:: { Microtask , MicrotaskQueue , UserMicrotask } ;
5757use crate :: realms:: { enter_realm, AlreadyInRealm , InRealm } ;
58- use crate :: script_module:: ModuleTree ;
58+ use crate :: script_module:: { DynamicModuleList , ModuleTree } ;
59+ use crate :: script_module:: { ModuleScript , ScriptFetchOptions } ;
5960use crate :: script_runtime:: {
6061 CommonScriptMsg , ContextForRequestInterrupt , JSContext as SafeJSContext , ScriptChan , ScriptPort ,
6162} ;
@@ -81,13 +82,16 @@ use embedder_traits::EmbedderMsg;
8182use ipc_channel:: ipc:: { self , IpcSender } ;
8283use ipc_channel:: router:: ROUTER ;
8384use js:: glue:: { IsWrapper , UnwrapObjectDynamic } ;
85+ use js:: jsapi:: Compile1 ;
86+ use js:: jsapi:: SetScriptPrivate ;
8487use js:: jsapi:: { CurrentGlobalOrNull , GetNonCCWObjectGlobal } ;
8588use js:: jsapi:: { HandleObject , Heap } ;
8689use js:: jsapi:: { JSContext , JSObject } ;
90+ use js:: jsval:: PrivateValue ;
8791use js:: jsval:: { JSVal , UndefinedValue } ;
8892use js:: panic:: maybe_resume_unwind;
8993use js:: rust:: transform_str_to_source_text;
90- use js:: rust:: wrappers:: Evaluate2 ;
94+ use js:: rust:: wrappers:: { JS_ExecuteScript , JS_GetScriptPrivate } ;
9195use js:: rust:: { get_object_class, CompileOptionsWrapper , ParentRuntime , Runtime } ;
9296use js:: rust:: { HandleValue , MutableHandleValue } ;
9397use js:: { JSCLASS_IS_DOMJSCLASS , JSCLASS_IS_GLOBAL } ;
@@ -306,6 +310,9 @@ pub struct GlobalScope {
306310
307311 /// The stack of active group labels for the Console APIs.
308312 console_group_stack : DomRefCell < Vec < DOMString > > ,
313+
314+ /// List of ongoing dynamic module imports.
315+ dynamic_modules : DomRefCell < DynamicModuleList > ,
309316}
310317
311318/// A wrapper for glue-code between the ipc router and the event-loop.
@@ -757,6 +764,7 @@ impl GlobalScope {
757764 frozen_supported_performance_entry_types : DomRefCell :: new ( Default :: default ( ) ) ,
758765 https_state : Cell :: new ( HttpsState :: None ) ,
759766 console_group_stack : DomRefCell :: new ( Vec :: new ( ) ) ,
767+ dynamic_modules : DomRefCell :: new ( DynamicModuleList :: new ( ) ) ,
760768 }
761769 }
762770
@@ -2537,8 +2545,21 @@ impl GlobalScope {
25372545 }
25382546
25392547 /// Evaluate JS code on this global scope.
2540- pub fn evaluate_js_on_global_with_result ( & self , code : & str , rval : MutableHandleValue ) -> bool {
2541- self . evaluate_script_on_global_with_result ( code, "" , rval, 1 )
2548+ pub fn evaluate_js_on_global_with_result (
2549+ & self ,
2550+ code : & str ,
2551+ rval : MutableHandleValue ,
2552+ fetch_options : ScriptFetchOptions ,
2553+ script_base_url : ServoUrl ,
2554+ ) -> bool {
2555+ self . evaluate_script_on_global_with_result (
2556+ code,
2557+ "" ,
2558+ rval,
2559+ 1 ,
2560+ fetch_options,
2561+ script_base_url,
2562+ )
25422563 }
25432564
25442565 /// Evaluate a JS script on this global scope.
@@ -2549,6 +2570,8 @@ impl GlobalScope {
25492570 filename : & str ,
25502571 rval : MutableHandleValue ,
25512572 line_number : u32 ,
2573+ fetch_options : ScriptFetchOptions ,
2574+ script_base_url : ServoUrl ,
25522575 ) -> bool {
25532576 let metadata = profile_time:: TimerMetadata {
25542577 url : if filename. is_empty ( ) {
@@ -2570,26 +2593,59 @@ impl GlobalScope {
25702593 let ar = enter_realm ( & * self ) ;
25712594
25722595 let _aes = AutoEntryScript :: new ( self ) ;
2573- let options =
2574- unsafe { CompileOptionsWrapper :: new ( * cx, filename. as_ptr ( ) , line_number) } ;
2575-
2576- debug ! ( "evaluating Dom string" ) ;
2577- let result = unsafe {
2578- Evaluate2 (
2579- * cx,
2580- options. ptr ,
2581- & mut transform_str_to_source_text ( code) ,
2582- rval,
2583- )
2584- } ;
25852596
2586- if !result {
2587- debug ! ( "error evaluating Dom string" ) ;
2588- unsafe { report_pending_exception ( * cx, true , InRealm :: Entered ( & ar) ) } ;
2589- }
2597+ unsafe {
2598+ let options = CompileOptionsWrapper :: new ( * cx, filename. as_ptr ( ) , line_number) ;
25902599
2591- maybe_resume_unwind ( ) ;
2592- result
2600+ debug ! ( "compiling Dom string" ) ;
2601+ rooted ! ( in( * cx) let compiled_script =
2602+ Compile1 (
2603+ * cx,
2604+ options. ptr,
2605+ & mut transform_str_to_source_text( code) ,
2606+ )
2607+ ) ;
2608+
2609+ if compiled_script. is_null ( ) {
2610+ debug ! ( "error compiling Dom string" ) ;
2611+ report_pending_exception ( * cx, true , InRealm :: Entered ( & ar) ) ;
2612+ return false ;
2613+ }
2614+
2615+ rooted ! ( in( * cx) let mut script_private = UndefinedValue ( ) ) ;
2616+ JS_GetScriptPrivate ( * compiled_script, script_private. handle_mut ( ) ) ;
2617+
2618+ // When `ScriptPrivate` for the compiled script is undefined,
2619+ // we need to set it so that it can be used in dynamic import context.
2620+ if script_private. is_undefined ( ) {
2621+ debug ! ( "Set script private for {}" , script_base_url) ;
2622+
2623+ let module_script_data = Rc :: new ( ModuleScript :: new (
2624+ script_base_url,
2625+ fetch_options,
2626+ // We can't initialize an module owner here because
2627+ // the executing context of script might be different
2628+ // from the dynamic import script's executing context.
2629+ None ,
2630+ ) ) ;
2631+
2632+ SetScriptPrivate (
2633+ * compiled_script,
2634+ & PrivateValue ( Rc :: into_raw ( module_script_data) as * const _ ) ,
2635+ ) ;
2636+ }
2637+
2638+ debug ! ( "evaluating Dom string" ) ;
2639+ let result = JS_ExecuteScript ( * cx, compiled_script. handle ( ) , rval) ;
2640+
2641+ if !result {
2642+ debug ! ( "error evaluating Dom string" ) ;
2643+ report_pending_exception ( * cx, true , InRealm :: Entered ( & ar) ) ;
2644+ }
2645+
2646+ maybe_resume_unwind ( ) ;
2647+ result
2648+ }
25932649 } ,
25942650 )
25952651 }
@@ -2991,6 +3047,10 @@ impl GlobalScope {
29913047 pub ( crate ) fn pop_console_group ( & self ) {
29923048 let _ = self . console_group_stack . borrow_mut ( ) . pop ( ) ;
29933049 }
3050+
3051+ pub ( crate ) fn dynamic_module_list ( & self ) -> RefMut < DynamicModuleList > {
3052+ self . dynamic_modules . borrow_mut ( )
3053+ }
29943054}
29953055
29963056fn timestamp_in_ms ( time : Timespec ) -> u64 {
0 commit comments