summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-02-05 21:04:17 -0500
committerLinus Groh <mail@linusgroh.de>2022-02-06 15:49:54 +0000
commitcb57475168edd8f167e5a71bac42a78b8a53a15f (patch)
tree11f79ed8d528a371fb40cc31b213b9eacdcc408e /Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp
parent460c2caaf73b5384f0eebb2b18e174dbc25246a5 (diff)
downloadserenity-cb57475168edd8f167e5a71bac42a78b8a53a15f.zip
LibJS: Implement BigInt.asUintN
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp13
1 files changed, 11 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp b/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp
index fd64dbb141..075899b960 100644
--- a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp
@@ -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)));
}
}