Browse Source

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

Andreas Kling 1 năm trước cách đây
mục cha
commit
afeb551d57

+ 1 - 0
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) \

+ 13 - 0
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, [&] {

+ 9 - 3
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<Value> 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)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/MathObject.h

@@ -24,6 +24,7 @@ public:
     static ThrowCompletionOr<Value> floor_impl(VM&, Value);
     static ThrowCompletionOr<Value> ceil_impl(VM&, Value);
     static ThrowCompletionOr<Value> round_impl(VM&, Value);
+    static ThrowCompletionOr<Value> exp_impl(VM&, Value);
 
 private:
     explicit MathObject(Realm&);