Module.cpp 4.1 KB

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