mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:17:44 +00:00
LibJS: Add PrivateEnvironment
This commit is contained in:
parent
eeb42c21d1
commit
13ead80ee6
13 changed files with 154 additions and 27 deletions
47
Userland/Libraries/LibJS/Runtime/PrivateEnvironment.cpp
Normal file
47
Userland/Libraries/LibJS/Runtime/PrivateEnvironment.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright (c) 2021, David Tuin <davidot@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/PrivateEnvironment.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
PrivateEnvironment::PrivateEnvironment(PrivateEnvironment* parent)
|
||||
: m_outer_environment(parent)
|
||||
, m_unique_id(s_next_id++)
|
||||
{
|
||||
// FIXME: We might want to delay getting the next unique id until required.
|
||||
VERIFY(s_next_id != 0);
|
||||
}
|
||||
|
||||
// Note: we start at one such that 0 can be invalid / default initialized.
|
||||
u64 PrivateEnvironment::s_next_id = 1u;
|
||||
|
||||
PrivateName PrivateEnvironment::resolve_private_identifier(FlyString const& identifier) const
|
||||
{
|
||||
auto name_or_end = find_private_name(identifier);
|
||||
|
||||
if (!name_or_end.is_end())
|
||||
return *name_or_end;
|
||||
|
||||
// Note: This verify ensures that we must either have a private name with a matching description
|
||||
// or have an outer environment. Combined this means that we assert that we always return a PrivateName.
|
||||
VERIFY(m_outer_environment);
|
||||
return m_outer_environment->resolve_private_identifier(identifier);
|
||||
}
|
||||
|
||||
void PrivateEnvironment::add_private_name(Badge<ClassExpression>, FlyString description)
|
||||
{
|
||||
// FIXME: there is a exception for getter setter pairs.
|
||||
VERIFY(find_private_name(description).is_end());
|
||||
m_private_names.empend(m_unique_id, move(description));
|
||||
}
|
||||
|
||||
bool PrivateName::operator==(PrivateName const& rhs) const
|
||||
{
|
||||
return unique_id == rhs.unique_id && description == rhs.description;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue