ModuleEnvironment.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Module.h>
  8. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  9. #include <LibJS/Runtime/Environment.h>
  10. namespace JS {
  11. // 9.1.1.5 Module Environment Records, https://tc39.es/ecma262/#sec-module-environment-records
  12. class ModuleEnvironment final : public DeclarativeEnvironment {
  13. JS_ENVIRONMENT(ModuleEnvironment, DeclarativeEnvironment);
  14. public:
  15. // Note: Module Environment Records support all of the declarative Environment Record methods listed
  16. // in Table 18 and share the same specifications for all of those methods except for
  17. // GetBindingValue, DeleteBinding, HasThisBinding and GetThisBinding.
  18. // In addition, module Environment Records support the methods listed in Table 24.
  19. virtual ThrowCompletionOr<Value> get_binding_value(VM&, DeprecatedFlyString const& name, bool strict) override;
  20. virtual ThrowCompletionOr<bool> delete_binding(VM&, DeprecatedFlyString const& name) override;
  21. virtual bool has_this_binding() const final { return true; }
  22. virtual ThrowCompletionOr<Value> get_this_binding(VM&) const final;
  23. ThrowCompletionOr<void> create_import_binding(DeprecatedFlyString name, Module* module, DeprecatedFlyString binding_name);
  24. private:
  25. explicit ModuleEnvironment(Environment* outer_environment);
  26. virtual void visit_edges(Visitor&) override;
  27. struct IndirectBinding {
  28. DeprecatedFlyString name;
  29. GCPtr<Module> module;
  30. DeprecatedFlyString binding_name;
  31. };
  32. IndirectBinding const* get_indirect_binding(DeprecatedFlyString const& name) const;
  33. virtual Optional<BindingAndIndex> find_binding_and_index(DeprecatedFlyString const& name) const override;
  34. // FIXME: Since we always access this via the name this could be a map.
  35. Vector<IndirectBinding> m_indirect_bindings;
  36. };
  37. }