Module.cpp 6.9 KB

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