@@ -722,8 +722,8 @@ namespace Js
722722 Recycler* recycler = GetScriptContext ()->GetRecycler ();
723723 this ->parentModuleList = RecyclerNew (recycler, ModuleRecordList, recycler);
724724 }
725- bool contains = this -> parentModuleList -> Contains (parentRecord);
726- if (!contains )
725+
726+ if (!this -> parentModuleList -> Contains (parentRecord) )
727727 {
728728 this ->parentModuleList ->Add (parentRecord);
729729 parentRecord->numPendingChildrenModule ++;
@@ -751,10 +751,18 @@ namespace Js
751751 if (requestedModuleList != nullptr )
752752 {
753753 EnsureChildModuleSet (scriptContext);
754+ ArenaAllocator* allocator = scriptContext->GeneralAllocator ();
755+ SList<LPCOLESTR> * moduleRecords = Anew (allocator, SList<LPCOLESTR>, allocator);
756+
757+ // Reverse the order for the host. So, host can read the files top-down
754758 requestedModuleList->MapUntil ([&](IdentPtr specifier) {
759+ LPCOLESTR moduleName = specifier->Psz ();
760+ return !moduleRecords->Prepend (moduleName);
761+ });
762+
763+ moduleRecords->MapUntil ([&](LPCOLESTR moduleName) {
755764 ModuleRecordBase* moduleRecordBase = nullptr ;
756765 SourceTextModuleRecord* moduleRecord = nullptr ;
757- LPCOLESTR moduleName = specifier->Psz ();
758766 bool itemFound = childrenModuleSet->TryGetValue (moduleName, &moduleRecord);
759767 if (!itemFound)
760768 {
@@ -779,6 +787,8 @@ namespace Js
779787 }
780788 return false ;
781789 });
790+ moduleRecords->Clear ();
791+
782792 if (FAILED (hr))
783793 {
784794 if (this ->errorObject == nullptr )
@@ -827,20 +837,15 @@ namespace Js
827837 SetWasDeclarationInitialized ();
828838 if (childrenModuleSet != nullptr )
829839 {
830- childrenModuleSet->Map ([](LPCOLESTR specifier, SourceTextModuleRecord* moduleRecord )
840+ childrenModuleSet->EachValue ([=]( SourceTextModuleRecord* childModuleRecord )
831841 {
832- Assert (moduleRecord->WasParsed ());
833- moduleRecord->shouldGenerateRootFunction =
834- moduleRecord->ModuleDeclarationInstantiation ();
842+ Assert (childModuleRecord->WasParsed ());
843+ childModuleRecord->ModuleDeclarationInstantiation ();
835844 });
836845
837- childrenModuleSet->Map ([](LPCOLESTR specifier, SourceTextModuleRecord* moduleRecord )
846+ childrenModuleSet->EachValue ([=]( SourceTextModuleRecord* childModuleRecord )
838847 {
839- if (moduleRecord->shouldGenerateRootFunction )
840- {
841- moduleRecord->shouldGenerateRootFunction = false ;
842- moduleRecord->GenerateRootFunction ();
843- }
848+ childModuleRecord->GenerateRootFunction ();
844849 });
845850 }
846851
@@ -866,6 +871,13 @@ namespace Js
866871
867872 void SourceTextModuleRecord::GenerateRootFunction ()
868873 {
874+ // On cyclic dependency, we may endup generating the root function twice
875+ // so make sure we don't
876+ if (this ->rootFunction != nullptr )
877+ {
878+ return ;
879+ }
880+
869881 ScriptContext* scriptContext = GetScriptContext ();
870882 Js::AutoDynamicCodeReference dynamicFunctionReference (scriptContext);
871883 CompileScriptException se;
0 commit comments