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