Browse Source

LibJS: Port Value::to_bigint() to NonnullGCPtr

Linus Groh 2 năm trước cách đây
mục cha
commit
9279b0780d

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

@@ -69,7 +69,7 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
     auto bits = TRY(vm.argument(0).to_index(vm));
 
     // 2. Set bigint to ? ToBigInt(bigint).
-    auto* bigint = TRY(vm.argument(1).to_bigint(vm));
+    auto bigint = TRY(vm.argument(1).to_bigint(vm));
 
     // 3. Let mod be ℝ(bigint) modulo 2^bits.
     // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to
@@ -95,7 +95,7 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
     auto bits = TRY(vm.argument(0).to_index(vm));
 
     // 2. Set bigint to ? ToBigInt(bigint).
-    auto* bigint = TRY(vm.argument(1).to_bigint(vm));
+    auto bigint = TRY(vm.argument(1).to_bigint(vm));
 
     // 3. Return the BigInt value that represents ℝ(bigint) modulo 2bits.
     // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to

+ 7 - 7
Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp

@@ -56,14 +56,14 @@ ThrowCompletionOr<NonnullGCPtr<Object>> InstantConstructor::construct(FunctionOb
     auto& vm = this->vm();
 
     // 2. Let epochNanoseconds be ? ToBigInt(epochNanoseconds).
-    auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
+    auto epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
 
     // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
-    if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
+    if (!is_valid_epoch_nanoseconds(epoch_nanoseconds))
         return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
 
     // 4. Return ? CreateTemporalInstant(epochNanoseconds, NewTarget).
-    return *TRY(create_temporal_instant(vm, *epoch_nanoseconds, &new_target));
+    return *TRY(create_temporal_instant(vm, epoch_nanoseconds, &new_target));
 }
 
 // 8.2.2 Temporal.Instant.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.from
@@ -125,7 +125,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
 JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds)
 {
     // 1. Set epochMicroseconds to ? ToBigInt(epochMicroseconds).
-    auto* epoch_microseconds = TRY(vm.argument(0).to_bigint(vm));
+    auto epoch_microseconds = TRY(vm.argument(0).to_bigint(vm));
 
     // 2. Let epochNanoseconds be epochMicroseconds × 1000ℤ.
     auto epoch_nanoseconds = BigInt::create(vm, epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 }));
@@ -142,14 +142,14 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds)
 JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_nanoseconds)
 {
     // 1. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
-    auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
+    auto epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
 
     // 2. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
-    if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
+    if (!is_valid_epoch_nanoseconds(epoch_nanoseconds))
         return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
 
     // 3. Return ! CreateTemporalInstant(epochNanoseconds).
-    return MUST(create_temporal_instant(vm, *epoch_nanoseconds));
+    return MUST(create_temporal_instant(vm, epoch_nanoseconds));
 }
 
 // 8.2.7 Temporal.Instant.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.instant.compare

+ 3 - 3
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimeConstructor.cpp

@@ -55,10 +55,10 @@ ThrowCompletionOr<NonnullGCPtr<Object>> ZonedDateTimeConstructor::construct(Func
     auto& vm = this->vm();
 
     // 2. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
-    auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
+    auto epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
 
     // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
-    if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
+    if (!is_valid_epoch_nanoseconds(epoch_nanoseconds))
         return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
 
     // 4. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
@@ -68,7 +68,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> ZonedDateTimeConstructor::construct(Func
     auto* calendar = TRY(to_temporal_calendar_with_iso_default(vm, vm.argument(2)));
 
     // 6. Return ? CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar, NewTarget).
-    return *TRY(create_temporal_zoned_date_time(vm, *epoch_nanoseconds, *time_zone, *calendar, &new_target));
+    return *TRY(create_temporal_zoned_date_time(vm, epoch_nanoseconds, *time_zone, *calendar, &new_target));
 }
 
 // 6.2.2 Temporal.ZonedDateTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.from

+ 6 - 6
Userland/Libraries/LibJS/Runtime/Value.cpp

@@ -742,7 +742,7 @@ ThrowCompletionOr<Value> Value::to_number(VM& vm) const
 static Optional<BigInt*> string_to_bigint(VM& vm, StringView string);
 
 // 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint
-ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
+ThrowCompletionOr<NonnullGCPtr<BigInt>> Value::to_bigint(VM& vm) const
 {
     // 1. Let prim be ? ToPrimitive(argument, number).
     auto primitive = TRY(to_primitive(vm, PreferredType::Number));
@@ -768,12 +768,12 @@ ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
     case BOOLEAN_TAG: {
         // Return 1n if prim is true and 0n if prim is false.
         auto value = primitive.as_bool() ? 1 : 0;
-        return BigInt::create(vm, Crypto::SignedBigInteger { value }).ptr();
+        return BigInt::create(vm, Crypto::SignedBigInteger { value });
     }
     // BigInt
     case BIGINT_TAG:
         // Return prim.
-        return &primitive.as_bigint();
+        return primitive.as_bigint();
     case STRING_TAG: {
         // 1. Let n be ! StringToBigInt(prim).
         auto bigint = string_to_bigint(vm, TRY(primitive.as_string().deprecated_string()));
@@ -783,7 +783,7 @@ ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
             return vm.throw_completion<SyntaxError>(ErrorType::BigIntInvalidValue, primitive);
 
         // 3. Return n.
-        return bigint.release_value();
+        return *bigint.release_value();
     }
     // Symbol
     case SYMBOL_TAG:
@@ -866,7 +866,7 @@ static Optional<BigInt*> string_to_bigint(VM& vm, StringView string)
 ThrowCompletionOr<i64> Value::to_bigint_int64(VM& vm) const
 {
     // 1. Let n be ? ToBigInt(argument).
-    auto* bigint = TRY(to_bigint(vm));
+    auto bigint = TRY(to_bigint(vm));
 
     // 2. Let int64bit be ℝ(n) modulo 2^64.
     // 3. If int64bit ≥ 2^63, return ℤ(int64bit - 2^64); otherwise return ℤ(int64bit).
@@ -877,7 +877,7 @@ ThrowCompletionOr<i64> Value::to_bigint_int64(VM& vm) const
 ThrowCompletionOr<u64> Value::to_bigint_uint64(VM& vm) const
 {
     // 1. Let n be ? ToBigInt(argument).
-    auto* bigint = TRY(to_bigint(vm));
+    auto bigint = TRY(to_bigint(vm));
 
     // 2. Let int64bit be ℝ(n) modulo 2^64.
     // 3. Return ℤ(int64bit).

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

@@ -374,7 +374,7 @@ public:
     ThrowCompletionOr<NonnullGCPtr<Object>> to_object(VM&) const;
     ThrowCompletionOr<Value> to_numeric(VM&) const;
     ThrowCompletionOr<Value> to_number(VM&) const;
-    ThrowCompletionOr<BigInt*> to_bigint(VM&) const;
+    ThrowCompletionOr<NonnullGCPtr<BigInt>> to_bigint(VM&) const;
     ThrowCompletionOr<i64> to_bigint_int64(VM&) const;
     ThrowCompletionOr<u64> to_bigint_uint64(VM&) const;
     ThrowCompletionOr<double> to_double(VM&) const;