2020-06-06 01:14:10 +01:00
|
|
|
|
/*
|
2023-04-13 00:47:15 +02:00
|
|
|
|
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
2020-06-06 01:14:10 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-06 01:14:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2022-02-05 20:10:52 -05:00
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2021-07-08 00:30:56 +01:00
|
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
|
#include <LibJS/Runtime/BigIntConstructor.h>
|
|
|
|
|
#include <LibJS/Runtime/BigIntObject.h>
|
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-09-27 20:18:30 +02:00
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2022-02-05 20:10:52 -05:00
|
|
|
|
static const Crypto::SignedBigInteger BIGINT_ONE { 1 };
|
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
|
BigIntConstructor::BigIntConstructor(Realm& realm)
|
2023-04-13 00:47:15 +02:00
|
|
|
|
: NativeFunction(realm.vm().names.BigInt.as_string(), realm.intrinsics().function_prototype())
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void BigIntConstructor::initialize(Realm& realm)
|
2020-06-20 15:40:48 +02:00
|
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
|
auto& vm = this->vm();
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
|
|
|
|
// 21.2.2.3 BigInt.prototype, https://tc39.es/ecma262/#sec-bigint.prototype
|
2022-08-27 00:54:55 +01:00
|
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().bigint_prototype(), 0);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
2022-02-05 20:10:52 -05:00
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2022-08-22 21:47:35 +01:00
|
|
|
|
define_native_function(realm, vm.names.asIntN, as_int_n, 2, attr);
|
|
|
|
|
define_native_function(realm, vm.names.asUintN, as_uint_n, 2, attr);
|
2021-07-08 02:49:53 +03:00
|
|
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
|
2021-10-20 21:16:30 +01:00
|
|
|
|
ThrowCompletionOr<Value> BigIntConstructor::call()
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
2021-07-08 00:30:56 +01:00
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
auto value = vm.argument(0);
|
|
|
|
|
|
|
|
|
|
// 2. Let prim be ? ToPrimitive(value, number).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto primitive = TRY(value.to_primitive(vm, Value::PreferredType::Number));
|
2021-07-08 00:30:56 +01:00
|
|
|
|
|
|
|
|
|
// 3. If Type(prim) is Number, return ? NumberToBigInt(prim).
|
|
|
|
|
if (primitive.is_number())
|
2022-08-21 20:38:35 +01:00
|
|
|
|
return TRY(number_to_bigint(vm, primitive));
|
2021-07-08 00:30:56 +01:00
|
|
|
|
|
2022-08-03 21:33:02 +02:00
|
|
|
|
// 4. Otherwise, return ? ToBigInt(prim).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return TRY(primitive.to_bigint(vm));
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
|
2022-12-14 19:18:10 +00:00
|
|
|
|
ThrowCompletionOr<NonnullGCPtr<Object>> BigIntConstructor::construct(FunctionObject&)
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "BigInt");
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.2.2.1 BigInt.asIntN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asintn
|
2021-10-29 00:13:55 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
2022-02-05 20:10:52 -05:00
|
|
|
|
// 1. Set bits to ? ToIndex(bits).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto bits = TRY(vm.argument(0).to_index(vm));
|
2022-02-05 20:10:52 -05:00
|
|
|
|
|
|
|
|
|
// 2. Set bigint to ? ToBigInt(bigint).
|
2023-04-13 15:31:53 +02:00
|
|
|
|
auto bigint = TRY(vm.argument(1).to_bigint(vm));
|
2022-02-05 20:10:52 -05:00
|
|
|
|
|
|
|
|
|
// 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<size_t>::max.
|
|
|
|
|
if ((bits == 0) && (mod >= BIGINT_ONE))
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, mod.minus(bits_shift_left));
|
2022-02-05 20:10:52 -05:00
|
|
|
|
if ((bits > 0) && (mod >= BIGINT_ONE.shift_left(bits - 1)))
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, mod.minus(bits_shift_left));
|
2022-02-05 20:10:52 -05:00
|
|
|
|
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, mod);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 21.2.2.2 BigInt.asUintN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asuintn
|
2021-10-29 00:13:55 +03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
2022-02-05 21:04:17 -05:00
|
|
|
|
// 1. Set bits to ? ToIndex(bits).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto bits = TRY(vm.argument(0).to_index(vm));
|
2022-02-05 21:04:17 -05:00
|
|
|
|
|
|
|
|
|
// 2. Set bigint to ? ToBigInt(bigint).
|
2023-04-13 15:31:53 +02:00
|
|
|
|
auto bigint = TRY(vm.argument(1).to_bigint(vm));
|
2022-02-05 21:04:17 -05:00
|
|
|
|
|
|
|
|
|
// 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.
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, modulo(bigint->big_integer(), BIGINT_ONE.shift_left(bits)));
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|