Module.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #pragma once
  8. #include <AK/DeprecatedFlyString.h>
  9. #include <LibJS/Heap/GCPtr.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. GCPtr<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. JS_CELL(GraphLoadingState, Cell);
  51. public:
  52. struct HostDefined : Cell {
  53. JS_CELL(HostDefined, Cell);
  54. public:
  55. virtual ~HostDefined() = default;
  56. };
  57. GCPtr<PromiseCapability> promise_capability; // [[PromiseCapability]]
  58. bool is_loading { false }; // [[IsLoading]]
  59. size_t pending_module_count { 0 }; // [[PendingModulesCount]]
  60. HashTable<JS::GCPtr<CyclicModule>> visited; // [[Visited]]
  61. GCPtr<HostDefined> host_defined; // [[HostDefined]]
  62. private:
  63. GraphLoadingState(GCPtr<PromiseCapability> promise_capability, bool is_loading, size_t pending_module_count, HashTable<JS::GCPtr<CyclicModule>> visited, GCPtr<HostDefined> host_defined)
  64. : promise_capability(move(promise_capability))
  65. , is_loading(is_loading)
  66. , pending_module_count(pending_module_count)
  67. , visited(move(visited))
  68. , host_defined(move(host_defined))
  69. {
  70. }
  71. virtual void visit_edges(Cell::Visitor&) override;
  72. };
  73. // 16.2.1.4 Abstract Module Records, https://tc39.es/ecma262/#sec-abstract-module-records
  74. class Module : public Cell {
  75. JS_CELL(Module, Cell);
  76. JS_DECLARE_ALLOCATOR(Module);
  77. public:
  78. virtual ~Module() override;
  79. Realm& realm() { return *m_realm; }
  80. Realm const& realm() const { return *m_realm; }
  81. StringView filename() const { return m_filename; }
  82. Environment* environment() { return m_environment; }
  83. Script::HostDefined* host_defined() const { return m_host_defined; }
  84. ThrowCompletionOr<Object*> get_module_namespace(VM& vm);
  85. virtual ThrowCompletionOr<void> link(VM& vm) = 0;
  86. virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) = 0;
  87. virtual ThrowCompletionOr<Vector<DeprecatedFlyString>> get_exported_names(VM& vm, Vector<Module*> export_star_set = {}) = 0;
  88. virtual ThrowCompletionOr<ResolvedBinding> resolve_export(VM& vm, DeprecatedFlyString const& export_name, Vector<ResolvedBinding> resolve_set = {}) = 0;
  89. virtual ThrowCompletionOr<u32> inner_module_linking(VM& vm, Vector<Module*>& stack, u32 index);
  90. virtual ThrowCompletionOr<u32> inner_module_evaluation(VM& vm, Vector<Module*>& stack, u32 index);
  91. virtual PromiseCapability& load_requested_modules(GCPtr<GraphLoadingState::HostDefined>) = 0;
  92. protected:
  93. Module(Realm&, ByteString filename, Script::HostDefined* host_defined = nullptr);
  94. virtual void visit_edges(Cell::Visitor&) override;
  95. void set_environment(Environment* environment)
  96. {
  97. m_environment = environment;
  98. }
  99. private:
  100. Object* module_namespace_create(VM& vm, Vector<DeprecatedFlyString> unambiguous_names);
  101. // These handles are only safe as long as the VM they live in is valid.
  102. // But evaluated modules SHOULD be stored in the VM so unless you intentionally
  103. // destroy the VM but keep the modules this should not happen. Because VM
  104. // stores modules with a RefPtr we cannot just store the VM as that leads to
  105. // cycles.
  106. GCPtr<Realm> m_realm; // [[Realm]]
  107. GCPtr<Environment> m_environment; // [[Environment]]
  108. GCPtr<Object> m_namespace; // [[Namespace]]
  109. Script::HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
  110. // Needed for potential lookups of modules.
  111. ByteString m_filename;
  112. };
  113. class CyclicModule;
  114. struct GraphLoadingState;
  115. void finish_loading_imported_module(ImportedModuleReferrer, ModuleRequest const&, ImportedModulePayload, ThrowCompletionOr<NonnullGCPtr<Module>> const&);
  116. }