diff --git a/Libraries/LibJS/Bytecode/Builtins.h b/Libraries/LibJS/Bytecode/Builtins.h index 31c478ba10d..63ee46add08 100644 --- a/Libraries/LibJS/Bytecode/Builtins.h +++ b/Libraries/LibJS/Bytecode/Builtins.h @@ -24,7 +24,8 @@ namespace JS::Bytecode { O(MathRound, math_round, Math, round, 1) \ O(MathSqrt, math_sqrt, Math, sqrt, 1) \ O(MathSin, math_sin, Math, sin, 1) \ - O(MathCos, math_cos, Math, cos, 1) + O(MathCos, math_cos, Math, cos, 1) \ + O(MathTan, math_tan, Math, tan, 1) enum class Builtin : u8 { #define DEFINE_BUILTIN_ENUM(name, ...) name, diff --git a/Libraries/LibJS/Bytecode/Interpreter.cpp b/Libraries/LibJS/Bytecode/Interpreter.cpp index f6dc83c79d3..a75acb443cc 100644 --- a/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -2823,6 +2823,8 @@ static ThrowCompletionOr dispatch_builtin_call(Bytecode::Interpreter& int return TRY(MathObject::sin_impl(interpreter.vm(), interpreter.get(arguments[0]))); case Builtin::MathCos: return TRY(MathObject::cos_impl(interpreter.vm(), interpreter.get(arguments[0]))); + case Builtin::MathTan: + return TRY(MathObject::tan_impl(interpreter.vm(), interpreter.get(arguments[0]))); case Bytecode::Builtin::__Count: VERIFY_NOT_REACHED(); } diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 8460dfbc5cc..7417ee369c5 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -44,7 +44,7 @@ void MathObject::initialize(Realm& realm) define_native_function(realm, vm.names.trunc, trunc, 1, attr); define_native_function(realm, vm.names.sin, sin, 1, attr, Bytecode::Builtin::MathSin); define_native_function(realm, vm.names.cos, cos, 1, attr, Bytecode::Builtin::MathCos); - define_native_function(realm, vm.names.tan, tan, 1, attr); + define_native_function(realm, vm.names.tan, tan, 1, attr, Bytecode::Builtin::MathTan); define_native_function(realm, vm.names.pow, pow, 2, attr, Bytecode::Builtin::MathPow); define_native_function(realm, vm.names.exp, exp, 1, attr, Bytecode::Builtin::MathExp); define_native_function(realm, vm.names.expm1, expm1, 1, attr); @@ -971,10 +971,10 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sqrt) } // 21.3.2.34 Math.tan ( x ), https://tc39.es/ecma262/#sec-math.tan -JS_DEFINE_NATIVE_FUNCTION(MathObject::tan) +ThrowCompletionOr MathObject::tan_impl(VM& vm, Value value) { // Let n be ? ToNumber(x). - auto number = TRY(vm.argument(0).to_number(vm)); + auto number = TRY(value.to_number(vm)); // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero()) @@ -988,6 +988,11 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::tan) return Value(::tan(number.as_double())); } +JS_DEFINE_NATIVE_FUNCTION(MathObject::tan) +{ + return tan_impl(vm, vm.argument(0)); +} + // 21.3.2.35 Math.tanh ( x ), https://tc39.es/ecma262/#sec-math.tanh JS_DEFINE_NATIVE_FUNCTION(MathObject::tanh) { diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 847b0e106da..99e9c52bca4 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -30,6 +30,7 @@ public: static ThrowCompletionOr imul_impl(VM&, Value, Value); static ThrowCompletionOr sin_impl(VM&, Value); static ThrowCompletionOr cos_impl(VM&, Value); + static ThrowCompletionOr tan_impl(VM&, Value); static Value random_impl();