Module.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  4. * Copyright (c) 2023, networkException <networkexception@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/CyclicModule.h>
  9. #include <LibJS/Module.h>
  10. #include <LibJS/Runtime/ModuleNamespaceObject.h>
  11. #include <LibJS/Runtime/ModuleRequest.h>
  12. #include <LibJS/Runtime/Promise.h>
  13. #include <LibJS/Runtime/VM.h>
  14. namespace JS {
  15. GC_DEFINE_ALLOCATOR(Module);
  16. GC_DEFINE_ALLOCATOR(GraphLoadingState);
  17. Module::Module(Realm& realm, ByteString filename, Script::HostDefined* host_defined)
  18. : m_realm(realm)
  19. , m_host_defined(host_defined)
  20. , m_filename(move(filename))
  21. {
  22. }
  23. Module::~Module() = default;
  24. void Module::visit_edges(Cell::Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. visitor.visit(m_realm);
  28. visitor.visit(m_environment);
  29. visitor.visit(m_namespace);
  30. if (m_host_defined)
  31. m_host_defined->visit_host_defined_self(visitor);
  32. }
  33. // 16.2.1.5.1.1 InnerModuleLinking ( module, stack, index ), https://tc39.es/ecma262/#sec-InnerModuleLinking
  34. ThrowCompletionOr<u32> Module::inner_module_linking(VM& vm, Vector<Module*>&, u32 index)
  35. {
  36. // 1. If module is not a Cyclic Module Record, then
  37. // a. Perform ? module.Link().
  38. TRY(link(vm));
  39. // b. Return index.
  40. return index;
  41. }
  42. // 16.2.1.5.2.1 InnerModuleEvaluation ( module, stack, index ), https://tc39.es/ecma262/#sec-innermoduleevaluation
  43. ThrowCompletionOr<u32> Module::inner_module_evaluation(VM& vm, Vector<Module*>&, u32 index)
  44. {
  45. // 1. If module is not a Cyclic Module Record, then
  46. // a. Let promise be ! module.Evaluate().
  47. auto promise = TRY(evaluate(vm));
  48. // b. Assert: promise.[[PromiseState]] is not pending.
  49. VERIFY(promise->state() != Promise::State::Pending);
  50. // c. If promise.[[PromiseState]] is rejected, then
  51. if (promise->state() == Promise::State::Rejected) {
  52. // i. Return ThrowCompletion(promise.[[PromiseResult]]).
  53. return throw_completion(promise->result());
  54. }
  55. // d. Return index.
  56. return index;
  57. }
  58. // 16.2.1.9 FinishLoadingImportedModule ( referrer, specifier, payload, result ), https://tc39.es/ecma262/#sec-FinishLoadingImportedModule
  59. void finish_loading_imported_module(ImportedModuleReferrer referrer, ModuleRequest const& module_request, ImportedModulePayload payload, ThrowCompletionOr<GC::Ref<Module>> const& result)
  60. {
  61. // 1. If result is a normal completion, then
  62. if (!result.is_error()) {
  63. // NOTE: Only Script and CyclicModule referrers have the [[LoadedModules]] internal slot.
  64. if (referrer.has<GC::Ref<Script>>() || referrer.has<GC::Ref<CyclicModule>>()) {
  65. auto& loaded_modules = referrer.visit(
  66. [](GC::Ref<JS::Realm>&) -> Vector<ModuleWithSpecifier>& {
  67. VERIFY_NOT_REACHED();
  68. __builtin_unreachable();
  69. },
  70. [](auto& script_or_module) -> Vector<ModuleWithSpecifier>& {
  71. return script_or_module->loaded_modules();
  72. });
  73. bool found_record = false;
  74. // a. If referrer.[[LoadedModules]] contains a Record whose [[Specifier]] is specifier, then
  75. for (auto const& record : loaded_modules) {
  76. if (record.specifier == module_request.module_specifier) {
  77. // i. Assert: That Record's [[Module]] is result.[[Value]].
  78. VERIFY(record.module == result.value());
  79. found_record = true;
  80. }
  81. }
  82. // b. Else,
  83. if (!found_record) {
  84. auto module = result.value();
  85. // i. Append the Record { [[Specifier]]: specifier, [[Module]]: result.[[Value]] } to referrer.[[LoadedModules]].
  86. loaded_modules.append(ModuleWithSpecifier {
  87. .specifier = module_request.module_specifier,
  88. .module = GC::Ref<Module>(*module) });
  89. }
  90. }
  91. }
  92. if (payload.has<GC::Ref<GraphLoadingState>>()) {
  93. // a. Perform ContinueModuleLoading(payload, result)
  94. continue_module_loading(payload.get<GC::Ref<GraphLoadingState>>(), result);
  95. }
  96. // Else,
  97. else {
  98. // a. Perform ContinueDynamicImport(payload, result).
  99. continue_dynamic_import(payload.get<GC::Ref<PromiseCapability>>(), result);
  100. }
  101. // 4. Return unused.
  102. }
  103. // 16.2.1.10 GetModuleNamespace ( module ), https://tc39.es/ecma262/#sec-getmodulenamespace
  104. ThrowCompletionOr<Object*> Module::get_module_namespace(VM& vm)
  105. {
  106. // 1. Assert: If module is a Cyclic Module Record, then module.[[Status]] is not unlinked.
  107. // FIXME: How do we check this without breaking encapsulation?
  108. // 2. Let namespace be module.[[Namespace]].
  109. auto* namespace_ = m_namespace.ptr();
  110. // 3. If namespace is empty, then
  111. if (!namespace_) {
  112. // a. Let exportedNames be ? module.GetExportedNames().
  113. auto exported_names = TRY(get_exported_names(vm));
  114. // b. Let unambiguousNames be a new empty List.
  115. Vector<DeprecatedFlyString> unambiguous_names;
  116. // c. For each element name of exportedNames, do
  117. for (auto& name : exported_names) {
  118. // i. Let resolution be ? module.ResolveExport(name).
  119. auto resolution = TRY(resolve_export(vm, name));
  120. // ii. If resolution is a ResolvedBinding Record, append name to unambiguousNames.
  121. if (resolution.is_valid())
  122. unambiguous_names.append(name);
  123. }
  124. // d. Set namespace to ModuleNamespaceCreate(module, unambiguousNames).
  125. namespace_ = module_namespace_create(unambiguous_names);
  126. VERIFY(m_namespace);
  127. // Note: This set the local variable 'namespace' and not the member variable which is done by ModuleNamespaceCreate
  128. }
  129. // 4. Return namespace.
  130. return namespace_;
  131. }
  132. // 10.4.6.12 ModuleNamespaceCreate ( module, exports ), https://tc39.es/ecma262/#sec-modulenamespacecreate
  133. Object* Module::module_namespace_create(Vector<DeprecatedFlyString> unambiguous_names)
  134. {
  135. auto& realm = this->realm();
  136. // 1. Assert: module.[[Namespace]] is empty.
  137. VERIFY(!m_namespace);
  138. // 2. Let internalSlotsList be the internal slots listed in Table 34.
  139. // 3. Let M be MakeBasicObject(internalSlotsList).
  140. // 4. Set M's essential internal methods to the definitions specified in 10.4.6.
  141. // 5. Set M.[[Module]] to module.
  142. // 6. Let sortedExports be a List whose elements are the elements of exports ordered as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn.
  143. // 7. Set M.[[Exports]] to sortedExports.
  144. // 8. Create own properties of M corresponding to the definitions in 28.3.
  145. auto module_namespace = realm.create<ModuleNamespaceObject>(realm, this, move(unambiguous_names));
  146. // 9. Set module.[[Namespace]] to M.
  147. m_namespace = make_root(module_namespace);
  148. // 10. Return M.
  149. return module_namespace;
  150. }
  151. }