From 460c2caaf73b5384f0eebb2b18e174dbc25246a5 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 5 Feb 2022 20:10:52 -0500 Subject: [PATCH] LibJS: Implement BigInt.asIntN --- .../LibJS/Runtime/BigIntConstructor.cpp | 30 ++++++- .../Tests/builtins/BigInt/BigInt.asIntN.js | 79 +++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js diff --git a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp b/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp index 9ae52c2abd..fd64dbb141 100644 --- a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -14,6 +15,8 @@ namespace JS { +static const Crypto::SignedBigInteger BIGINT_ONE { 1 }; + BigIntConstructor::BigIntConstructor(GlobalObject& global_object) : NativeFunction(vm().names.BigInt.as_string(), *global_object.function_prototype()) { @@ -27,9 +30,8 @@ void BigIntConstructor::initialize(GlobalObject& global_object) // 21.2.2.3 BigInt.prototype, https://tc39.es/ecma262/#sec-bigint.prototype define_direct_property(vm.names.prototype, global_object.bigint_prototype(), 0); - // TODO: Implement these functions below and uncomment this. - // u8 attr = Attribute::Writable | Attribute::Configurable; - // define_native_function(vm.names.asIntN, as_int_n, 2, attr); + 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_direct_property(vm.names.length, Value(1), Attribute::Configurable); @@ -67,7 +69,27 @@ ThrowCompletionOr BigIntConstructor::construct(FunctionObject&) // 21.2.2.1 BigInt.asIntN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asintn JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_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. Let mod be ℝ(bigint) modulo 2^bits. + // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to + // drop the most significant bits. + auto bits_shift_left = BIGINT_ONE.shift_left(bits); + auto mod = modulo(bigint->big_integer(), bits_shift_left); + + // 4. If mod ≥ 2^(bits-1), return ℤ(mod - 2^bits); otherwise, return ℤ(mod). + // NOTE: Some of the below conditionals are non-standard, but are to protect SignedBigInteger from + // allocating an absurd amount of memory if `bits - 1` overflows to NumericLimits::max. + if ((bits == 0) && (mod >= BIGINT_ONE)) + return js_bigint(vm, mod.minus(bits_shift_left)); + if ((bits > 0) && (mod >= BIGINT_ONE.shift_left(bits - 1))) + return js_bigint(vm, mod.minus(bits_shift_left)); + + return js_bigint(vm, mod); } // 21.2.2.2 BigInt.asUintN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asuintn diff --git a/Userland/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js b/Userland/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js new file mode 100644 index 0000000000..e448a7c72a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js @@ -0,0 +1,79 @@ +describe("errors", () => { + test("invalid index", () => { + expect(() => { + BigInt.asIntN(-1, 0n); + }).toThrowWithMessage(RangeError, "Index must be a positive integer"); + + expect(() => { + BigInt.asIntN(Symbol(), 0n); + }).toThrowWithMessage(TypeError, "Cannot convert symbol to number"); + }); + + test("invalid BigInt", () => { + expect(() => { + BigInt.asIntN(1, 1); + }).toThrowWithMessage(TypeError, "Cannot convert number to BigInt"); + + expect(() => { + BigInt.asIntN(1, Symbol()); + }).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt"); + + expect(() => { + BigInt.asIntN(1, "foo"); + }).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo"); + }); +}); + +describe("correct behavior", () => { + test("length is 2", () => { + expect(BigInt.asIntN).toHaveLength(2); + }); + + test("basic functionality", () => { + expect(BigInt.asIntN(0, -2n)).toBe(0n); + expect(BigInt.asIntN(0, -1n)).toBe(0n); + expect(BigInt.asIntN(0, 0n)).toBe(0n); + expect(BigInt.asIntN(0, 1n)).toBe(0n); + expect(BigInt.asIntN(0, 2n)).toBe(0n); + + expect(BigInt.asIntN(1, -3n)).toBe(-1n); + expect(BigInt.asIntN(1, -2n)).toBe(0n); + expect(BigInt.asIntN(1, -1n)).toBe(-1n); + expect(BigInt.asIntN(1, 0n)).toBe(0n); + expect(BigInt.asIntN(1, 1n)).toBe(-1n); + expect(BigInt.asIntN(1, 2n)).toBe(0n); + expect(BigInt.asIntN(1, 3n)).toBe(-1n); + + expect(BigInt.asIntN(2, -3n)).toBe(1n); + expect(BigInt.asIntN(2, -2n)).toBe(-2n); + expect(BigInt.asIntN(2, -1n)).toBe(-1n); + expect(BigInt.asIntN(2, 0n)).toBe(0n); + expect(BigInt.asIntN(2, 1n)).toBe(1n); + expect(BigInt.asIntN(2, 2n)).toBe(-2n); + expect(BigInt.asIntN(2, 3n)).toBe(-1n); + + expect(BigInt.asIntN(4, -3n)).toBe(-3n); + expect(BigInt.asIntN(4, -2n)).toBe(-2n); + expect(BigInt.asIntN(4, -1n)).toBe(-1n); + expect(BigInt.asIntN(4, 0n)).toBe(0n); + expect(BigInt.asIntN(4, 1n)).toBe(1n); + expect(BigInt.asIntN(4, 2n)).toBe(2n); + expect(BigInt.asIntN(4, 3n)).toBe(3n); + + const extremelyBigInt = 123456789123456789123456789123456789123456789123456789n; + + expect(BigInt.asIntN(0, extremelyBigInt)).toBe(0n); + expect(BigInt.asIntN(1, extremelyBigInt)).toBe(-1n); + expect(BigInt.asIntN(2, extremelyBigInt)).toBe(1n); + expect(BigInt.asIntN(4, extremelyBigInt)).toBe(5n); + expect(BigInt.asIntN(128, extremelyBigInt)).toBe(-99061374399389259395070030194384019691n); + expect(BigInt.asIntN(256, extremelyBigInt)).toBe(extremelyBigInt); + + expect(BigInt.asIntN(0, -extremelyBigInt)).toBe(0n); + expect(BigInt.asIntN(1, -extremelyBigInt)).toBe(-1n); + expect(BigInt.asIntN(2, -extremelyBigInt)).toBe(-1n); + expect(BigInt.asIntN(4, -extremelyBigInt)).toBe(-5n); + expect(BigInt.asIntN(128, -extremelyBigInt)).toBe(99061374399389259395070030194384019691n); + expect(BigInt.asIntN(256, -extremelyBigInt)).toBe(-extremelyBigInt); + }); +});