1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

LibJS: Move initialize_instance_elements() from VM to Object

This makes more sense as an Object method rather than living within the
VM class for no good reason. Most of the other 7.3.xx AOs already work
the same way.
Also add spec comments while we're here.
This commit is contained in:
Linus Groh 2022-12-07 00:22:23 +00:00
parent cdeaced54e
commit daec065fde
7 changed files with 25 additions and 16 deletions

View file

@ -524,7 +524,7 @@ Completion SuperCall::execute(Interpreter& interpreter) const
[[maybe_unused]] auto& f = this_er.function_object();
// 11. Perform ? InitializeInstanceElements(result, F).
TRY(vm.initialize_instance_elements(*result, f));
TRY(result->initialize_instance_elements(f));
// 12. Return result.
return Value { result };

View file

@ -678,7 +678,7 @@ ThrowCompletionOr<void> SuperCall::execute_impl(Bytecode::Interpreter& interpret
// NOTE: This is implied by the strong C++ type.
// 11. Perform ? InitializeInstanceElements(result, F).
TRY(vm.initialize_instance_elements(*result, f));
TRY(result->initialize_instance_elements(f));
// 12. Return result.
interpreter.accumulator() = result;

View file

@ -231,7 +231,7 @@ ThrowCompletionOr<Object*> ECMAScriptFunctionObject::internal_construct(MarkedVe
ordinary_call_bind_this(callee_context, this_argument);
// b. Let initializeResult be Completion(InitializeInstanceElements(thisArgument, F)).
auto initialize_result = vm.initialize_instance_elements(*this_argument, *this);
auto initialize_result = this_argument->initialize_instance_elements(*this);
// c. If initializeResult is an abrupt completion, then
if (initialize_result.is_throw_completion()) {

View file

@ -606,6 +606,27 @@ ThrowCompletionOr<void> Object::define_field(ClassFieldDefinition const& field)
return {};
}
// 7.3.33 InitializeInstanceElements ( O, constructor ), https://tc39.es/ecma262/#sec-initializeinstanceelements
ThrowCompletionOr<void> Object::initialize_instance_elements(ECMAScriptFunctionObject& constructor)
{
// 1. Let methods be the value of constructor.[[PrivateMethods]].
// 2. For each PrivateElement method of methods, do
for (auto const& method : constructor.private_methods()) {
// a. Perform ? PrivateMethodOrAccessorAdd(O, method).
TRY(private_method_or_accessor_add(method));
}
// 3. Let fields be the value of constructor.[[Fields]].
// 4. For each element fieldRecord of fields, do
for (auto const& field : constructor.fields()) {
// a. Perform ? DefineField(O, fieldRecord).
TRY(define_field(field));
}
// 5. Return unused.
return {};
}
// 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
// 10.1.1 [[GetPrototypeOf]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getprototypeof

View file

@ -108,6 +108,7 @@ public:
ThrowCompletionOr<Value> private_get(PrivateName const& name);
ThrowCompletionOr<void> private_set(PrivateName const& name, Value value);
ThrowCompletionOr<void> define_field(ClassFieldDefinition const&);
ThrowCompletionOr<void> initialize_instance_elements(ECMAScriptFunctionObject& constructor);
// 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots

View file

@ -608,17 +608,6 @@ ThrowCompletionOr<Reference> VM::resolve_binding(FlyString const& name, Environm
// But this is not actually correct as GetIdentifierReference (or really the methods it calls) can throw.
}
// 7.3.33 InitializeInstanceElements ( O, constructor ), https://tc39.es/ecma262/#sec-initializeinstanceelements
ThrowCompletionOr<void> VM::initialize_instance_elements(Object& object, ECMAScriptFunctionObject& constructor)
{
for (auto& method : constructor.private_methods())
TRY(object.private_method_or_accessor_add(method));
for (auto& field : constructor.fields())
TRY(object.define_field(field));
return {};
}
// 9.4.4 ResolveThisBinding ( ), https://tc39.es/ecma262/#sec-resolvethisbinding
ThrowCompletionOr<Value> VM::resolve_this_binding()
{

View file

@ -206,8 +206,6 @@ public:
Function<void(Promise&)> on_promise_unhandled_rejection;
Function<void(Promise&)> on_promise_rejection_handled;
ThrowCompletionOr<void> initialize_instance_elements(Object& object, ECMAScriptFunctionObject& constructor);
CustomData* custom_data() { return m_custom_data; }
ThrowCompletionOr<void> destructuring_assignment_evaluation(NonnullRefPtr<BindingPattern> const& target, Value value);