diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp index b58c63a9ea..f67ce9ea2a 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -395,4 +396,31 @@ Value new_regexp(VM& vm, ParsedRegex const& parsed_regex, DeprecatedString const return regexp_object; } +// 13.3.8.1 https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation +MarkedVector argument_list_evaluation(Bytecode::Interpreter& interpreter) +{ + // Note: Any spreading and actual evaluation is handled in preceding opcodes + // Note: The spec uses the concept of a list, while we create a temporary array + // in the preceding opcodes, so we have to convert in a manner that is not + // visible to the user + auto& vm = interpreter.vm(); + + MarkedVector argument_values { vm.heap() }; + auto arguments = interpreter.accumulator(); + + auto& argument_array = arguments.as_array(); + auto array_length = argument_array.indexed_properties().array_like_size(); + + argument_values.ensure_capacity(array_length); + + for (size_t i = 0; i < array_length; ++i) { + if (auto maybe_value = argument_array.indexed_properties().get(i); maybe_value.has_value()) + argument_values.append(maybe_value.release_value().value); + else + argument_values.append(js_undefined()); + } + + return argument_values; +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h index 1c9f115e88..372ab7c1e9 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h @@ -31,5 +31,6 @@ struct CalleeAndThis { }; ThrowCompletionOr get_callee_and_this_from_environment(Bytecode::Interpreter&, DeprecatedFlyString const& name, u32 cache_index); Value new_regexp(VM&, ParsedRegex const&, DeprecatedString const& pattern, DeprecatedString const& flags); +MarkedVector argument_list_evaluation(Bytecode::Interpreter&); } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index e4abe68034..fa8e461dab 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -1027,33 +1027,6 @@ ThrowCompletionOr JumpUndefined::execute_impl(Bytecode::Interpreter&) cons __builtin_unreachable(); } -// 13.3.8.1 https://tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation -static MarkedVector argument_list_evaluation(Bytecode::Interpreter& interpreter) -{ - // Note: Any spreading and actual evaluation is handled in preceding opcodes - // Note: The spec uses the concept of a list, while we create a temporary array - // in the preceding opcodes, so we have to convert in a manner that is not - // visible to the user - auto& vm = interpreter.vm(); - - MarkedVector argument_values { vm.heap() }; - auto arguments = interpreter.accumulator(); - - auto& argument_array = arguments.as_array(); - auto array_length = argument_array.indexed_properties().array_like_size(); - - argument_values.ensure_capacity(array_length); - - for (size_t i = 0; i < array_length; ++i) { - if (auto maybe_value = argument_array.indexed_properties().get(i); maybe_value.has_value()) - argument_values.append(maybe_value.release_value().value); - else - argument_values.append(js_undefined()); - } - - return argument_values; -} - ThrowCompletionOr Call::execute_impl(Bytecode::Interpreter& interpreter) const { auto& vm = interpreter.vm(); diff --git a/Userland/Libraries/LibJS/JIT/Compiler.cpp b/Userland/Libraries/LibJS/JIT/Compiler.cpp index 7ce97d0aad..8947b3f002 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.cpp +++ b/Userland/Libraries/LibJS/JIT/Compiler.cpp @@ -923,6 +923,25 @@ void Compiler::compile_call(Bytecode::Op::Call const& op) check_exception(); } +static Value cxx_call_with_argument_array(VM& vm, Value callee, Value this_value, Bytecode::Op::CallType call_type, Optional const& expression_string) +{ + TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, expression_string)); + auto argument_values = argument_list_evaluation(vm.bytecode_interpreter()); + return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values))); +} + +void Compiler::compile_call_with_argument_array(Bytecode::Op::CallWithArgumentArray const& op) +{ + load_vm_register(ARG1, op.callee()); + load_vm_register(ARG2, op.this_value()); + m_assembler.mov( + Assembler::Operand::Register(ARG3), + Assembler::Operand::Imm(to_underlying(op.call_type()))); + native_call((void*)cxx_call_with_argument_array); + store_vm_register(Bytecode::Register::accumulator(), RET); + check_exception(); +} + static Value cxx_typeof_variable(VM& vm, DeprecatedFlyString const& identifier) { return TRY_OR_SET_EXCEPTION(Bytecode::typeof_variable(vm, identifier)); @@ -1153,6 +1172,9 @@ OwnPtr Compiler::compile(Bytecode::Executable& bytecode_execut case Bytecode::Instruction::Type::Call: compiler.compile_call(static_cast(op)); break; + case Bytecode::Instruction::Type::CallWithArgumentArray: + compiler.compile_call_with_argument_array(static_cast(op)); + break; case Bytecode::Instruction::Type::TypeofVariable: compiler.compile_typeof_variable(static_cast(op)); break; diff --git a/Userland/Libraries/LibJS/JIT/Compiler.h b/Userland/Libraries/LibJS/JIT/Compiler.h index 52c70acb27..2e4ceed5a9 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.h +++ b/Userland/Libraries/LibJS/JIT/Compiler.h @@ -111,6 +111,7 @@ private: void compile_put_by_value(Bytecode::Op::PutByValue const&); void compile_call(Bytecode::Op::Call const&); + void compile_call_with_argument_array(Bytecode::Op::CallWithArgumentArray const&); void compile_typeof_variable(Bytecode::Op::TypeofVariable const&); void compile_set_variable(Bytecode::Op::SetVariable const&); void compile_continue_pending_unwind(Bytecode::Op::ContinuePendingUnwind const&);