Module.cpp 4.4 KB

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