Ver Fonte

LibJS: Implement BigInt loose-equality according to the spec

Timothy Flynn há 3 anos atrás
pai
commit
9ad3debf35

+ 6 - 6
Userland/Libraries/LibJS/Runtime/Value.cpp

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

+ 10 - 0
Userland/Libraries/LibJS/Tests/builtins/BigInt/bigint-basic.js

@@ -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", () => {