BigInt.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. NonnullGCPtr<BigInt> BigInt::create(VM& vm, Crypto::SignedBigInteger big_integer)
  12. {
  13. return *vm.heap().allocate_without_realm<BigInt>(move(big_integer));
  14. }
  15. BigInt::BigInt(Crypto::SignedBigInteger big_integer)
  16. : m_big_integer(move(big_integer))
  17. {
  18. VERIFY(!m_big_integer.is_invalid());
  19. }
  20. // 21.2.1.1.1 NumberToBigInt ( number ), https://tc39.es/ecma262/#sec-numbertobigint
  21. ThrowCompletionOr<BigInt*> number_to_bigint(VM& vm, Value number)
  22. {
  23. VERIFY(number.is_number());
  24. // 1. If IsIntegralNumber(number) is false, throw a RangeError exception.
  25. if (!number.is_integral_number())
  26. return vm.throw_completion<RangeError>(ErrorType::BigIntFromNonIntegral);
  27. // 2. Return the BigInt value that represents ℝ(number).
  28. return BigInt::create(vm, Crypto::SignedBigInteger { number.as_double() }).ptr();
  29. }
  30. }