@@ -12,8 +12,10 @@ use crate::dom::bindings::inheritance::Castable;
1212use crate :: dom:: bindings:: refcounted:: Trusted ;
1313use crate :: dom:: bindings:: reflector:: DomObject ;
1414use crate :: dom:: bindings:: root:: { Dom , DomRoot } ;
15+ use crate :: dom:: bindings:: settings_stack:: AutoEntryScript ;
1516use crate :: dom:: bindings:: str:: { DOMString , USVString } ;
1617use crate :: dom:: document:: Document ;
18+ use crate :: dom:: domexception:: { DOMErrorName , DOMException } ;
1719use crate :: dom:: element:: {
1820 cors_setting_for_element, reflect_cross_origin_attribute, set_cross_origin_attribute,
1921} ;
@@ -26,7 +28,8 @@ use crate::dom::node::{BindContext, ChildrenMutation, CloneChildrenFlag, Node};
2628use crate :: dom:: performanceresourcetiming:: InitiatorType ;
2729use crate :: dom:: virtualmethods:: VirtualMethods ;
2830use crate :: network_listener:: { self , NetworkListener , PreInvoke , ResourceTimingListener } ;
29- use crate :: script_module:: { fetch_module_script_graph, ModuleOwner } ;
31+ use crate :: script_module:: { execute_module, instantiate_module_tree} ;
32+ use crate :: script_module:: { fetch_module_script_graph, ModuleObject , ModuleOwner } ;
3033use dom_struct:: dom_struct;
3134use encoding_rs:: Encoding ;
3235use html5ever:: { LocalName , Prefix } ;
@@ -127,7 +130,7 @@ static SCRIPT_JS_MIMES: StaticStringVec = &[
127130 "text/x-javascript" ,
128131] ;
129132
130- #[ derive( JSTraceable , MallocSizeOf ) ]
133+ #[ derive( JSTraceable , MallocSizeOf , PartialEq ) ]
131134pub enum ScriptType {
132135 Classic ,
133136 Module ,
@@ -546,9 +549,9 @@ impl HTMLScriptElement {
546549 ) ;
547550
548551 if r#async {
549- // doc.add_asap_script(self);
552+ doc. add_asap_script ( self ) ;
550553 } else {
551- // doc.add_deferred_script(self);
554+ doc. add_deferred_script ( self ) ;
552555 } ;
553556 } ,
554557 } ;
@@ -628,7 +631,7 @@ impl HTMLScriptElement {
628631 }
629632
630633 /// <https://html.spec.whatwg.org/multipage/#execute-the-script-block>
631- pub fn execute ( & self , result : Result < ScriptOrigin , NetworkError > ) {
634+ pub fn execute ( & self , result : ScriptResult ) {
632635 // Step 1.
633636 let doc = document_from_node ( self ) ;
634637 if self . parser_inserted . get ( ) && & * doc != & * self . parser_document {
@@ -646,7 +649,9 @@ impl HTMLScriptElement {
646649 Ok ( script) => script,
647650 } ;
648651
649- self . unminify_js ( & mut script) ;
652+ if script. type_ == ScriptType :: Classic {
653+ self . unminify_js ( & mut script) ;
654+ }
650655
651656 // Step 3.
652657 let neutralized_doc = if script. external {
@@ -662,21 +667,24 @@ impl HTMLScriptElement {
662667 let document = document_from_node ( self ) ;
663668 let old_script = document. GetCurrentScript ( ) ;
664669
665- // Step 5.a.1.
666- document. set_current_script ( Some ( self ) ) ;
667-
668- // Step 5.a.2.
669- self . run_a_classic_script ( & script) ;
670-
671- // Step 6.
672- document. set_current_script ( old_script. deref ( ) ) ;
670+ match script. type_ {
671+ ScriptType :: Classic => {
672+ document. set_current_script ( Some ( self ) ) ;
673+ self . run_a_classic_script ( & script) ;
674+ document. set_current_script ( old_script. deref ( ) ) ;
675+ } ,
676+ ScriptType :: Module => {
677+ assert ! ( old_script. is_none( ) ) ;
678+ self . run_a_module_script ( & script, false ) ;
679+ } ,
680+ }
673681
674- // Step 7 .
682+ // Step 5 .
675683 if let Some ( doc) = neutralized_doc {
676684 doc. decr_ignore_destructive_writes_counter ( ) ;
677685 }
678686
679- // Step 8 .
687+ // Step 6 .
680688 if script. external {
681689 self . dispatch_load_event ( ) ;
682690 }
@@ -708,6 +716,42 @@ impl HTMLScriptElement {
708716 ) ;
709717 }
710718
719+ #[ allow( unsafe_code) ]
720+ /// https://html.spec.whatwg.org/multipage/#run-a-module-script
721+ pub fn run_a_module_script ( & self , script : & ScriptOrigin , _rethrow_errors : bool ) {
722+ // TODO use a settings object rather than this element's document/window
723+ // Step 2
724+ let document = document_from_node ( self ) ;
725+ if !document. is_fully_active ( ) || !document. is_scripting_enabled ( ) {
726+ return ;
727+ }
728+
729+ // Step 4
730+ let window = window_from_node ( self ) ;
731+ let global = window. upcast :: < GlobalScope > ( ) ;
732+ let _aes = AutoEntryScript :: new ( & global) ;
733+
734+ // TODO: Step 6. Check script's error to throw
735+
736+ let module_map = global. get_module_map ( ) . borrow ( ) ;
737+
738+ if let Some ( module_record) = module_map. get ( & script. url ) {
739+ // Step 7-1.
740+ if let ModuleObject :: Fetched ( Some ( record) ) = module_record {
741+ unsafe {
742+ // Step 7-2.
743+ let _instantiated = instantiate_module_tree ( global, record. handle ( ) ) ;
744+ let evaluated = execute_module ( global, record. handle ( ) ) ;
745+
746+ if evaluated. is_err ( ) {
747+ let _exception =
748+ DOMException :: new ( & global, DOMErrorName :: QuotaExceededError ) ;
749+ }
750+ }
751+ }
752+ }
753+ }
754+
711755 pub fn queue_error_event ( & self ) {
712756 let window = window_from_node ( self ) ;
713757 window
0 commit comments