|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2020, Linus Groh <linusg@serenityos.org> |
| 2 | + * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org> |
3 | 3 | *
|
4 | 4 | * SPDX-License-Identifier: BSD-2-Clause
|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | #include <AK/String.h>
|
8 |
| -#include <LibCrypto/BigInt/SignedBigInteger.h> |
| 8 | +#include <LibJS/Runtime/BigInt.h> |
9 | 9 | #include <LibJS/Runtime/BigIntConstructor.h>
|
10 | 10 | #include <LibJS/Runtime/BigIntObject.h>
|
11 | 11 | #include <LibJS/Runtime/Error.h>
|
@@ -42,20 +42,22 @@ BigIntConstructor::~BigIntConstructor()
|
42 | 42 | // 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
|
43 | 43 | Value BigIntConstructor::call()
|
44 | 44 | {
|
45 |
| - auto primitive = vm().argument(0).to_primitive(global_object(), Value::PreferredType::Number); |
46 |
| - if (vm().exception()) |
47 |
| - return {}; |
48 |
| - if (primitive.is_number()) { |
49 |
| - if (!primitive.is_integral_number()) { |
50 |
| - vm().throw_exception<RangeError>(global_object(), ErrorType::BigIntIntArgument); |
51 |
| - return {}; |
52 |
| - } |
53 |
| - return js_bigint(heap(), Crypto::SignedBigInteger { primitive.as_i32() }); |
54 |
| - } |
55 |
| - auto* bigint = vm().argument(0).to_bigint(global_object()); |
56 |
| - if (vm().exception()) |
| 45 | + auto& vm = this->vm(); |
| 46 | + auto& global_object = this->global_object(); |
| 47 | + |
| 48 | + auto value = vm.argument(0); |
| 49 | + |
| 50 | + // 2. Let prim be ? ToPrimitive(value, number). |
| 51 | + auto primitive = value.to_primitive(global_object, Value::PreferredType::Number); |
| 52 | + if (vm.exception()) |
57 | 53 | return {};
|
58 |
| - return bigint; |
| 54 | + |
| 55 | + // 3. If Type(prim) is Number, return ? NumberToBigInt(prim). |
| 56 | + if (primitive.is_number()) |
| 57 | + return number_to_bigint(global_object, primitive); |
| 58 | + |
| 59 | + // 4. Otherwise, return ? ToBigInt(value). |
| 60 | + return value.to_bigint(global_object); |
59 | 61 | }
|
60 | 62 |
|
61 | 63 | // 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
|
|
0 commit comments