Serenity Operating System
1/*
2 * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/DeprecatedString.h>
8#include <LibJS/Runtime/AbstractOperations.h>
9#include <LibJS/Runtime/BigInt.h>
10#include <LibJS/Runtime/BigIntConstructor.h>
11#include <LibJS/Runtime/BigIntObject.h>
12#include <LibJS/Runtime/Error.h>
13#include <LibJS/Runtime/GlobalObject.h>
14#include <LibJS/Runtime/VM.h>
15
16namespace JS {
17
18static const Crypto::SignedBigInteger BIGINT_ONE { 1 };
19
20BigIntConstructor::BigIntConstructor(Realm& realm)
21 : NativeFunction(realm.vm().names.BigInt.as_string(), *realm.intrinsics().function_prototype())
22{
23}
24
25ThrowCompletionOr<void> BigIntConstructor::initialize(Realm& realm)
26{
27 auto& vm = this->vm();
28 MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
29
30 // 21.2.2.3 BigInt.prototype, https://tc39.es/ecma262/#sec-bigint.prototype
31 define_direct_property(vm.names.prototype, realm.intrinsics().bigint_prototype(), 0);
32
33 u8 attr = Attribute::Writable | Attribute::Configurable;
34 define_native_function(realm, vm.names.asIntN, as_int_n, 2, attr);
35 define_native_function(realm, vm.names.asUintN, as_uint_n, 2, attr);
36
37 define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
38
39 return {};
40}
41
42// 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
43ThrowCompletionOr<Value> BigIntConstructor::call()
44{
45 auto& vm = this->vm();
46
47 auto value = vm.argument(0);
48
49 // 2. Let prim be ? ToPrimitive(value, number).
50 auto primitive = TRY(value.to_primitive(vm, Value::PreferredType::Number));
51
52 // 3. If Type(prim) is Number, return ? NumberToBigInt(prim).
53 if (primitive.is_number())
54 return TRY(number_to_bigint(vm, primitive));
55
56 // 4. Otherwise, return ? ToBigInt(prim).
57 return TRY(primitive.to_bigint(vm));
58}
59
60// 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
61ThrowCompletionOr<NonnullGCPtr<Object>> BigIntConstructor::construct(FunctionObject&)
62{
63 return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "BigInt");
64}
65
66// 21.2.2.1 BigInt.asIntN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asintn
67JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
68{
69 // 1. Set bits to ? ToIndex(bits).
70 auto bits = TRY(vm.argument(0).to_index(vm));
71
72 // 2. Set bigint to ? ToBigInt(bigint).
73 auto* bigint = TRY(vm.argument(1).to_bigint(vm));
74
75 // 3. Let mod be ℝ(bigint) modulo 2^bits.
76 // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to
77 // drop the most significant bits.
78 auto bits_shift_left = BIGINT_ONE.shift_left(bits);
79 auto mod = modulo(bigint->big_integer(), bits_shift_left);
80
81 // 4. If mod ≥ 2^(bits-1), return ℤ(mod - 2^bits); otherwise, return ℤ(mod).
82 // NOTE: Some of the below conditionals are non-standard, but are to protect SignedBigInteger from
83 // allocating an absurd amount of memory if `bits - 1` overflows to NumericLimits<size_t>::max.
84 if ((bits == 0) && (mod >= BIGINT_ONE))
85 return BigInt::create(vm, mod.minus(bits_shift_left));
86 if ((bits > 0) && (mod >= BIGINT_ONE.shift_left(bits - 1)))
87 return BigInt::create(vm, mod.minus(bits_shift_left));
88
89 return BigInt::create(vm, mod);
90}
91
92// 21.2.2.2 BigInt.asUintN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asuintn
93JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
94{
95 // 1. Set bits to ? ToIndex(bits).
96 auto bits = TRY(vm.argument(0).to_index(vm));
97
98 // 2. Set bigint to ? ToBigInt(bigint).
99 auto* bigint = TRY(vm.argument(1).to_bigint(vm));
100
101 // 3. Return the BigInt value that represents ℝ(bigint) modulo 2bits.
102 // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to
103 // drop the most significant bits.
104 return BigInt::create(vm, modulo(bigint->big_integer(), BIGINT_ONE.shift_left(bits)));
105}
106
107}