Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibJS/Runtime/BigInt.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibCrypto/BigInt/SignedBigInteger.h>
8
#include <LibJS/Heap/Heap.h>
9
#include <LibJS/Runtime/BigInt.h>
10
#include <LibJS/Runtime/GlobalObject.h>
11
12
namespace JS {
13
14
JS_DEFINE_ALLOCATOR(BigInt);
15
16
NonnullGCPtr<BigInt> BigInt::create(VM& vm, Crypto::SignedBigInteger big_integer)
17
0
{
18
0
    return vm.heap().allocate_without_realm<BigInt>(move(big_integer));
19
0
}
20
21
BigInt::BigInt(Crypto::SignedBigInteger big_integer)
22
0
    : m_big_integer(move(big_integer))
23
0
{
24
0
    VERIFY(!m_big_integer.is_invalid());
25
0
}
26
27
ErrorOr<String> BigInt::to_string() const
28
0
{
29
0
    return String::formatted("{}n", TRY(m_big_integer.to_base(10)));
30
0
}
31
32
// 21.2.1.1.1 NumberToBigInt ( number ), https://tc39.es/ecma262/#sec-numbertobigint
33
ThrowCompletionOr<BigInt*> number_to_bigint(VM& vm, Value number)
34
0
{
35
0
    VERIFY(number.is_number());
36
37
    // 1. If IsIntegralNumber(number) is false, throw a RangeError exception.
38
0
    if (!number.is_integral_number())
39
0
        return vm.throw_completion<RangeError>(ErrorType::BigIntFromNonIntegral);
40
41
    // 2. Return the BigInt value that represents ℝ(number).
42
0
    return BigInt::create(vm, Crypto::SignedBigInteger { number.as_double() }).ptr();
43
0
}
44
45
}