mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:47:44 +00:00
LibJS/JIT: Add builtin for Math.sqrt()
This commit is contained in:
parent
1d8a601f96
commit
94b634f029
4 changed files with 26 additions and 5 deletions
|
@ -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,
|
||||
|
|
|
@ -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, [&] {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
|
||||
|
@ -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<Value> 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)
|
||||
{
|
||||
|
|
|
@ -19,6 +19,7 @@ public:
|
|||
virtual ~MathObject() override = default;
|
||||
|
||||
static ThrowCompletionOr<Value> log_impl(VM&, Value);
|
||||
static ThrowCompletionOr<Value> sqrt_impl(VM&, Value);
|
||||
|
||||
private:
|
||||
explicit MathObject(Realm&);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue