1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:27:35 +00:00

LibJS: Add spec comments to mul()

This commit is contained in:
Linus Groh 2022-12-10 00:08:06 +00:00
parent 51cdf2cdef
commit f0f476079b

View file

@ -1764,14 +1764,36 @@ ThrowCompletionOr<Value> sub(VM& vm, Value lhs, Value rhs)
}
// 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
// MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
ThrowCompletionOr<Value> mul(VM& vm, Value lhs, Value rhs)
{
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
// 1-2, 6. N/A.
// 3. Let lnum be ? ToNumeric(lval).
auto lhs_numeric = TRY(lhs.to_numeric(vm));
// 4. Let rnum be ? ToNumeric(rval).
auto rhs_numeric = TRY(rhs.to_numeric(vm));
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()));
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
// [...]
// 8. Return operation(lnum, rnum).
if (both_number(lhs_numeric, rhs_numeric)) {
// 6.1.6.1.4 Number::multiply ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-multiply
auto x = lhs_numeric.as_double();
auto y = rhs_numeric.as_double();
return Value(x * y);
}
if (both_bigint(lhs_numeric, rhs_numeric)) {
// 6.1.6.2.4 BigInt::multiply ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-multiply
auto x = lhs_numeric.as_bigint().big_integer();
auto y = rhs_numeric.as_bigint().big_integer();
// 1. Return the BigInt value that represents the product of x and y.
return BigInt::create(vm, x.multiplied_by(y));
}
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "multiplication");
}