From e137e48a881846f0619498d705b38daf902dc583 Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Sat, 14 Mar 2020 22:14:13 +0000 Subject: [PATCH] Bug 1560038 - Use i64 for storing epoch instead of usize. r=emilio epoch is stored as double in ICU. Storing it as usize caused overflow on 32-bit Windows 7 which resulted in dates being formatted to epoch=0. This patch switches to use f64 on the Rust side. Depends on D66504 Differential Revision: https://phabricator.services.mozilla.com/D66872 --HG-- extra : moz-landing-system : lando --- intl/l10n/FluentBundle.cpp | 32 ++++++++++++++--------- intl/l10n/rust/fluent-ffi/src/builtins.rs | 6 ++--- intl/l10n/rust/fluent-ffi/src/bundle.rs | 3 +-- intl/l10n/rust/fluent-ffi/src/ffi.rs | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/intl/l10n/FluentBundle.cpp b/intl/l10n/FluentBundle.cpp index 411aebca5dfb..fb8f2bd8c703 100644 --- a/intl/l10n/FluentBundle.cpp +++ b/intl/l10n/FluentBundle.cpp @@ -78,8 +78,9 @@ already_AddRefed FluentBundle::Constructor( } if (!raw) { - aRv.ThrowInvalidStateError("Failed to create the FluentBundle. Check the " - "locales and pseudo strategy arguments."); + aRv.ThrowInvalidStateError( + "Failed to create the FluentBundle. Check the " + "locales and pseudo strategy arguments."); return nullptr; } @@ -177,8 +178,9 @@ void FluentBundle::FormatPattern(JSContext* aCx, const FluentPattern& aPattern, &argValues, &aRetVal, &errors); if (!succeeded) { - return aRv.ThrowInvalidStateError("Failed to format the FluentPattern. Likely the " - "pattern could not be retrieved from the bundle."); + return aRv.ThrowInvalidStateError( + "Failed to format the FluentPattern. Likely the " + "pattern could not be retrieved from the bundle."); } if (aErrors.WasPassed()) { @@ -208,8 +210,7 @@ ffi::RawNumberFormatter* FluentBuiltInNumberFormatterCreate( if (aOptions->style == ffi::FluentNumberStyleRaw::Currency) { UErrorCode ec = U_ZERO_ERROR; - formatter = - formatter.unit(icu::CurrencyUnit(aOptions->currency.get(), ec)); + formatter = formatter.unit(icu::CurrencyUnit(aOptions->currency.get(), ec)); MOZ_ASSERT(U_SUCCESS(ec), "Failed to format the currency unit."); } if (aOptions->style == ffi::FluentNumberStyleRaw::Percent) { @@ -234,14 +235,17 @@ ffi::RawNumberFormatter* FluentBuiltInNumberFormatterCreate( formatter.locale(aLocale->get()).clone().orphan()); } -uint8_t* FluentBuiltInNumberFormatterFormat(const ffi::RawNumberFormatter* aFormatter, - double input, uint32_t* aOutCount) { +uint8_t* FluentBuiltInNumberFormatterFormat( + const ffi::RawNumberFormatter* aFormatter, double input, + uint32_t* aOutCount) { auto formatter = - reinterpret_cast(aFormatter); + reinterpret_cast( + aFormatter); UErrorCode ec = U_ZERO_ERROR; icu::number::FormattedNumber result = formatter->formatDouble(input, ec); icu::UnicodeString str = result.toTempString(ec); - return reinterpret_cast(ToNewUTF8String(nsDependentSubstring(str.getBuffer(), str.length()), aOutCount)); + return reinterpret_cast(ToNewUTF8String( + nsDependentSubstring(str.getBuffer(), str.length()), aOutCount)); } void FluentBuiltInNumberFormatterDestroy(ffi::RawNumberFormatter* aFormatter) { @@ -297,14 +301,16 @@ ffi::RawDateTimeFormatter* FluentBuiltInDateTimeFormatterCreate( return reinterpret_cast(dtmf); } -uint8_t* FluentBuiltInDateTimeFormatterFormat(const ffi::RawDateTimeFormatter* aFormatter, - uintptr_t input, uint32_t* aOutCount) { +uint8_t* FluentBuiltInDateTimeFormatterFormat( + const ffi::RawDateTimeFormatter* aFormatter, double input, + uint32_t* aOutCount) { auto formatter = reinterpret_cast(aFormatter); UDate myDate = input; icu::UnicodeString str; formatter->format(myDate, str); - return reinterpret_cast(ToNewUTF8String(nsDependentSubstring(str.getBuffer(), str.length()), aOutCount)); + return reinterpret_cast(ToNewUTF8String( + nsDependentSubstring(str.getBuffer(), str.length()), aOutCount)); } void FluentBuiltInDateTimeFormatterDestroy( diff --git a/intl/l10n/rust/fluent-ffi/src/builtins.rs b/intl/l10n/rust/fluent-ffi/src/builtins.rs index 98fe1928b3e2..d0ebe4a3a74a 100644 --- a/intl/l10n/rust/fluent-ffi/src/builtins.rs +++ b/intl/l10n/rust/fluent-ffi/src/builtins.rs @@ -375,7 +375,7 @@ impl From<&FluentDateTimeOptions> for FluentDateTimeOptionsRaw { #[derive(Debug, PartialEq, Clone)] pub struct FluentDateTime { - epoch: usize, + epoch: f64, options: FluentDateTimeOptions, } @@ -406,7 +406,7 @@ impl std::fmt::Display for FluentDateTime { } impl FluentDateTime { - pub fn new(epoch: usize, options: FluentDateTimeOptions) -> Self { + pub fn new(epoch: f64, options: FluentDateTimeOptions) -> Self { Self { epoch, options } } } @@ -436,7 +436,7 @@ impl DateTimeFormat { } } - pub fn format(&self, input: usize) -> String { + pub fn format(&self, input: f64) -> String { unsafe { let mut byte_count = 0; let buffer = ffi::FluentBuiltInDateTimeFormatterFormat( diff --git a/intl/l10n/rust/fluent-ffi/src/bundle.rs b/intl/l10n/rust/fluent-ffi/src/bundle.rs index b676ef2c693a..4cb477d933d5 100644 --- a/intl/l10n/rust/fluent-ffi/src/bundle.rs +++ b/intl/l10n/rust/fluent-ffi/src/bundle.rs @@ -118,8 +118,7 @@ pub unsafe extern "C" fn fluent_bundle_new( if let Some(FluentValue::Number(n)) = args.get(0) { let mut options = FluentDateTimeOptions::default(); options.merge(&named); - let epoch = n.value as usize; - FluentValue::Custom(Box::new(FluentDateTime::new(epoch, options))) + FluentValue::Custom(Box::new(FluentDateTime::new(n.value, options))) } else { FluentValue::None } diff --git a/intl/l10n/rust/fluent-ffi/src/ffi.rs b/intl/l10n/rust/fluent-ffi/src/ffi.rs index b59da29245e2..94d43d5ce035 100644 --- a/intl/l10n/rust/fluent-ffi/src/ffi.rs +++ b/intl/l10n/rust/fluent-ffi/src/ffi.rs @@ -146,7 +146,7 @@ extern "C" { ) -> *mut RawDateTimeFormatter; pub fn FluentBuiltInDateTimeFormatterFormat( formatter: *const RawDateTimeFormatter, - input: usize, + input: f64, out_count: &mut u32, ) -> *mut u8; pub fn FluentBuiltInDateTimeFormatterDestroy(formatter: *mut RawDateTimeFormatter);