Module.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/CyclicModule.h>
  8. #include <LibJS/Module.h>
  9. #include <LibJS/Runtime/ModuleNamespaceObject.h>
  10. #include <LibJS/Runtime/Promise.h>
  11. #include <LibJS/Runtime/VM.h>
  12. namespace JS {
  13. Module::Module(Realm& realm, DeprecatedString filename, Script::HostDefined* host_defined)
  14. : m_realm(realm)
  15. , m_host_defined(host_defined)
  16. , m_filename(move(filename))
  17. {
  18. }
  19. Module::~Module() = default;
  20. void Module::visit_edges(Cell::Visitor& visitor)
  21. {
  22. Base::visit_edges(visitor);
  23. visitor.visit(m_realm);
  24. visitor.visit(m_environment);
  25. visitor.visit(m_namespace);
  26. if (m_host_defined)
  27. m_host_defined->visit_host_defined_self(visitor);
  28. }
  29. // 16.2.1.5.1.1 InnerModuleLinking ( module, stack, index ), https://tc39.es/ecma262/#sec-InnerModuleLinking
  30. ThrowCompletionOr<u32> Module::inner_module_linking(VM& vm, Vector<Module*>&, u32 index)
  31. {
  32. // 1. If module is not a Cyclic Module Record, then
  33. // a. Perform ? module.Link().
  34. TRY(link(vm));
  35. // b. Return index.
  36. return index;
  37. }
  38. // 16.2.1.5.2.1 InnerModuleEvaluation ( module, stack, index ), https://tc39.es/ecma262/#sec-innermoduleevaluation
  39. ThrowCompletionOr<u32> Module::inner_module_evaluation(VM& vm, Vector<Module*>&, u32 index)
  40. {
  41. // 1. If module is not a Cyclic Module Record, then
  42. // a. Let promise be ! module.Evaluate().
  43. auto promise = TRY(evaluate(vm));
  44. // b. Assert: promise.[[PromiseState]] is not pending.
  45. VERIFY(promise->state() != Promise::State::Pending);
  46. // c. If promise.[[PromiseState]] is rejected, then
  47. if (promise->state() == Promise::State::Rejected) {
  48. // i. Return ThrowCompletion(promise.[[PromiseResult]]).
  49. return throw_completion(promise->result());
  50. }
  51. // d. Return index.
  52. return index;
  53. }
  54. // 16.2.1.10 GetModuleNamespace ( module ), https://tc39.es/ecma262/#sec-getmodulenamespace
  55. ThrowCompletionOr<Object*> Module::get_module_namespace(VM& vm)
  56. {
  57. // 1. Assert: If module is a Cyclic Module Record, then module.[[Status]] is not unlinked.
  58. // FIXME: How do we check this without breaking encapsulation?
  59. // 2. Let namespace be module.[[Namespace]].
  60. auto* namespace_ = m_namespace.ptr();
  61. // 3. If namespace is empty, then
  62. if (!namespace_) {
  63. // a. Let exportedNames be ? module.GetExportedNames().
  64. auto exported_names = TRY(get_exported_names(vm));
  65. // b. Let unambiguousNames be a new empty List.
  66. Vector<FlyString> unambiguous_names;
  67. // c. For each element name of exportedNames, do
  68. for (auto& name : exported_names) {
  69. // i. Let resolution be ? module.ResolveExport(name).
  70. auto resolution = TRY(resolve_export(vm, name));
  71. // ii. If resolution is a ResolvedBinding Record, append name to unambiguousNames.
  72. if (resolution.is_valid())
  73. unambiguous_names.append(name);
  74. }
  75. // d. Set namespace to ModuleNamespaceCreate(module, unambiguousNames).
  76. namespace_ = module_namespace_create(vm, unambiguous_names);
  77. VERIFY(m_namespace);
  78. // Note: This set the local variable 'namespace' and not the member variable which is done by ModuleNamespaceCreate
  79. }
  80. // 4. Return namespace.
  81. return namespace_;
  82. }
  83. // 10.4.6.12 ModuleNamespaceCreate ( module, exports ), https://tc39.es/ecma262/#sec-modulenamespacecreate
  84. Object* Module::module_namespace_create(VM& vm, Vector<FlyString> unambiguous_names)
  85. {
  86. auto& realm = this->realm();
  87. // 1. Assert: module.[[Namespace]] is empty.
  88. VERIFY(!m_namespace);
  89. // 2. Let internalSlotsList be the internal slots listed in Table 34.
  90. // 3. Let M be MakeBasicObject(internalSlotsList).
  91. // 4. Set M's essential internal methods to the definitions specified in 10.4.6.
  92. // 5. Set M.[[Module]] to module.
  93. // 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.
  94. // 7. Set M.[[Exports]] to sortedExports.
  95. // 8. Create own properties of M corresponding to the definitions in 28.3.
  96. Object* module_namespace = vm.heap().allocate<ModuleNamespaceObject>(realm, realm, this, move(unambiguous_names));
  97. // 9. Set module.[[Namespace]] to M.
  98. m_namespace = make_handle(module_namespace);
  99. // 10. Return M.
  100. return module_namespace;
  101. }
  102. }