summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PrivateEnvironment.h
blob: fa195c5e5285738361d39605a52fe36fdc04e544 (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
/*
 * Copyright (c) 2021, David Tuin <davidot@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/DeprecatedFlyString.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibJS/Heap/Cell.h>

namespace JS {

struct PrivateName {
    PrivateName() = default;
    PrivateName(u64 unique_id, DeprecatedFlyString description)
        : unique_id(unique_id)
        , description(move(description))
    {
    }

    u64 unique_id { 0 };
    DeprecatedFlyString description;

    bool operator==(PrivateName const& rhs) const;
};

class PrivateEnvironment : public Cell {
    JS_CELL(PrivateEnvironment, Cell);

public:
    PrivateName resolve_private_identifier(DeprecatedFlyString const& identifier) const;

    void add_private_name(Badge<ClassExpression>, DeprecatedFlyString description);

private:
    explicit PrivateEnvironment(PrivateEnvironment* parent);

    virtual void visit_edges(Visitor&) override;

    auto find_private_name(DeprecatedFlyString const& description) const
    {
        return m_private_names.find_if([&](PrivateName const& private_name) {
            return private_name.description == description;
        });
    }

    static u64 s_next_id;

    GCPtr<PrivateEnvironment> m_outer_environment; // [[OuterEnv]]
    Vector<PrivateName> m_private_names;           // [[Names]]
    u64 m_unique_id;
};

}