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 :: ImageBitmapBinding :: {
@@ -51,7 +51,8 @@ use crate::dom::workerglobalscope::WorkerGlobalScope;
5151use crate :: dom:: workletglobalscope:: WorkletGlobalScope ;
5252use crate :: microtask:: { Microtask , MicrotaskQueue , UserMicrotask } ;
5353use crate :: realms:: { enter_realm, AlreadyInRealm , InRealm } ;
54- use crate :: script_module:: ModuleTree ;
54+ use crate :: script_module:: { DynamicModuleList , ModuleTree } ;
55+ use crate :: script_module:: { ModuleScript , ScriptFetchOptions } ;
5556use crate :: script_runtime:: {
5657 CommonScriptMsg , ContextForRequestInterrupt , JSContext as SafeJSContext , ScriptChan , ScriptPort ,
5758} ;
@@ -77,13 +78,16 @@ use embedder_traits::EmbedderMsg;
7778use ipc_channel:: ipc:: { self , IpcSender } ;
7879use ipc_channel:: router:: ROUTER ;
7980use js:: glue:: { IsWrapper , UnwrapObjectDynamic } ;
81+ use js:: jsapi:: CompileForNonSyntacticScope1 ;
8082use js:: jsapi:: { CurrentGlobalOrNull , GetNonCCWObjectGlobal } ;
83+ use js:: jsapi:: { GetScriptPrivate , SetScriptPrivate } ;
8184use js:: jsapi:: { HandleObject , Heap } ;
8285use js:: jsapi:: { JSContext , JSObject } ;
86+ use js:: jsval:: PrivateValue ;
8387use js:: jsval:: { JSVal , UndefinedValue } ;
8488use js:: panic:: maybe_resume_unwind;
8589use js:: rust:: transform_str_to_source_text;
86- use js:: rust:: wrappers:: Evaluate2 ;
90+ use js:: rust:: wrappers:: JS_ExecuteScript ;
8791use js:: rust:: { get_object_class, CompileOptionsWrapper , ParentRuntime , Runtime } ;
8892use js:: rust:: { HandleValue , MutableHandleValue } ;
8993use js:: { JSCLASS_IS_DOMJSCLASS , JSCLASS_IS_GLOBAL } ;
@@ -298,6 +302,9 @@ pub struct GlobalScope {
298302
299303 /// The stack of active group labels for the Console APIs.
300304 console_group_stack : DomRefCell < Vec < DOMString > > ,
305+
306+ /// List of ongoing dynamic module imports.
307+ dynamic_modules : DomRefCell < DynamicModuleList > ,
301308}
302309
303310/// A wrapper for glue-code between the ipc router and the event-loop.
@@ -748,6 +755,7 @@ impl GlobalScope {
748755 frozen_supported_performance_entry_types : DomRefCell :: new ( Default :: default ( ) ) ,
749756 https_state : Cell :: new ( HttpsState :: None ) ,
750757 console_group_stack : DomRefCell :: new ( Vec :: new ( ) ) ,
758+ dynamic_modules : DomRefCell :: new ( DynamicModuleList :: new ( ) ) ,
751759 }
752760 }
753761
@@ -2529,7 +2537,7 @@ impl GlobalScope {
25292537
25302538 /// Evaluate JS code on this global scope.
25312539 pub fn evaluate_js_on_global_with_result ( & self , code : & str , rval : MutableHandleValue ) -> bool {
2532- self . evaluate_script_on_global_with_result ( code, "" , rval, 1 )
2540+ self . evaluate_script_on_global_with_result ( code, "" , rval, 1 , None )
25332541 }
25342542
25352543 /// Evaluate a JS script on this global scope.
@@ -2540,6 +2548,7 @@ impl GlobalScope {
25402548 filename : & str ,
25412549 rval : MutableHandleValue ,
25422550 line_number : u32 ,
2551+ script_url : Option < ServoUrl > ,
25432552 ) -> bool {
25442553 let metadata = profile_time:: TimerMetadata {
25452554 url : if filename. is_empty ( ) {
@@ -2561,26 +2570,51 @@ impl GlobalScope {
25612570 let ar = enter_realm ( & * self ) ;
25622571
25632572 let _aes = AutoEntryScript :: new ( self ) ;
2564- let options =
2565- unsafe { CompileOptionsWrapper :: new ( * cx, filename. as_ptr ( ) , line_number) } ;
2566-
2567- debug ! ( "evaluating Dom string" ) ;
2568- let result = unsafe {
2569- Evaluate2 (
2570- * cx,
2571- options. ptr ,
2572- & mut transform_str_to_source_text ( code) ,
2573- rval,
2574- )
2575- } ;
25762573
2577- if !result {
2578- debug ! ( "error evaluating Dom string" ) ;
2579- unsafe { report_pending_exception ( * cx, true , InRealm :: Entered ( & ar) ) } ;
2580- }
2574+ unsafe {
2575+ let options = CompileOptionsWrapper :: new ( * cx, filename. as_ptr ( ) , line_number) ;
2576+
2577+ rooted ! ( in( * cx) let compiled_script = {
2578+ CompileForNonSyntacticScope1 (
2579+ * cx,
2580+ options. ptr,
2581+ & mut transform_str_to_source_text( code) ,
2582+ )
2583+ } ) ;
2584+
2585+ // When `ScriptPrivate` for the compiled script is undefined,
2586+ // we need to set it so that it can be used in dynamic import context.
2587+ if GetScriptPrivate ( * compiled_script) . is_undefined ( ) {
2588+ let base_url = script_url. unwrap_or ( self . api_base_url ( ) ) ;
2589+
2590+ debug ! ( "Set script private for {}" , base_url) ;
2591+
2592+ let module_script_data = Box :: new ( ModuleScript :: new (
2593+ base_url,
2594+ ScriptFetchOptions :: default_classic_script ( & self ) ,
2595+ // We can't initialize an module owner here because
2596+ // the executing context of script might be different
2597+ // from the dynamic import script's executing context.
2598+ None ,
2599+ ) ) ;
2600+
2601+ SetScriptPrivate (
2602+ * compiled_script,
2603+ & PrivateValue ( Box :: into_raw ( module_script_data) as * const _ ) ,
2604+ ) ;
2605+ }
2606+
2607+ debug ! ( "evaluating Dom string" ) ;
2608+ let result = JS_ExecuteScript ( * cx, compiled_script. handle ( ) , rval) ;
2609+
2610+ if !result {
2611+ debug ! ( "error evaluating Dom string" ) ;
2612+ report_pending_exception ( * cx, true , InRealm :: Entered ( & ar) ) ;
2613+ }
25812614
2582- maybe_resume_unwind ( ) ;
2583- result
2615+ maybe_resume_unwind ( ) ;
2616+ result
2617+ }
25842618 } ,
25852619 )
25862620 }
@@ -2953,6 +2987,10 @@ impl GlobalScope {
29532987 pub ( crate ) fn pop_console_group ( & self ) {
29542988 let _ = self . console_group_stack . borrow_mut ( ) . pop ( ) ;
29552989 }
2990+
2991+ pub ( crate ) fn dynamic_module_list ( & self ) -> RefMut < DynamicModuleList > {
2992+ self . dynamic_modules . borrow_mut ( )
2993+ }
29562994}
29572995
29582996fn timestamp_in_ms ( time : Timespec ) -> u64 {
0 commit comments