SourceTextModuleRecord.h 9.3 KB

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