Module.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/DeprecatedFlyString.h>
  9. #include <LibGC/Ptr.h>
  10. #include <LibJS/ModuleLoading.h>
  11. #include <LibJS/Runtime/Environment.h>
  12. #include <LibJS/Runtime/Realm.h>
  13. #include <LibJS/Script.h>
  14. namespace JS {
  15. struct ResolvedBinding {
  16. enum Type {
  17. BindingName,
  18. Namespace,
  19. Ambiguous,
  20. Null,
  21. };
  22. static ResolvedBinding null()
  23. {
  24. return {};
  25. }
  26. static ResolvedBinding ambiguous()
  27. {
  28. ResolvedBinding binding;
  29. binding.type = Ambiguous;
  30. return binding;
  31. }
  32. Type type { Null };
  33. GC::Ptr<Module> module;
  34. DeprecatedFlyString export_name;
  35. bool is_valid() const
  36. {
  37. return type == BindingName || type == Namespace;
  38. }
  39. bool is_namespace() const
  40. {
  41. return type == Namespace;
  42. }
  43. bool is_ambiguous() const
  44. {
  45. return type == Ambiguous;
  46. }
  47. };
  48. // https://tc39.es/ecma262/#graphloadingstate-record
  49. struct GraphLoadingState : public Cell {
  50. GC_CELL(GraphLoadingState, Cell);
  51. GC_DECLARE_ALLOCATOR(GraphLoadingState);
  52. public:
  53. struct HostDefined : Cell {
  54. GC_CELL(HostDefined, Cell);
  55. public:
  56. virtual ~HostDefined() = default;
  57. };
  58. GC::Ptr<PromiseCapability> promise_capability; // [[PromiseCapability]]
  59. bool is_loading { false }; // [[IsLoading]]
  60. size_t pending_module_count { 0 }; // [[PendingModulesCount]]
  61. HashTable<GC::Ptr<CyclicModule>> visited; // [[Visited]]
  62. GC::Ptr<HostDefined> host_defined; // [[HostDefined]]
  63. private:
  64. GraphLoadingState(GC::Ptr<PromiseCapability> promise_capability, bool is_loading, size_t pending_module_count, HashTable<GC::Ptr<CyclicModule>> visited, GC::Ptr<HostDefined> host_defined)
  65. : promise_capability(move(promise_capability))
  66. , is_loading(is_loading)
  67. , pending_module_count(pending_module_count)
  68. , visited(move(visited))
  69. , host_defined(move(host_defined))
  70. {
  71. }
  72. virtual void visit_edges(Cell::Visitor&) override;
  73. };
  74. // 16.2.1.4 Abstract Module Records, https://tc39.es/ecma262/#sec-abstract-module-records
  75. class Module : public Cell {
  76. GC_CELL(Module, Cell);
  77. GC_DECLARE_ALLOCATOR(Module);
  78. public:
  79. virtual ~Module() override;
  80. Realm& realm() { return *m_realm; }
  81. Realm const& realm() const { return *m_realm; }
  82. StringView filename() const { return m_filename; }
  83. Environment* environment() { return m_environment; }
  84. Script::HostDefined* host_defined() const { return m_host_defined; }
  85. ThrowCompletionOr<Object*> get_module_namespace(VM& vm);
  86. virtual ThrowCompletionOr<void> link(VM& vm) = 0;
  87. virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) = 0;
  88. virtual ThrowCompletionOr<Vector<DeprecatedFlyString>> get_exported_names(VM& vm, Vector<Module*> export_star_set = {}) = 0;
  89. virtual ThrowCompletionOr<ResolvedBinding> resolve_export(VM& vm, DeprecatedFlyString const& export_name, Vector<ResolvedBinding> resolve_set = {}) = 0;
  90. virtual ThrowCompletionOr<u32> inner_module_linking(VM& vm, Vector<Module*>& stack, u32 index);
  91. virtual ThrowCompletionOr<u32> inner_module_evaluation(VM& vm, Vector<Module*>& stack, u32 index);
  92. virtual PromiseCapability& load_requested_modules(GC::Ptr<GraphLoadingState::HostDefined>) = 0;
  93. protected:
  94. Module(Realm&, ByteString filename, Script::HostDefined* host_defined = nullptr);
  95. virtual void visit_edges(Cell::Visitor&) override;
  96. void set_environment(Environment* environment)
  97. {
  98. m_environment = environment;
  99. }
  100. private:
  101. Object* module_namespace_create(Vector<DeprecatedFlyString> unambiguous_names);
  102. // These handles are only safe as long as the VM they live in is valid.
  103. // But evaluated modules SHOULD be stored in the VM so unless you intentionally
  104. // destroy the VM but keep the modules this should not happen. Because VM
  105. // stores modules with a RefPtr we cannot just store the VM as that leads to
  106. // cycles.
  107. GC::Ptr<Realm> m_realm; // [[Realm]]
  108. GC::Ptr<Environment> m_environment; // [[Environment]]
  109. GC::Ptr<Object> m_namespace; // [[Namespace]]
  110. Script::HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
  111. // Needed for potential lookups of modules.
  112. ByteString m_filename;
  113. };
  114. class CyclicModule;
  115. struct GraphLoadingState;
  116. void finish_loading_imported_module(ImportedModuleReferrer, ModuleRequest const&, ImportedModulePayload, ThrowCompletionOr<GC::Ref<Module>> const&);
  117. }