diff --git a/Userland/Libraries/LibJS/Bytecode/Builtins.h b/Userland/Libraries/LibJS/Bytecode/Builtins.h index e69762819c..651e4ffed8 100644 --- a/Userland/Libraries/LibJS/Bytecode/Builtins.h +++ b/Userland/Libraries/LibJS/Bytecode/Builtins.h @@ -14,7 +14,8 @@ namespace JS::Bytecode { // TitleCaseName, snake_case_name, base, property, argument_count #define JS_ENUMERATE_BUILTINS(O) \ O(MathAbs, math_abs, Math, abs, 1) \ - O(MathLog, math_log, Math, log, 1) + O(MathLog, math_log, Math, log, 1) \ + O(MathSqrt, math_sqrt, Math, sqrt, 1) enum class Builtin { #define DEFINE_BUILTIN_ENUM(name, ...) name, diff --git a/Userland/Libraries/LibJS/JIT/Compiler.cpp b/Userland/Libraries/LibJS/JIT/Compiler.cpp index 6d13442599..64deff02c7 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.cpp +++ b/Userland/Libraries/LibJS/JIT/Compiler.cpp @@ -2637,6 +2637,19 @@ void Compiler::compile_builtin_math_log(Assembler::Label&, Assembler::Label& end m_assembler.jump(end); } +static Value cxx_math_sqrt(VM& vm, Value, Value value) +{ + return TRY_OR_SET_EXCEPTION(MathObject::sqrt_impl(vm, value)); +} + +void Compiler::compile_builtin_math_sqrt(Assembler::Label&, Assembler::Label& end) +{ + native_call((void*)cxx_math_sqrt); + 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 19608dede4..0d731d1cc2 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2023, Andreas Kling * Copyright (c) 2020-2023, Linus Groh * Copyright (c) 2021, Idan Horowitz * Copyright (c) 2023, Shannon Booth @@ -31,7 +31,7 @@ void MathObject::initialize(Realm& realm) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(realm, vm.names.abs, abs, 1, attr, Bytecode::Builtin::MathAbs); define_native_function(realm, vm.names.random, random, 0, attr); - define_native_function(realm, vm.names.sqrt, sqrt, 1, attr); + define_native_function(realm, vm.names.sqrt, sqrt, 1, attr, Bytecode::Builtin::MathSqrt); define_native_function(realm, vm.names.floor, floor, 1, attr); define_native_function(realm, vm.names.ceil, ceil, 1, attr); define_native_function(realm, vm.names.round, round, 1, attr); @@ -821,10 +821,10 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sinh) } // 21.3.2.32 Math.sqrt ( x ), https://tc39.es/ecma262/#sec-math.sqrt -JS_DEFINE_NATIVE_FUNCTION(MathObject::sqrt) +ThrowCompletionOr MathObject::sqrt_impl(VM& vm, Value x) { // 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 one of NaN, +0𝔽, -0𝔽, or +∞𝔽, return n. if (number.is_nan() || number.as_double() == 0 || number.is_positive_infinity()) @@ -838,6 +838,12 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sqrt) return Value(::sqrt(number.as_double())); } +// 21.3.2.32 Math.sqrt ( x ), https://tc39.es/ecma262/#sec-math.sqrt +JS_DEFINE_NATIVE_FUNCTION(MathObject::sqrt) +{ + return sqrt_impl(vm, vm.argument(0)); +} + // 21.3.2.33 Math.tan ( x ), https://tc39.es/ecma262/#sec-math.tan JS_DEFINE_NATIVE_FUNCTION(MathObject::tan) { diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.h b/Userland/Libraries/LibJS/Runtime/MathObject.h index 7fdb665f39..8bf0bf4b34 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.h +++ b/Userland/Libraries/LibJS/Runtime/MathObject.h @@ -19,6 +19,7 @@ public: virtual ~MathObject() override = default; static ThrowCompletionOr log_impl(VM&, Value); + static ThrowCompletionOr sqrt_impl(VM&, Value); private: explicit MathObject(Realm&);