summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-12-10 00:07:53 +0000
committerLinus Groh <mail@linusgroh.de>2022-12-10 11:23:23 +0000
commit51cdf2cdef060fc2983ac043970ab2dd435fc5b1 (patch)
tree37c936364924761757b36139fb1be896ab90ee1a /Userland/Libraries/LibJS
parentd5359164b1f5f73f7e989eb823f6314276146832 (diff)
downloadserenity-51cdf2cdef060fc2983ac043970ab2dd435fc5b1.zip
LibJS: Add spec comments to sub()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp31
1 files changed, 25 insertions, 6 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp
index 6a1bbfdaa6..93cabf534e 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Value.cpp
@@ -1729,18 +1729,37 @@ ThrowCompletionOr<Value> add(VM& vm, Value lhs, Value rhs)
}
// 13.8.2 The Subtraction Operator ( - ), https://tc39.es/ecma262/#sec-subtraction-operator-minus
+// AdditiveExpression : AdditiveExpression - MultiplicativeExpression
ThrowCompletionOr<Value> sub(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));
+
+ // 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)) {
- double lhsd = lhs_numeric.as_double();
- double rhsd = rhs_numeric.as_double();
- double interm = lhsd - rhsd;
- return Value(interm);
+ // 6.1.6.1.8 Number::subtract ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-subtract
+ auto x = lhs_numeric.as_double();
+ auto y = rhs_numeric.as_double();
+ // 1. Return Number::add(x, Number::unaryMinus(y)).
+ return Value(x - y);
}
- if (both_bigint(lhs_numeric, rhs_numeric))
- return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()));
+ if (both_bigint(lhs_numeric, rhs_numeric)) {
+ // 6.1.6.2.8 BigInt::subtract ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-subtract
+ auto x = lhs_numeric.as_bigint().big_integer();
+ auto y = rhs_numeric.as_bigint().big_integer();
+ // 1. Return the BigInt value that represents the difference x minus y.
+ return BigInt::create(vm, x.minus(y));
+ }
+
+ // 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "subtraction");
}