1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:27:35 +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:
Andreas Kling 2021-06-23 12:26:37 +02:00
parent 2822da8c8f
commit 9d49a5478a
8 changed files with 360 additions and 4 deletions

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/AST.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/ObjectEnvironmentRecord.h>
namespace JS {
@ -39,4 +40,69 @@ bool ObjectEnvironmentRecord::delete_from_environment_record(FlyString const& na
return m_object.delete_property(name);
}
// 9.1.1.2.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-hasbinding-n
bool ObjectEnvironmentRecord::has_binding(FlyString const& name) const
{
bool found_binding = m_object.has_property(name);
if (!found_binding)
return false;
// FIXME: Implement the rest of this operation.
return true;
}
void ObjectEnvironmentRecord::create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted)
{
PropertyAttributes attributes;
attributes.set_enumerable();
attributes.set_has_enumerable();
attributes.set_writable();
attributes.set_has_writable();
attributes.set_has_configurable();
if (can_be_deleted)
attributes.set_configurable();
m_object.define_property(name, js_undefined(), attributes, true);
}
void ObjectEnvironmentRecord::create_immutable_binding(GlobalObject&, FlyString const&, bool)
{
VERIFY_NOT_REACHED();
}
void ObjectEnvironmentRecord::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
{
set_mutable_binding(global_object, name, value, false);
}
void ObjectEnvironmentRecord::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
{
bool still_exists = m_object.has_property(name);
if (!still_exists && strict) {
global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
return;
}
// FIXME: This should use the Set abstract operation.
// FIXME: Set returns a bool, so this may need to return a bool as well.
m_object.put(name, value);
}
Value ObjectEnvironmentRecord::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
{
if (!m_object.has_property(name)) {
if (!strict)
return js_undefined();
global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
return {};
}
// FIXME: This should use the Get abstract operation.
return m_object.get(name);
}
bool ObjectEnvironmentRecord::delete_binding(GlobalObject&, FlyString const& name)
{
return m_object.delete_property(name);
}
}