1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

LibJS: Only coerce value once in BigInt constructor

See https://github.com/tc39/ecma262/pull/2812.
This commit is contained in:
davidot 2022-08-03 21:33:02 +02:00 committed by Linus Groh
parent 8c11786145
commit 301bba8c19
Notes: sideshowbarker 2024-07-17 18:08:55 +09:00
2 changed files with 16 additions and 2 deletions

View file

@ -52,8 +52,8 @@ ThrowCompletionOr<Value> BigIntConstructor::call()
if (primitive.is_number())
return TRY(number_to_bigint(global_object, primitive));
// 4. Otherwise, return ? ToBigInt(value).
return TRY(value.to_bigint(global_object));
// 4. Otherwise, return ? ToBigInt(prim).
return TRY(primitive.to_bigint(global_object));
}
// 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value

View file

@ -63,6 +63,20 @@ describe("correct behavior", () => {
expect(BigInt("0X10")).toBe(16n);
expect(BigInt(`0x${"f".repeat(25)}`)).toBe(1267650600228229401496703205375n);
});
test("only coerces value once", () => {
let calls = 0;
const value = {
[Symbol.toPrimitive]() {
expect(calls).toBe(0);
++calls;
return "123";
},
};
expect(BigInt(value)).toEqual(123n);
expect(calls).toBe(1);
});
});
describe("errors", () => {