diff --git a/Libraries/LibJS/Bytecode/Builtins.h b/Libraries/LibJS/Bytecode/Builtins.h index b7ea0a63451..31c478ba10d 100644 --- a/Libraries/LibJS/Bytecode/Builtins.h +++ b/Libraries/LibJS/Bytecode/Builtins.h @@ -23,7 +23,8 @@ namespace JS::Bytecode { O(MathRandom, math_random, Math, random, 0) \ O(MathRound, math_round, Math, round, 1) \ O(MathSqrt, math_sqrt, Math, sqrt, 1) \ - O(MathSin, math_sin, Math, sin, 1) + O(MathSin, math_sin, Math, sin, 1) \ + O(MathCos, math_cos, Math, cos, 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 542a8fef729..f6dc83c79d3 100644 --- a/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -2821,6 +2821,8 @@ static ThrowCompletionOr dispatch_builtin_call(Bytecode::Interpreter& int return TRY(MathObject::sqrt_impl(interpreter.vm(), interpreter.get(arguments[0]))); case Builtin::MathSin: 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 Bytecode::Builtin::__Count: VERIFY_NOT_REACHED(); } diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 9a0e5214ea3..8460dfbc5cc 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -43,7 +43,7 @@ void MathObject::initialize(Realm& realm) define_native_function(realm, vm.names.min, min, 2, attr); 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); + 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.pow, pow, 2, attr, Bytecode::Builtin::MathPow); define_native_function(realm, vm.names.exp, exp, 1, attr, Bytecode::Builtin::MathExp); @@ -392,10 +392,10 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::clz32) } // 21.3.2.12 Math.cos ( x ), https://tc39.es/ecma262/#sec-math.cos -JS_DEFINE_NATIVE_FUNCTION(MathObject::cos) +ThrowCompletionOr MathObject::cos_impl(VM& vm, Value value) { // 1. 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 +∞𝔽, or n is -∞𝔽, return NaN. if (number.is_nan() || number.is_infinity()) @@ -409,6 +409,11 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::cos) return Value(::cos(number.as_double())); } +JS_DEFINE_NATIVE_FUNCTION(MathObject::cos) +{ + return cos_impl(vm, vm.argument(0)); +} + // 21.3.2.13 Math.cosh ( x ), https://tc39.es/ecma262/#sec-math.cosh JS_DEFINE_NATIVE_FUNCTION(MathObject::cosh) { diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 73f371f7c3d..847b0e106da 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -29,6 +29,7 @@ public: static ThrowCompletionOr sum_precise_impl(VM&, Value); static ThrowCompletionOr imul_impl(VM&, Value, Value); static ThrowCompletionOr sin_impl(VM&, Value); + static ThrowCompletionOr cos_impl(VM&, Value); static Value random_impl();