From 9f61cda27e2d6fbd1a8b6004ca6c643ac5469725 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 21 Oct 2023 15:45:10 +0200 Subject: [PATCH] LibJS/Bytecode: Move NewFunction impl into CommonImplementations --- .../LibJS/Bytecode/CommonImplementations.cpp | 21 +++++++++++++++++++ .../LibJS/Bytecode/CommonImplementations.h | 1 + .../Libraries/LibJS/Bytecode/Interpreter.cpp | 15 +------------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp index f1da5e2464..960eac6c0a 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp @@ -264,4 +264,25 @@ ThrowCompletionOr set_variable( return {}; } +Value new_function(VM& vm, FunctionExpression const& function_node, Optional const& lhs_name, Optional const& home_object) +{ + Value value; + + if (!function_node.has_name()) { + DeprecatedFlyString name = {}; + if (lhs_name.has_value()) + name = vm.bytecode_interpreter().current_executable().get_identifier(lhs_name.value()); + value = function_node.instantiate_ordinary_function_expression(vm, name); + } else { + value = ECMAScriptFunctionObject::create(*vm.current_realm(), function_node.name(), function_node.source_text(), function_node.body(), function_node.parameters(), function_node.function_length(), function_node.local_variables_names(), vm.lexical_environment(), vm.running_execution_context().private_environment, function_node.kind(), function_node.is_strict_mode(), function_node.might_need_arguments_object(), function_node.contains_direct_call_to_eval(), function_node.is_arrow_function()); + } + + if (home_object.has_value()) { + auto home_object_value = vm.bytecode_interpreter().reg(home_object.value()); + static_cast(value.as_function()).set_home_object(&home_object_value.as_object()); + } + + return value; +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h index c23567b7bc..8a64905840 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h @@ -22,5 +22,6 @@ template ThrowCompletionOr throw_if_needed_for_call(Interpreter&, InstructionType const&, Value callee); ThrowCompletionOr typeof_variable(VM&, DeprecatedFlyString const&); ThrowCompletionOr set_variable(VM&, DeprecatedFlyString const&, Value, Op::EnvironmentMode, Op::SetVariable::InitializationMode); +Value new_function(VM&, FunctionExpression const&, Optional const& lhs_name, Optional const& home_object); } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index f8e7dd38f7..539f7f8e0e 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -1207,20 +1207,7 @@ ThrowCompletionOr SuperCallWithArgumentArray::execute_impl(Bytecode::Inter ThrowCompletionOr NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const { auto& vm = interpreter.vm(); - - if (!m_function_node.has_name()) { - DeprecatedFlyString name = {}; - if (m_lhs_name.has_value()) - name = interpreter.current_executable().get_identifier(m_lhs_name.value()); - interpreter.accumulator() = m_function_node.instantiate_ordinary_function_expression(vm, name); - } else { - interpreter.accumulator() = ECMAScriptFunctionObject::create(interpreter.realm(), m_function_node.name(), m_function_node.source_text(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), m_function_node.local_variables_names(), vm.lexical_environment(), vm.running_execution_context().private_environment, m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.might_need_arguments_object(), m_function_node.contains_direct_call_to_eval(), m_function_node.is_arrow_function()); - } - - if (m_home_object.has_value()) { - auto home_object_value = interpreter.reg(m_home_object.value()); - static_cast(interpreter.accumulator().as_function()).set_home_object(&home_object_value.as_object()); - } + interpreter.accumulator() = new_function(vm, m_function_node, m_lhs_name, m_home_object); return {}; }