diff options
author | Linus Groh <mail@linusgroh.de> | 2022-12-10 00:08:06 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-10 11:23:23 +0000 |
commit | f0f476079b21bc44ca4163c824fefe4f2fb9a1f1 (patch) | |
tree | ddc4723a1b5f142e6972626f7979c679ce283ff3 /Userland | |
parent | 51cdf2cdef060fc2983ac043970ab2dd435fc5b1 (diff) | |
download | serenity-f0f476079b21bc44ca4163c824fefe4f2fb9a1f1.zip |
LibJS: Add spec comments to mul()
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Value.cpp | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 93cabf534e..d1dfadd246 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -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"); } |