summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-09-25 00:01:09 +0200
committerLinus Groh <mail@linusgroh.de>2021-09-25 17:51:30 +0200
commit76eb8fe7178c2f03115fa1c4b5d0cb43a0a99064 (patch)
tree704286042e668da19aecc0bc31eb1e6527aeb556 /Userland/Libraries
parent136451c3aff8785af23be7b000176bde4fd90755 (diff)
downloadserenity-76eb8fe7178c2f03115fa1c4b5d0cb43a0a99064.zip
LibJS: Move [[Fields]] to ECMAScriptFunctionObject
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp18
-rw-r--r--Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h11
-rw-r--r--Userland/Libraries/LibJS/Runtime/FunctionObject.cpp23
-rw-r--r--Userland/Libraries/LibJS/Runtime/FunctionObject.h12
-rw-r--r--Userland/Libraries/LibJS/Runtime/VM.cpp6
-rw-r--r--Userland/Libraries/LibJS/Runtime/VM.h2
6 files changed, 33 insertions, 39 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp
index bebb6436bc..2d9f6eef35 100644
--- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp
@@ -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);
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h
index 5dd9fd5d1f..a620e67aa7 100644
--- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h
+++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h
@@ -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;
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
index 0bc775a0ca..3004a004e2 100644
--- a/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
@@ -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);
- }
}
}
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionObject.h b/Userland/Libraries/LibJS/Runtime/FunctionObject.h
index fbd3d3932e..ca41124d64 100644
--- a/Userland/Libraries/LibJS/Runtime/FunctionObject.h
+++ b/Userland/Libraries/LibJS/Runtime/FunctionObject.h
@@ -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;
};
}
diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp
index 17fe692db6..4622e60859 100644
--- a/Userland/Libraries/LibJS/Runtime/VM.cpp
+++ b/Userland/Libraries/LibJS/Runtime/VM.cpp
@@ -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 {};
}
diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h
index 4fa6d89e48..6f1f3ba2f1 100644
--- a/Userland/Libraries/LibJS/Runtime/VM.h
+++ b/Userland/Libraries/LibJS/Runtime/VM.h
@@ -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; }