/src/serenity/Userland/Libraries/LibJS/Runtime/Environment.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/StringView.h> |
10 | | #include <LibJS/Runtime/Completion.h> |
11 | | #include <LibJS/Runtime/Object.h> |
12 | | |
13 | | namespace JS { |
14 | | |
15 | | struct Variable { |
16 | | Value value; |
17 | | DeclarationKind declaration_kind; |
18 | | }; |
19 | | |
20 | | #define JS_ENVIRONMENT(class_, base_class) JS_CELL(class_, base_class) |
21 | | |
22 | | class Environment : public Cell { |
23 | | JS_CELL(Environment, Cell); |
24 | | |
25 | | public: |
26 | | enum class InitializeBindingHint { |
27 | | Normal, |
28 | | SyncDispose, |
29 | | }; |
30 | | |
31 | 0 | virtual bool has_this_binding() const { return false; } |
32 | 0 | virtual ThrowCompletionOr<Value> get_this_binding(VM&) const { return Value {}; } |
33 | | |
34 | 0 | virtual Object* with_base_object() const { return nullptr; } |
35 | | |
36 | 0 | virtual ThrowCompletionOr<bool> has_binding([[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] Optional<size_t>* out_index = nullptr) const { return false; } |
37 | 0 | virtual ThrowCompletionOr<void> create_mutable_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] bool can_be_deleted) { return {}; } |
38 | 0 | virtual ThrowCompletionOr<void> create_immutable_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] bool strict) { return {}; } |
39 | 0 | virtual ThrowCompletionOr<void> initialize_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, Value, InitializeBindingHint) { return {}; } |
40 | 0 | virtual ThrowCompletionOr<void> set_mutable_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name, Value, [[maybe_unused]] bool strict) { return {}; } |
41 | 0 | virtual ThrowCompletionOr<Value> get_binding_value(VM&, [[maybe_unused]] DeprecatedFlyString const& name, [[maybe_unused]] bool strict) { return Value {}; } |
42 | 0 | virtual ThrowCompletionOr<bool> delete_binding(VM&, [[maybe_unused]] DeprecatedFlyString const& name) { return false; } |
43 | | |
44 | | // [[OuterEnv]] |
45 | 0 | Environment* outer_environment() { return m_outer_environment; } |
46 | 0 | Environment const* outer_environment() const { return m_outer_environment; } |
47 | | |
48 | 0 | [[nodiscard]] bool is_declarative_environment() const { return m_declarative; } |
49 | 0 | virtual bool is_global_environment() const { return false; } |
50 | 0 | virtual bool is_function_environment() const { return false; } |
51 | | |
52 | | template<typename T> |
53 | | bool fast_is() const = delete; |
54 | | |
55 | | // This flag is set on the entire variable environment chain when direct eval() is performed. |
56 | | // It is used to disable non-local variable access caching. |
57 | 0 | bool is_permanently_screwed_by_eval() const { return m_permanently_screwed_by_eval; } |
58 | | void set_permanently_screwed_by_eval(); |
59 | | |
60 | | protected: |
61 | | enum class IsDeclarative { |
62 | | No, |
63 | | Yes, |
64 | | }; |
65 | | explicit Environment(Environment* parent, IsDeclarative = IsDeclarative::No); |
66 | | |
67 | | virtual void visit_edges(Visitor&) override; |
68 | | |
69 | | private: |
70 | | bool m_permanently_screwed_by_eval { false }; |
71 | | bool m_declarative { false }; |
72 | | |
73 | | GCPtr<Environment> m_outer_environment; |
74 | | }; |
75 | | |
76 | | } |