Environment.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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) \
  16. public: \
  17. using Base = base_class; \
  18. virtual StringView class_name() const override { return #class_; }
  19. class Environment : public Cell {
  20. public:
  21. virtual bool has_this_binding() const { return false; }
  22. virtual ThrowCompletionOr<Value> get_this_binding(GlobalObject&) const { return Value {}; }
  23. virtual Object* with_base_object() const { return nullptr; }
  24. virtual ThrowCompletionOr<bool> has_binding([[maybe_unused]] FlyString const& name, [[maybe_unused]] Optional<size_t>* out_index = nullptr) const { return false; }
  25. virtual ThrowCompletionOr<void> create_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool can_be_deleted) { return {}; }
  26. virtual ThrowCompletionOr<void> create_immutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return {}; }
  27. virtual ThrowCompletionOr<void> initialize_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value) { return {}; }
  28. virtual ThrowCompletionOr<void> set_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value, [[maybe_unused]] bool strict) { return {}; }
  29. virtual ThrowCompletionOr<Value> get_binding_value(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return Value {}; }
  30. virtual ThrowCompletionOr<bool> delete_binding(GlobalObject&, [[maybe_unused]] FlyString const& name) { return false; }
  31. // [[OuterEnv]]
  32. Environment* outer_environment() { return m_outer_environment; }
  33. Environment const* outer_environment() const { return m_outer_environment; }
  34. virtual bool is_global_environment() const { return false; }
  35. virtual bool is_declarative_environment() const { return false; }
  36. virtual bool is_function_environment() const { return false; }
  37. template<typename T>
  38. bool fast_is() const = delete;
  39. virtual StringView class_name() const override { return "Environment"sv; }
  40. // This flag is set on the entire variable environment chain when direct eval() is performed.
  41. // It is used to disable non-local variable access caching.
  42. bool is_permanently_screwed_by_eval() const { return m_permanently_screwed_by_eval; }
  43. void set_permanently_screwed_by_eval();
  44. protected:
  45. explicit Environment(Environment* parent);
  46. virtual void visit_edges(Visitor&) override;
  47. private:
  48. virtual bool is_environment() const final { return true; }
  49. bool m_permanently_screwed_by_eval { false };
  50. Environment* m_outer_environment { nullptr };
  51. };
  52. }