1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibJS/JIT: Add builtin for Math.log()

Note that we still call out to a C++ helper, but by having a builtin,
we still avoid the cost of a full JS function call.
This commit is contained in:
Andreas Kling 2023-11-24 09:31:23 +01:00
parent a6106ca221
commit 1d8a601f96
4 changed files with 28 additions and 5 deletions

View file

@ -58,7 +58,7 @@ void MathObject::initialize(Realm& realm)
define_native_function(realm, vm.names.fround, fround, 1, attr);
define_native_function(realm, vm.names.hypot, hypot, 2, attr);
define_native_function(realm, vm.names.imul, imul, 2, attr);
define_native_function(realm, vm.names.log, log, 1, attr);
define_native_function(realm, vm.names.log, log, 1, attr, Bytecode::Builtin::MathLog);
define_native_function(realm, vm.names.log2, log2, 1, attr);
define_native_function(realm, vm.names.log10, log10, 1, attr);
define_native_function(realm, vm.names.sinh, sinh, 1, attr);
@ -555,10 +555,10 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
}
// 21.3.2.20 Math.log ( x ), https://tc39.es/ecma262/#sec-math.log
JS_DEFINE_NATIVE_FUNCTION(MathObject::log)
ThrowCompletionOr<Value> MathObject::log_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 NaN or n is +∞𝔽, return n.
if (number.is_nan() || number.is_positive_infinity())
@ -580,6 +580,12 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::log)
return Value(::log(number.as_double()));
}
// 21.3.2.20 Math.log ( x ), https://tc39.es/ecma262/#sec-math.log
JS_DEFINE_NATIVE_FUNCTION(MathObject::log)
{
return log_impl(vm, vm.argument(0));
}
// 21.3.2.21 Math.log1p ( x ), https://tc39.es/ecma262/#sec-math.log1p
JS_DEFINE_NATIVE_FUNCTION(MathObject::log1p)
{