BigInt.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCrypto/BigInt/SignedBigInteger.h>
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/BigInt.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS {
  11. BigInt::BigInt(Crypto::SignedBigInteger big_integer)
  12. : m_big_integer(move(big_integer))
  13. {
  14. VERIFY(!m_big_integer.is_invalid());
  15. }
  16. BigInt* js_bigint(Heap& heap, Crypto::SignedBigInteger big_integer)
  17. {
  18. return heap.allocate_without_realm<BigInt>(move(big_integer));
  19. }
  20. BigInt* js_bigint(VM& vm, Crypto::SignedBigInteger big_integer)
  21. {
  22. return js_bigint(vm.heap(), move(big_integer));
  23. }
  24. // 21.2.1.1.1 NumberToBigInt ( number ), https://tc39.es/ecma262/#sec-numbertobigint
  25. ThrowCompletionOr<BigInt*> number_to_bigint(VM& vm, Value number)
  26. {
  27. VERIFY(number.is_number());
  28. // 1. If IsIntegralNumber(number) is false, throw a RangeError exception.
  29. if (!number.is_integral_number())
  30. return vm.throw_completion<RangeError>(ErrorType::BigIntFromNonIntegral);
  31. // 2. Return the BigInt value that represents ℝ(number).
  32. return js_bigint(vm, Crypto::SignedBigInteger { number.as_double() });
  33. }
  34. }