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

@ -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;
}
}

View file

@ -13,6 +13,14 @@
namespace JS {
struct Binding {
Value value;
bool strict;
bool mutable_ { false };
bool can_be_deleted { false };
bool initialized { false };
};
class DeclarativeEnvironmentRecord : public EnvironmentRecord {
JS_OBJECT(DeclarativeEnvironmentRecord, EnvironmentRecord);
@ -40,6 +48,14 @@ public:
EnvironmentRecordType type() const { return m_environment_record_type; }
virtual bool has_binding(FlyString const& name) const override;
virtual void create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted) override;
virtual void create_immutable_binding(GlobalObject&, FlyString const& name, bool strict) override;
virtual void initialize_binding(GlobalObject&, FlyString const& name, Value) override;
virtual void set_mutable_binding(GlobalObject&, FlyString const& name, Value, bool strict) override;
virtual Value get_binding_value(GlobalObject&, FlyString const& name, bool strict) override;
virtual bool delete_binding(GlobalObject&, FlyString const& name) override;
protected:
virtual void visit_edges(Visitor&) override;
@ -48,6 +64,7 @@ private:
EnvironmentRecordType m_environment_record_type : 8 { EnvironmentRecordType::Declarative };
HashMap<FlyString, Variable> m_variables;
HashMap<FlyString, Binding> m_bindings;
};
template<>

View file

@ -26,6 +26,14 @@ public:
virtual bool has_this_binding() const { return false; }
virtual Value get_this_binding(GlobalObject&) const { return {}; }
virtual bool has_binding([[maybe_unused]] FlyString const& name) const { return false; }
virtual void create_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool can_be_deleted) { }
virtual void create_immutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { }
virtual void initialize_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value) { }
virtual void set_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value, [[maybe_unused]] bool strict) { }
virtual Value get_binding_value(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return {}; }
virtual bool delete_binding(GlobalObject&, [[maybe_unused]] FlyString const& name) { return false; }
// [[OuterEnv]]
EnvironmentRecord* outer_environment() { return m_outer_environment; }
EnvironmentRecord const* outer_environment() const { return m_outer_environment; }

View file

@ -15,6 +15,7 @@
M(BigIntBadOperatorOtherType, "Cannot use {} operator with BigInt and other type") \
M(BigIntIntArgument, "BigInt argument must be an integer") \
M(BigIntInvalidValue, "Invalid value for BigInt: {}") \
M(BindingNotInitialized, "Binding {} is not initialized") \
M(ClassConstructorWithoutNew, "Class constructor {} must be called with 'new'") \
M(ClassExtendsValueNotAConstructorOrNull, "Class extends value {} is not a constructor or null") \
M(ClassExtendsValueInvalidPrototype, "Class extends value has an invalid prototype {}") \
@ -160,12 +161,12 @@
M(TypedArrayPrototypeOneArg, "TypedArray.prototype.{}() requires at least one argument") \
M(TypedArrayFailedSettingIndex, "Failed setting value of index {} of typed array") \
M(UnknownIdentifier, "'{}' is not defined") \
M(URIMalformed, "URI malformed") \
/* LibWeb bindings */ \
M(URIMalformed, "URI malformed") /* LibWeb bindings */ \
M(NotAByteString, "Argument to {}() must be a byte string") \
M(BadArgCountOne, "{}() needs one argument") \
M(BadArgCountAtLeastOne, "{}() needs at least one argument") \
M(BadArgCountMany, "{}() needs {} arguments")
M(BadArgCountMany, "{}() needs {} arguments") \
M(FixmeAddAnErrorString, "FIXME: Add a string for this error.")
namespace JS {

View file

@ -55,4 +55,148 @@ Value GlobalEnvironmentRecord::global_this_value() const
return &m_global_object;
}
// 9.1.1.4.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n
bool GlobalEnvironmentRecord::has_binding(FlyString const& name) const
{
if (m_declarative_record->has_binding(name))
return true;
return m_object_record->has_binding(name);
}
void GlobalEnvironmentRecord::create_mutable_binding(GlobalObject& global_object, FlyString const& name, bool can_be_deleted)
{
if (m_declarative_record->has_binding(name)) {
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
return;
}
m_declarative_record->create_mutable_binding(global_object, name, can_be_deleted);
}
void GlobalEnvironmentRecord::create_immutable_binding(GlobalObject& global_object, FlyString const& name, bool strict)
{
if (m_declarative_record->has_binding(name)) {
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
return;
}
m_declarative_record->create_immutable_binding(global_object, name, strict);
}
void GlobalEnvironmentRecord::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
{
if (m_declarative_record->has_binding(name)) {
m_declarative_record->initialize_binding(global_object, name, value);
return;
}
m_object_record->initialize_binding(global_object, name, value);
}
void GlobalEnvironmentRecord::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
{
if (m_declarative_record->has_binding(name)) {
m_declarative_record->set_mutable_binding(global_object, name, value, strict);
return;
}
m_object_record->set_mutable_binding(global_object, name, value, strict);
}
Value GlobalEnvironmentRecord::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
{
if (m_declarative_record->has_binding(name))
return m_declarative_record->get_binding_value(global_object, name, strict);
return m_object_record->get_binding_value(global_object, name, strict);
}
bool GlobalEnvironmentRecord::delete_binding(GlobalObject& global_object, FlyString const& name)
{
if (m_declarative_record->has_binding(name))
return m_declarative_record->delete_binding(global_object, name);
bool existing_prop = m_object_record->object().has_own_property(name);
if (existing_prop) {
bool status = m_object_record->delete_binding(global_object, name);
if (status) {
m_var_names.remove_all_matching([&](auto& entry) { return entry == name; });
}
return status;
}
return true;
}
bool GlobalEnvironmentRecord::has_var_declaration(FlyString const& name) const
{
return m_var_names.contains_slow(name);
}
bool GlobalEnvironmentRecord::has_lexical_declaration(FlyString const& name) const
{
return m_declarative_record->has_binding(name);
}
bool GlobalEnvironmentRecord::has_restricted_global_property(FlyString const& name) const
{
auto existing_prop = m_global_object.get_own_property_descriptor(name);
if (!existing_prop.has_value() || existing_prop.value().value.is_undefined())
return false;
if (existing_prop.value().attributes.is_configurable())
return false;
return true;
}
bool GlobalEnvironmentRecord::can_declare_global_var(FlyString const& name) const
{
bool has_property = m_object_record->object().has_own_property(name);
if (has_property)
return true;
return m_object_record->object().is_extensible();
}
bool GlobalEnvironmentRecord::can_declare_global_function(FlyString const& name) const
{
auto existing_prop = m_object_record->object().get_own_property_descriptor(name);
if (!existing_prop.has_value() || existing_prop.value().value.is_undefined())
return m_object_record->object().is_extensible();
if (existing_prop.value().attributes.is_configurable())
return true;
if (existing_prop.value().is_data_descriptor() && existing_prop.value().attributes.is_writable() && existing_prop.value().attributes.is_enumerable())
return true;
return false;
}
void GlobalEnvironmentRecord::create_global_var_binding(FlyString const& name, bool can_be_deleted)
{
bool has_property = m_object_record->object().has_own_property(name);
bool extensible = m_object_record->object().is_extensible();
if (!has_property && extensible) {
m_object_record->create_mutable_binding(static_cast<GlobalObject&>(m_object_record->object()), name, can_be_deleted);
m_object_record->initialize_binding(m_object_record->global_object(), name, js_undefined());
}
if (!m_var_names.contains_slow(name))
m_var_names.append(name);
}
void GlobalEnvironmentRecord::create_global_function_binding(FlyString const& name, Value value, bool can_be_deleted)
{
auto existing_prop = m_object_record->object().get_own_property_descriptor(name);
PropertyDescriptor desc;
if (!existing_prop.has_value() || existing_prop.value().value.is_undefined() || existing_prop.value().attributes.is_configurable()) {
desc.value = value;
desc.attributes.set_has_writable();
desc.attributes.set_writable();
desc.attributes.set_has_enumerable();
desc.attributes.set_enumerable();
desc.attributes.set_has_configurable();
if (can_be_deleted)
desc.attributes.set_configurable();
} else {
desc.value = value;
}
// FIXME: This should be DefinePropertyOrThrow, followed by Set
m_object_record->object().define_property(name, value, desc.attributes);
if (vm().exception())
return;
if (!m_var_names.contains_slow(name))
m_var_names.append(name);
}
}

View file

@ -22,14 +22,30 @@ public:
virtual bool has_this_binding() const final { return true; }
virtual Value get_this_binding(GlobalObject&) const final;
virtual bool has_binding(FlyString const& name) const override;
virtual void create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted) override;
virtual void create_immutable_binding(GlobalObject&, FlyString const& name, bool strict) override;
virtual void initialize_binding(GlobalObject&, FlyString const& name, Value) override;
virtual void set_mutable_binding(GlobalObject&, FlyString const& name, Value, bool strict) override;
virtual Value get_binding_value(GlobalObject&, FlyString const& name, bool strict) override;
virtual bool delete_binding(GlobalObject&, FlyString const& name) override;
Value global_this_value() const;
// [[ObjectRecord]]
ObjectEnvironmentRecord& object_record() { return *m_object_record; }
// [[DeclarativeReco rd]]
// [[DeclarativeRecord]]
DeclarativeEnvironmentRecord& declarative_record() { return *m_declarative_record; }
bool has_var_declaration(FlyString const& name) const;
bool has_lexical_declaration(FlyString const& name) const;
bool has_restricted_global_property(FlyString const& name) const;
bool can_declare_global_var(FlyString const& name) const;
bool can_declare_global_function(FlyString const& name) const;
void create_global_var_binding(FlyString const& name, bool can_be_deleted);
void create_global_function_binding(FlyString const& name, Value, bool can_be_deleted);
private:
virtual bool is_global_environment_record() const override { return true; }
virtual void visit_edges(Visitor&) override;
@ -38,6 +54,8 @@ private:
ObjectEnvironmentRecord* m_object_record { nullptr };
DeclarativeEnvironmentRecord* m_declarative_record { nullptr };
Vector<FlyString> m_var_names;
};
template<>

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);
}
}

View file

@ -20,6 +20,14 @@ public:
virtual void put_into_environment_record(FlyString const&, Variable) override;
virtual bool delete_from_environment_record(FlyString const&) override;
virtual bool has_binding(FlyString const& name) const override;
virtual void create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted) override;
virtual void create_immutable_binding(GlobalObject&, FlyString const& name, bool strict) override;
virtual void initialize_binding(GlobalObject&, FlyString const& name, Value) override;
virtual void set_mutable_binding(GlobalObject&, FlyString const& name, Value, bool strict) override;
virtual Value get_binding_value(GlobalObject&, FlyString const& name, bool strict) override;
virtual bool delete_binding(GlobalObject&, FlyString const& name) override;
Object& object() { return m_object; }
private: