Environment.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringView.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/Object.h>
  10. namespace JS {
  11. struct Variable {
  12. Value value;
  13. DeclarationKind declaration_kind;
  14. };
  15. #define JS_ENVIRONMENT(class_, base_class) JS_CELL(class_, base_class)
  16. class Environment : public Cell {
  17. JS_CELL(Environment, Cell);
  18. public:
  19. virtual bool has_this_binding() const { return false; }
  20. virtual ThrowCompletionOr<Value> get_this_binding(VM&) const { return Value {}; }
  21. virtual Object* with_base_object() const { return nullptr; }
  22. virtual ThrowCompletionOr<bool> has_binding([[maybe_unused]] FlyString const& name, [[maybe_unused]] Optional<size_t>* out_index = nullptr) const { return false; }
  23. virtual ThrowCompletionOr<void> create_mutable_binding(VM&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool can_be_deleted) { return {}; }
  24. virtual ThrowCompletionOr<void> create_immutable_binding(VM&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return {}; }
  25. virtual ThrowCompletionOr<void> initialize_binding(VM&, [[maybe_unused]] FlyString const& name, Value) { return {}; }
  26. virtual ThrowCompletionOr<void> set_mutable_binding(VM&, [[maybe_unused]] FlyString const& name, Value, [[maybe_unused]] bool strict) { return {}; }
  27. virtual ThrowCompletionOr<Value> get_binding_value(VM&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return Value {}; }
  28. virtual ThrowCompletionOr<bool> delete_binding(VM&, [[maybe_unused]] FlyString const& name) { return false; }
  29. // [[OuterEnv]]
  30. Environment* outer_environment() { return m_outer_environment; }
  31. Environment const* outer_environment() const { return m_outer_environment; }
  32. virtual bool is_global_environment() const { return false; }
  33. virtual bool is_declarative_environment() const { return false; }
  34. virtual bool is_function_environment() const { return false; }
  35. template<typename T>
  36. bool fast_is() const = delete;
  37. // This flag is set on the entire variable environment chain when direct eval() is performed.
  38. // It is used to disable non-local variable access caching.
  39. bool is_permanently_screwed_by_eval() const { return m_permanently_screwed_by_eval; }
  40. void set_permanently_screwed_by_eval();
  41. protected:
  42. explicit Environment(Environment* parent);
  43. virtual void visit_edges(Visitor&) override;
  44. private:
  45. virtual bool is_environment() const final { return true; }
  46. bool m_permanently_screwed_by_eval { false };
  47. Environment* m_outer_environment { nullptr };
  48. };
  49. }