diff --git a/Userland/Libraries/LibJS/Bytecode/Builtins.h b/Userland/Libraries/LibJS/Bytecode/Builtins.h index d0c4f252db..abd2f6fb44 100644 --- a/Userland/Libraries/LibJS/Bytecode/Builtins.h +++ b/Userland/Libraries/LibJS/Bytecode/Builtins.h @@ -16,6 +16,7 @@ namespace JS::Bytecode { O(MathAbs, math_abs, Math, abs, 1) \ O(MathLog, math_log, Math, log, 1) \ O(MathPow, math_pow, Math, pow, 2) \ + O(MathExp, math_exp, Math, exp, 1) \ O(MathCeil, math_ceil, Math, ceil, 1) \ O(MathFloor, math_floor, Math, floor, 1) \ O(MathRound, math_round, Math, round, 1) \ diff --git a/Userland/Libraries/LibJS/JIT/Compiler.cpp b/Userland/Libraries/LibJS/JIT/Compiler.cpp index 2284be288c..f28e1d74aa 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.cpp +++ b/Userland/Libraries/LibJS/JIT/Compiler.cpp @@ -2702,6 +2702,19 @@ void Compiler::compile_builtin_math_round(Assembler::Label&, Assembler::Label& e m_assembler.jump(end); } +static Value cxx_math_exp(VM& vm, Value, Value value) +{ + return TRY_OR_SET_EXCEPTION(MathObject::exp_impl(vm, value)); +} + +void Compiler::compile_builtin_math_exp(Assembler::Label&, Assembler::Label& end) +{ + native_call((void*)cxx_math_exp); + 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 6480a20aa7..4fa109ec56 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -42,7 +42,7 @@ void MathObject::initialize(Realm& realm) define_native_function(realm, vm.names.cos, cos, 1, attr); define_native_function(realm, vm.names.tan, tan, 1, attr); define_native_function(realm, vm.names.pow, pow, 2, attr, Bytecode::Builtin::MathPow); - define_native_function(realm, vm.names.exp, exp, 1, attr); + define_native_function(realm, vm.names.exp, exp, 1, attr, Bytecode::Builtin::MathExp); define_native_function(realm, vm.names.expm1, expm1, 1, attr); define_native_function(realm, vm.names.sign, sign, 1, attr); define_native_function(realm, vm.names.clz32, clz32, 1, attr); @@ -422,10 +422,10 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::cosh) } // 21.3.2.14 Math.exp ( x ), https://tc39.es/ecma262/#sec-math.exp -JS_DEFINE_NATIVE_FUNCTION(MathObject::exp) +ThrowCompletionOr MathObject::exp_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 either NaN or +∞𝔽, return n. if (number.is_nan() || number.is_positive_infinity()) @@ -443,6 +443,12 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::exp) return Value(::exp(number.as_double())); } +// 21.3.2.14 Math.exp ( x ), https://tc39.es/ecma262/#sec-math.exp +JS_DEFINE_NATIVE_FUNCTION(MathObject::exp) +{ + return exp_impl(vm, vm.argument(0)); +} + // 21.3.2.15 Math.expm1 ( x ), https://tc39.es/ecma262/#sec-math.expm1 JS_DEFINE_NATIVE_FUNCTION(MathObject::expm1) { diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.h b/Userland/Libraries/LibJS/Runtime/MathObject.h index 5546f9fc61..1ba9ae2e50 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.h +++ b/Userland/Libraries/LibJS/Runtime/MathObject.h @@ -24,6 +24,7 @@ public: static ThrowCompletionOr floor_impl(VM&, Value); static ThrowCompletionOr ceil_impl(VM&, Value); static ThrowCompletionOr round_impl(VM&, Value); + static ThrowCompletionOr exp_impl(VM&, Value); private: explicit MathObject(Realm&);