1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

LibJS: Implement BigInt loose-equality according to the spec

This commit is contained in:
Timothy Flynn 2022-01-31 10:24:12 -05:00 committed by Linus Groh
parent 281b0411f2
commit 9ad3debf35
2 changed files with 16 additions and 6 deletions

View file

@ -1385,8 +1385,6 @@ bool is_strictly_equal(Value lhs, Value rhs)
// 7.2.14 IsLooselyEqual ( x, y ), https://tc39.es/ecma262/#sec-islooselyequal
ThrowCompletionOr<bool> is_loosely_equal(GlobalObject& global_object, Value lhs, Value rhs)
{
auto& vm = global_object.vm();
// 1. If Type(x) is the same as Type(y), then
if (same_type_for_equality(lhs, rhs)) {
// a. Return IsStrictlyEqual(x, y).
@ -1420,13 +1418,15 @@ ThrowCompletionOr<bool> is_loosely_equal(GlobalObject& global_object, Value lhs,
// 7. If Type(x) is BigInt and Type(y) is String, then
if (lhs.is_bigint() && rhs.is_string()) {
auto& rhs_string = rhs.as_string().string();
// a. Let n be ! StringToBigInt(y).
// b. If n is NaN, return false.
if (!is_valid_bigint_value(rhs_string))
auto bigint = rhs.string_to_bigint(global_object);
// b. If n is undefined, return false.
if (!bigint.has_value())
return false;
// c. Return IsLooselyEqual(x, n).
return is_loosely_equal(global_object, lhs, js_bigint(vm, Crypto::SignedBigInteger::from_base(10, rhs_string)));
return is_loosely_equal(global_object, lhs, *bigint);
}
// 8. If Type(x) is String and Type(y) is BigInt, return IsLooselyEqual(y, x).

View file

@ -99,6 +99,16 @@ describe("correct behavior", () => {
expect(Object(2n) == 1n).toBeFalse();
expect(1n != Object(2n)).toBeTrue();
expect(Object(2n) != 1n).toBeTrue();
expect(2n == "2").toBeTrue();
expect(2n == "0b10").toBeTrue();
expect(2n == "0o2").toBeTrue();
expect(2n == "0x2").toBeTrue();
expect(1n == "2").toBeFalse();
expect(1n == "0b10").toBeFalse();
expect(1n == "0o2").toBeFalse();
expect(1n == "0x2").toBeFalse();
});
test("strong equality operators", () => {