From 22862cb3452762dc0f8875b6570084992b6fb059 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Bargull?= Date: Mon, 15 Apr 2024 18:27:21 +0000 Subject: [PATCH] Bug 1874684 - Part 11: Remove no longer needed BigInt code path in TemporalDurationToString. r=sfink The value restrictions from the previous parts allow to remove the `BigInt` code paths from TemporalDurationToString. Differential Revision: https://phabricator.services.mozilla.com/D198545 --- js/src/builtin/temporal/Duration.cpp | 60 +++++++++++------------------------- js/src/builtin/temporal/Instant.cpp | 6 ---- js/src/builtin/temporal/Instant.h | 7 ----- 3 files changed, 18 insertions(+), 55 deletions(-) diff --git a/js/src/builtin/temporal/Duration.cpp b/js/src/builtin/temporal/Duration.cpp index 272c8031e94b..09e0b9c3a93f 100644 --- a/js/src/builtin/temporal/Duration.cpp +++ b/js/src/builtin/temporal/Duration.cpp @@ -56,7 +56,6 @@ #include "js/RootingAPI.h" #include "js/Value.h" #include "util/StringBuffer.h" -#include "vm/BigIntType.h" #include "vm/BytecodeUtil.h" #include "vm/GlobalObject.h" #include "vm/JSAtomState.h" @@ -2435,21 +2434,11 @@ bool js::temporal::AdjustRoundedDurationDays( timeZone, mozilla::SomeRef(precalculatedPlainDateTime), result); } -static bool BigIntToStringBuilder(JSContext* cx, Handle num, - JSStringBuilder& sb) { - MOZ_ASSERT(!num->isNegative()); - - JSLinearString* str = BigInt::toString(cx, num, 10); - if (!str) { - return false; - } - return sb.append(str); -} - -static bool NumberToStringBuilder(JSContext* cx, int64_t num, +static bool NumberToStringBuilder(JSContext* cx, double num, JSStringBuilder& sb) { + MOZ_ASSERT(IsInteger(num)); MOZ_ASSERT(num >= 0); - MOZ_ASSERT(num < int64_t(DOUBLE_INTEGRAL_PRECISION_LIMIT)); + MOZ_ASSERT(num < DOUBLE_INTEGRAL_PRECISION_LIMIT); ToCStringBuf cbuf; size_t length; @@ -2458,26 +2447,6 @@ static bool NumberToStringBuilder(JSContext* cx, int64_t num, return sb.append(numStr, length); } -static bool NumberToStringBuilder(JSContext* cx, double num, - JSStringBuilder& sb) { - MOZ_ASSERT(IsInteger(num)); - MOZ_ASSERT(num >= 0); - - if (num < DOUBLE_INTEGRAL_PRECISION_LIMIT) { - ToCStringBuf cbuf; - size_t length; - const char* numStr = NumberToCString(&cbuf, num, &length); - - return sb.append(numStr, length); - } - - Rooted bi(cx, BigInt::createFromDouble(cx, num)); - if (!bi) { - return false; - } - return BigIntToStringBuilder(cx, bi, sb); -} - static Duration AbsoluteDuration(const Duration& duration) { return { std::abs(duration.years), std::abs(duration.months), @@ -2554,19 +2523,26 @@ static JSString* TemporalDurationToString(JSContext* cx, MOZ_ASSERT(IsValidDuration(duration)); MOZ_ASSERT(precision != Precision::Minute()); + // Fast path for zero durations. + if (duration == Duration{} && + (precision == Precision::Auto() || precision.value() == 0)) { + return NewStringCopyZ(cx, "PT0S"); + } + // Convert to absolute values up front. This is okay to do, because when the // duration is valid, all components have the same sign. const auto& [years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds] = AbsoluteDuration(duration); - // Fast path for zero durations. - if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && - minutes == 0 && seconds == 0 && milliseconds == 0 && microseconds == 0 && - nanoseconds == 0 && - (precision == Precision::Auto() || precision.value() == 0)) { - return NewStringCopyZ(cx, "PT0S"); - } + // Years to seconds parts are all safe integers for valid durations. + MOZ_ASSERT(years < DOUBLE_INTEGRAL_PRECISION_LIMIT); + MOZ_ASSERT(months < DOUBLE_INTEGRAL_PRECISION_LIMIT); + MOZ_ASSERT(weeks < DOUBLE_INTEGRAL_PRECISION_LIMIT); + MOZ_ASSERT(days < DOUBLE_INTEGRAL_PRECISION_LIMIT); + MOZ_ASSERT(hours < DOUBLE_INTEGRAL_PRECISION_LIMIT); + MOZ_ASSERT(minutes < DOUBLE_INTEGRAL_PRECISION_LIMIT); + MOZ_ASSERT(seconds < DOUBLE_INTEGRAL_PRECISION_LIMIT); auto secondsDuration = NormalizeTimeDuration(0.0, 0.0, seconds, milliseconds, microseconds, nanoseconds); @@ -2667,7 +2643,7 @@ static JSString* TemporalDurationToString(JSContext* cx, // Step 12. if (hasSecondsPart) { // Step 12.a. - if (!NumberToStringBuilder(cx, secondsDuration.seconds, result)) { + if (!NumberToStringBuilder(cx, double(secondsDuration.seconds), result)) { return nullptr; } diff --git a/js/src/builtin/temporal/Instant.cpp b/js/src/builtin/temporal/Instant.cpp index f59be7347270..5f30980d755a 100644 --- a/js/src/builtin/temporal/Instant.cpp +++ b/js/src/builtin/temporal/Instant.cpp @@ -343,12 +343,6 @@ BigInt* js::temporal::ToEpochNanoseconds(JSContext* cx, return ::ToBigInt(cx, instant); } -BigInt* js::temporal::ToNanoseconds(JSContext* cx, - const NormalizedTimeDuration& duration) { - MOZ_ASSERT(IsValidNormalizedTimeDuration(duration)); - return ::ToBigInt(cx, duration); -} - /** * GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond, * microsecond, nanosecond [ , offsetNanoseconds ] ) diff --git a/js/src/builtin/temporal/Instant.h b/js/src/builtin/temporal/Instant.h index bad2bcff16a1..80137e03666d 100644 --- a/js/src/builtin/temporal/Instant.h +++ b/js/src/builtin/temporal/Instant.h @@ -88,13 +88,6 @@ Instant ToInstant(const JS::BigInt* epochNanoseconds); JS::BigInt* ToEpochNanoseconds(JSContext* cx, const Instant& instant); /** - * Convert a normalized time duration to a BigInt. The input must be a valid - * normalized time duration. - */ -JS::BigInt* ToNanoseconds(JSContext* cx, - const NormalizedTimeDuration& duration); - -/** * ToTemporalInstant ( item ) */ Wrapped ToTemporalInstant(JSContext* cx, -- 2.11.4.GIT