From 9279b0780d50baf6d393de93bbad531c110d9f04 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 13 Apr 2023 15:31:53 +0200 Subject: [PATCH] LibJS: Port Value::to_bigint() to NonnullGCPtr --- .../Libraries/LibJS/Runtime/BigIntConstructor.cpp | 4 ++-- .../LibJS/Runtime/Temporal/InstantConstructor.cpp | 14 +++++++------- .../Runtime/Temporal/ZonedDateTimeConstructor.cpp | 6 +++--- Userland/Libraries/LibJS/Runtime/Value.cpp | 12 ++++++------ Userland/Libraries/LibJS/Runtime/Value.h | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp b/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp index ace1b00b63..6c25896d40 100644 --- a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp +++ b/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 diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp index eda4be9c07..342317027d 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp @@ -56,14 +56,14 @@ ThrowCompletionOr> 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(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(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 diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimeConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimeConstructor.cpp index f03dfd453e..1b4a5808e0 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimeConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimeConstructor.cpp @@ -55,10 +55,10 @@ ThrowCompletionOr> 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(ErrorType::TemporalInvalidEpochNanoseconds); // 4. Let timeZone be ? ToTemporalTimeZone(timeZoneLike). @@ -68,7 +68,7 @@ ThrowCompletionOr> 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 diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 362fd94bbd..b89bdcf4c0 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -742,7 +742,7 @@ ThrowCompletionOr Value::to_number(VM& vm) const static Optional string_to_bigint(VM& vm, StringView string); // 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint -ThrowCompletionOr Value::to_bigint(VM& vm) const +ThrowCompletionOr> 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 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 Value::to_bigint(VM& vm) const return vm.throw_completion(ErrorType::BigIntInvalidValue, primitive); // 3. Return n. - return bigint.release_value(); + return *bigint.release_value(); } // Symbol case SYMBOL_TAG: @@ -866,7 +866,7 @@ static Optional string_to_bigint(VM& vm, StringView string) ThrowCompletionOr 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 Value::to_bigint_int64(VM& vm) const ThrowCompletionOr 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). diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 0139637cb1..88a7c85cb9 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -374,7 +374,7 @@ public: ThrowCompletionOr> to_object(VM&) const; ThrowCompletionOr to_numeric(VM&) const; ThrowCompletionOr to_number(VM&) const; - ThrowCompletionOr to_bigint(VM&) const; + ThrowCompletionOr> to_bigint(VM&) const; ThrowCompletionOr to_bigint_int64(VM&) const; ThrowCompletionOr to_bigint_uint64(VM&) const; ThrowCompletionOr to_double(VM&) const;