mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 09:18:11 +00:00
LibJS: Start implementing spec-compliant variable bindings
This patch adds the concept of variable bindings to the various environment record classes. The bindings are not yet hooked up to anything, this is just fleshing out all the operations. Most of this is following the spec exactly, but in a few cases we are missing the requisite abstract operations to do the exact right thing. I've added FIXME's in those cases where I noticed it.
This commit is contained in:
parent
2822da8c8f
commit
9d49a5478a
8 changed files with 360 additions and 4 deletions
|
@ -51,6 +51,8 @@ void DeclarativeEnvironmentRecord::visit_edges(Visitor& visitor)
|
|||
Base::visit_edges(visitor);
|
||||
for (auto& it : m_variables)
|
||||
visitor.visit(it.value.value);
|
||||
for (auto& it : m_bindings)
|
||||
visitor.visit(it.value.value);
|
||||
}
|
||||
|
||||
Optional<Variable> DeclarativeEnvironmentRecord::get_from_environment_record(FlyString const& name) const
|
||||
|
@ -68,4 +70,96 @@ bool DeclarativeEnvironmentRecord::delete_from_environment_record(FlyString cons
|
|||
return m_variables.remove(name);
|
||||
}
|
||||
|
||||
// 9.1.1.1.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-hasbinding-n
|
||||
bool DeclarativeEnvironmentRecord::has_binding(FlyString const& name) const
|
||||
{
|
||||
return m_bindings.contains(name);
|
||||
}
|
||||
|
||||
void DeclarativeEnvironmentRecord::create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted)
|
||||
{
|
||||
auto result = m_bindings.set(name,
|
||||
Binding {
|
||||
.value = {},
|
||||
.strict = false,
|
||||
.mutable_ = true,
|
||||
.can_be_deleted = can_be_deleted,
|
||||
.initialized = false,
|
||||
});
|
||||
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
||||
}
|
||||
|
||||
void DeclarativeEnvironmentRecord::create_immutable_binding(GlobalObject&, FlyString const& name, bool strict)
|
||||
{
|
||||
auto result = m_bindings.set(name,
|
||||
Binding {
|
||||
.value = {},
|
||||
.strict = strict,
|
||||
.mutable_ = false,
|
||||
.can_be_deleted = false,
|
||||
.initialized = false,
|
||||
});
|
||||
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
||||
}
|
||||
|
||||
void DeclarativeEnvironmentRecord::initialize_binding(GlobalObject&, FlyString const& name, Value value)
|
||||
{
|
||||
auto it = m_bindings.find(name);
|
||||
VERIFY(it != m_bindings.end());
|
||||
VERIFY(it->value.initialized == false);
|
||||
it->value.value = value;
|
||||
it->value.initialized = true;
|
||||
}
|
||||
|
||||
void DeclarativeEnvironmentRecord::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
|
||||
{
|
||||
auto it = m_bindings.find(name);
|
||||
if (it == m_bindings.end()) {
|
||||
if (strict) {
|
||||
global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
|
||||
return;
|
||||
}
|
||||
create_mutable_binding(global_object, name, true);
|
||||
initialize_binding(global_object, name, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (it->value.strict)
|
||||
strict = true;
|
||||
|
||||
if (!it->value.initialized) {
|
||||
global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (it->value.mutable_) {
|
||||
it->value.value = value;
|
||||
} else {
|
||||
if (strict) {
|
||||
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::InvalidAssignToConst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Value DeclarativeEnvironmentRecord::get_binding_value(GlobalObject& global_object, FlyString const& name, bool)
|
||||
{
|
||||
auto it = m_bindings.find(name);
|
||||
VERIFY(it != m_bindings.end());
|
||||
if (!it->value.initialized) {
|
||||
global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name);
|
||||
return {};
|
||||
}
|
||||
return it->value.value;
|
||||
}
|
||||
|
||||
bool DeclarativeEnvironmentRecord::delete_binding(GlobalObject&, FlyString const& name)
|
||||
{
|
||||
auto it = m_bindings.find(name);
|
||||
VERIFY(it != m_bindings.end());
|
||||
if (!it->value.can_be_deleted)
|
||||
return false;
|
||||
m_bindings.remove(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue