Просмотр исходного кода

LibJS: Convert the NumberToBigInt AO to ThrowCompletionOr

Idan Horowitz 3 лет назад
Родитель
Сommit
306c25f3c2

+ 3 - 5
Userland/Libraries/LibJS/Runtime/BigInt.cpp

@@ -32,16 +32,14 @@ BigInt* js_bigint(VM& vm, Crypto::SignedBigInteger big_integer)
 }
 
 // 21.2.1.1.1 NumberToBigInt ( number ), https://tc39.es/ecma262/#sec-numbertobigint
-BigInt* number_to_bigint(GlobalObject& global_object, Value number)
+ThrowCompletionOr<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 {};
-    }
+    if (!number.is_integral_number())
+        return vm.throw_completion<RangeError>(global_object, ErrorType::BigIntFromNonIntegral);
 
     // 2. Return the BigInt value that represents ℝ(number).
     return js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)number.as_double()));

+ 1 - 1
Userland/Libraries/LibJS/Runtime/BigInt.h

@@ -27,6 +27,6 @@ private:
 
 BigInt* js_bigint(Heap&, Crypto::SignedBigInteger);
 BigInt* js_bigint(VM&, Crypto::SignedBigInteger);
-BigInt* number_to_bigint(GlobalObject&, Value);
+ThrowCompletionOr<BigInt*> number_to_bigint(GlobalObject&, Value);
 
 }

+ 1 - 1
Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp

@@ -52,7 +52,7 @@ ThrowCompletionOr<Value> BigIntConstructor::call()
 
     // 3. If Type(prim) is Number, return ? NumberToBigInt(prim).
     if (primitive.is_number())
-        return number_to_bigint(global_object, primitive);
+        return TRY(number_to_bigint(global_object, primitive));
 
     // 4. Otherwise, return ? ToBigInt(value).
     return TRY(value.to_bigint(global_object));

+ 1 - 3
Userland/Libraries/LibJS/Runtime/DatePrototype.cpp

@@ -753,9 +753,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(DatePrototype::to_temporal_instant)
     auto t = this_object->value_of();
 
     // 2. Let ns be ? NumberToBigInt(t) × 10^6.
-    auto* ns = number_to_bigint(global_object, t);
-    if (vm.exception())
-        return {};
+    auto* ns = TRY_OR_DISCARD(number_to_bigint(global_object, t));
     ns = js_bigint(vm, ns->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));
 
     // 3. Return ! CreateTemporalInstant(ns).

+ 2 - 6
Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp

@@ -87,9 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
     auto epoch_seconds_value = TRY(vm.argument(0).to_number(global_object));
 
     // 2. Set epochSeconds to ? NumberToBigInt(epochSeconds).
-    auto* epoch_seconds = number_to_bigint(global_object, epoch_seconds_value);
-    if (auto* exception = vm.exception())
-        return throw_completion(exception->value());
+    auto* epoch_seconds = TRY(number_to_bigint(global_object, epoch_seconds_value));
 
     // 3. Let epochNanoseconds be epochSeconds × 10^9ℤ.
     auto* epoch_nanoseconds = js_bigint(vm, epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 }));
@@ -109,9 +107,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
     auto epoch_milliseconds_value = TRY(vm.argument(0).to_number(global_object));
 
     // 2. Set epochMilliseconds to ? NumberToBigInt(epochMilliseconds).
-    auto* epoch_milliseconds = number_to_bigint(global_object, epoch_milliseconds_value);
-    if (auto* exception = vm.exception())
-        return throw_completion(exception->value());
+    auto* epoch_milliseconds = TRY(number_to_bigint(global_object, epoch_milliseconds_value));
 
     // 3. Let epochNanoseconds be epochMilliseconds × 10^6ℤ.
     auto* epoch_nanoseconds = js_bigint(vm, epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }));