diff --git a/Libraries/LibJS/Bytecode/Builtins.h b/Libraries/LibJS/Bytecode/Builtins.h index ee8f262b808..a49f4802db7 100644 --- a/Libraries/LibJS/Bytecode/Builtins.h +++ b/Libraries/LibJS/Bytecode/Builtins.h @@ -19,6 +19,7 @@ namespace JS::Bytecode { O(MathExp, math_exp, Math, exp, 1) \ O(MathCeil, math_ceil, Math, ceil, 1) \ O(MathFloor, math_floor, Math, floor, 1) \ + O(MathImul, math_imul, Math, imul, 2) \ O(MathRound, math_round, Math, round, 1) \ O(MathSqrt, math_sqrt, Math, sqrt, 1) diff --git a/Libraries/LibJS/Bytecode/Interpreter.cpp b/Libraries/LibJS/Bytecode/Interpreter.cpp index 146cee8ee96..cefb789d7d9 100644 --- a/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -2597,6 +2597,8 @@ static ThrowCompletionOr dispatch_builtin_call(Bytecode::Interpreter& int return TRY(MathObject::ceil_impl(interpreter.vm(), interpreter.get(arguments[0]))); case Builtin::MathFloor: return TRY(MathObject::floor_impl(interpreter.vm(), interpreter.get(arguments[0]))); + case Builtin::MathImul: + return TRY(MathObject::imul_impl(interpreter.vm(), interpreter.get(arguments[0]), interpreter.get(arguments[1]))); case Builtin::MathRound: return TRY(MathObject::round_impl(interpreter.vm(), interpreter.get(arguments[0]))); case Builtin::MathSqrt: diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 0f6841c3d02..f9065bf7e6e 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -60,7 +60,7 @@ void MathObject::initialize(Realm& realm) define_native_function(realm, vm.names.fround, fround, 1, attr); define_native_function(realm, vm.names.f16round, f16round, 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.imul, imul, 2, attr, Bytecode::Builtin::MathImul); 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); @@ -587,19 +587,25 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::hypot) } // 21.3.2.19 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul -JS_DEFINE_NATIVE_FUNCTION(MathObject::imul) +ThrowCompletionOr MathObject::imul_impl(VM& vm, Value arg_a, Value arg_b) { // 1. Let a be ℝ(? ToUint32(x)). - auto a = TRY(vm.argument(0).to_u32(vm)); + auto const a = TRY(arg_a.to_u32(vm)); // 2. Let b be ℝ(? ToUint32(y)). - auto b = TRY(vm.argument(1).to_u32(vm)); + auto const b = TRY(arg_b.to_u32(vm)); // 3. Let product be (a × b) modulo 2^32. // 4. If product ≥ 2^31, return 𝔽(product - 2^32); otherwise return 𝔽(product). return Value(static_cast(a * b)); } +// 21.3.2.19 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul +JS_DEFINE_NATIVE_FUNCTION(MathObject::imul) +{ + return imul_impl(vm, vm.argument(0), vm.argument(1)); +} + // 21.3.2.20 Math.log ( x ), https://tc39.es/ecma262/#sec-math.log ThrowCompletionOr MathObject::log_impl(VM& vm, Value x) { diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index 12f415f2031..9a938aa5412 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -27,6 +27,7 @@ public: static ThrowCompletionOr exp_impl(VM&, Value); static ThrowCompletionOr abs_impl(VM&, Value); static ThrowCompletionOr sum_precise_impl(VM&, Value); + static ThrowCompletionOr imul_impl(VM&, Value, Value); private: explicit MathObject(Realm&);