Environment.h 3.1 KB

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