mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 13:37:43 +00:00
LibJS: Move [[Fields]] to ECMAScriptFunctionObject
This commit is contained in:
parent
136451c3af
commit
76eb8fe717
6 changed files with 33 additions and 39 deletions
|
@ -100,6 +100,11 @@ void ECMAScriptFunctionObject::visit_edges(Visitor& visitor)
|
|||
visitor.visit(m_environment);
|
||||
visitor.visit(m_realm);
|
||||
visitor.visit(m_home_object);
|
||||
|
||||
for (auto& field : m_fields) {
|
||||
field.name.visit_edges(visitor);
|
||||
visitor.visit(field.initializer);
|
||||
}
|
||||
}
|
||||
|
||||
FunctionEnvironment* ECMAScriptFunctionObject::create_environment(FunctionObject& function_being_invoked)
|
||||
|
@ -245,4 +250,17 @@ void ECMAScriptFunctionObject::set_name(const FlyString& name)
|
|||
VERIFY(success);
|
||||
}
|
||||
|
||||
// 7.3.31 DefineField ( receiver, fieldRecord ), https://tc39.es/ecma262/#sec-definefield
|
||||
void ECMAScriptFunctionObject::InstanceField::define_field(VM& vm, Object& receiver) const
|
||||
{
|
||||
Value init_value = js_undefined();
|
||||
if (initializer) {
|
||||
auto init_value_or_error = vm.call(*initializer, receiver.value_of());
|
||||
if (init_value_or_error.is_error())
|
||||
return;
|
||||
init_value = init_value_or_error.release_value();
|
||||
}
|
||||
receiver.create_data_property_or_throw(name, init_value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -58,6 +58,16 @@ public:
|
|||
Object* home_object() const { return m_home_object; }
|
||||
void set_home_object(Object* home_object) { m_home_object = home_object; }
|
||||
|
||||
struct InstanceField {
|
||||
StringOrSymbol name;
|
||||
ECMAScriptFunctionObject* initializer { nullptr };
|
||||
|
||||
void define_field(VM& vm, Object& receiver) const;
|
||||
};
|
||||
|
||||
Vector<InstanceField> const& fields() const { return m_fields; }
|
||||
void add_field(StringOrSymbol property_key, ECMAScriptFunctionObject* initializer) { m_fields.empend(property_key, initializer); }
|
||||
|
||||
protected:
|
||||
virtual bool is_strict_mode() const final { return m_strict; }
|
||||
|
||||
|
@ -77,6 +87,7 @@ private:
|
|||
ThisMode m_this_mode { ThisMode::Global }; // [[ThisMode]]
|
||||
bool m_strict { false }; // [[Strict]]
|
||||
Object* m_home_object { nullptr }; // [[HomeObject]]
|
||||
Vector<InstanceField> m_fields; // [[Fields]]
|
||||
bool m_is_class_constructor { false }; // [[IsClassConstructor]]
|
||||
|
||||
FlyString m_name;
|
||||
|
|
|
@ -66,24 +66,6 @@ BoundFunction* FunctionObject::bind(Value bound_this_value, Vector<Value> argume
|
|||
return heap().allocate<BoundFunction>(global_object(), global_object(), target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
|
||||
}
|
||||
|
||||
void FunctionObject::add_field(StringOrSymbol property_key, FunctionObject* initializer)
|
||||
{
|
||||
m_fields.empend(property_key, initializer);
|
||||
}
|
||||
|
||||
// 7.3.31 DefineField ( receiver, fieldRecord ), https://tc39.es/ecma262/#sec-definefield
|
||||
void FunctionObject::InstanceField::define_field(VM& vm, Object& receiver) const
|
||||
{
|
||||
Value init_value = js_undefined();
|
||||
if (initializer) {
|
||||
auto init_value_or_error = vm.call(*initializer, receiver.value_of());
|
||||
if (init_value_or_error.is_error())
|
||||
return;
|
||||
init_value = init_value_or_error.release_value();
|
||||
}
|
||||
receiver.create_data_property_or_throw(name, init_value);
|
||||
}
|
||||
|
||||
void FunctionObject::visit_edges(Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
|
@ -92,11 +74,6 @@ void FunctionObject::visit_edges(Visitor& visitor)
|
|||
|
||||
for (auto argument : m_bound_arguments)
|
||||
visitor.visit(argument);
|
||||
|
||||
for (auto& field : m_fields) {
|
||||
field.name.visit_edges(visitor);
|
||||
visitor.visit(field.initializer);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,17 +42,6 @@ public:
|
|||
// This is for IsSimpleParameterList (static semantics)
|
||||
bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
|
||||
|
||||
// [[Fields]]
|
||||
struct InstanceField {
|
||||
StringOrSymbol name;
|
||||
FunctionObject* initializer { nullptr };
|
||||
|
||||
void define_field(VM& vm, Object& receiver) const;
|
||||
};
|
||||
|
||||
Vector<InstanceField> const& fields() const { return m_fields; }
|
||||
void add_field(StringOrSymbol property_key, FunctionObject* initializer);
|
||||
|
||||
protected:
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
|
||||
|
@ -66,7 +55,6 @@ private:
|
|||
Value m_bound_this;
|
||||
Vector<Value> m_bound_arguments;
|
||||
bool m_has_simple_parameter_list { false };
|
||||
Vector<InstanceField> m_fields;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -461,7 +461,7 @@ static void append_bound_and_passed_arguments(MarkedValueList& arguments, Vector
|
|||
}
|
||||
|
||||
// 7.3.32 InitializeInstanceElements ( O, constructor ), https://tc39.es/ecma262/#sec-initializeinstanceelements
|
||||
void VM::initialize_instance_elements(Object& object, FunctionObject& constructor)
|
||||
void VM::initialize_instance_elements(Object& object, ECMAScriptFunctionObject& constructor)
|
||||
{
|
||||
for (auto& field : constructor.fields()) {
|
||||
field.define_field(*this, object);
|
||||
|
@ -509,9 +509,9 @@ Value VM::construct(FunctionObject& function, FunctionObject& new_target, Option
|
|||
// If we are a Derived constructor, |this| has not been constructed before super is called.
|
||||
callee_context.this_value = this_argument;
|
||||
|
||||
if (!is<ECMAScriptFunctionObject>(function) || static_cast<ECMAScriptFunctionObject&>(function).constructor_kind() == ECMAScriptFunctionObject::ConstructorKind::Base) {
|
||||
if (is<ECMAScriptFunctionObject>(function) && static_cast<ECMAScriptFunctionObject&>(function).constructor_kind() == ECMAScriptFunctionObject::ConstructorKind::Base) {
|
||||
VERIFY(this_argument.is_object());
|
||||
initialize_instance_elements(this_argument.as_object(), function);
|
||||
initialize_instance_elements(this_argument.as_object(), static_cast<ECMAScriptFunctionObject&>(function));
|
||||
if (exception())
|
||||
return {};
|
||||
}
|
||||
|
|
|
@ -280,7 +280,7 @@ public:
|
|||
Function<void(const Promise&)> on_promise_unhandled_rejection;
|
||||
Function<void(const Promise&)> on_promise_rejection_handled;
|
||||
|
||||
void initialize_instance_elements(Object& object, FunctionObject& constructor);
|
||||
void initialize_instance_elements(Object& object, ECMAScriptFunctionObject& constructor);
|
||||
|
||||
CustomData* custom_data() { return m_custom_data; }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue