/src/serenity/Userland/Libraries/LibJS/Runtime/PrivateEnvironment.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, David Tuin <davidot@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/Runtime/PrivateEnvironment.h> |
8 | | |
9 | | namespace JS { |
10 | | |
11 | | JS_DEFINE_ALLOCATOR(PrivateEnvironment); |
12 | | |
13 | | PrivateEnvironment::PrivateEnvironment(PrivateEnvironment* parent) |
14 | 0 | : m_outer_environment(parent) |
15 | 0 | , m_unique_id(s_next_id++) |
16 | 0 | { |
17 | | // FIXME: We might want to delay getting the next unique id until required. |
18 | 0 | VERIFY(s_next_id != 0); |
19 | 0 | } |
20 | | |
21 | | // Note: we start at one such that 0 can be invalid / default initialized. |
22 | | u64 PrivateEnvironment::s_next_id = 1u; |
23 | | |
24 | | PrivateName PrivateEnvironment::resolve_private_identifier(DeprecatedFlyString const& identifier) const |
25 | 0 | { |
26 | 0 | auto name_or_end = find_private_name(identifier); |
27 | |
|
28 | 0 | if (!name_or_end.is_end()) |
29 | 0 | return *name_or_end; |
30 | | |
31 | | // Note: This verify ensures that we must either have a private name with a matching description |
32 | | // or have an outer environment. Combined this means that we assert that we always return a PrivateName. |
33 | 0 | VERIFY(m_outer_environment); |
34 | 0 | return m_outer_environment->resolve_private_identifier(identifier); |
35 | 0 | } |
36 | | |
37 | | void PrivateEnvironment::add_private_name(DeprecatedFlyString description) |
38 | 0 | { |
39 | 0 | if (!find_private_name(description).is_end()) |
40 | 0 | return; |
41 | | |
42 | 0 | m_private_names.empend(m_unique_id, move(description)); |
43 | 0 | } |
44 | | |
45 | | bool PrivateName::operator==(PrivateName const& rhs) const |
46 | 0 | { |
47 | 0 | return unique_id == rhs.unique_id && description == rhs.description; |
48 | 0 | } |
49 | | |
50 | | void PrivateEnvironment::visit_edges(Visitor& visitor) |
51 | 0 | { |
52 | 0 | Base::visit_edges(visitor); |
53 | 0 | visitor.visit(m_outer_environment); |
54 | 0 | } |
55 | | |
56 | | } |