mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:27:35 +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:
parent
a6106ca221
commit
1d8a601f96
4 changed files with 28 additions and 5 deletions
|
@ -12,8 +12,9 @@
|
|||
namespace JS::Bytecode {
|
||||
|
||||
// TitleCaseName, snake_case_name, base, property, argument_count
|
||||
#define JS_ENUMERATE_BUILTINS(O) \
|
||||
O(MathAbs, math_abs, Math, abs, 1)
|
||||
#define JS_ENUMERATE_BUILTINS(O) \
|
||||
O(MathAbs, math_abs, Math, abs, 1) \
|
||||
O(MathLog, math_log, Math, log, 1)
|
||||
|
||||
enum class Builtin {
|
||||
#define DEFINE_BUILTIN_ENUM(name, ...) name,
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
|
||||
#include <LibJS/Runtime/FunctionEnvironment.h>
|
||||
#include <LibJS/Runtime/GlobalEnvironment.h>
|
||||
#include <LibJS/Runtime/MathObject.h>
|
||||
#include <LibJS/Runtime/ObjectEnvironment.h>
|
||||
#include <LibJS/Runtime/VM.h>
|
||||
#include <LibJS/Runtime/ValueInlines.h>
|
||||
|
@ -2623,6 +2624,19 @@ void Compiler::compile_builtin(Bytecode::Builtin builtin, Assembler::Label& slow
|
|||
}
|
||||
}
|
||||
|
||||
static Value cxx_math_log(VM& vm, Value, Value value)
|
||||
{
|
||||
return TRY_OR_SET_EXCEPTION(MathObject::log_impl(vm, value));
|
||||
}
|
||||
|
||||
void Compiler::compile_builtin_math_log(Assembler::Label&, Assembler::Label& end)
|
||||
{
|
||||
native_call((void*)cxx_math_log);
|
||||
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, [&] {
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -18,6 +18,8 @@ public:
|
|||
virtual void initialize(Realm&) override;
|
||||
virtual ~MathObject() override = default;
|
||||
|
||||
static ThrowCompletionOr<Value> log_impl(VM&, Value);
|
||||
|
||||
private:
|
||||
explicit MathObject(Realm&);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue