mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:27:35 +00:00
LibJS: Add support for public fields in classes
This commit is contained in:
parent
3b6a8d1d53
commit
def8b44c40
9 changed files with 265 additions and 11 deletions
|
@ -66,6 +66,23 @@ 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) {
|
||||
init_value = vm.call(*initializer, receiver.value_of());
|
||||
if (vm.exception())
|
||||
return;
|
||||
}
|
||||
receiver.create_data_property_or_throw(name, init_value);
|
||||
}
|
||||
|
||||
void FunctionObject::visit_edges(Visitor& visitor)
|
||||
{
|
||||
Object::visit_edges(visitor);
|
||||
|
@ -75,6 +92,9 @@ void FunctionObject::visit_edges(Visitor& visitor)
|
|||
|
||||
for (auto argument : m_bound_arguments)
|
||||
visitor.visit(argument);
|
||||
|
||||
for (auto& field : m_fields)
|
||||
visitor.visit(field.initializer);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -63,6 +63,17 @@ 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;
|
||||
|
||||
|
@ -79,6 +90,7 @@ private:
|
|||
ConstructorKind m_constructor_kind = ConstructorKind::Base;
|
||||
ThisMode m_this_mode { ThisMode::Global };
|
||||
bool m_has_simple_parameter_list { false };
|
||||
Vector<InstanceField> m_fields;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -457,6 +457,16 @@ 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)
|
||||
{
|
||||
for (auto& field : constructor.fields()) {
|
||||
field.define_field(*this, object);
|
||||
if (exception())
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Value VM::construct(FunctionObject& function, FunctionObject& new_target, Optional<MarkedValueList> arguments)
|
||||
{
|
||||
auto& global_object = function.global_object();
|
||||
|
@ -494,6 +504,14 @@ 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 (function.constructor_kind() == FunctionObject::ConstructorKind::Base) {
|
||||
VERIFY(this_argument.is_object());
|
||||
initialize_instance_elements(this_argument.as_object(), function);
|
||||
if (exception())
|
||||
return {};
|
||||
}
|
||||
|
||||
auto result = function.construct(new_target);
|
||||
|
||||
pop_execution_context();
|
||||
|
|
|
@ -267,6 +267,8 @@ 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);
|
||||
|
||||
private:
|
||||
VM();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue