mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 22:12:44 +00:00 
			
		
		
		
	 f58891ed42
			
		
	
	
		f58891ed42
		
	
	
	
	
		
			
			We already have js_string(VM&, ...) and js_symbol(VM&, ...) overloads, so feels very familiar.
		
			
				
	
	
		
			50 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | ||
|  * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
 | ||
|  *
 | ||
|  * SPDX-License-Identifier: BSD-2-Clause
 | ||
|  */
 | ||
| 
 | ||
| #include <LibCrypto/BigInt/SignedBigInteger.h>
 | ||
| #include <LibJS/Heap/Heap.h>
 | ||
| #include <LibJS/Runtime/BigInt.h>
 | ||
| #include <LibJS/Runtime/GlobalObject.h>
 | ||
| 
 | ||
| namespace JS {
 | ||
| 
 | ||
| BigInt::BigInt(Crypto::SignedBigInteger big_integer)
 | ||
|     : m_big_integer(move(big_integer))
 | ||
| {
 | ||
|     VERIFY(!m_big_integer.is_invalid());
 | ||
| }
 | ||
| 
 | ||
| BigInt::~BigInt()
 | ||
| {
 | ||
| }
 | ||
| 
 | ||
| BigInt* js_bigint(Heap& heap, Crypto::SignedBigInteger big_integer)
 | ||
| {
 | ||
|     return heap.allocate_without_global_object<BigInt>(move(big_integer));
 | ||
| }
 | ||
| 
 | ||
| BigInt* js_bigint(VM& vm, Crypto::SignedBigInteger big_integer)
 | ||
| {
 | ||
|     return js_bigint(vm.heap(), move(big_integer));
 | ||
| }
 | ||
| 
 | ||
| // 21.2.1.1.1 NumberToBigInt ( number ), https://tc39.es/ecma262/#sec-numbertobigint
 | ||
| BigInt* number_to_bigint(GlobalObject& global_object, Value number)
 | ||
| {
 | ||
|     VERIFY(number.is_number());
 | ||
|     auto& vm = global_object.vm();
 | ||
| 
 | ||
|     // 1. If IsIntegralNumber(number) is false, throw a RangeError exception.
 | ||
|     if (!number.is_integral_number()) {
 | ||
|         vm.throw_exception<RangeError>(global_object, ErrorType::BigIntFromNonIntegral);
 | ||
|         return {};
 | ||
|     }
 | ||
| 
 | ||
|     // 2. Return the BigInt value that represents ℝ(number).
 | ||
|     return js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)number.as_double()));
 | ||
| }
 | ||
| 
 | ||
| }
 |