summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Environment.h
blob: 265d54fc6b725abd8552fe90636f4db2d6040e2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibJS/Runtime/Object.h>

namespace JS {

struct Variable {
    Value value;
    DeclarationKind declaration_kind;
};

#define JS_ENVIRONMENT(class_, base_class) \
public:                                    \
    using Base = base_class;               \
    virtual char const* class_name() const override { return #class_; }

class Environment : public Cell {
public:
    GlobalObject& global_object() { return *m_global_object; }
    GlobalObject const& global_object() const { return *m_global_object; }

    virtual void initialize(GlobalObject&) override;

    virtual Optional<Variable> get_from_environment(FlyString const&) const = 0;
    virtual bool put_into_environment(FlyString const&, Variable) = 0;
    virtual bool delete_from_environment(FlyString const&) = 0;

    virtual bool has_this_binding() const { return false; }
    virtual Value get_this_binding(GlobalObject&) const { return {}; }

    virtual Object* with_base_object() const { return nullptr; }

    virtual bool has_binding([[maybe_unused]] FlyString const& name) const { return false; }
    virtual void create_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool can_be_deleted) { }
    virtual void create_immutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { }
    virtual void initialize_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value) { }
    virtual void set_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value, [[maybe_unused]] bool strict) { }
    virtual Value get_binding_value(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return {}; }
    virtual bool delete_binding(GlobalObject&, [[maybe_unused]] FlyString const& name) { return false; }

    // [[OuterEnv]]
    Environment* outer_environment() { return m_outer_environment; }
    Environment const* outer_environment() const { return m_outer_environment; }

    virtual bool is_global_environment() const { return false; }
    virtual bool is_declarative_environment() const { return false; }
    virtual bool is_function_environment() const { return false; }

    template<typename T>
    bool fast_is() const = delete;

    virtual char const* class_name() const override { return "Environment"; }

protected:
    explicit Environment(Environment* parent);

    virtual void visit_edges(Visitor&) override;

private:
    virtual bool is_environment() const final { return true; }

    GlobalObject* m_global_object { nullptr };
    Environment* m_outer_environment { nullptr };
};

}