From 8447544e17cbc5ec5dbbf6ac508636b8dc325de1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 24 Nov 2023 09:50:11 +0100 Subject: [PATCH] LibJS/JIT: Add builtin for Math.round() --- Userland/Libraries/LibJS/Bytecode/Builtins.h | 1 + Userland/Libraries/LibJS/JIT/Compiler.cpp | 13 +++++++++++++ Userland/Libraries/LibJS/Runtime/MathObject.cpp | 12 +++++++++--- Userland/Libraries/LibJS/Runtime/MathObject.h | 1 + 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Builtins.h b/Userland/Libraries/LibJS/Bytecode/Builtins.h index 053a280504..d0c4f252db 100644 --- a/Userland/Libraries/LibJS/Bytecode/Builtins.h +++ b/Userland/Libraries/LibJS/Bytecode/Builtins.h @@ -18,6 +18,7 @@ namespace JS::Bytecode { O(MathPow, math_pow, Math, pow, 2) \ O(MathCeil, math_ceil, Math, ceil, 1) \ O(MathFloor, math_floor, Math, floor, 1) \ + O(MathRound, math_round, Math, round, 1) \ O(MathSqrt, math_sqrt, Math, sqrt, 1) enum class Builtin { diff --git a/Userland/Libraries/LibJS/JIT/Compiler.cpp b/Userland/Libraries/LibJS/JIT/Compiler.cpp index 69816679ae..2284be288c 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.cpp +++ b/Userland/Libraries/LibJS/JIT/Compiler.cpp @@ -2689,6 +2689,19 @@ void Compiler::compile_builtin_math_ceil(Assembler::Label&, Assembler::Label& en m_assembler.jump(end); } +static Value cxx_math_round(VM& vm, Value, Value value) +{ + return TRY_OR_SET_EXCEPTION(MathObject::round_impl(vm, value)); +} + +void Compiler::compile_builtin_math_round(Assembler::Label&, Assembler::Label& end) +{ + native_call((void*)cxx_math_round); + store_accumulator(RET); + check_exception(); + m_assembler.jump(end); +} + void Compiler::compile_builtin_math_abs(Assembler::Label& slow_case, Assembler::Label& end) { branch_if_int32(ARG2, [&] { diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index 6f551ba50b..6480a20aa7 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -34,7 +34,7 @@ void MathObject::initialize(Realm& realm) define_native_function(realm, vm.names.sqrt, sqrt, 1, attr, Bytecode::Builtin::MathSqrt); define_native_function(realm, vm.names.floor, floor, 1, attr, Bytecode::Builtin::MathFloor); define_native_function(realm, vm.names.ceil, ceil, 1, attr, Bytecode::Builtin::MathCeil); - define_native_function(realm, vm.names.round, round, 1, attr); + define_native_function(realm, vm.names.round, round, 1, attr, Bytecode::Builtin::MathRound); define_native_function(realm, vm.names.max, max, 2, attr); define_native_function(realm, vm.names.min, min, 2, attr); define_native_function(realm, vm.names.trunc, trunc, 1, attr); @@ -770,10 +770,10 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::random) } // 21.3.2.28 Math.round ( x ), https://tc39.es/ecma262/#sec-math.round -JS_DEFINE_NATIVE_FUNCTION(MathObject::round) +ThrowCompletionOr MathObject::round_impl(VM& vm, Value x) { // 1. Let n be ? ToNumber(x). - auto number = TRY(vm.argument(0).to_number(vm)); + auto number = TRY(x.to_number(vm)); // 2. If n is not finite or n is an integral Number, return n. if (!number.is_finite_number() || number.as_double() == ::trunc(number.as_double())) @@ -788,6 +788,12 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::round) return Value(integer); } +// 21.3.2.28 Math.round ( x ), https://tc39.es/ecma262/#sec-math.round +JS_DEFINE_NATIVE_FUNCTION(MathObject::round) +{ + return round_impl(vm, vm.argument(0)); +} + // 21.3.2.29 Math.sign ( x ), https://tc39.es/ecma262/#sec-math.sign JS_DEFINE_NATIVE_FUNCTION(MathObject::sign) { diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.h b/Userland/Libraries/LibJS/Runtime/MathObject.h index 5b44f545a5..5546f9fc61 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.h +++ b/Userland/Libraries/LibJS/Runtime/MathObject.h @@ -23,6 +23,7 @@ public: static ThrowCompletionOr pow_impl(VM&, Value base, Value exponent); static ThrowCompletionOr floor_impl(VM&, Value); static ThrowCompletionOr ceil_impl(VM&, Value); + static ThrowCompletionOr round_impl(VM&, Value); private: explicit MathObject(Realm&);