2020-06-05 21:14:10 -03:00
|
|
|
|
/*
|
2023-04-12 19:47:15 -03:00
|
|
|
|
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
2020-06-05 21:14:10 -03:00
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-05 21:14:10 -03:00
|
|
|
|
*/
|
|
|
|
|
|
|
2022-02-05 22:10:52 -03:00
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2021-07-07 20:30:56 -03:00
|
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2020-06-05 21:14:10 -03:00
|
|
|
|
#include <LibJS/Runtime/BigIntConstructor.h>
|
|
|
|
|
|
#include <LibJS/Runtime/BigIntObject.h>
|
|
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-09-27 15:18:30 -03:00
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
2023-10-06 12:54:21 -03:00
|
|
|
|
#include <LibJS/Runtime/ValueInlines.h>
|
2020-06-05 21:14:10 -03:00
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC_DEFINE_ALLOCATOR(BigIntConstructor);
|
2023-11-19 05:45:05 -03:00
|
|
|
|
|
2022-08-15 20:20:49 -03:00
|
|
|
|
BigIntConstructor::BigIntConstructor(Realm& realm)
|
2023-04-12 19:47:15 -03:00
|
|
|
|
: NativeFunction(realm.vm().names.BigInt.as_string(), realm.intrinsics().function_prototype())
|
2020-06-05 21:14:10 -03:00
|
|
|
|
{
|
2020-06-20 10:40:48 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
|
void BigIntConstructor::initialize(Realm& realm)
|
2020-06-20 10:40:48 -03:00
|
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
|
auto& vm = this->vm();
|
2023-08-07 03:41:28 -03:00
|
|
|
|
Base::initialize(realm);
|
2021-06-12 20:22:35 -03:00
|
|
|
|
|
|
|
|
|
|
// 21.2.2.3 BigInt.prototype, https://tc39.es/ecma262/#sec-bigint.prototype
|
2022-08-26 20:54:55 -03:00
|
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().bigint_prototype(), 0);
|
2021-06-12 20:22:35 -03:00
|
|
|
|
|
2022-02-05 22:10:52 -03:00
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2022-08-22 17:47:35 -03: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-07 20:49:53 -03:00
|
|
|
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-06-05 21:14:10 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
|
// 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
|
2021-10-20 17:16:30 -03:00
|
|
|
|
ThrowCompletionOr<Value> BigIntConstructor::call()
|
2020-06-05 21:14:10 -03:00
|
|
|
|
{
|
2021-07-07 20:30:56 -03:00
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
|
|
auto value = vm.argument(0);
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Let prim be ? ToPrimitive(value, number).
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto primitive = TRY(value.to_primitive(vm, Value::PreferredType::Number));
|
2021-07-07 20:30:56 -03:00
|
|
|
|
|
|
|
|
|
|
// 3. If Type(prim) is Number, return ? NumberToBigInt(prim).
|
|
|
|
|
|
if (primitive.is_number())
|
2022-08-21 16:38:35 -03:00
|
|
|
|
return TRY(number_to_bigint(vm, primitive));
|
2021-07-07 20:30:56 -03:00
|
|
|
|
|
2022-08-03 16:33:02 -03:00
|
|
|
|
// 4. Otherwise, return ? ToBigInt(prim).
|
2022-08-21 10:00:56 -03:00
|
|
|
|
return TRY(primitive.to_bigint(vm));
|
2020-06-05 21:14:10 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
|
// 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
|
2024-11-14 12:01:23 -03:00
|
|
|
|
ThrowCompletionOr<GC::Ref<Object>> BigIntConstructor::construct(FunctionObject&)
|
2020-06-05 21:14:10 -03:00
|
|
|
|
{
|
2022-08-16 16:33:17 -03:00
|
|
|
|
return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "BigInt");
|
2020-06-05 21:14:10 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
|
// 21.2.2.1 BigInt.asIntN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asintn
|
2021-10-28 18:13:55 -03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
|
2020-06-05 21:14:10 -03:00
|
|
|
|
{
|
2022-02-05 22:10:52 -03:00
|
|
|
|
// 1. Set bits to ? ToIndex(bits).
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto bits = TRY(vm.argument(0).to_index(vm));
|
2022-02-05 22:10:52 -03:00
|
|
|
|
|
|
|
|
|
|
// 2. Set bigint to ? ToBigInt(bigint).
|
2023-04-13 10:31:53 -03:00
|
|
|
|
auto bigint = TRY(vm.argument(1).to_bigint(vm));
|
2022-02-05 22:10:52 -03:00
|
|
|
|
|
2025-03-18 18:31:39 -03:00
|
|
|
|
// OPTIMIZATION: mod = bigint (mod 2^0) = 0 < 2^(0-1) = 0.5
|
|
|
|
|
|
if (bits == 0)
|
2025-04-25 15:54:45 -03:00
|
|
|
|
return BigInt::create(vm, 0);
|
2025-03-18 18:31:39 -03:00
|
|
|
|
|
2026-05-21 10:48:23 -03:00
|
|
|
|
// OPTIMIZATION: This condition guarantees bigint is within the signed bits-bit range, so steps 3-5 return bigint.
|
|
|
|
|
|
if (bigint->big_integer().is_negative()
|
|
|
|
|
|
&& bigint->big_integer().unsigned_value().one_based_index_of_highest_set_bit() < bits) {
|
|
|
|
|
|
return bigint;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-05 22:10:52 -03:00
|
|
|
|
// 3. Let mod be ℝ(bigint) modulo 2^bits.
|
2025-03-18 18:31:39 -03:00
|
|
|
|
auto const mod = TRY_OR_THROW_OOM(vm, bigint->big_integer().mod_power_of_two(bits));
|
|
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: mod < 2^(bits-1)
|
|
|
|
|
|
if (mod.is_zero())
|
2025-04-25 15:54:45 -03:00
|
|
|
|
return BigInt::create(vm, 0);
|
2025-03-18 18:31:39 -03:00
|
|
|
|
|
|
|
|
|
|
// 4. If mod ≥ 2^(bits-1), return ℤ(mod - 2^bits); ...
|
|
|
|
|
|
if (auto top_bit_index = mod.unsigned_value().one_based_index_of_highest_set_bit(); top_bit_index >= bits) {
|
|
|
|
|
|
// twos complement decode
|
2025-04-25 15:54:45 -03:00
|
|
|
|
auto decoded = TRY_OR_THROW_OOM(vm, mod.unsigned_value().bitwise_not_fill_to_one_based_index(bits)).plus(1);
|
2026-05-22 09:50:05 -03:00
|
|
|
|
return BigInt::create(vm, Crypto::SignedBigInteger { move(decoded), true });
|
2025-03-18 18:31:39 -03:00
|
|
|
|
}
|
2022-02-05 22:10:52 -03:00
|
|
|
|
|
2025-03-18 18:31:39 -03:00
|
|
|
|
// ... otherwise, return ℤ(mod).
|
2022-12-06 19:03:52 -03:00
|
|
|
|
return BigInt::create(vm, mod);
|
2020-06-05 21:14:10 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
|
// 21.2.2.2 BigInt.asUintN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asuintn
|
2021-10-28 18:13:55 -03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
|
2020-06-05 21:14:10 -03:00
|
|
|
|
{
|
2022-02-05 23:04:17 -03:00
|
|
|
|
// 1. Set bits to ? ToIndex(bits).
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto bits = TRY(vm.argument(0).to_index(vm));
|
2022-02-05 23:04:17 -03:00
|
|
|
|
|
|
|
|
|
|
// 2. Set bigint to ? ToBigInt(bigint).
|
2023-04-13 10:31:53 -03:00
|
|
|
|
auto bigint = TRY(vm.argument(1).to_bigint(vm));
|
2022-02-05 23:04:17 -03:00
|
|
|
|
|
2025-03-18 18:31:39 -03:00
|
|
|
|
// 3. Return the BigInt value that represents ℝ(bigint) modulo 2^bits.
|
|
|
|
|
|
auto const mod = TRY_OR_THROW_OOM(vm, bigint->big_integer().mod_power_of_two(bits));
|
|
|
|
|
|
|
|
|
|
|
|
return BigInt::create(vm, mod);
|
2020-06-05 21:14:10 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|