From 7204b292c5a72e712fa3a8dd6be6afba76e53b05 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 29 Dec 2021 10:33:46 +0100 Subject: [PATCH] LibJS: Implement and use the MakeMethod AO Two direct uses of the set_home_object() setter remain, we should fix those up and remove it eventually. --- Userland/Libraries/LibJS/AST.cpp | 6 +++--- .../Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp | 9 +++++++++ .../Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 5213de5964..00e4bf7d20 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -1316,7 +1316,7 @@ ThrowCompletionOr ClassMethod::class_element_evaluatio return throw_completion(exception->value()); auto& method_function = static_cast(method_value.as_function()); - method_function.set_home_object(&target); + method_function.make_method(target); auto set_function_name = [&](String prefix = "") { auto property_name = property_key.visit( @@ -1422,7 +1422,7 @@ ThrowCompletionOr ClassField::class_element_evaluation // FIXME: A potential optimization is not creating the functions here since these are never directly accessible. auto function_code = create_ast_node(m_initializer->source_range(), copy_initializer.release_nonnull(), name); initializer = ECMAScriptFunctionObject::create(interpreter.global_object(), String::empty(), *function_code, {}, 0, interpreter.lexical_environment(), interpreter.vm().running_execution_context().private_environment, FunctionKind::Regular, true, false, m_contains_direct_call_to_eval, false); - initializer->set_home_object(&target); + initializer->make_method(target); } return ClassValue { @@ -1458,7 +1458,7 @@ ThrowCompletionOr StaticInitializer::class_element_eva // Note: The function bodyFunction is never directly accessible to ECMAScript code. auto* body_function = ECMAScriptFunctionObject::create(global_object, "", *m_function_body, {}, 0, lexical_environment, private_scope, FunctionKind::Regular, true, false, m_contains_direct_call_to_eval, false); - body_function->set_home_object(&home_object); + body_function->make_method(home_object); return ClassValue { normal_completion(body_function) }; } diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp index 33349d9f07..560f955d40 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp @@ -294,6 +294,15 @@ void ECMAScriptFunctionObject::visit_edges(Visitor& visitor) } } +// 10.2.7 MakeMethod ( F, homeObject ), https://tc39.es/ecma262/#sec-makemethod +void ECMAScriptFunctionObject::make_method(Object& home_object) +{ + // 1. Set F.[[HomeObject]] to homeObject. + m_home_object = &home_object; + + // 2. Return NormalCompletion(undefined). +} + // 10.2.11 FunctionDeclarationInstantiation ( func, argumentsList ), https://tc39.es/ecma262/#sec-functiondeclarationinstantiation ThrowCompletionOr ECMAScriptFunctionObject::function_declaration_instantiation(Interpreter* interpreter) { diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h index 21ea05bc59..9a0c614a1e 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h @@ -37,6 +37,8 @@ public: virtual ThrowCompletionOr internal_call(Value this_argument, MarkedValueList arguments_list) override; virtual ThrowCompletionOr internal_construct(MarkedValueList arguments_list, FunctionObject& new_target) override; + void make_method(Object& home_object); + Statement const& ecmascript_code() const { return m_ecmascript_code; } Vector const& formal_parameters() const { return m_formal_parameters; };