SourceTextModuleRecord.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. class SourceTextModuleRecord;
  9. typedef JsUtil::BaseDictionary<LPCOLESTR, SourceTextModuleRecord*, ArenaAllocator, PowerOf2SizePolicy> ChildModuleRecordSet;
  10. typedef JsUtil::BaseDictionary<SourceTextModuleRecord*, SourceTextModuleRecord*, ArenaAllocator, PowerOf2SizePolicy> ParentModuleRecordSet;
  11. typedef JsUtil::BaseDictionary<PropertyId, uint, ArenaAllocator, PowerOf2SizePolicy> LocalExportMap;
  12. typedef JsUtil::BaseDictionary<PropertyId, ModuleNameRecord, ArenaAllocator, PowerOf2SizePolicy> ResolvedExportMap;
  13. typedef JsUtil::List<PropertyId, ArenaAllocator> LocalExportIndexList;
  14. class SourceTextModuleRecord : public ModuleRecordBase
  15. {
  16. public:
  17. friend class ModuleNamespace;
  18. friend class JavascriptLibrary;
  19. SourceTextModuleRecord(ScriptContext* scriptContext);
  20. IdentPtrList* GetRequestedModuleList() const { return requestedModuleList; }
  21. ModuleImportOrExportEntryList* GetImportEntryList() const { return importRecordList; }
  22. ModuleImportOrExportEntryList* GetLocalExportEntryList() const { return localExportRecordList; }
  23. ModuleImportOrExportEntryList* GetIndirectExportEntryList() const { return indirectExportRecordList; }
  24. ModuleImportOrExportEntryList* GetStarExportRecordList() const { return starExportRecordList; }
  25. virtual ExportedNames* GetExportedNames(ExportModuleRecordList* exportStarSet) override;
  26. virtual bool IsSourceTextModuleRecord() override { return true; } // we don't really have other kind of modulerecord at this time.
  27. // return false when "ambiguous".
  28. // otherwise nullptr means "null" where we have circular reference/cannot resolve.
  29. bool ResolveExport(PropertyId exportName, ResolveSet* resolveSet, ModuleNameRecord** exportRecord) override;
  30. bool ResolveImport(PropertyId localName, ModuleNameRecord** importRecord);
  31. bool ModuleDeclarationInstantiation() override;
  32. void GenerateRootFunction();
  33. Var ModuleEvaluation() override;
  34. virtual ModuleNamespace* GetNamespace();
  35. virtual void SetNamespace(ModuleNamespace* moduleNamespace);
  36. void Finalize(bool isShutdown) override;
  37. void Dispose(bool isShutdown) override { return; }
  38. void Mark(Recycler * recycler) override { return; }
  39. HRESULT ResolveExternalModuleDependencies();
  40. void EnsureChildModuleSet(ScriptContext *scriptContext);
  41. bool ConfirmChildrenParsed();
  42. void* GetHostDefined() const { return hostDefined; }
  43. void SetHostDefined(void* hostObj) { hostDefined = hostObj; }
  44. void SetSpecifier(Var specifier) { this->normalizedSpecifier = specifier; }
  45. Var GetSpecifier() const { return normalizedSpecifier; }
  46. const char16 *GetSpecifierSz() const { return VarTo<JavascriptString>(this->normalizedSpecifier)->GetSz(); }
  47. void SetModuleUrl(Var moduleUrl) { this->moduleUrl = moduleUrl; }
  48. Var GetModuleUrl() const { return moduleUrl;}
  49. const char16 *GetModuleUrlSz() const { return VarTo<JavascriptString>(this->moduleUrl)->GetSz(); }
  50. Var GetErrorObject() const { return errorObject; }
  51. bool WasParsed() const { return wasParsed; }
  52. void SetWasParsed() { wasParsed = true; }
  53. bool WasDeclarationInitialized() const { return wasDeclarationInitialized; }
  54. void SetWasDeclarationInitialized() { wasDeclarationInitialized = true; }
  55. void SetIsRootModule() { isRootModule = true; }
  56. JavascriptPromise *GetPromise() { return this->promise; }
  57. void SetPromise(JavascriptPromise *value) { this->promise = value; }
  58. void SetImportRecordList(ModuleImportOrExportEntryList* importList) { importRecordList = importList; }
  59. void SetLocalExportRecordList(ModuleImportOrExportEntryList* localExports) { localExportRecordList = localExports; }
  60. void SetIndirectExportRecordList(ModuleImportOrExportEntryList* indirectExports) { indirectExportRecordList = indirectExports; }
  61. void SetStarExportRecordList(ModuleImportOrExportEntryList* starExports) { starExportRecordList = starExports; }
  62. void SetRequestedModuleList(IdentPtrList* requestModules) { requestedModuleList = requestModules; }
  63. ScriptContext* GetScriptContext() const { return scriptContext; }
  64. HRESULT ParseSource(__in_bcount(sourceLength) byte* sourceText, uint32 sourceLength, SRCINFO * srcInfo, Var* exceptionVar, bool isUtf8);
  65. HRESULT OnHostException(void* errorVar);
  66. static SourceTextModuleRecord* FromHost(void* hostModuleRecord)
  67. {
  68. SourceTextModuleRecord* moduleRecord = static_cast<SourceTextModuleRecord*>(hostModuleRecord);
  69. Assert((moduleRecord == nullptr) || (moduleRecord->magicNumber == moduleRecord->ModuleMagicNumber));
  70. return moduleRecord;
  71. }
  72. static bool Is(void* hostModuleRecord)
  73. {
  74. SourceTextModuleRecord* moduleRecord = static_cast<SourceTextModuleRecord*>(hostModuleRecord);
  75. if (moduleRecord != nullptr && (moduleRecord->magicNumber == moduleRecord->ModuleMagicNumber))
  76. {
  77. return true;
  78. }
  79. return false;
  80. }
  81. static SourceTextModuleRecord* Create(ScriptContext* scriptContext);
  82. uint GetLocalExportSlotIndexByExportName(PropertyId exportNameId);
  83. uint GetLocalExportSlotIndexByLocalName(PropertyId localNameId);
  84. Field(Var)* GetLocalExportSlots() const { return localExportSlots; }
  85. uint GetLocalExportSlotCount() const { return localSlotCount; }
  86. uint GetModuleId() const { return moduleId; }
  87. uint GetLocalExportCount() const { return localExportCount; }
  88. ModuleNameRecord* GetNamespaceNameRecord() { return &namespaceRecord; }
  89. SourceTextModuleRecord* GetChildModuleRecord(LPCOLESTR specifier) const;
  90. void SetParent(SourceTextModuleRecord* parentRecord, LPCOLESTR moduleName);
  91. Utf8SourceInfo* GetSourceInfo() { return this->pSourceInfo; }
  92. static Var ResolveOrRejectDynamicImportPromise(bool isResolve, Var value, ScriptContext *scriptContext, SourceTextModuleRecord *mr = nullptr, bool useReturn = true);
  93. Var PostProcessDynamicModuleImport();
  94. private:
  95. const static uint InvalidModuleIndex = 0xffffffff;
  96. const static uint InvalidSlotCount = 0xffffffff;
  97. const static uint InvalidSlotIndex = 0xffffffff;
  98. // TODO: move non-GC fields out to avoid false reference?
  99. // This is the parsed tree resulted from compilation.
  100. Field(bool) confirmedReady = false;
  101. Field(bool) notifying = false;
  102. Field(bool) wasParsed;
  103. Field(bool) wasDeclarationInitialized;
  104. Field(bool) parentsNotified;
  105. Field(bool) isRootModule;
  106. Field(bool) hadNotifyHostReady;
  107. Field(ParseNodeProg *) parseTree;
  108. Field(Utf8SourceInfo*) pSourceInfo;
  109. Field(uint) sourceIndex;
  110. FieldNoBarrier(Parser*) parser; // we'll need to keep the parser around till we are done with bytecode gen.
  111. Field(ScriptContext*) scriptContext;
  112. Field(IdentPtrList*) requestedModuleList;
  113. Field(ModuleImportOrExportEntryList*) importRecordList;
  114. Field(ModuleImportOrExportEntryList*) localExportRecordList;
  115. Field(ModuleImportOrExportEntryList*) indirectExportRecordList;
  116. Field(ModuleImportOrExportEntryList*) starExportRecordList;
  117. Field(ChildModuleRecordSet*) childrenModuleSet;
  118. Field(ModuleRecordList*) parentModuleList;
  119. Field(LocalExportMap*) localExportMapByExportName; // from propertyId to index map: for bytecode gen.
  120. Field(LocalExportMap*) localExportMapByLocalName; // from propertyId to index map: for bytecode gen.
  121. Field(LocalExportIndexList*) localExportIndexList; // from index to propertyId: for typehandler.
  122. Field(ExportedNames*) exportedNames;
  123. Field(ResolvedExportMap*) resolvedExportMap;
  124. Field(Js::JavascriptFunction*) rootFunction;
  125. Field(void*) hostDefined;
  126. Field(Var) moduleUrl;
  127. Field(Var) normalizedSpecifier;
  128. Field(Var) errorObject;
  129. Field(Field(Var)*) localExportSlots;
  130. Field(uint) localSlotCount;
  131. // module export allows aliasing, like export {foo as foo1, foo2, foo3}.
  132. Field(uint) localExportCount;
  133. Field(uint) moduleId;
  134. Field(ModuleNameRecord) namespaceRecord;
  135. Field(JavascriptPromise*) promise;
  136. HRESULT PostParseProcess();
  137. HRESULT PrepareForModuleDeclarationInitialization();
  138. void ReleaseParserResources();
  139. void ReleaseParserResourcesForHierarchy();
  140. void ImportModuleListsFromParser();
  141. HRESULT OnChildModuleReady(SourceTextModuleRecord* childModule, Var errorObj);
  142. void NotifyParentsAsNeeded();
  143. void CleanupBeforeExecution();
  144. void InitializeLocalImports();
  145. void InitializeLocalExports();
  146. void InitializeIndirectExports();
  147. bool ParentsNotified() const { return parentsNotified; }
  148. void SetParentsNotified() { parentsNotified = true; }
  149. PropertyId EnsurePropertyIdForIdentifier(IdentPtr pid);
  150. LocalExportMap* GetLocalExportMap() const { return localExportMapByExportName; }
  151. LocalExportIndexList* GetLocalExportIndexList() const { return localExportIndexList; }
  152. ResolvedExportMap* GetExportedNamesMap() const { return resolvedExportMap; }
  153. };
  154. struct ServerSourceTextModuleRecord
  155. {
  156. uint moduleId;
  157. Field(Var)* localExportSlotsAddr;
  158. };
  159. }