LibJS: Implement BigInt.asUintN

This commit is contained in:
Timothy Flynn 2022-02-05 21:04:17 -05:00 committed by Linus Groh
parent 460c2caaf7
commit cb57475168
Notes: sideshowbarker 2024-07-17 19:42:42 +09:00
2 changed files with 88 additions and 2 deletions

View File

@ -32,7 +32,7 @@ void BigIntConstructor::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.asIntN, as_int_n, 2, attr);
// define_native_function(vm.names.asUintN, as_uint_n, 2, attr);
define_native_function(vm.names.asUintN, as_uint_n, 2, attr);
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
}
@ -95,7 +95,16 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
// 21.2.2.2 BigInt.asUintN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asuintn
JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
{
TODO();
// 1. Set bits to ? ToIndex(bits).
auto bits = TRY(vm.argument(0).to_index(global_object));
// 2. Set bigint to ? ToBigInt(bigint).
auto* bigint = TRY(vm.argument(1).to_bigint(global_object));
// 3. Return the BigInt value that represents (bigint) modulo 2bits.
// FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to
// drop the most significant bits.
return js_bigint(vm, modulo(bigint->big_integer(), BIGINT_ONE.shift_left(bits)));
}
}

View File

@ -0,0 +1,77 @@
describe("errors", () => {
test("invalid index", () => {
expect(() => {
BigInt.asUintN(-1, 0n);
}).toThrowWithMessage(RangeError, "Index must be a positive integer");
expect(() => {
BigInt.asUintN(Symbol(), 0n);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
});
test("invalid BigInt", () => {
expect(() => {
BigInt.asUintN(1, 1);
}).toThrowWithMessage(TypeError, "Cannot convert number to BigInt");
expect(() => {
BigInt.asUintN(1, Symbol());
}).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
expect(() => {
BigInt.asUintN(1, "foo");
}).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo");
});
});
describe("correct behavior", () => {
test("basic functionality", () => {
expect(BigInt.asUintN(0, -2n)).toBe(0n);
expect(BigInt.asUintN(0, -1n)).toBe(0n);
expect(BigInt.asUintN(0, 0n)).toBe(0n);
expect(BigInt.asUintN(0, 1n)).toBe(0n);
expect(BigInt.asUintN(0, 2n)).toBe(0n);
expect(BigInt.asUintN(1, -3n)).toBe(1n);
expect(BigInt.asUintN(1, -2n)).toBe(0n);
expect(BigInt.asUintN(1, -1n)).toBe(1n);
expect(BigInt.asUintN(1, 0n)).toBe(0n);
expect(BigInt.asUintN(1, 1n)).toBe(1n);
expect(BigInt.asUintN(1, 2n)).toBe(0n);
expect(BigInt.asUintN(1, 3n)).toBe(1n);
expect(BigInt.asUintN(2, -3n)).toBe(1n);
expect(BigInt.asUintN(2, -2n)).toBe(2n);
expect(BigInt.asUintN(2, -1n)).toBe(3n);
expect(BigInt.asUintN(2, 0n)).toBe(0n);
expect(BigInt.asUintN(2, 1n)).toBe(1n);
expect(BigInt.asUintN(2, 2n)).toBe(2n);
expect(BigInt.asUintN(2, 3n)).toBe(3n);
expect(BigInt.asUintN(4, -3n)).toBe(13n);
expect(BigInt.asUintN(4, -2n)).toBe(14n);
expect(BigInt.asUintN(4, -1n)).toBe(15n);
expect(BigInt.asUintN(4, 0n)).toBe(0n);
expect(BigInt.asUintN(4, 1n)).toBe(1n);
expect(BigInt.asUintN(4, 2n)).toBe(2n);
expect(BigInt.asUintN(4, 3n)).toBe(3n);
const extremelyBigInt = 123456789123456789123456789123456789123456789123456789n;
expect(BigInt.asUintN(0, extremelyBigInt)).toBe(0n);
expect(BigInt.asUintN(1, extremelyBigInt)).toBe(1n);
expect(BigInt.asUintN(2, extremelyBigInt)).toBe(1n);
expect(BigInt.asUintN(4, extremelyBigInt)).toBe(5n);
expect(BigInt.asUintN(128, extremelyBigInt)).toBe(241220992521549204068304577237384191765n);
expect(BigInt.asUintN(256, extremelyBigInt)).toBe(extremelyBigInt);
expect(BigInt.asUintN(0, -extremelyBigInt)).toBe(0n);
expect(BigInt.asUintN(1, -extremelyBigInt)).toBe(1n);
expect(BigInt.asUintN(2, -extremelyBigInt)).toBe(3n);
expect(BigInt.asUintN(4, -extremelyBigInt)).toBe(11n);
expect(BigInt.asUintN(128, -extremelyBigInt)).toBe(99061374399389259395070030194384019691n);
expect(BigInt.asUintN(256, -extremelyBigInt)).toBe(
115792089237316195423570861551898784396480861208851440582668460551124006183147n
);
});
});