Module.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/FlyString.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibJS/Runtime/Environment.h>
  11. #include <LibJS/Runtime/Realm.h>
  12. namespace JS {
  13. struct ResolvedBinding {
  14. enum Type {
  15. BindingName,
  16. Namespace,
  17. Ambiguous,
  18. Null,
  19. };
  20. static ResolvedBinding null()
  21. {
  22. return {};
  23. }
  24. static ResolvedBinding ambiguous()
  25. {
  26. ResolvedBinding binding;
  27. binding.type = Ambiguous;
  28. return binding;
  29. }
  30. Type type { Null };
  31. Module* module { nullptr };
  32. FlyString export_name;
  33. bool is_valid() const
  34. {
  35. return type == BindingName || type == Namespace;
  36. }
  37. bool is_namespace() const
  38. {
  39. return type == Namespace;
  40. }
  41. bool is_ambiguous() const
  42. {
  43. return type == Ambiguous;
  44. }
  45. };
  46. // 16.2.1.4 Abstract Module Records, https://tc39.es/ecma262/#sec-abstract-module-records
  47. class Module : public Cell {
  48. JS_CELL(Module, Cell);
  49. public:
  50. virtual ~Module() override;
  51. Realm& realm() { return *m_realm; }
  52. Realm const& realm() const { return *m_realm; }
  53. StringView filename() const { return m_filename; }
  54. Environment* environment() { return m_environment; }
  55. ThrowCompletionOr<Object*> get_module_namespace(VM& vm);
  56. virtual ThrowCompletionOr<void> link(VM& vm) = 0;
  57. virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) = 0;
  58. virtual ThrowCompletionOr<Vector<FlyString>> get_exported_names(VM& vm, Vector<Module*> export_star_set = {}) = 0;
  59. virtual ThrowCompletionOr<ResolvedBinding> resolve_export(VM& vm, FlyString const& export_name, Vector<ResolvedBinding> resolve_set = {}) = 0;
  60. virtual ThrowCompletionOr<u32> inner_module_linking(VM& vm, Vector<Module*>& stack, u32 index);
  61. virtual ThrowCompletionOr<u32> inner_module_evaluation(VM& vm, Vector<Module*>& stack, u32 index);
  62. protected:
  63. Module(Realm&, String filename);
  64. virtual void visit_edges(Cell::Visitor&) override;
  65. void set_environment(Environment* environment)
  66. {
  67. m_environment = environment;
  68. }
  69. private:
  70. Object* module_namespace_create(VM& vm, Vector<FlyString> unambiguous_names);
  71. // These handles are only safe as long as the VM they live in is valid.
  72. // But evaluated modules SHOULD be stored in the VM so unless you intentionally
  73. // destroy the VM but keep the modules this should not happen. Because VM
  74. // stores modules with a RefPtr we cannot just store the VM as that leads to
  75. // cycles.
  76. GCPtr<Realm> m_realm; // [[Realm]]
  77. GCPtr<Environment> m_environment; // [[Environment]]
  78. GCPtr<Object> m_namespace; // [[Namespace]]
  79. // Needed for potential lookups of modules.
  80. String m_filename;
  81. };
  82. }