SourceTextModuleRecord.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. #include "RuntimeLanguagePch.h"
  6. #include "Types/PropertyIndexRanges.h"
  7. #include "Types/SimpleDictionaryPropertyDescriptor.h"
  8. #include "Types/SimpleDictionaryTypeHandler.h"
  9. #include "ModuleNamespace.h"
  10. namespace Js
  11. {
  12. const uint32 ModuleRecordBase::ModuleMagicNumber = *(const uint32*)"Mode";
  13. SourceTextModuleRecord::SourceTextModuleRecord(ScriptContext* scriptContext) :
  14. ModuleRecordBase(scriptContext->GetLibrary()),
  15. scriptContext(scriptContext),
  16. parseTree(nullptr),
  17. parser(nullptr),
  18. pSourceInfo(nullptr),
  19. rootFunction(nullptr),
  20. requestedModuleList(nullptr),
  21. importRecordList(nullptr),
  22. localExportRecordList(nullptr),
  23. indirectExportRecordList(nullptr),
  24. starExportRecordList(nullptr),
  25. childrenModuleSet(nullptr),
  26. parentModuleList(nullptr),
  27. localExportMapByExportName(nullptr),
  28. localExportMapByLocalName(nullptr),
  29. localExportIndexList(nullptr),
  30. normalizedSpecifier(nullptr),
  31. errorObject(nullptr),
  32. hostDefined(nullptr),
  33. exportedNames(nullptr),
  34. resolvedExportMap(nullptr),
  35. wasParsed(false),
  36. wasDeclarationInitialized(false),
  37. isRootModule(false),
  38. hadNotifyHostReady(false),
  39. localExportSlots(nullptr),
  40. numUnInitializedChildrenModule(0),
  41. moduleId(InvalidModuleIndex),
  42. localSlotCount(InvalidSlotCount),
  43. localExportCount(0)
  44. {
  45. namespaceRecord.module = this;
  46. namespaceRecord.bindingName = PropertyIds::star_;
  47. }
  48. SourceTextModuleRecord* SourceTextModuleRecord::Create(ScriptContext* scriptContext)
  49. {
  50. Recycler* recycler = scriptContext->GetRecycler();
  51. SourceTextModuleRecord* childModuleRecord;
  52. childModuleRecord = RecyclerNewFinalized(recycler, Js::SourceTextModuleRecord, scriptContext);
  53. // There is no real reference to lifetime management in ecmascript
  54. // The life time of a module record should be controlled by the module registry as defined in WHATWG module loader spec
  55. // in practice the modulerecord lifetime should be the same as the scriptcontext so it could be retrieved for the same
  56. // site. Host might hold a reference to the module as well after initializing the module.
  57. // In our implementation, we'll use the moduleId in bytecode to identify the module.
  58. childModuleRecord->moduleId = scriptContext->GetLibrary()->EnsureModuleRecordList()->Add(childModuleRecord);
  59. return childModuleRecord;
  60. }
  61. void SourceTextModuleRecord::Finalize(bool isShutdown)
  62. {
  63. parseTree = nullptr;
  64. requestedModuleList = nullptr;
  65. importRecordList = nullptr;
  66. localExportRecordList = nullptr;
  67. indirectExportRecordList = nullptr;
  68. starExportRecordList = nullptr;
  69. childrenModuleSet = nullptr;
  70. parentModuleList = nullptr;
  71. if (!isShutdown)
  72. {
  73. if (parser != nullptr)
  74. {
  75. AllocatorDelete(ArenaAllocator, scriptContext->GeneralAllocator(), parser);
  76. parser = nullptr;
  77. }
  78. }
  79. }
  80. HRESULT SourceTextModuleRecord::ParseSource(__in_bcount(sourceLength) byte* sourceText, uint32 sourceLength, SRCINFO * srcInfo, Var* exceptionVar, bool isUtf8)
  81. {
  82. Assert(!wasParsed);
  83. Assert(parser == nullptr);
  84. HRESULT hr = NOERROR;
  85. ScriptContext* scriptContext = GetScriptContext();
  86. CompileScriptException se;
  87. ArenaAllocator* allocator = scriptContext->GeneralAllocator();
  88. *exceptionVar = nullptr;
  89. if (!scriptContext->GetConfig()->IsES6ModuleEnabled())
  90. {
  91. return E_NOTIMPL;
  92. }
  93. // Host indicates that the current module failed to load.
  94. if (sourceText == nullptr)
  95. {
  96. Assert(sourceLength == 0);
  97. hr = E_FAIL;
  98. JavascriptError *pError = scriptContext->GetLibrary()->CreateError();
  99. JavascriptError::SetErrorMessageProperties(pError, hr, _u("host failed to download module"), scriptContext);
  100. *exceptionVar = pError;
  101. }
  102. else
  103. {
  104. try
  105. {
  106. AUTO_NESTED_HANDLED_EXCEPTION_TYPE((ExceptionType)(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  107. this->parser = (Parser*)AllocatorNew(ArenaAllocator, allocator, Parser, scriptContext);
  108. srcInfo->moduleID = moduleId;
  109. LoadScriptFlag loadScriptFlag = (LoadScriptFlag)(LoadScriptFlag_Expression | LoadScriptFlag_Module |
  110. (isUtf8 ? LoadScriptFlag_Utf8Source : LoadScriptFlag_None));
  111. Utf8SourceInfo* pResultSourceInfo = nullptr;
  112. this->parseTree = scriptContext->ParseScript(parser, sourceText,
  113. sourceLength, srcInfo, &se, &pResultSourceInfo, _u("module"),
  114. loadScriptFlag, &sourceIndex, nullptr);
  115. this->pSourceInfo = pResultSourceInfo;
  116. if (parseTree == nullptr)
  117. {
  118. hr = E_FAIL;
  119. }
  120. }
  121. catch (Js::OutOfMemoryException)
  122. {
  123. hr = E_OUTOFMEMORY;
  124. se.ProcessError(nullptr, E_OUTOFMEMORY, nullptr);
  125. }
  126. catch (Js::StackOverflowException)
  127. {
  128. hr = VBSERR_OutOfStack;
  129. se.ProcessError(nullptr, VBSERR_OutOfStack, nullptr);
  130. }
  131. if (SUCCEEDED(hr))
  132. {
  133. hr = PostParseProcess();
  134. }
  135. }
  136. if (FAILED(hr))
  137. {
  138. if (*exceptionVar == nullptr)
  139. {
  140. *exceptionVar = JavascriptError::CreateFromCompileScriptException(scriptContext, &se);
  141. }
  142. if (this->parser)
  143. {
  144. this->parseTree = nullptr;
  145. AllocatorDelete(ArenaAllocator, allocator, this->parser);
  146. this->parser = nullptr;
  147. }
  148. if (this->errorObject == nullptr)
  149. {
  150. this->errorObject = *exceptionVar;
  151. }
  152. NotifyParentsAsNeeded();
  153. }
  154. return hr;
  155. }
  156. void SourceTextModuleRecord::NotifyParentsAsNeeded()
  157. {
  158. // Notify the parent modules that this child module is either in fault state or finished.
  159. if (this->parentModuleList != nullptr)
  160. {
  161. parentModuleList->Map([=](uint i, SourceTextModuleRecord* parentModule)
  162. {
  163. parentModule->OnChildModuleReady(this, this->errorObject);
  164. });
  165. }
  166. }
  167. void SourceTextModuleRecord::ImportModuleListsFromParser()
  168. {
  169. Assert(scriptContext->GetConfig()->IsES6ModuleEnabled());
  170. PnModule* moduleParseNode = static_cast<PnModule*>(&this->parseTree->sxModule);
  171. SetrequestedModuleList(moduleParseNode->requestedModules);
  172. SetImportRecordList(moduleParseNode->importEntries);
  173. SetStarExportRecordList(moduleParseNode->starExportEntries);
  174. SetIndirectExportRecordList(moduleParseNode->indirectExportEntries);
  175. SetLocalExportRecordList(moduleParseNode->localExportEntries);
  176. }
  177. HRESULT SourceTextModuleRecord::PostParseProcess()
  178. {
  179. HRESULT hr = NOERROR;
  180. SetWasParsed();
  181. ImportModuleListsFromParser();
  182. hr = ResolveExternalModuleDependencies();
  183. if (SUCCEEDED(hr))
  184. {
  185. hr = PrepareForModuleDeclarationInitialization();
  186. }
  187. return hr;
  188. }
  189. HRESULT SourceTextModuleRecord::PrepareForModuleDeclarationInitialization()
  190. {
  191. HRESULT hr = NO_ERROR;
  192. if (numUnInitializedChildrenModule == 0)
  193. {
  194. NotifyParentsAsNeeded();
  195. if (!WasDeclarationInitialized() && isRootModule)
  196. {
  197. // TODO: move this as a promise call? if parser is called from a different thread
  198. // We'll need to call the bytecode gen in the main thread as we are accessing GC.
  199. ScriptContext* scriptContext = GetScriptContext();
  200. Assert(!scriptContext->GetThreadContext()->IsScriptActive());
  201. Assert(this->errorObject == nullptr);
  202. ModuleDeclarationInstantiation();
  203. if (!hadNotifyHostReady)
  204. {
  205. hr = scriptContext->GetHostScriptContext()->NotifyHostAboutModuleReady(this, this->errorObject);
  206. hadNotifyHostReady = true;
  207. }
  208. }
  209. }
  210. return hr;
  211. }
  212. HRESULT SourceTextModuleRecord::OnChildModuleReady(SourceTextModuleRecord* childModule, Var childException)
  213. {
  214. HRESULT hr = NOERROR;
  215. if (childException != nullptr)
  216. {
  217. // propagate the error up as needed.
  218. if (this->errorObject == nullptr)
  219. {
  220. this->errorObject = childException;
  221. }
  222. NotifyParentsAsNeeded();
  223. if (isRootModule && !hadNotifyHostReady)
  224. {
  225. hr = scriptContext->GetHostScriptContext()->NotifyHostAboutModuleReady(this, this->errorObject);
  226. hadNotifyHostReady = true;
  227. }
  228. }
  229. else
  230. {
  231. if (numUnInitializedChildrenModule == 0)
  232. {
  233. return NOERROR; // this is only in case of recursive module reference. Let the higher stack frame handle this module.
  234. }
  235. numUnInitializedChildrenModule--;
  236. hr = PrepareForModuleDeclarationInitialization();
  237. }
  238. return hr;
  239. }
  240. ModuleNamespace* SourceTextModuleRecord::GetNamespace()
  241. {
  242. Assert(localExportSlots != nullptr);
  243. Assert(static_cast<ModuleNamespace*>(localExportSlots[GetLocalExportSlotCount()]) == __super::GetNamespace());
  244. return static_cast<ModuleNamespace*>(localExportSlots[GetLocalExportSlotCount()]);
  245. }
  246. void SourceTextModuleRecord::SetNamespace(ModuleNamespace* moduleNamespace)
  247. {
  248. Assert(localExportSlots != nullptr);
  249. __super::SetNamespace(moduleNamespace);
  250. localExportSlots[GetLocalExportSlotCount()] = moduleNamespace;
  251. }
  252. ExportedNames* SourceTextModuleRecord::GetExportedNames(ExportModuleRecordList* exportStarSet)
  253. {
  254. if (exportedNames != nullptr)
  255. {
  256. return exportedNames;
  257. }
  258. ArenaAllocator* allocator = scriptContext->GeneralAllocator();
  259. if (exportStarSet == nullptr)
  260. {
  261. exportStarSet = (ExportModuleRecordList*)AllocatorNew(ArenaAllocator, allocator, ExportModuleRecordList, allocator);
  262. }
  263. if (exportStarSet->Has(this))
  264. {
  265. return nullptr;
  266. }
  267. exportStarSet->Prepend(this);
  268. ExportedNames* tempExportedNames = nullptr;
  269. if (this->localExportRecordList != nullptr)
  270. {
  271. tempExportedNames = (ExportedNames*)AllocatorNew(ArenaAllocator, allocator, ExportedNames, allocator);
  272. this->localExportRecordList->Map([=](ModuleImportOrExportEntry exportEntry) {
  273. PropertyId exportNameId = EnsurePropertyIdForIdentifier(exportEntry.exportName);
  274. tempExportedNames->Prepend(exportNameId);
  275. });
  276. }
  277. if (this->indirectExportRecordList != nullptr)
  278. {
  279. if (tempExportedNames == nullptr)
  280. {
  281. tempExportedNames = (ExportedNames*)AllocatorNew(ArenaAllocator, allocator, ExportedNames, allocator);
  282. }
  283. this->indirectExportRecordList->Map([=](ModuleImportOrExportEntry exportEntry) {
  284. PropertyId exportedNameId = EnsurePropertyIdForIdentifier(exportEntry.exportName);
  285. tempExportedNames->Prepend(exportedNameId);
  286. });
  287. }
  288. if (this->starExportRecordList != nullptr)
  289. {
  290. if (tempExportedNames == nullptr)
  291. {
  292. tempExportedNames = (ExportedNames*)AllocatorNew(ArenaAllocator, allocator, ExportedNames, allocator);
  293. }
  294. this->starExportRecordList->Map([=](ModuleImportOrExportEntry exportEntry) {
  295. Assert(exportEntry.moduleRequest != nullptr);
  296. SourceTextModuleRecord* moduleRecord;
  297. if (this->childrenModuleSet->TryGetValue(exportEntry.moduleRequest->Psz(), &moduleRecord))
  298. {
  299. Assert(moduleRecord->WasParsed());
  300. Assert(moduleRecord->WasDeclarationInitialized()); // we should be half way during initialization
  301. Assert(!moduleRecord->WasEvaluated());
  302. ExportedNames* starExportedNames = moduleRecord->GetExportedNames(exportStarSet);
  303. // We are not rejecting ambiguous resolution at this time.
  304. if (starExportedNames != nullptr)
  305. {
  306. starExportedNames->Map([&](PropertyId propertyId) {
  307. if (propertyId != PropertyIds::default_ && !tempExportedNames->Has(propertyId))
  308. {
  309. tempExportedNames->Prepend(propertyId);
  310. }
  311. });
  312. }
  313. }
  314. #if DBG
  315. else
  316. {
  317. AssertMsg(false, "dependent modules should have been initialized");
  318. }
  319. #endif
  320. });
  321. }
  322. exportedNames = tempExportedNames;
  323. return tempExportedNames;
  324. }
  325. bool SourceTextModuleRecord::ResolveImport(PropertyId localName, ModuleNameRecord** importRecord)
  326. {
  327. *importRecord = nullptr;
  328. importRecordList->MapUntil([&](ModuleImportOrExportEntry& importEntry) {
  329. Js::PropertyId localNamePid = EnsurePropertyIdForIdentifier(importEntry.localName);
  330. if (localNamePid == localName)
  331. {
  332. SourceTextModuleRecord* childModule = this->GetChildModuleRecord(importEntry.moduleRequest->Psz());
  333. Js::PropertyId importName = EnsurePropertyIdForIdentifier(importEntry.importName);
  334. if (importName == Js::PropertyIds::star_)
  335. {
  336. *importRecord = childModule->GetNamespaceNameRecord();
  337. }
  338. else
  339. {
  340. childModule->ResolveExport(importName, nullptr, nullptr, importRecord);
  341. }
  342. return true;
  343. }
  344. return false;
  345. });
  346. return *importRecord != nullptr;
  347. }
  348. // return false when "ambiguous".
  349. // otherwise nullptr means "null" where we have circular reference/cannot resolve.
  350. bool SourceTextModuleRecord::ResolveExport(PropertyId exportName, ResolveSet* resolveSet, ExportModuleRecordList* exportStarSet, ModuleNameRecord** exportRecord)
  351. {
  352. ArenaAllocator* allocator = scriptContext->GeneralAllocator();
  353. if (resolvedExportMap == nullptr)
  354. {
  355. resolvedExportMap = AllocatorNew(ArenaAllocator, allocator, ResolvedExportMap, allocator);
  356. }
  357. if (resolvedExportMap->TryGetReference(exportName, exportRecord))
  358. {
  359. return true;
  360. }
  361. // TODO: use per-call/loop allocator?
  362. if (exportStarSet == nullptr)
  363. {
  364. exportStarSet = (ExportModuleRecordList*)AllocatorNew(ArenaAllocator, allocator, ExportModuleRecordList, allocator);
  365. }
  366. if (resolveSet == nullptr)
  367. {
  368. resolveSet = (ResolveSet*)AllocatorNew(ArenaAllocator, allocator, ResolveSet, allocator);
  369. }
  370. *exportRecord = nullptr;
  371. bool hasCircularRef = false;
  372. resolveSet->MapUntil([&](ModuleNameRecord moduleNameRecord) {
  373. if (moduleNameRecord.module == this && moduleNameRecord.bindingName == exportName)
  374. {
  375. *exportRecord = nullptr;
  376. hasCircularRef = true;
  377. return true;
  378. }
  379. return false;
  380. });
  381. if (hasCircularRef)
  382. {
  383. Assert(*exportRecord == nullptr);
  384. return true;
  385. }
  386. resolveSet->Prepend({ this, exportName });
  387. if (localExportRecordList != nullptr)
  388. {
  389. PropertyId localNameId = Js::Constants::NoProperty;
  390. localExportRecordList->MapUntil([&](ModuleImportOrExportEntry exportEntry) {
  391. PropertyId exportNameId = EnsurePropertyIdForIdentifier(exportEntry.exportName);
  392. if (exportNameId == exportName)
  393. {
  394. localNameId = EnsurePropertyIdForIdentifier(exportEntry.localName);
  395. return true;
  396. }
  397. return false;
  398. });
  399. if (localNameId != Js::Constants::NoProperty)
  400. {
  401. // Check to see if we are exporting something we imported from another module without using a re-export.
  402. // ex: import { foo } from 'module'; export { foo };
  403. ModuleRecordBase* sourceModule = this;
  404. ModuleNameRecord* importRecord = nullptr;
  405. if (this->importRecordList != nullptr
  406. && this->ResolveImport(localNameId, &importRecord)
  407. && importRecord != nullptr)
  408. {
  409. sourceModule = importRecord->module;
  410. localNameId = importRecord->bindingName;
  411. }
  412. resolvedExportMap->AddNew(exportName, { sourceModule, localNameId });
  413. // return the address from Map buffer.
  414. resolvedExportMap->TryGetReference(exportName, exportRecord);
  415. return true;
  416. }
  417. }
  418. if (indirectExportRecordList != nullptr)
  419. {
  420. bool isAmbiguous = false;
  421. indirectExportRecordList->MapUntil([&](ModuleImportOrExportEntry exportEntry) {
  422. PropertyId reexportNameId = EnsurePropertyIdForIdentifier(exportEntry.exportName);
  423. if (exportName != reexportNameId)
  424. {
  425. return false;
  426. }
  427. PropertyId importNameId = EnsurePropertyIdForIdentifier(exportEntry.importName);
  428. SourceTextModuleRecord* childModuleRecord = GetChildModuleRecord(exportEntry.moduleRequest->Psz());
  429. if (childModuleRecord == nullptr)
  430. {
  431. JavascriptError::ThrowReferenceError(scriptContext, JSERR_CannotResolveModule, exportEntry.moduleRequest->Psz());
  432. }
  433. else
  434. {
  435. isAmbiguous = !childModuleRecord->ResolveExport(importNameId, resolveSet, exportStarSet, exportRecord);
  436. if (isAmbiguous)
  437. {
  438. // ambiguous; don't need to search further
  439. return true;
  440. }
  441. else
  442. {
  443. // found a resolution. done;
  444. if (*exportRecord != nullptr)
  445. {
  446. return true;
  447. }
  448. }
  449. }
  450. return false;
  451. });
  452. if (isAmbiguous)
  453. {
  454. return false;
  455. }
  456. if (*exportRecord != nullptr)
  457. {
  458. return true;
  459. }
  460. }
  461. if (exportName == PropertyIds::default_)
  462. {
  463. JavascriptError* errorObj = scriptContext->GetLibrary()->CreateSyntaxError();
  464. JavascriptError::SetErrorMessage(errorObj, JSERR_ModuleResolveExport, scriptContext->GetPropertyName(exportName)->GetBuffer(), scriptContext);
  465. this->errorObject = errorObj;
  466. return false;
  467. }
  468. if (exportStarSet->Has(this))
  469. {
  470. *exportRecord = nullptr;
  471. return true;
  472. }
  473. exportStarSet->Prepend(this);
  474. bool ambiguousResolution = false;
  475. if (this->starExportRecordList != nullptr)
  476. {
  477. ModuleNameRecord* starResolution = nullptr;
  478. starExportRecordList->MapUntil([&](ModuleImportOrExportEntry starExportEntry) {
  479. ModuleNameRecord* currentResolution = nullptr;
  480. SourceTextModuleRecord* childModule = GetChildModuleRecord(starExportEntry.moduleRequest->Psz());
  481. if (childModule == nullptr)
  482. {
  483. JavascriptError::ThrowReferenceError(GetScriptContext(), JSERR_CannotResolveModule, starExportEntry.moduleRequest->Psz());
  484. }
  485. if (childModule->errorObject != nullptr)
  486. {
  487. JavascriptExceptionOperators::Throw(childModule->errorObject, GetScriptContext());
  488. }
  489. // if ambigious, return "ambigious"
  490. if (!childModule->ResolveExport(exportName, resolveSet, exportStarSet, &currentResolution))
  491. {
  492. ambiguousResolution = true;
  493. return true;
  494. }
  495. if (currentResolution != nullptr)
  496. {
  497. if (starResolution == nullptr)
  498. {
  499. starResolution = currentResolution;
  500. }
  501. else
  502. {
  503. if (currentResolution->bindingName != starResolution->bindingName ||
  504. currentResolution->module != starResolution->module)
  505. {
  506. ambiguousResolution = true;
  507. }
  508. return true;
  509. }
  510. }
  511. return false;
  512. });
  513. if (!ambiguousResolution)
  514. {
  515. *exportRecord = starResolution;
  516. }
  517. }
  518. return !ambiguousResolution;
  519. }
  520. HRESULT SourceTextModuleRecord::ResolveExternalModuleDependencies()
  521. {
  522. ScriptContext* scriptContext = GetScriptContext();
  523. Recycler* recycler = scriptContext->GetRecycler();
  524. HRESULT hr = NOERROR;
  525. if (requestedModuleList != nullptr)
  526. {
  527. if (nullptr == childrenModuleSet)
  528. {
  529. ArenaAllocator* allocator = scriptContext->GeneralAllocator();
  530. childrenModuleSet = (ChildModuleRecordSet*)AllocatorNew(ArenaAllocator, allocator, ChildModuleRecordSet, allocator);
  531. }
  532. requestedModuleList->MapUntil([&](IdentPtr specifier) {
  533. ModuleRecordBase* moduleRecordBase = nullptr;
  534. SourceTextModuleRecord* moduleRecord = nullptr;
  535. LPCOLESTR moduleName = specifier->Psz();
  536. bool itemFound = childrenModuleSet->TryGetValue(moduleName, &moduleRecord);
  537. if (!itemFound)
  538. {
  539. hr = scriptContext->GetHostScriptContext()->FetchImportedModule(this, moduleName, &moduleRecordBase);
  540. if (FAILED(hr))
  541. {
  542. return true;
  543. }
  544. moduleRecord = SourceTextModuleRecord::FromHost(moduleRecordBase);
  545. childrenModuleSet->AddNew(moduleName, moduleRecord);
  546. if (moduleRecord->parentModuleList == nullptr)
  547. {
  548. moduleRecord->parentModuleList = RecyclerNew(recycler, ModuleRecordList, recycler);
  549. }
  550. moduleRecord->parentModuleList->Add(this);
  551. if (!moduleRecord->WasDeclarationInitialized())
  552. {
  553. numUnInitializedChildrenModule++;
  554. }
  555. }
  556. return false;
  557. });
  558. if (FAILED(hr))
  559. {
  560. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  561. JavascriptError::SetErrorMessageProperties(error, hr, _u("fetch import module failed"), scriptContext);
  562. this->errorObject = error;
  563. NotifyParentsAsNeeded();
  564. }
  565. }
  566. return hr;
  567. }
  568. void SourceTextModuleRecord::CleanupBeforeExecution()
  569. {
  570. // zero out fields is more a defense in depth as those fields are not needed anymore
  571. Assert(wasParsed);
  572. Assert(wasEvaluated);
  573. Assert(wasDeclarationInitialized);
  574. // Debugger can reparse the source and generate the byte code again. Don't cleanup the
  575. // helper information for now.
  576. }
  577. void SourceTextModuleRecord::ModuleDeclarationInstantiation()
  578. {
  579. ScriptContext* scriptContext = GetScriptContext();
  580. if (this->WasDeclarationInitialized())
  581. {
  582. return;
  583. }
  584. try
  585. {
  586. AUTO_NESTED_HANDLED_EXCEPTION_TYPE((ExceptionType)(ExceptionType_OutOfMemory|ExceptionType_JavascriptException));
  587. InitializeLocalExports();
  588. InitializeLocalImports();
  589. InitializeIndirectExports();
  590. }
  591. catch (const JavascriptException& err)
  592. {
  593. this->errorObject = err.GetAndClear()->GetThrownObject(scriptContext);
  594. }
  595. if (this->errorObject != nullptr)
  596. {
  597. NotifyParentsAsNeeded();
  598. return;
  599. }
  600. SetWasDeclarationInitialized();
  601. if (childrenModuleSet != nullptr)
  602. {
  603. childrenModuleSet->Map([](LPCOLESTR specifier, SourceTextModuleRecord* moduleRecord)
  604. {
  605. Assert(moduleRecord->WasParsed());
  606. moduleRecord->ModuleDeclarationInstantiation();
  607. });
  608. }
  609. ModuleNamespace::GetModuleNamespace(this);
  610. Js::AutoDynamicCodeReference dynamicFunctionReference(scriptContext);
  611. Assert(this == scriptContext->GetLibrary()->GetModuleRecord(this->pSourceInfo->GetSrcInfo()->moduleID));
  612. CompileScriptException se;
  613. this->rootFunction = scriptContext->GenerateRootFunction(parseTree, sourceIndex, this->parser, this->pSourceInfo->GetParseFlags(), &se, _u("module"));
  614. if (rootFunction == nullptr)
  615. {
  616. this->errorObject = JavascriptError::CreateFromCompileScriptException(scriptContext, &se);
  617. NotifyParentsAsNeeded();
  618. }
  619. else
  620. {
  621. scriptContext->GetDebugContext()->RegisterFunction(this->rootFunction->GetFunctionBody(), nullptr);
  622. }
  623. }
  624. Var SourceTextModuleRecord::ModuleEvaluation()
  625. {
  626. #if DBG
  627. if (childrenModuleSet != nullptr)
  628. {
  629. childrenModuleSet->EachValue([=](SourceTextModuleRecord* childModuleRecord)
  630. {
  631. AssertMsg(childModuleRecord->WasParsed(), "child module needs to have been parsed");
  632. AssertMsg(childModuleRecord->WasDeclarationInitialized(), "child module needs to have been initialized.");
  633. });
  634. }
  635. #endif
  636. if (!scriptContext->GetConfig()->IsES6ModuleEnabled())
  637. {
  638. return nullptr;
  639. }
  640. Assert(this->errorObject == nullptr);
  641. if (this->errorObject != nullptr)
  642. {
  643. JavascriptExceptionOperators::Throw(errorObject, scriptContext);
  644. }
  645. Assert(!WasEvaluated());
  646. SetWasEvaluated();
  647. // we shouldn't evaluate if there are existing failure. This is defense in depth as the host shouldn't
  648. // call into evaluation if there was previous fialure on the module.
  649. if (this->errorObject)
  650. {
  651. return this->errorObject;
  652. }
  653. if (childrenModuleSet != nullptr)
  654. {
  655. childrenModuleSet->EachValue([=](SourceTextModuleRecord* childModuleRecord)
  656. {
  657. if (!childModuleRecord->WasEvaluated())
  658. {
  659. childModuleRecord->ModuleEvaluation();
  660. }
  661. });
  662. }
  663. CleanupBeforeExecution();
  664. Arguments outArgs(CallInfo(CallFlags_Value, 0), nullptr);
  665. return rootFunction->CallRootFunction(outArgs, scriptContext, true);
  666. }
  667. HRESULT SourceTextModuleRecord::OnHostException(void* errorVar)
  668. {
  669. if (!RecyclableObject::Is(errorVar))
  670. {
  671. return E_INVALIDARG;
  672. }
  673. AssertMsg(!WasParsed(), "shouldn't be called after a module is parsed");
  674. if (WasParsed())
  675. {
  676. return E_INVALIDARG;
  677. }
  678. this->errorObject = errorVar;
  679. // a sub module failed to download. we need to notify that the parent that sub module failed and we need to report back.
  680. return NOERROR;
  681. }
  682. SourceTextModuleRecord* SourceTextModuleRecord::GetChildModuleRecord(LPCOLESTR specifier) const
  683. {
  684. SourceTextModuleRecord* childModuleRecord = nullptr;
  685. if (childrenModuleSet == nullptr)
  686. {
  687. AssertMsg(false, "We should have some child modulerecords first before trying to get child modulerecord.");
  688. return nullptr;
  689. }
  690. if (!childrenModuleSet->TryGetValue(specifier, &childModuleRecord))
  691. {
  692. return nullptr;
  693. }
  694. return childModuleRecord;
  695. }
  696. void SourceTextModuleRecord::InitializeLocalImports()
  697. {
  698. if (importRecordList != nullptr)
  699. {
  700. importRecordList->Map([&](ModuleImportOrExportEntry& importEntry) {
  701. Js::PropertyId importName = EnsurePropertyIdForIdentifier(importEntry.importName);
  702. SourceTextModuleRecord* childModule = this->GetChildModuleRecord(importEntry.moduleRequest->Psz());
  703. ModuleNameRecord* importRecord = nullptr;
  704. // We don't need to initialize anything for * import.
  705. if (importName != Js::PropertyIds::star_)
  706. {
  707. if (!childModule->ResolveExport(importName, nullptr, nullptr, &importRecord)
  708. || importRecord == nullptr)
  709. {
  710. JavascriptError* errorObj = scriptContext->GetLibrary()->CreateSyntaxError();
  711. JavascriptError::SetErrorMessage(errorObj, JSERR_ModuleResolveImport, importEntry.importName->Psz(), scriptContext);
  712. this->errorObject = errorObj;
  713. return;
  714. }
  715. }
  716. });
  717. }
  718. }
  719. // Local exports are stored in the slotarray in the SourceTextModuleRecord.
  720. void SourceTextModuleRecord::InitializeLocalExports()
  721. {
  722. Recycler* recycler = scriptContext->GetRecycler();
  723. Var undefineValue = scriptContext->GetLibrary()->GetUndefined();
  724. if (localSlotCount == InvalidSlotCount)
  725. {
  726. uint currentSlotCount = 0;
  727. if (localExportRecordList != nullptr)
  728. {
  729. ArenaAllocator* allocator = scriptContext->GeneralAllocator();
  730. localExportMapByExportName = AllocatorNew(ArenaAllocator, allocator, LocalExportMap, allocator);
  731. localExportMapByLocalName = AllocatorNew(ArenaAllocator, allocator, LocalExportMap, allocator);
  732. localExportIndexList = AllocatorNew(ArenaAllocator, allocator, LocalExportIndexList, allocator);
  733. localExportRecordList->Map([&](ModuleImportOrExportEntry exportEntry)
  734. {
  735. Assert(exportEntry.moduleRequest == nullptr);
  736. Assert(exportEntry.importName == nullptr);
  737. PropertyId exportNameId = EnsurePropertyIdForIdentifier(exportEntry.exportName);
  738. PropertyId localNameId = EnsurePropertyIdForIdentifier(exportEntry.localName);
  739. // We could have exports that look local but actually exported from other module
  740. // import {foo} from "module1.js"; export {foo};
  741. ModuleNameRecord* importRecord = nullptr;
  742. if (this->GetImportEntryList() != nullptr
  743. && this->ResolveImport(localNameId, &importRecord)
  744. && importRecord != nullptr)
  745. {
  746. return;
  747. }
  748. // 2G is too big already.
  749. if (localExportCount >= INT_MAX)
  750. {
  751. JavascriptError::ThrowRangeError(scriptContext, JSERR_TooManyImportExports);
  752. }
  753. localExportCount++;
  754. uint exportSlot = UINT_MAX;
  755. for (uint i = 0; i < (uint)localExportIndexList->Count(); i++)
  756. {
  757. if (localExportIndexList->Item(i) == localNameId)
  758. {
  759. exportSlot = i;
  760. break;
  761. }
  762. }
  763. if (exportSlot == UINT_MAX)
  764. {
  765. exportSlot = currentSlotCount;
  766. localExportMapByLocalName->Add(localNameId, exportSlot);
  767. localExportIndexList->Add(localNameId);
  768. Assert(localExportIndexList->Item(currentSlotCount) == localNameId);
  769. currentSlotCount++;
  770. if (currentSlotCount >= INT_MAX)
  771. {
  772. JavascriptError::ThrowRangeError(scriptContext, JSERR_TooManyImportExports);
  773. }
  774. }
  775. localExportMapByExportName->Add(exportNameId, exportSlot);
  776. });
  777. }
  778. // Namespace object will be added to the end of the array though invisible through namespace object itself.
  779. localExportSlots = RecyclerNewArray(recycler, Var, currentSlotCount + 1);
  780. for (uint i = 0; i < currentSlotCount; i++)
  781. {
  782. localExportSlots[i] = undefineValue;
  783. }
  784. localExportSlots[currentSlotCount] = nullptr;
  785. localSlotCount = currentSlotCount;
  786. #if ENABLE_NATIVE_CODEGEN
  787. if (JITManager::GetJITManager()->IsOOPJITEnabled())
  788. {
  789. HRESULT hr = JITManager::GetJITManager()->AddModuleRecordInfo(
  790. scriptContext->GetRemoteScriptAddr(),
  791. this->GetModuleId(),
  792. (intptr_t)this->GetLocalExportSlots());
  793. JITManager::HandleServerCallResult(hr);
  794. }
  795. #endif
  796. }
  797. }
  798. PropertyId SourceTextModuleRecord::EnsurePropertyIdForIdentifier(IdentPtr pid)
  799. {
  800. PropertyId propertyId = pid->GetPropertyId();
  801. if (propertyId == Js::Constants::NoProperty)
  802. {
  803. propertyId = scriptContext->GetOrAddPropertyIdTracked(pid->Psz(), pid->Cch());
  804. pid->SetPropertyId(propertyId);
  805. }
  806. return propertyId;
  807. }
  808. void SourceTextModuleRecord::InitializeIndirectExports()
  809. {
  810. ModuleNameRecord* exportRecord = nullptr;
  811. if (indirectExportRecordList != nullptr)
  812. {
  813. indirectExportRecordList->Map([&](ModuleImportOrExportEntry exportEntry)
  814. {
  815. PropertyId propertyId = EnsurePropertyIdForIdentifier(exportEntry.importName);
  816. SourceTextModuleRecord* childModuleRecord = GetChildModuleRecord(exportEntry.moduleRequest->Psz());
  817. if (childModuleRecord == nullptr)
  818. {
  819. JavascriptError* errorObj = scriptContext->GetLibrary()->CreateReferenceError();
  820. JavascriptError::SetErrorMessage(errorObj, JSERR_CannotResolveModule, exportEntry.moduleRequest->Psz(), scriptContext);
  821. this->errorObject = errorObj;
  822. return;
  823. }
  824. if (!childModuleRecord->ResolveExport(propertyId, nullptr, nullptr, &exportRecord) ||
  825. (exportRecord == nullptr))
  826. {
  827. JavascriptError* errorObj = scriptContext->GetLibrary()->CreateSyntaxError();
  828. JavascriptError::SetErrorMessage(errorObj, JSERR_ModuleResolveExport, exportEntry.exportName->Psz(), scriptContext);
  829. this->errorObject = errorObj;
  830. return;
  831. }
  832. });
  833. }
  834. }
  835. uint SourceTextModuleRecord::GetLocalExportSlotIndexByExportName(PropertyId exportNameId)
  836. {
  837. Assert(localSlotCount != 0);
  838. Assert(localExportSlots != nullptr);
  839. uint slotIndex = InvalidSlotIndex;
  840. if (!localExportMapByExportName->TryGetValue(exportNameId, &slotIndex))
  841. {
  842. AssertMsg(false, "exportNameId is not in local export list");
  843. return InvalidSlotIndex;
  844. }
  845. else
  846. {
  847. return slotIndex;
  848. }
  849. }
  850. uint SourceTextModuleRecord::GetLocalExportSlotIndexByLocalName(PropertyId localNameId)
  851. {
  852. Assert(localSlotCount != 0 || localNameId == PropertyIds::star_);
  853. Assert(localExportSlots != nullptr);
  854. uint slotIndex = InvalidSlotIndex;
  855. if (localNameId == PropertyIds::star_)
  856. {
  857. return localSlotCount; // namespace is put on the last slot.
  858. } else if (!localExportMapByLocalName->TryGetValue(localNameId, &slotIndex))
  859. {
  860. AssertMsg(false, "exportNameId is not in local export list");
  861. return InvalidSlotIndex;
  862. }
  863. return slotIndex;
  864. }
  865. #if DBG
  866. void SourceTextModuleRecord::AddParent(SourceTextModuleRecord* parentRecord, LPCWSTR specifier, uint32 specifierLength)
  867. {
  868. }
  869. #endif
  870. }