LibJS: Add builtin for Math.imul()

This commit is contained in:
Andreas Kling 2025-04-03 11:58:30 +02:00 committed by Andreas Kling
parent ab5d5d8b50
commit 714e8aec8a
Notes: github-actions[bot] 2025-04-03 11:57:40 +00:00
4 changed files with 14 additions and 4 deletions

View file

@ -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)

View file

@ -2597,6 +2597,8 @@ static ThrowCompletionOr<Value> 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:

View file

@ -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<Value> 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<i32>(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<Value> MathObject::log_impl(VM& vm, Value x)
{

View file

@ -27,6 +27,7 @@ public:
static ThrowCompletionOr<Value> exp_impl(VM&, Value);
static ThrowCompletionOr<Value> abs_impl(VM&, Value);
static ThrowCompletionOr<Value> sum_precise_impl(VM&, Value);
static ThrowCompletionOr<Value> imul_impl(VM&, Value, Value);
private:
explicit MathObject(Realm&);