2020-06-06 01:14:10 +01:00
|
|
|
|
/*
|
2022-08-16 20:33:17 +01:00
|
|
|
|
* Copyright (c) 2020-2022, 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
2020-09-27 20:18:30 +02:00
|
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2021-07-08 00:30:56 +01:00
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
BigInt::BigInt(Crypto::SignedBigInteger big_integer)
|
|
|
|
|
: m_big_integer(move(big_integer))
|
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY(!m_big_integer.is_invalid());
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BigInt* js_bigint(Heap& heap, Crypto::SignedBigInteger big_integer)
|
|
|
|
|
{
|
2022-08-16 00:20:50 +01:00
|
|
|
|
return heap.allocate_without_realm<BigInt>(move(big_integer));
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 00:14:48 +01:00
|
|
|
|
BigInt* js_bigint(VM& vm, Crypto::SignedBigInteger big_integer)
|
|
|
|
|
{
|
|
|
|
|
return js_bigint(vm.heap(), move(big_integer));
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 00:30:56 +01:00
|
|
|
|
// 21.2.1.1.1 NumberToBigInt ( number ), https://tc39.es/ecma262/#sec-numbertobigint
|
2022-08-21 20:38:35 +01:00
|
|
|
|
ThrowCompletionOr<BigInt*> number_to_bigint(VM& vm, Value number)
|
2021-07-08 00:30:56 +01:00
|
|
|
|
{
|
|
|
|
|
VERIFY(number.is_number());
|
|
|
|
|
|
|
|
|
|
// 1. If IsIntegralNumber(number) is false, throw a RangeError exception.
|
2021-10-23 03:26:55 +03:00
|
|
|
|
if (!number.is_integral_number())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::BigIntFromNonIntegral);
|
2021-07-08 00:30:56 +01:00
|
|
|
|
|
|
|
|
|
// 2. Return the BigInt value that represents ℝ(number).
|
2022-08-26 00:49:50 +02:00
|
|
|
|
return js_bigint(vm, Crypto::SignedBigInteger { number.as_double() });
|
2021-07-08 00:30:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|