SourceTextModuleRecord.h 9.7 KB

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