ModuleEnvironment.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. JS_DECLARE_ALLOCATOR(ModuleEnvironment);
  15. public:
  16. // Note: Module Environment Records support all of the declarative Environment Record methods listed
  17. // in Table 18 and share the same specifications for all of those methods except for
  18. // GetBindingValue, DeleteBinding, HasThisBinding and GetThisBinding.
  19. // In addition, module Environment Records support the methods listed in Table 24.
  20. virtual ThrowCompletionOr<Value> get_binding_value(VM&, DeprecatedFlyString const& name, bool strict) override;
  21. virtual ThrowCompletionOr<bool> delete_binding(VM&, DeprecatedFlyString const& name) override;
  22. virtual bool has_this_binding() const final { return true; }
  23. virtual ThrowCompletionOr<Value> get_this_binding(VM&) const final;
  24. ThrowCompletionOr<void> create_import_binding(DeprecatedFlyString name, Module* module, DeprecatedFlyString binding_name);
  25. private:
  26. explicit ModuleEnvironment(Environment* outer_environment);
  27. virtual void visit_edges(Visitor&) override;
  28. struct IndirectBinding {
  29. DeprecatedFlyString name;
  30. GCPtr<Module> module;
  31. DeprecatedFlyString binding_name;
  32. };
  33. IndirectBinding const* get_indirect_binding(DeprecatedFlyString const& name) const;
  34. virtual Optional<BindingAndIndex> find_binding_and_index(DeprecatedFlyString const& name) const override;
  35. // FIXME: Since we always access this via the name this could be a map.
  36. Vector<IndirectBinding> m_indirect_bindings;
  37. };
  38. }