ModuleEnvironment.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. ModuleEnvironment(Environment* outer_environment);
  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(GlobalObject&, FlyString const& name, bool strict) override;
  21. virtual ThrowCompletionOr<bool> delete_binding(GlobalObject&, FlyString const& name) override;
  22. virtual bool has_this_binding() const final { return true; }
  23. virtual ThrowCompletionOr<Value> get_this_binding(GlobalObject&) const final;
  24. ThrowCompletionOr<void> create_import_binding(FlyString name, Module* module, FlyString binding_name);
  25. // Note: Although the spec does not explicitly say this we also have to implement HasBinding as
  26. // the HasBinding method of Declarative Environment records states:
  27. // "It determines if the argument identifier is one of the identifiers bound by the record"
  28. // And this means that we have to include the indirect bindings of a Module Environment.
  29. virtual ThrowCompletionOr<bool> has_binding(FlyString const& name, Optional<size_t>* = nullptr) const override;
  30. private:
  31. struct IndirectBinding {
  32. FlyString name;
  33. Module* module;
  34. FlyString binding_name;
  35. };
  36. IndirectBinding const* get_indirect_binding(FlyString const& name) const;
  37. // FIXME: Since we always access this via the name this could be a map.
  38. Vector<IndirectBinding> m_indirect_bindings;
  39. };
  40. }