| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* Intl.DateTimeFormat implementation. */ |
| 8 |
|
| 9 |
#include "builtin/intl/DateTimeFormat.h" |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#include "mozilla/intl/Calendar.h" |
| 13 |
#include "mozilla/intl/DateIntervalFormat.h" |
| 14 |
#include "mozilla/intl/DateTimeFormat.h" |
| 15 |
#include "mozilla/intl/DateTimePart.h" |
| 16 |
#include "mozilla/intl/Locale.h" |
| 17 |
#include "mozilla/intl/TimeZone.h" |
| 18 |
#include "mozilla/Range.h" |
| 19 |
#include "mozilla/Span.h" |
| 20 |
|
| 21 |
#include "jsdate.h" |
| 22 |
|
| 23 |
#include "builtin/Array.h" |
| 24 |
#include "builtin/intl/CommonFunctions.h" |
| 25 |
#include "builtin/intl/FormatBuffer.h" |
| 26 |
#include "builtin/intl/LanguageTag.h" |
| 27 |
#include "builtin/intl/SharedIntlData.h" |
| 28 |
#include "builtin/temporal/Calendar.h" |
| 29 |
#include "builtin/temporal/Instant.h" |
| 30 |
#include "builtin/temporal/PlainDate.h" |
| 31 |
#include "builtin/temporal/PlainDateTime.h" |
| 32 |
#include "builtin/temporal/PlainMonthDay.h" |
| 33 |
#include "builtin/temporal/PlainTime.h" |
| 34 |
#include "builtin/temporal/PlainYearMonth.h" |
| 35 |
#include "builtin/temporal/Temporal.h" |
| 36 |
#include "builtin/temporal/TemporalParser.h" |
| 37 |
#include "builtin/temporal/TimeZone.h" |
| 38 |
#include "builtin/temporal/ZonedDateTime.h" |
| 39 |
#include "gc/GCContext.h" |
| 40 |
#include "js/Date.h" |
| 41 |
#include "js/experimental/Intl.h" // JS::AddMozDateTimeFormatConstructor |
| 42 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 43 |
#include "js/GCAPI.h" |
| 44 |
#include "js/PropertyAndElement.h" // JS_DefineFunctions, JS_DefineProperties |
| 45 |
#include "js/PropertySpec.h" |
| 46 |
#include "js/StableStringChars.h" |
| 47 |
#include "js/Wrapper.h" |
| 48 |
#include "vm/DateTime.h" |
| 49 |
#include "vm/GlobalObject.h" |
| 50 |
#include "vm/JSContext.h" |
| 51 |
#include "vm/PlainObject.h" // js::PlainObject |
| 52 |
#include "vm/Runtime.h" |
| 53 |
|
| 54 |
#include "vm/GeckoProfiler-inl.h" |
| 55 |
#include "vm/JSObject-inl.h" |
| 56 |
#include "vm/NativeObject-inl.h" |
| 57 |
|
| 58 |
using namespace js; |
| 59 |
using namespace js::temporal; |
| 60 |
|
| 61 |
using JS::AutoStableStringChars; |
| 62 |
using JS::ClippedTime; |
| 63 |
using JS::TimeClip; |
| 64 |
|
| 65 |
using js::intl::DateTimeFormatOptions; |
| 66 |
using js::intl::FormatBuffer; |
| 67 |
using js::intl::INITIAL_CHAR_BUFFER_SIZE; |
| 68 |
using js::intl::SharedIntlData; |
| 69 |
|
| 70 |
const JSClassOps DateTimeFormatObject::classOps_ = { |
| 71 |
nullptr, // addProperty |
| 72 |
nullptr, // delProperty |
| 73 |
nullptr, // enumerate |
| 74 |
nullptr, // newEnumerate |
| 75 |
nullptr, // resolve |
| 76 |
nullptr, // mayResolve |
| 77 |
DateTimeFormatObject::finalize, // finalize |
| 78 |
nullptr, // call |
| 79 |
nullptr, // construct |
| 80 |
nullptr, // trace |
| 81 |
}; |
| 82 |
|
| 83 |
const JSClass DateTimeFormatObject::class_ = { |
| 84 |
"Intl.DateTimeFormat", |
| 85 |
JSCLASS_HAS_RESERVED_SLOTS(DateTimeFormatObject::SLOT_COUNT) | |
| 86 |
JSCLASS_HAS_CACHED_PROTO(JSProto_DateTimeFormat) | |
| 87 |
JSCLASS_FOREGROUND_FINALIZE, |
| 88 |
&DateTimeFormatObject::classOps_, |
| 89 |
&DateTimeFormatObject::classSpec_, |
| 90 |
}; |
| 91 |
|
| 92 |
const JSClass& DateTimeFormatObject::protoClass_ = PlainObject::class_; |
| 93 |
|
| 94 |
static bool dateTimeFormat_toSource(JSContext* cx, unsigned argc, Value* vp) { |
| 95 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 96 |
args.rval().setString(cx->names().DateTimeFormat); |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
static const JSFunctionSpec dateTimeFormat_static_methods[] = { |
| 101 |
JS_SELF_HOSTED_FN("supportedLocalesOf", |
| 102 |
"Intl_DateTimeFormat_supportedLocalesOf", 1, 0), |
| 103 |
JS_FS_END, |
| 104 |
}; |
| 105 |
|
| 106 |
static const JSFunctionSpec dateTimeFormat_methods[] = { |
| 107 |
JS_SELF_HOSTED_FN("resolvedOptions", "Intl_DateTimeFormat_resolvedOptions", |
| 108 |
0, 0), |
| 109 |
JS_SELF_HOSTED_FN("formatToParts", "Intl_DateTimeFormat_formatToParts", 1, |
| 110 |
0), |
| 111 |
JS_SELF_HOSTED_FN("formatRange", "Intl_DateTimeFormat_formatRange", 2, 0), |
| 112 |
JS_SELF_HOSTED_FN("formatRangeToParts", |
| 113 |
"Intl_DateTimeFormat_formatRangeToParts", 2, 0), |
| 114 |
JS_FN("toSource", dateTimeFormat_toSource, 0, 0), |
| 115 |
JS_FS_END, |
| 116 |
}; |
| 117 |
|
| 118 |
static const JSPropertySpec dateTimeFormat_properties[] = { |
| 119 |
JS_SELF_HOSTED_GET("format", "$Intl_DateTimeFormat_format_get", 0), |
| 120 |
JS_STRING_SYM_PS(toStringTag, "Intl.DateTimeFormat", JSPROP_READONLY), |
| 121 |
JS_PS_END, |
| 122 |
}; |
| 123 |
|
| 124 |
static bool DateTimeFormat(JSContext* cx, unsigned argc, Value* vp); |
| 125 |
|
| 126 |
const ClassSpec DateTimeFormatObject::classSpec_ = { |
| 127 |
GenericCreateConstructor<DateTimeFormat, 0, gc::AllocKind::FUNCTION>, |
| 128 |
GenericCreatePrototype<DateTimeFormatObject>, |
| 129 |
dateTimeFormat_static_methods, |
| 130 |
nullptr, |
| 131 |
dateTimeFormat_methods, |
| 132 |
dateTimeFormat_properties, |
| 133 |
nullptr, |
| 134 |
ClassSpec::DontDefineConstructor, |
| 135 |
}; |
| 136 |
|
| 137 |
/** |
| 138 |
* 12.2.1 Intl.DateTimeFormat([ locales [, options]]) |
| 139 |
* |
| 140 |
* ES2017 Intl draft rev 94045d234762ad107a3d09bb6f7381a65f1a2f9b |
| 141 |
*/ |
| 142 |
static bool DateTimeFormat(JSContext* cx, const CallArgs& args, bool construct, |
| 143 |
HandleString required, HandleString defaults, |
| 144 |
DateTimeFormatOptions dtfOptions) { |
| 145 |
AutoJSConstructorProfilerEntry pseudoFrame(cx, "Intl.DateTimeFormat"); |
| 146 |
|
| 147 |
// Step 1 (Handled by OrdinaryCreateFromConstructor fallback code). |
| 148 |
|
| 149 |
// Step 2 (Inlined 9.1.14, OrdinaryCreateFromConstructor). |
| 150 |
JSProtoKey protoKey = dtfOptions == DateTimeFormatOptions::Standard |
| 151 |
? JSProto_DateTimeFormat |
| 152 |
: JSProto_Null; |
| 153 |
RootedObject proto(cx); |
| 154 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, protoKey, &proto)) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
Rooted<DateTimeFormatObject*> dateTimeFormat(cx); |
| 159 |
dateTimeFormat = NewObjectWithClassProto<DateTimeFormatObject>(cx, proto); |
| 160 |
if (!dateTimeFormat) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
RootedValue thisValue( |
| 165 |
cx, construct ? ObjectValue(*dateTimeFormat) : args.thisv()); |
| 166 |
HandleValue locales = args.get(0); |
| 167 |
HandleValue options = args.get(1); |
| 168 |
|
| 169 |
// Step 3. |
| 170 |
return intl::InitializeDateTimeFormatObject( |
| 171 |
cx, dateTimeFormat, thisValue, locales, options, required, defaults, |
| 172 |
UndefinedHandleValue, dtfOptions, args.rval()); |
| 173 |
} |
| 174 |
|
| 175 |
static bool DateTimeFormat(JSContext* cx, unsigned argc, Value* vp) { |
| 176 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 177 |
|
| 178 |
Handle<PropertyName*> required = cx->names().any; |
| 179 |
Handle<PropertyName*> defaults = cx->names().date; |
| 180 |
return DateTimeFormat(cx, args, args.isConstructing(), required, defaults, |
| 181 |
DateTimeFormatOptions::Standard); |
| 182 |
} |
| 183 |
|
| 184 |
static bool MozDateTimeFormat(JSContext* cx, unsigned argc, Value* vp) { |
| 185 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 186 |
|
| 187 |
// Don't allow to call mozIntl.DateTimeFormat as a function. That way we |
| 188 |
// don't need to worry how to handle the legacy initialization semantics |
| 189 |
// when applied on mozIntl.DateTimeFormat. |
| 190 |
if (!ThrowIfNotConstructing(cx, args, "mozIntl.DateTimeFormat")) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
Handle<PropertyName*> required = cx->names().any; |
| 195 |
Handle<PropertyName*> defaults = cx->names().date; |
| 196 |
return DateTimeFormat(cx, args, true, required, defaults, |
| 197 |
DateTimeFormatOptions::EnableMozExtensions); |
| 198 |
} |
| 199 |
|
| 200 |
bool js::intl_CreateDateTimeFormat(JSContext* cx, unsigned argc, Value* vp) { |
| 201 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 202 |
MOZ_ASSERT(args.length() == 4); |
| 203 |
MOZ_ASSERT(!args.isConstructing()); |
| 204 |
|
| 205 |
RootedString required(cx, args[2].toString()); |
| 206 |
RootedString defaults(cx, args[3].toString()); |
| 207 |
|
| 208 |
// intl_CreateDateTimeFormat is an intrinsic for self-hosted JavaScript, so it |
| 209 |
// cannot be used with "new", but it still has to be treated as a constructor. |
| 210 |
return DateTimeFormat(cx, args, true, required, defaults, |
| 211 |
DateTimeFormatOptions::Standard); |
| 212 |
} |
| 213 |
|
| 214 |
void js::DateTimeFormatObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 215 |
MOZ_ASSERT(gcx->onMainThread()); |
| 216 |
|
| 217 |
auto* dateTimeFormat = &obj->as<DateTimeFormatObject>(); |
| 218 |
mozilla::intl::DateTimeFormat* df = dateTimeFormat->getDateFormat(); |
| 219 |
mozilla::intl::DateIntervalFormat* dif = |
| 220 |
dateTimeFormat->getDateIntervalFormat(); |
| 221 |
|
| 222 |
if (df) { |
| 223 |
intl::RemoveICUCellMemory( |
| 224 |
gcx, obj, DateTimeFormatObject::UDateFormatEstimatedMemoryUse); |
| 225 |
|
| 226 |
delete df; |
| 227 |
} |
| 228 |
|
| 229 |
if (dif) { |
| 230 |
intl::RemoveICUCellMemory( |
| 231 |
gcx, obj, DateTimeFormatObject::UDateIntervalFormatEstimatedMemoryUse); |
| 232 |
|
| 233 |
delete dif; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
bool JS::AddMozDateTimeFormatConstructor(JSContext* cx, |
| 238 |
JS::Handle<JSObject*> intl) { |
| 239 |
RootedObject ctor( |
| 240 |
cx, GlobalObject::createConstructor(cx, MozDateTimeFormat, |
| 241 |
cx->names().DateTimeFormat, 0)); |
| 242 |
if (!ctor) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
RootedObject proto( |
| 247 |
cx, GlobalObject::createBlankPrototype<PlainObject>(cx, cx->global())); |
| 248 |
if (!proto) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
if (!LinkConstructorAndPrototype(cx, ctor, proto)) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
// 12.3.2 |
| 257 |
if (!JS_DefineFunctions(cx, ctor, dateTimeFormat_static_methods)) { |
| 258 |
return false; |
| 259 |
} |
| 260 |
|
| 261 |
// 12.4.4 and 12.4.5 |
| 262 |
if (!JS_DefineFunctions(cx, proto, dateTimeFormat_methods)) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
// 12.4.2 and 12.4.3 |
| 267 |
if (!JS_DefineProperties(cx, proto, dateTimeFormat_properties)) { |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
RootedValue ctorValue(cx, ObjectValue(*ctor)); |
| 272 |
return DefineDataProperty(cx, intl, cx->names().DateTimeFormat, ctorValue, 0); |
| 273 |
} |
| 274 |
|
| 275 |
static bool DefaultCalendar(JSContext* cx, const UniqueChars& locale, |
| 276 |
MutableHandleValue rval) { |
| 277 |
auto calendar = mozilla::intl::Calendar::TryCreate(locale.get()); |
| 278 |
if (calendar.isErr()) { |
| 279 |
intl::ReportInternalError(cx, calendar.unwrapErr()); |
| 280 |
return false; |
| 281 |
} |
| 282 |
|
| 283 |
auto type = calendar.unwrap()->GetBcp47Type(); |
| 284 |
if (type.isErr()) { |
| 285 |
intl::ReportInternalError(cx, type.unwrapErr()); |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
JSString* str = NewStringCopy<CanGC>(cx, type.unwrap()); |
| 290 |
if (!str) { |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
rval.setString(str); |
| 295 |
return true; |
| 296 |
} |
| 297 |
|
| 298 |
bool js::intl_availableCalendars(JSContext* cx, unsigned argc, Value* vp) { |
| 299 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 300 |
MOZ_ASSERT(args.length() == 1); |
| 301 |
MOZ_ASSERT(args[0].isString()); |
| 302 |
|
| 303 |
UniqueChars locale = intl::EncodeLocale(cx, args[0].toString()); |
| 304 |
if (!locale) { |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
RootedObject calendars(cx, NewDenseEmptyArray(cx)); |
| 309 |
if (!calendars) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
// We need the default calendar for the locale as the first result. |
| 314 |
RootedValue defaultCalendar(cx); |
| 315 |
if (!DefaultCalendar(cx, locale, &defaultCalendar)) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
if (!NewbornArrayPush(cx, calendars, defaultCalendar)) { |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
// Now get the calendars that "would make a difference", i.e., not the |
| 324 |
// default. |
| 325 |
auto keywords = |
| 326 |
mozilla::intl::Calendar::GetBcp47KeywordValuesForLocale(locale.get()); |
| 327 |
if (keywords.isErr()) { |
| 328 |
intl::ReportInternalError(cx, keywords.unwrapErr()); |
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
for (auto keyword : keywords.unwrap()) { |
| 333 |
if (keyword.isErr()) { |
| 334 |
intl::ReportInternalError(cx); |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
JSString* jscalendar = NewStringCopy<CanGC>(cx, keyword.unwrap()); |
| 339 |
if (!jscalendar) { |
| 340 |
return false; |
| 341 |
} |
| 342 |
if (!NewbornArrayPush(cx, calendars, StringValue(jscalendar))) { |
| 343 |
return false; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
args.rval().setObject(*calendars); |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
bool js::intl_defaultCalendar(JSContext* cx, unsigned argc, Value* vp) { |
| 352 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 353 |
MOZ_ASSERT(args.length() == 1); |
| 354 |
MOZ_ASSERT(args[0].isString()); |
| 355 |
|
| 356 |
UniqueChars locale = intl::EncodeLocale(cx, args[0].toString()); |
| 357 |
if (!locale) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
return DefaultCalendar(cx, locale, args.rval()); |
| 362 |
} |
| 363 |
|
| 364 |
bool js::intl_IsValidTimeZoneName(JSContext* cx, unsigned argc, Value* vp) { |
| 365 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 366 |
MOZ_ASSERT(args.length() == 1); |
| 367 |
MOZ_ASSERT(args[0].isString()); |
| 368 |
|
| 369 |
SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 370 |
|
| 371 |
RootedString timeZone(cx, args[0].toString()); |
| 372 |
Rooted<JSAtom*> validatedTimeZone(cx); |
| 373 |
if (!sharedIntlData.validateTimeZoneName(cx, timeZone, &validatedTimeZone)) { |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
if (validatedTimeZone) { |
| 378 |
cx->markAtom(validatedTimeZone); |
| 379 |
args.rval().setString(validatedTimeZone); |
| 380 |
} else { |
| 381 |
args.rval().setNull(); |
| 382 |
} |
| 383 |
return true; |
| 384 |
} |
| 385 |
|
| 386 |
JSLinearString* js::intl::CanonicalizeTimeZone(JSContext* cx, |
| 387 |
Handle<JSString*> timeZone) { |
| 388 |
SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 389 |
|
| 390 |
// Some time zone names are canonicalized differently by ICU -- handle those |
| 391 |
// first. |
| 392 |
Rooted<JSAtom*> ianaTimeZone(cx); |
| 393 |
if (!sharedIntlData.tryCanonicalizeTimeZoneConsistentWithIANA( |
| 394 |
cx, timeZone, &ianaTimeZone)) { |
| 395 |
return nullptr; |
| 396 |
} |
| 397 |
|
| 398 |
JSLinearString* resultTimeZone; |
| 399 |
if (ianaTimeZone) { |
| 400 |
cx->markAtom(ianaTimeZone); |
| 401 |
resultTimeZone = ianaTimeZone; |
| 402 |
} else { |
| 403 |
AutoStableStringChars stableChars(cx); |
| 404 |
if (!stableChars.initTwoByte(cx, timeZone)) { |
| 405 |
return nullptr; |
| 406 |
} |
| 407 |
|
| 408 |
// Call into ICU to canonicalize the time zone. |
| 409 |
FormatBuffer<char16_t, INITIAL_CHAR_BUFFER_SIZE> canonicalTimeZone(cx); |
| 410 |
auto result = mozilla::intl::TimeZone::GetCanonicalTimeZoneID( |
| 411 |
stableChars.twoByteRange(), canonicalTimeZone); |
| 412 |
if (result.isErr()) { |
| 413 |
ReportInternalError(cx, result.unwrapErr()); |
| 414 |
return nullptr; |
| 415 |
} |
| 416 |
|
| 417 |
resultTimeZone = canonicalTimeZone.toString(cx); |
| 418 |
if (!resultTimeZone) { |
| 419 |
return nullptr; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
MOZ_ASSERT(!StringEqualsLiteral(resultTimeZone, "Etc/Unknown"), |
| 424 |
"Invalid canonical time zone"); |
| 425 |
|
| 426 |
// Links to UTC are handled by SharedIntlData. |
| 427 |
MOZ_ASSERT(!StringEqualsLiteral(resultTimeZone, "GMT")); |
| 428 |
MOZ_ASSERT(!StringEqualsLiteral(resultTimeZone, "Etc/UTC")); |
| 429 |
MOZ_ASSERT(!StringEqualsLiteral(resultTimeZone, "Etc/GMT")); |
| 430 |
|
| 431 |
return resultTimeZone; |
| 432 |
} |
| 433 |
|
| 434 |
bool js::intl_canonicalizeTimeZone(JSContext* cx, unsigned argc, Value* vp) { |
| 435 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 436 |
MOZ_ASSERT(args.length() == 1); |
| 437 |
MOZ_ASSERT(args[0].isString()); |
| 438 |
|
| 439 |
RootedString timeZone(cx, args[0].toString()); |
| 440 |
auto* result = intl::CanonicalizeTimeZone(cx, timeZone); |
| 441 |
if (!result) { |
| 442 |
return false; |
| 443 |
} |
| 444 |
|
| 445 |
args.rval().setString(result); |
| 446 |
return true; |
| 447 |
} |
| 448 |
|
| 449 |
bool js::intl_defaultTimeZone(JSContext* cx, unsigned argc, Value* vp) { |
| 450 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 451 |
MOZ_ASSERT(args.length() == 0); |
| 452 |
|
| 453 |
FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> timeZone(cx); |
| 454 |
auto result = |
| 455 |
DateTimeInfo::timeZoneId(DateTimeInfo::forceUTC(cx->realm()), timeZone); |
| 456 |
if (result.isErr()) { |
| 457 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
JSString* str = timeZone.toString(cx); |
| 462 |
if (!str) { |
| 463 |
return false; |
| 464 |
} |
| 465 |
|
| 466 |
args.rval().setString(str); |
| 467 |
return true; |
| 468 |
} |
| 469 |
|
| 470 |
bool js::intl_defaultTimeZoneOffset(JSContext* cx, unsigned argc, Value* vp) { |
| 471 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 472 |
MOZ_ASSERT(args.length() == 0); |
| 473 |
|
| 474 |
auto offset = |
| 475 |
DateTimeInfo::getRawOffsetMs(DateTimeInfo::forceUTC(cx->realm())); |
| 476 |
if (offset.isErr()) { |
| 477 |
intl::ReportInternalError(cx, offset.unwrapErr()); |
| 478 |
return false; |
| 479 |
} |
| 480 |
|
| 481 |
args.rval().setInt32(offset.unwrap()); |
| 482 |
return true; |
| 483 |
} |
| 484 |
|
| 485 |
bool js::intl_isDefaultTimeZone(JSContext* cx, unsigned argc, Value* vp) { |
| 486 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 487 |
MOZ_ASSERT(args.length() == 1); |
| 488 |
MOZ_ASSERT(args[0].isString() || args[0].isUndefined()); |
| 489 |
|
| 490 |
// |undefined| is the default value when the Intl runtime caches haven't |
| 491 |
// yet been initialized. Handle it the same way as a cache miss. |
| 492 |
if (args[0].isUndefined()) { |
| 493 |
args.rval().setBoolean(false); |
| 494 |
return true; |
| 495 |
} |
| 496 |
|
| 497 |
FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> chars(cx); |
| 498 |
auto result = |
| 499 |
DateTimeInfo::timeZoneId(DateTimeInfo::forceUTC(cx->realm()), chars); |
| 500 |
if (result.isErr()) { |
| 501 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 502 |
return false; |
| 503 |
} |
| 504 |
|
| 505 |
JSLinearString* str = args[0].toString()->ensureLinear(cx); |
| 506 |
if (!str) { |
| 507 |
return false; |
| 508 |
} |
| 509 |
|
| 510 |
bool equals; |
| 511 |
if (str->length() == chars.length()) { |
| 512 |
JS::AutoCheckCannotGC nogc; |
| 513 |
equals = |
| 514 |
str->hasLatin1Chars() |
| 515 |
? EqualChars(str->latin1Chars(nogc), chars.data(), str->length()) |
| 516 |
: EqualChars(str->twoByteChars(nogc), chars.data(), str->length()); |
| 517 |
} else { |
| 518 |
equals = false; |
| 519 |
} |
| 520 |
|
| 521 |
args.rval().setBoolean(equals); |
| 522 |
return true; |
| 523 |
} |
| 524 |
|
| 525 |
enum class HourCycle { |
| 526 |
// 12 hour cycle, from 0 to 11. |
| 527 |
H11, |
| 528 |
|
| 529 |
// 12 hour cycle, from 1 to 12. |
| 530 |
H12, |
| 531 |
|
| 532 |
// 24 hour cycle, from 0 to 23. |
| 533 |
H23, |
| 534 |
|
| 535 |
// 24 hour cycle, from 1 to 24. |
| 536 |
H24 |
| 537 |
}; |
| 538 |
|
| 539 |
static UniqueChars DateTimeFormatLocale( |
| 540 |
JSContext* cx, HandleObject internals, |
| 541 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::HourCycle> hourCycle = |
| 542 |
mozilla::Nothing()) { |
| 543 |
RootedValue value(cx); |
| 544 |
if (!GetProperty(cx, internals, internals, cx->names().locale, &value)) { |
| 545 |
return nullptr; |
| 546 |
} |
| 547 |
|
| 548 |
// ICU expects calendar, numberingSystem, and hourCycle as Unicode locale |
| 549 |
// extensions on locale. |
| 550 |
|
| 551 |
mozilla::intl::Locale tag; |
| 552 |
{ |
| 553 |
Rooted<JSLinearString*> locale(cx, value.toString()->ensureLinear(cx)); |
| 554 |
if (!locale) { |
| 555 |
return nullptr; |
| 556 |
} |
| 557 |
|
| 558 |
if (!intl::ParseLocale(cx, locale, tag)) { |
| 559 |
return nullptr; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
JS::RootedVector<intl::UnicodeExtensionKeyword> keywords(cx); |
| 564 |
|
| 565 |
if (!GetProperty(cx, internals, internals, cx->names().calendar, &value)) { |
| 566 |
return nullptr; |
| 567 |
} |
| 568 |
|
| 569 |
{ |
| 570 |
JSLinearString* calendar = value.toString()->ensureLinear(cx); |
| 571 |
if (!calendar) { |
| 572 |
return nullptr; |
| 573 |
} |
| 574 |
|
| 575 |
if (!keywords.emplaceBack("ca", calendar)) { |
| 576 |
return nullptr; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
if (!GetProperty(cx, internals, internals, cx->names().numberingSystem, |
| 581 |
&value)) { |
| 582 |
return nullptr; |
| 583 |
} |
| 584 |
|
| 585 |
{ |
| 586 |
JSLinearString* numberingSystem = value.toString()->ensureLinear(cx); |
| 587 |
if (!numberingSystem) { |
| 588 |
return nullptr; |
| 589 |
} |
| 590 |
|
| 591 |
if (!keywords.emplaceBack("nu", numberingSystem)) { |
| 592 |
return nullptr; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
if (hourCycle) { |
| 597 |
JSAtom* hourCycleStr; |
| 598 |
switch (*hourCycle) { |
| 599 |
case mozilla::intl::DateTimeFormat::HourCycle::H11: |
| 600 |
hourCycleStr = cx->names().h11; |
| 601 |
break; |
| 602 |
case mozilla::intl::DateTimeFormat::HourCycle::H12: |
| 603 |
hourCycleStr = cx->names().h12; |
| 604 |
break; |
| 605 |
case mozilla::intl::DateTimeFormat::HourCycle::H23: |
| 606 |
hourCycleStr = cx->names().h23; |
| 607 |
break; |
| 608 |
case mozilla::intl::DateTimeFormat::HourCycle::H24: |
| 609 |
hourCycleStr = cx->names().h24; |
| 610 |
break; |
| 611 |
} |
| 612 |
|
| 613 |
if (!keywords.emplaceBack("hc", hourCycleStr)) { |
| 614 |
return nullptr; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
// |ApplyUnicodeExtensionToTag| applies the new keywords to the front of |
| 619 |
// the Unicode extension subtag. We're then relying on ICU to follow RFC |
| 620 |
// 6067, which states that any trailing keywords using the same key |
| 621 |
// should be ignored. |
| 622 |
if (!intl::ApplyUnicodeExtensionToTag(cx, tag, keywords)) { |
| 623 |
return nullptr; |
| 624 |
} |
| 625 |
|
| 626 |
FormatBuffer<char> buffer(cx); |
| 627 |
if (auto result = tag.ToString(buffer); result.isErr()) { |
| 628 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 629 |
return nullptr; |
| 630 |
} |
| 631 |
return buffer.extractStringZ(); |
| 632 |
} |
| 633 |
|
| 634 |
static bool AssignTextComponent( |
| 635 |
JSContext* cx, HandleObject internals, Handle<PropertyName*> property, |
| 636 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::Text>* text) { |
| 637 |
RootedValue value(cx); |
| 638 |
if (!GetProperty(cx, internals, internals, property, &value)) { |
| 639 |
return false; |
| 640 |
} |
| 641 |
|
| 642 |
if (value.isString()) { |
| 643 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 644 |
if (!string) { |
| 645 |
return false; |
| 646 |
} |
| 647 |
if (StringEqualsLiteral(string, "narrow")) { |
| 648 |
*text = mozilla::Some(mozilla::intl::DateTimeFormat::Text::Narrow); |
| 649 |
} else if (StringEqualsLiteral(string, "short")) { |
| 650 |
*text = mozilla::Some(mozilla::intl::DateTimeFormat::Text::Short); |
| 651 |
} else { |
| 652 |
MOZ_ASSERT(StringEqualsLiteral(string, "long")); |
| 653 |
*text = mozilla::Some(mozilla::intl::DateTimeFormat::Text::Long); |
| 654 |
} |
| 655 |
} else { |
| 656 |
MOZ_ASSERT(value.isUndefined()); |
| 657 |
} |
| 658 |
|
| 659 |
return true; |
| 660 |
} |
| 661 |
|
| 662 |
static bool AssignNumericComponent( |
| 663 |
JSContext* cx, HandleObject internals, Handle<PropertyName*> property, |
| 664 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::Numeric>* numeric) { |
| 665 |
RootedValue value(cx); |
| 666 |
if (!GetProperty(cx, internals, internals, property, &value)) { |
| 667 |
return false; |
| 668 |
} |
| 669 |
|
| 670 |
if (value.isString()) { |
| 671 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 672 |
if (!string) { |
| 673 |
return false; |
| 674 |
} |
| 675 |
if (StringEqualsLiteral(string, "numeric")) { |
| 676 |
*numeric = mozilla::Some(mozilla::intl::DateTimeFormat::Numeric::Numeric); |
| 677 |
} else { |
| 678 |
MOZ_ASSERT(StringEqualsLiteral(string, "2-digit")); |
| 679 |
*numeric = |
| 680 |
mozilla::Some(mozilla::intl::DateTimeFormat::Numeric::TwoDigit); |
| 681 |
} |
| 682 |
} else { |
| 683 |
MOZ_ASSERT(value.isUndefined()); |
| 684 |
} |
| 685 |
|
| 686 |
return true; |
| 687 |
} |
| 688 |
|
| 689 |
static bool AssignMonthComponent( |
| 690 |
JSContext* cx, HandleObject internals, Handle<PropertyName*> property, |
| 691 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::Month>* month) { |
| 692 |
RootedValue value(cx); |
| 693 |
if (!GetProperty(cx, internals, internals, property, &value)) { |
| 694 |
return false; |
| 695 |
} |
| 696 |
|
| 697 |
if (value.isString()) { |
| 698 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 699 |
if (!string) { |
| 700 |
return false; |
| 701 |
} |
| 702 |
if (StringEqualsLiteral(string, "numeric")) { |
| 703 |
*month = mozilla::Some(mozilla::intl::DateTimeFormat::Month::Numeric); |
| 704 |
} else if (StringEqualsLiteral(string, "2-digit")) { |
| 705 |
*month = mozilla::Some(mozilla::intl::DateTimeFormat::Month::TwoDigit); |
| 706 |
} else if (StringEqualsLiteral(string, "long")) { |
| 707 |
*month = mozilla::Some(mozilla::intl::DateTimeFormat::Month::Long); |
| 708 |
} else if (StringEqualsLiteral(string, "short")) { |
| 709 |
*month = mozilla::Some(mozilla::intl::DateTimeFormat::Month::Short); |
| 710 |
} else { |
| 711 |
MOZ_ASSERT(StringEqualsLiteral(string, "narrow")); |
| 712 |
*month = mozilla::Some(mozilla::intl::DateTimeFormat::Month::Narrow); |
| 713 |
} |
| 714 |
} else { |
| 715 |
MOZ_ASSERT(value.isUndefined()); |
| 716 |
} |
| 717 |
|
| 718 |
return true; |
| 719 |
} |
| 720 |
|
| 721 |
static bool AssignTimeZoneNameComponent( |
| 722 |
JSContext* cx, HandleObject internals, Handle<PropertyName*> property, |
| 723 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::TimeZoneName>* tzName) { |
| 724 |
RootedValue value(cx); |
| 725 |
if (!GetProperty(cx, internals, internals, property, &value)) { |
| 726 |
return false; |
| 727 |
} |
| 728 |
|
| 729 |
if (value.isString()) { |
| 730 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 731 |
if (!string) { |
| 732 |
return false; |
| 733 |
} |
| 734 |
if (StringEqualsLiteral(string, "long")) { |
| 735 |
*tzName = |
| 736 |
mozilla::Some(mozilla::intl::DateTimeFormat::TimeZoneName::Long); |
| 737 |
} else if (StringEqualsLiteral(string, "short")) { |
| 738 |
*tzName = |
| 739 |
mozilla::Some(mozilla::intl::DateTimeFormat::TimeZoneName::Short); |
| 740 |
} else if (StringEqualsLiteral(string, "shortOffset")) { |
| 741 |
*tzName = mozilla::Some( |
| 742 |
mozilla::intl::DateTimeFormat::TimeZoneName::ShortOffset); |
| 743 |
} else if (StringEqualsLiteral(string, "longOffset")) { |
| 744 |
*tzName = mozilla::Some( |
| 745 |
mozilla::intl::DateTimeFormat::TimeZoneName::LongOffset); |
| 746 |
} else if (StringEqualsLiteral(string, "shortGeneric")) { |
| 747 |
*tzName = mozilla::Some( |
| 748 |
mozilla::intl::DateTimeFormat::TimeZoneName::ShortGeneric); |
| 749 |
} else { |
| 750 |
MOZ_ASSERT(StringEqualsLiteral(string, "longGeneric")); |
| 751 |
*tzName = mozilla::Some( |
| 752 |
mozilla::intl::DateTimeFormat::TimeZoneName::LongGeneric); |
| 753 |
} |
| 754 |
} else { |
| 755 |
MOZ_ASSERT(value.isUndefined()); |
| 756 |
} |
| 757 |
|
| 758 |
return true; |
| 759 |
} |
| 760 |
|
| 761 |
static bool AssignHourCycleComponent( |
| 762 |
JSContext* cx, HandleObject internals, Handle<PropertyName*> property, |
| 763 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::HourCycle>* hourCycle) { |
| 764 |
RootedValue value(cx); |
| 765 |
if (!GetProperty(cx, internals, internals, property, &value)) { |
| 766 |
return false; |
| 767 |
} |
| 768 |
|
| 769 |
if (value.isString()) { |
| 770 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 771 |
if (!string) { |
| 772 |
return false; |
| 773 |
} |
| 774 |
if (StringEqualsLiteral(string, "h11")) { |
| 775 |
*hourCycle = mozilla::Some(mozilla::intl::DateTimeFormat::HourCycle::H11); |
| 776 |
} else if (StringEqualsLiteral(string, "h12")) { |
| 777 |
*hourCycle = mozilla::Some(mozilla::intl::DateTimeFormat::HourCycle::H12); |
| 778 |
} else if (StringEqualsLiteral(string, "h23")) { |
| 779 |
*hourCycle = mozilla::Some(mozilla::intl::DateTimeFormat::HourCycle::H23); |
| 780 |
} else { |
| 781 |
MOZ_ASSERT(StringEqualsLiteral(string, "h24")); |
| 782 |
*hourCycle = mozilla::Some(mozilla::intl::DateTimeFormat::HourCycle::H24); |
| 783 |
} |
| 784 |
} else { |
| 785 |
MOZ_ASSERT(value.isUndefined()); |
| 786 |
} |
| 787 |
|
| 788 |
return true; |
| 789 |
} |
| 790 |
|
| 791 |
static bool AssignHour12Component(JSContext* cx, HandleObject internals, |
| 792 |
mozilla::Maybe<bool>* hour12) { |
| 793 |
RootedValue value(cx); |
| 794 |
if (!GetProperty(cx, internals, internals, cx->names().hour12, &value)) { |
| 795 |
return false; |
| 796 |
} |
| 797 |
if (value.isBoolean()) { |
| 798 |
*hour12 = mozilla::Some(value.toBoolean()); |
| 799 |
} else { |
| 800 |
MOZ_ASSERT(value.isUndefined()); |
| 801 |
} |
| 802 |
|
| 803 |
return true; |
| 804 |
} |
| 805 |
|
| 806 |
static bool AssignDateTimeLength( |
| 807 |
JSContext* cx, HandleObject internals, Handle<PropertyName*> property, |
| 808 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::Style>* style) { |
| 809 |
RootedValue value(cx); |
| 810 |
if (!GetProperty(cx, internals, internals, property, &value)) { |
| 811 |
return false; |
| 812 |
} |
| 813 |
|
| 814 |
if (value.isString()) { |
| 815 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 816 |
if (!string) { |
| 817 |
return false; |
| 818 |
} |
| 819 |
if (StringEqualsLiteral(string, "full")) { |
| 820 |
*style = mozilla::Some(mozilla::intl::DateTimeFormat::Style::Full); |
| 821 |
} else if (StringEqualsLiteral(string, "long")) { |
| 822 |
*style = mozilla::Some(mozilla::intl::DateTimeFormat::Style::Long); |
| 823 |
} else if (StringEqualsLiteral(string, "medium")) { |
| 824 |
*style = mozilla::Some(mozilla::intl::DateTimeFormat::Style::Medium); |
| 825 |
} else { |
| 826 |
MOZ_ASSERT(StringEqualsLiteral(string, "short")); |
| 827 |
*style = mozilla::Some(mozilla::intl::DateTimeFormat::Style::Short); |
| 828 |
} |
| 829 |
} else { |
| 830 |
MOZ_ASSERT(value.isUndefined()); |
| 831 |
} |
| 832 |
|
| 833 |
return true; |
| 834 |
} |
| 835 |
|
| 836 |
enum class Required { Date, Time, YearMonth, MonthDay, Any }; |
| 837 |
|
| 838 |
enum class Defaults { Date, Time, YearMonth, MonthDay, ZonedDateTime, All }; |
| 839 |
|
| 840 |
enum class Inherit { All, Relevant }; |
| 841 |
|
| 842 |
struct DateTimeFormatArgs { |
| 843 |
Required required; |
| 844 |
Defaults defaults; |
| 845 |
Inherit inherit; |
| 846 |
}; |
| 847 |
|
| 848 |
/** |
| 849 |
* Get the "required" argument passed to CreateDateTimeFormat. |
| 850 |
*/ |
| 851 |
static bool GetRequired(JSContext* cx, Handle<JSObject*> internals, |
| 852 |
Required* result) { |
| 853 |
Rooted<Value> value(cx); |
| 854 |
if (!GetProperty(cx, internals, internals, cx->names().required, &value)) { |
| 855 |
return false; |
| 856 |
} |
| 857 |
MOZ_ASSERT(value.isString()); |
| 858 |
|
| 859 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 860 |
if (!string) { |
| 861 |
return false; |
| 862 |
} |
| 863 |
|
| 864 |
if (StringEqualsLiteral(string, "date")) { |
| 865 |
*result = Required::Date; |
| 866 |
} else if (StringEqualsLiteral(string, "time")) { |
| 867 |
*result = Required::Time; |
| 868 |
} else { |
| 869 |
MOZ_ASSERT(StringEqualsLiteral(string, "any")); |
| 870 |
*result = Required::Any; |
| 871 |
} |
| 872 |
return true; |
| 873 |
} |
| 874 |
|
| 875 |
/** |
| 876 |
* Get the "defaults" argument passed to CreateDateTimeFormat. |
| 877 |
*/ |
| 878 |
static bool GetDefaults(JSContext* cx, Handle<JSObject*> internals, |
| 879 |
Defaults* result) { |
| 880 |
Rooted<Value> value(cx); |
| 881 |
if (!GetProperty(cx, internals, internals, cx->names().defaults, &value)) { |
| 882 |
return false; |
| 883 |
} |
| 884 |
MOZ_ASSERT(value.isString()); |
| 885 |
|
| 886 |
JSLinearString* string = value.toString()->ensureLinear(cx); |
| 887 |
if (!string) { |
| 888 |
return false; |
| 889 |
} |
| 890 |
|
| 891 |
if (StringEqualsLiteral(string, "date")) { |
| 892 |
*result = Defaults::Date; |
| 893 |
} else if (StringEqualsLiteral(string, "time")) { |
| 894 |
*result = Defaults::Time; |
| 895 |
} else { |
| 896 |
MOZ_ASSERT(StringEqualsLiteral(string, "all")); |
| 897 |
*result = Defaults::All; |
| 898 |
} |
| 899 |
return true; |
| 900 |
} |
| 901 |
|
| 902 |
/** |
| 903 |
* Compute the (required, defaults, inherit) arguments passed to |
| 904 |
* GetDateTimeFormat. |
| 905 |
*/ |
| 906 |
static bool GetDateTimeFormatArgs(JSContext* cx, Handle<JSObject*> internals, |
| 907 |
DateTimeValueKind kind, |
| 908 |
DateTimeFormatArgs* result) { |
| 909 |
switch (kind) { |
| 910 |
case DateTimeValueKind::Number: { |
| 911 |
Required required; |
| 912 |
if (!GetRequired(cx, internals, &required)) { |
| 913 |
return false; |
| 914 |
} |
| 915 |
Defaults defaults; |
| 916 |
if (!GetDefaults(cx, internals, &defaults)) { |
| 917 |
return false; |
| 918 |
} |
| 919 |
*result = {required, defaults, Inherit::All}; |
| 920 |
return true; |
| 921 |
} |
| 922 |
case DateTimeValueKind::TemporalDate: |
| 923 |
*result = {Required::Date, Defaults::Date, Inherit::Relevant}; |
| 924 |
return true; |
| 925 |
case DateTimeValueKind::TemporalTime: |
| 926 |
*result = {Required::Time, Defaults::Time, Inherit::Relevant}; |
| 927 |
return true; |
| 928 |
case DateTimeValueKind::TemporalDateTime: |
| 929 |
*result = {Required::Any, Defaults::All, Inherit::Relevant}; |
| 930 |
return true; |
| 931 |
case DateTimeValueKind::TemporalYearMonth: |
| 932 |
*result = {Required::YearMonth, Defaults::YearMonth, Inherit::Relevant}; |
| 933 |
return true; |
| 934 |
case DateTimeValueKind::TemporalMonthDay: |
| 935 |
*result = {Required::MonthDay, Defaults::MonthDay, Inherit::Relevant}; |
| 936 |
return true; |
| 937 |
case DateTimeValueKind::TemporalZonedDateTime: |
| 938 |
*result = {Required::Any, Defaults::ZonedDateTime, Inherit::All}; |
| 939 |
return true; |
| 940 |
case DateTimeValueKind::TemporalInstant: |
| 941 |
*result = {Required::Any, Defaults::All, Inherit::All}; |
| 942 |
return true; |
| 943 |
} |
| 944 |
MOZ_CRASH("invalid date-time value kind"); |
| 945 |
} |
| 946 |
|
| 947 |
enum class DateTimeField { |
| 948 |
Weekday, |
| 949 |
Era, |
| 950 |
Year, |
| 951 |
Month, |
| 952 |
Day, |
| 953 |
DayPeriod, |
| 954 |
Hour, |
| 955 |
Minute, |
| 956 |
Second, |
| 957 |
FractionalSecondDigits, |
| 958 |
}; |
| 959 |
|
| 960 |
/** |
| 961 |
* GetDateTimeFormat ( formats, matcher, options, required, defaults, inherit ) |
| 962 |
* |
| 963 |
* https://tc39.es/proposal-temporal/#sec-getdatetimeformat |
| 964 |
*/ |
| 965 |
static mozilla::Maybe<mozilla::intl::DateTimeFormat::ComponentsBag> |
| 966 |
GetDateTimeFormat(const mozilla::intl::DateTimeFormat::ComponentsBag& options, |
| 967 |
Required required, Defaults defaults, Inherit inherit) { |
| 968 |
// Steps 1-5. |
| 969 |
mozilla::EnumSet<DateTimeField> requiredOptions; |
| 970 |
switch (required) { |
| 971 |
case Required::Date: |
| 972 |
requiredOptions = { |
| 973 |
DateTimeField::Weekday, |
| 974 |
DateTimeField::Year, |
| 975 |
DateTimeField::Month, |
| 976 |
DateTimeField::Day, |
| 977 |
}; |
| 978 |
break; |
| 979 |
case Required::Time: |
| 980 |
requiredOptions = { |
| 981 |
DateTimeField::DayPeriod, |
| 982 |
DateTimeField::Hour, |
| 983 |
DateTimeField::Minute, |
| 984 |
DateTimeField::Second, |
| 985 |
DateTimeField::FractionalSecondDigits, |
| 986 |
}; |
| 987 |
break; |
| 988 |
case Required::YearMonth: |
| 989 |
requiredOptions = { |
| 990 |
DateTimeField::Year, |
| 991 |
DateTimeField::Month, |
| 992 |
}; |
| 993 |
break; |
| 994 |
case Required::MonthDay: |
| 995 |
requiredOptions = { |
| 996 |
DateTimeField::Month, |
| 997 |
DateTimeField::Day, |
| 998 |
}; |
| 999 |
break; |
| 1000 |
case Required::Any: |
| 1001 |
requiredOptions = { |
| 1002 |
DateTimeField::Weekday, |
| 1003 |
DateTimeField::Year, |
| 1004 |
DateTimeField::Month, |
| 1005 |
DateTimeField::Day, |
| 1006 |
DateTimeField::DayPeriod, |
| 1007 |
DateTimeField::Hour, |
| 1008 |
DateTimeField::Minute, |
| 1009 |
DateTimeField::Second, |
| 1010 |
DateTimeField::FractionalSecondDigits, |
| 1011 |
}; |
| 1012 |
break; |
| 1013 |
} |
| 1014 |
MOZ_ASSERT(!requiredOptions.contains(DateTimeField::Era), |
| 1015 |
"standalone era not supported"); |
| 1016 |
|
| 1017 |
// Steps 6-10. |
| 1018 |
mozilla::EnumSet<DateTimeField> defaultOptions; |
| 1019 |
switch (defaults) { |
| 1020 |
case Defaults::Date: |
| 1021 |
defaultOptions = { |
| 1022 |
DateTimeField::Year, |
| 1023 |
DateTimeField::Month, |
| 1024 |
DateTimeField::Day, |
| 1025 |
}; |
| 1026 |
break; |
| 1027 |
case Defaults::Time: |
| 1028 |
defaultOptions = { |
| 1029 |
DateTimeField::Hour, |
| 1030 |
DateTimeField::Minute, |
| 1031 |
DateTimeField::Second, |
| 1032 |
}; |
| 1033 |
break; |
| 1034 |
case Defaults::YearMonth: |
| 1035 |
defaultOptions = { |
| 1036 |
DateTimeField::Year, |
| 1037 |
DateTimeField::Month, |
| 1038 |
}; |
| 1039 |
break; |
| 1040 |
case Defaults::MonthDay: |
| 1041 |
defaultOptions = { |
| 1042 |
DateTimeField::Month, |
| 1043 |
DateTimeField::Day, |
| 1044 |
}; |
| 1045 |
break; |
| 1046 |
case Defaults::ZonedDateTime: |
| 1047 |
case Defaults::All: |
| 1048 |
defaultOptions = { |
| 1049 |
DateTimeField::Year, DateTimeField::Month, DateTimeField::Day, |
| 1050 |
DateTimeField::Hour, DateTimeField::Minute, DateTimeField::Second, |
| 1051 |
}; |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
MOZ_ASSERT(!defaultOptions.contains(DateTimeField::Weekday)); |
| 1055 |
MOZ_ASSERT(!defaultOptions.contains(DateTimeField::Era)); |
| 1056 |
MOZ_ASSERT(!defaultOptions.contains(DateTimeField::DayPeriod)); |
| 1057 |
MOZ_ASSERT(!defaultOptions.contains(DateTimeField::FractionalSecondDigits)); |
| 1058 |
|
| 1059 |
// Steps 11-12. |
| 1060 |
mozilla::intl::DateTimeFormat::ComponentsBag formatOptions; |
| 1061 |
if (inherit == Inherit::All) { |
| 1062 |
// Step 11.a. |
| 1063 |
formatOptions = options; |
| 1064 |
} else { |
| 1065 |
// Step 12.a. (Implicit) |
| 1066 |
|
| 1067 |
// Step 12.b. |
| 1068 |
switch (required) { |
| 1069 |
case Required::Date: |
| 1070 |
case Required::YearMonth: |
| 1071 |
case Required::Any: |
| 1072 |
formatOptions.era = options.era; |
| 1073 |
break; |
| 1074 |
case Required::Time: |
| 1075 |
case Required::MonthDay: |
| 1076 |
// |era| option not applicable for these types. |
| 1077 |
break; |
| 1078 |
} |
| 1079 |
|
| 1080 |
// Step 12.c. |
| 1081 |
switch (required) { |
| 1082 |
case Required::Time: |
| 1083 |
case Required::Any: |
| 1084 |
formatOptions.hourCycle = options.hourCycle; |
| 1085 |
formatOptions.hour12 = options.hour12; |
| 1086 |
break; |
| 1087 |
case Required::Date: |
| 1088 |
case Required::YearMonth: |
| 1089 |
case Required::MonthDay: |
| 1090 |
// |hourCycle| and |hour12| options not applicable for these types. |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
// Steps 13-14. |
| 1096 |
// |
| 1097 |
// Ignore "era" to workaround a spec bug. |
| 1098 |
// |
| 1099 |
// FIXME: spec bug - https://github.com/tc39/proposal-temporal/issues/3049 |
| 1100 |
bool anyPresent = options.weekday || options.year || options.month || |
| 1101 |
options.day || options.dayPeriod || options.hour || |
| 1102 |
options.minute || options.second || |
| 1103 |
options.fractionalSecondDigits; |
| 1104 |
|
| 1105 |
// Step 15. |
| 1106 |
bool needDefaults = true; |
| 1107 |
|
| 1108 |
// Step 16. (Loop unrolled) |
| 1109 |
if (requiredOptions.contains(DateTimeField::Weekday) && options.weekday) { |
| 1110 |
formatOptions.weekday = options.weekday; |
| 1111 |
needDefaults = false; |
| 1112 |
} |
| 1113 |
if (requiredOptions.contains(DateTimeField::Year) && options.year) { |
| 1114 |
formatOptions.year = options.year; |
| 1115 |
needDefaults = false; |
| 1116 |
} |
| 1117 |
if (requiredOptions.contains(DateTimeField::Month) && options.month) { |
| 1118 |
formatOptions.month = options.month; |
| 1119 |
needDefaults = false; |
| 1120 |
} |
| 1121 |
if (requiredOptions.contains(DateTimeField::Day) && options.day) { |
| 1122 |
formatOptions.day = options.day; |
| 1123 |
needDefaults = false; |
| 1124 |
} |
| 1125 |
if (requiredOptions.contains(DateTimeField::DayPeriod) && options.dayPeriod) { |
| 1126 |
formatOptions.dayPeriod = options.dayPeriod; |
| 1127 |
needDefaults = false; |
| 1128 |
} |
| 1129 |
if (requiredOptions.contains(DateTimeField::Hour) && options.hour) { |
| 1130 |
formatOptions.hour = options.hour; |
| 1131 |
needDefaults = false; |
| 1132 |
} |
| 1133 |
if (requiredOptions.contains(DateTimeField::Minute) && options.minute) { |
| 1134 |
formatOptions.minute = options.minute; |
| 1135 |
needDefaults = false; |
| 1136 |
} |
| 1137 |
if (requiredOptions.contains(DateTimeField::Second) && options.second) { |
| 1138 |
formatOptions.second = options.second; |
| 1139 |
needDefaults = false; |
| 1140 |
} |
| 1141 |
if (requiredOptions.contains(DateTimeField::FractionalSecondDigits) && |
| 1142 |
options.fractionalSecondDigits) { |
| 1143 |
formatOptions.fractionalSecondDigits = options.fractionalSecondDigits; |
| 1144 |
needDefaults = false; |
| 1145 |
} |
| 1146 |
|
| 1147 |
// Step 17. |
| 1148 |
if (needDefaults) { |
| 1149 |
// Step 17.a. |
| 1150 |
if (anyPresent && inherit == Inherit::Relevant) { |
| 1151 |
return mozilla::Nothing(); |
| 1152 |
} |
| 1153 |
|
| 1154 |
// Step 17.b. (Loop unrolled) |
| 1155 |
auto numericOption = |
| 1156 |
mozilla::Some(mozilla::intl::DateTimeFormat::Numeric::Numeric); |
| 1157 |
if (defaultOptions.contains(DateTimeField::Year)) { |
| 1158 |
formatOptions.year = numericOption; |
| 1159 |
} |
| 1160 |
if (defaultOptions.contains(DateTimeField::Month)) { |
| 1161 |
formatOptions.month = |
| 1162 |
mozilla::Some(mozilla::intl::DateTimeFormat::Month::Numeric); |
| 1163 |
} |
| 1164 |
if (defaultOptions.contains(DateTimeField::Day)) { |
| 1165 |
formatOptions.day = numericOption; |
| 1166 |
} |
| 1167 |
if (defaultOptions.contains(DateTimeField::Hour)) { |
| 1168 |
formatOptions.hour = numericOption; |
| 1169 |
} |
| 1170 |
if (defaultOptions.contains(DateTimeField::Minute)) { |
| 1171 |
formatOptions.minute = numericOption; |
| 1172 |
} |
| 1173 |
if (defaultOptions.contains(DateTimeField::Second)) { |
| 1174 |
formatOptions.second = numericOption; |
| 1175 |
} |
| 1176 |
|
| 1177 |
// Step 17.c. |
| 1178 |
if (defaults == Defaults::ZonedDateTime && !formatOptions.timeZoneName) { |
| 1179 |
formatOptions.timeZoneName = |
| 1180 |
mozilla::Some(mozilla::intl::DateTimeFormat::TimeZoneName::Short); |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
// Steps 18-20. (Performed in caller). |
| 1185 |
|
| 1186 |
return mozilla::Some(formatOptions); |
| 1187 |
} |
| 1188 |
|
| 1189 |
/** |
| 1190 |
* AdjustDateTimeStyleFormat ( formats, baseFormat, matcher, allowedOptions ) |
| 1191 |
* |
| 1192 |
* https://tc39.es/proposal-temporal/#sec-adjustdatetimestyleformat |
| 1193 |
*/ |
| 1194 |
static mozilla::Result<mozilla::intl::DateTimeFormat::ComponentsBag, |
| 1195 |
mozilla::intl::ICUError> |
| 1196 |
AdjustDateTimeStyleFormat(mozilla::intl::DateTimeFormat* baseFormat, |
| 1197 |
mozilla::EnumSet<DateTimeField> allowedOptions) { |
| 1198 |
// Step 1. |
| 1199 |
mozilla::intl::DateTimeFormat::ComponentsBag formatOptions; |
| 1200 |
|
| 1201 |
// Step 2. (Loop unrolled) |
| 1202 |
auto result = baseFormat->ResolveComponents(); |
| 1203 |
if (result.isErr()) { |
| 1204 |
return result.propagateErr(); |
| 1205 |
} |
| 1206 |
auto options = result.unwrap(); |
| 1207 |
|
| 1208 |
if (allowedOptions.contains(DateTimeField::Era) && options.era) { |
| 1209 |
formatOptions.era = options.era; |
| 1210 |
} |
| 1211 |
if (allowedOptions.contains(DateTimeField::Weekday) && options.weekday) { |
| 1212 |
formatOptions.weekday = options.weekday; |
| 1213 |
} |
| 1214 |
if (allowedOptions.contains(DateTimeField::Year) && options.year) { |
| 1215 |
formatOptions.year = options.year; |
| 1216 |
} |
| 1217 |
if (allowedOptions.contains(DateTimeField::Month) && options.month) { |
| 1218 |
formatOptions.month = options.month; |
| 1219 |
} |
| 1220 |
if (allowedOptions.contains(DateTimeField::Day) && options.day) { |
| 1221 |
formatOptions.day = options.day; |
| 1222 |
} |
| 1223 |
if (allowedOptions.contains(DateTimeField::DayPeriod) && options.dayPeriod) { |
| 1224 |
formatOptions.dayPeriod = options.dayPeriod; |
| 1225 |
} |
| 1226 |
if (allowedOptions.contains(DateTimeField::Hour) && options.hour) { |
| 1227 |
formatOptions.hour = options.hour; |
| 1228 |
formatOptions.hourCycle = options.hourCycle; |
| 1229 |
} |
| 1230 |
if (allowedOptions.contains(DateTimeField::Minute) && options.minute) { |
| 1231 |
formatOptions.minute = options.minute; |
| 1232 |
} |
| 1233 |
if (allowedOptions.contains(DateTimeField::Second) && options.second) { |
| 1234 |
formatOptions.second = options.second; |
| 1235 |
} |
| 1236 |
if (allowedOptions.contains(DateTimeField::FractionalSecondDigits) && |
| 1237 |
options.fractionalSecondDigits) { |
| 1238 |
formatOptions.fractionalSecondDigits = options.fractionalSecondDigits; |
| 1239 |
} |
| 1240 |
|
| 1241 |
// Steps 3-5. (Performed in caller) |
| 1242 |
|
| 1243 |
return formatOptions; |
| 1244 |
} |
| 1245 |
|
| 1246 |
static const char* DateTimeValueKindToString(DateTimeValueKind kind) { |
| 1247 |
switch (kind) { |
| 1248 |
case DateTimeValueKind::Number: |
| 1249 |
return "number"; |
| 1250 |
case DateTimeValueKind::TemporalDate: |
| 1251 |
return "Temporal.PlainDate"; |
| 1252 |
case DateTimeValueKind::TemporalTime: |
| 1253 |
return "Temporal.PlainTime"; |
| 1254 |
case DateTimeValueKind::TemporalDateTime: |
| 1255 |
return "Temporal.PlainDateTime"; |
| 1256 |
case DateTimeValueKind::TemporalYearMonth: |
| 1257 |
return "Temporal.PlainYearMonth"; |
| 1258 |
case DateTimeValueKind::TemporalMonthDay: |
| 1259 |
return "Temporal.PlainMonthDay"; |
| 1260 |
case DateTimeValueKind::TemporalZonedDateTime: |
| 1261 |
return "Temporal.ZonedDateTime"; |
| 1262 |
case DateTimeValueKind::TemporalInstant: |
| 1263 |
return "Temporal.Instant"; |
| 1264 |
} |
| 1265 |
MOZ_CRASH("invalid date-time value kind"); |
| 1266 |
} |
| 1267 |
|
| 1268 |
class TimeZoneOffsetString { |
| 1269 |
static constexpr std::u16string_view GMT = u"GMT"; |
| 1270 |
|
| 1271 |
// Time zone offset string format is "±hh:mm". |
| 1272 |
static constexpr size_t offsetLength = 6; |
| 1273 |
|
| 1274 |
// ICU custom time zones are in the format "GMT±hh:mm". |
| 1275 |
char16_t timeZone_[GMT.size() + offsetLength] = {}; |
| 1276 |
|
| 1277 |
TimeZoneOffsetString() = default; |
| 1278 |
|
| 1279 |
public: |
| 1280 |
TimeZoneOffsetString(const TimeZoneOffsetString& other) { *this = other; } |
| 1281 |
|
| 1282 |
TimeZoneOffsetString& operator=(const TimeZoneOffsetString& other) { |
| 1283 |
std::copy_n(other.timeZone_, std::size(timeZone_), timeZone_); |
| 1284 |
return *this; |
| 1285 |
} |
| 1286 |
|
| 1287 |
operator mozilla::Span<const char16_t>() const { |
| 1288 |
return mozilla::Span(timeZone_); |
| 1289 |
} |
| 1290 |
|
| 1291 |
/** |
| 1292 |
* |timeZone| is either a canonical IANA time zone identifier or a normalized |
| 1293 |
* time zone offset string. |
| 1294 |
*/ |
| 1295 |
static mozilla::Maybe<TimeZoneOffsetString> from( |
| 1296 |
const JSLinearString* timeZone) { |
| 1297 |
MOZ_RELEASE_ASSERT(!timeZone->empty(), "time zone is a non-empty string"); |
| 1298 |
|
| 1299 |
// If the time zone string starts with either "+" or "-", it is a normalized |
| 1300 |
// time zone offset string, because (canonical) IANA time zone identifiers |
| 1301 |
// can't start with "+" or "-". |
| 1302 |
char16_t timeZoneSign = timeZone->latin1OrTwoByteChar(0); |
| 1303 |
MOZ_ASSERT(timeZoneSign != 0x2212, |
| 1304 |
"Minus sign is normalized to Ascii minus"); |
| 1305 |
if (timeZoneSign != '+' && timeZoneSign != '-') { |
| 1306 |
return mozilla::Nothing(); |
| 1307 |
} |
| 1308 |
|
| 1309 |
// Release assert because we don't want CopyChars to write out-of-bounds. |
| 1310 |
MOZ_RELEASE_ASSERT(timeZone->length() == offsetLength); |
| 1311 |
|
| 1312 |
// Self-hosted code has normalized offset strings to the format "±hh:mm". |
| 1313 |
MOZ_ASSERT(mozilla::IsAsciiDigit(timeZone->latin1OrTwoByteChar(1))); |
| 1314 |
MOZ_ASSERT(mozilla::IsAsciiDigit(timeZone->latin1OrTwoByteChar(2))); |
| 1315 |
MOZ_ASSERT(timeZone->latin1OrTwoByteChar(3) == ':'); |
| 1316 |
MOZ_ASSERT(mozilla::IsAsciiDigit(timeZone->latin1OrTwoByteChar(4))); |
| 1317 |
MOZ_ASSERT(mozilla::IsAsciiDigit(timeZone->latin1OrTwoByteChar(5))); |
| 1318 |
|
| 1319 |
// Self-hosted code has verified the offset is at most ±23:59. |
| 1320 |
#ifdef DEBUG |
| 1321 |
auto twoDigit = [&](size_t offset) { |
| 1322 |
auto c1 = timeZone->latin1OrTwoByteChar(offset); |
| 1323 |
auto c2 = timeZone->latin1OrTwoByteChar(offset + 1); |
| 1324 |
return mozilla::AsciiAlphanumericToNumber(c1) * 10 + |
| 1325 |
mozilla::AsciiAlphanumericToNumber(c2); |
| 1326 |
}; |
| 1327 |
|
| 1328 |
int32_t hours = twoDigit(1); |
| 1329 |
MOZ_ASSERT(0 <= hours && hours <= 23); |
| 1330 |
|
| 1331 |
int32_t minutes = twoDigit(4); |
| 1332 |
MOZ_ASSERT(0 <= minutes && minutes <= 59); |
| 1333 |
#endif |
| 1334 |
|
| 1335 |
TimeZoneOffsetString result{}; |
| 1336 |
|
| 1337 |
// Copy the string "GMT" followed by the offset string. |
| 1338 |
size_t copied = GMT.copy(result.timeZone_, GMT.size()); |
| 1339 |
CopyChars(result.timeZone_ + copied, *timeZone); |
| 1340 |
|
| 1341 |
return mozilla::Some(result); |
| 1342 |
} |
| 1343 |
}; |
| 1344 |
|
| 1345 |
/** |
| 1346 |
* Returns a new mozilla::intl::DateTimeFormat with the locale and date-time |
| 1347 |
* formatting options of the given DateTimeFormat. |
| 1348 |
*/ |
| 1349 |
static mozilla::intl::DateTimeFormat* NewDateTimeFormat( |
| 1350 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 1351 |
DateTimeValueKind kind) { |
| 1352 |
RootedValue value(cx); |
| 1353 |
|
| 1354 |
RootedObject internals(cx, intl::GetInternalsObject(cx, dateTimeFormat)); |
| 1355 |
if (!internals) { |
| 1356 |
return nullptr; |
| 1357 |
} |
| 1358 |
|
| 1359 |
UniqueChars locale = DateTimeFormatLocale(cx, internals); |
| 1360 |
if (!locale) { |
| 1361 |
return nullptr; |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (!GetProperty(cx, internals, internals, cx->names().timeZone, &value)) { |
| 1365 |
return nullptr; |
| 1366 |
} |
| 1367 |
|
| 1368 |
Rooted<JSLinearString*> timeZoneString(cx, |
| 1369 |
value.toString()->ensureLinear(cx)); |
| 1370 |
if (!timeZoneString) { |
| 1371 |
return nullptr; |
| 1372 |
} |
| 1373 |
|
| 1374 |
AutoStableStringChars timeZone(cx); |
| 1375 |
mozilla::Span<const char16_t> timeZoneChars{}; |
| 1376 |
|
| 1377 |
auto timeZoneOffset = TimeZoneOffsetString::from(timeZoneString); |
| 1378 |
if (timeZoneOffset) { |
| 1379 |
timeZoneChars = *timeZoneOffset; |
| 1380 |
} else { |
| 1381 |
if (!timeZone.initTwoByte(cx, timeZoneString)) { |
| 1382 |
return nullptr; |
| 1383 |
} |
| 1384 |
timeZoneChars = timeZone.twoByteRange(); |
| 1385 |
} |
| 1386 |
|
| 1387 |
if (!GetProperty(cx, internals, internals, cx->names().pattern, &value)) { |
| 1388 |
return nullptr; |
| 1389 |
} |
| 1390 |
bool hasPattern = value.isString(); |
| 1391 |
|
| 1392 |
if (!GetProperty(cx, internals, internals, cx->names().timeStyle, &value)) { |
| 1393 |
return nullptr; |
| 1394 |
} |
| 1395 |
bool hasStyle = value.isString(); |
| 1396 |
if (!hasStyle) { |
| 1397 |
if (!GetProperty(cx, internals, internals, cx->names().dateStyle, &value)) { |
| 1398 |
return nullptr; |
| 1399 |
} |
| 1400 |
hasStyle = value.isString(); |
| 1401 |
} |
| 1402 |
|
| 1403 |
mozilla::UniquePtr<mozilla::intl::DateTimeFormat> df = nullptr; |
| 1404 |
if (hasPattern) { |
| 1405 |
// This is a DateTimeFormat defined by a pattern option. This is internal |
| 1406 |
// to Mozilla, and not part of the ECMA-402 API. |
| 1407 |
if (!GetProperty(cx, internals, internals, cx->names().pattern, &value)) { |
| 1408 |
return nullptr; |
| 1409 |
} |
| 1410 |
|
| 1411 |
AutoStableStringChars pattern(cx); |
| 1412 |
if (!pattern.initTwoByte(cx, value.toString())) { |
| 1413 |
return nullptr; |
| 1414 |
} |
| 1415 |
|
| 1416 |
auto dfResult = mozilla::intl::DateTimeFormat::TryCreateFromPattern( |
| 1417 |
mozilla::MakeStringSpan(locale.get()), pattern.twoByteRange(), |
| 1418 |
mozilla::Some(timeZoneChars)); |
| 1419 |
if (dfResult.isErr()) { |
| 1420 |
intl::ReportInternalError(cx, dfResult.unwrapErr()); |
| 1421 |
return nullptr; |
| 1422 |
} |
| 1423 |
|
| 1424 |
df = dfResult.unwrap(); |
| 1425 |
} else if (hasStyle) { |
| 1426 |
// This is a DateTimeFormat defined by a time style or date style. |
| 1427 |
mozilla::intl::DateTimeFormat::StyleBag style; |
| 1428 |
if (!AssignDateTimeLength(cx, internals, cx->names().timeStyle, |
| 1429 |
&style.time)) { |
| 1430 |
return nullptr; |
| 1431 |
} |
| 1432 |
if (!AssignDateTimeLength(cx, internals, cx->names().dateStyle, |
| 1433 |
&style.date)) { |
| 1434 |
return nullptr; |
| 1435 |
} |
| 1436 |
if (!AssignHourCycleComponent(cx, internals, cx->names().hourCycle, |
| 1437 |
&style.hourCycle)) { |
| 1438 |
return nullptr; |
| 1439 |
} |
| 1440 |
|
| 1441 |
if (!AssignHour12Component(cx, internals, &style.hour12)) { |
| 1442 |
return nullptr; |
| 1443 |
} |
| 1444 |
|
| 1445 |
switch (kind) { |
| 1446 |
case DateTimeValueKind::TemporalDate: |
| 1447 |
case DateTimeValueKind::TemporalYearMonth: |
| 1448 |
case DateTimeValueKind::TemporalMonthDay: { |
| 1449 |
if (!style.date) { |
| 1450 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1451 |
JSMSG_INVALID_FORMAT_OPTIONS, |
| 1452 |
DateTimeValueKindToString(kind)); |
| 1453 |
return nullptr; |
| 1454 |
} |
| 1455 |
break; |
| 1456 |
} |
| 1457 |
|
| 1458 |
case DateTimeValueKind::TemporalTime: { |
| 1459 |
if (!style.time) { |
| 1460 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1461 |
JSMSG_INVALID_FORMAT_OPTIONS, |
| 1462 |
DateTimeValueKindToString(kind)); |
| 1463 |
return nullptr; |
| 1464 |
} |
| 1465 |
break; |
| 1466 |
} |
| 1467 |
|
| 1468 |
case DateTimeValueKind::Number: |
| 1469 |
case DateTimeValueKind::TemporalDateTime: |
| 1470 |
case DateTimeValueKind::TemporalZonedDateTime: |
| 1471 |
case DateTimeValueKind::TemporalInstant: |
| 1472 |
break; |
| 1473 |
} |
| 1474 |
|
| 1475 |
SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 1476 |
auto* dtpg = sharedIntlData.getDateTimePatternGenerator(cx, locale.get()); |
| 1477 |
if (!dtpg) { |
| 1478 |
return nullptr; |
| 1479 |
} |
| 1480 |
|
| 1481 |
auto dfResult = mozilla::intl::DateTimeFormat::TryCreateFromStyle( |
| 1482 |
mozilla::MakeStringSpan(locale.get()), style, dtpg, |
| 1483 |
mozilla::Some(timeZoneChars)); |
| 1484 |
if (dfResult.isErr()) { |
| 1485 |
intl::ReportInternalError(cx, dfResult.unwrapErr()); |
| 1486 |
return nullptr; |
| 1487 |
} |
| 1488 |
df = dfResult.unwrap(); |
| 1489 |
|
| 1490 |
mozilla::EnumSet<DateTimeField> allowedOptions; |
| 1491 |
switch (kind) { |
| 1492 |
case DateTimeValueKind::TemporalDate: |
| 1493 |
allowedOptions = { |
| 1494 |
DateTimeField::Weekday, DateTimeField::Era, DateTimeField::Year, |
| 1495 |
DateTimeField::Month, DateTimeField::Day, |
| 1496 |
}; |
| 1497 |
break; |
| 1498 |
case DateTimeValueKind::TemporalTime: |
| 1499 |
allowedOptions = { |
| 1500 |
DateTimeField::DayPeriod, |
| 1501 |
DateTimeField::Hour, |
| 1502 |
DateTimeField::Minute, |
| 1503 |
DateTimeField::Second, |
| 1504 |
DateTimeField::FractionalSecondDigits, |
| 1505 |
}; |
| 1506 |
break; |
| 1507 |
case DateTimeValueKind::TemporalDateTime: |
| 1508 |
allowedOptions = { |
| 1509 |
DateTimeField::Weekday, DateTimeField::Era, |
| 1510 |
DateTimeField::Year, DateTimeField::Month, |
| 1511 |
DateTimeField::Day, DateTimeField::DayPeriod, |
| 1512 |
DateTimeField::Hour, DateTimeField::Minute, |
| 1513 |
DateTimeField::Second, DateTimeField::FractionalSecondDigits, |
| 1514 |
}; |
| 1515 |
break; |
| 1516 |
case DateTimeValueKind::TemporalYearMonth: |
| 1517 |
allowedOptions = { |
| 1518 |
DateTimeField::Era, |
| 1519 |
DateTimeField::Year, |
| 1520 |
DateTimeField::Month, |
| 1521 |
}; |
| 1522 |
break; |
| 1523 |
case DateTimeValueKind::TemporalMonthDay: |
| 1524 |
allowedOptions = { |
| 1525 |
DateTimeField::Month, |
| 1526 |
DateTimeField::Day, |
| 1527 |
}; |
| 1528 |
break; |
| 1529 |
|
| 1530 |
case DateTimeValueKind::Number: |
| 1531 |
case DateTimeValueKind::TemporalZonedDateTime: |
| 1532 |
case DateTimeValueKind::TemporalInstant: |
| 1533 |
break; |
| 1534 |
} |
| 1535 |
|
| 1536 |
if (!allowedOptions.isEmpty()) { |
| 1537 |
auto adjusted = AdjustDateTimeStyleFormat(df.get(), allowedOptions); |
| 1538 |
if (adjusted.isErr()) { |
| 1539 |
intl::ReportInternalError(cx, dfResult.unwrapErr()); |
| 1540 |
return nullptr; |
| 1541 |
} |
| 1542 |
auto bag = adjusted.unwrap(); |
| 1543 |
|
| 1544 |
auto dfResult = mozilla::intl::DateTimeFormat::TryCreateFromComponents( |
| 1545 |
mozilla::MakeStringSpan(locale.get()), bag, dtpg, |
| 1546 |
mozilla::Some(timeZoneChars)); |
| 1547 |
if (dfResult.isErr()) { |
| 1548 |
intl::ReportInternalError(cx, dfResult.unwrapErr()); |
| 1549 |
return nullptr; |
| 1550 |
} |
| 1551 |
df = dfResult.unwrap(); |
| 1552 |
} |
| 1553 |
} else { |
| 1554 |
// This is a DateTimeFormat defined by a components bag. |
| 1555 |
mozilla::intl::DateTimeFormat::ComponentsBag bag; |
| 1556 |
|
| 1557 |
if (!AssignTextComponent(cx, internals, cx->names().era, &bag.era)) { |
| 1558 |
return nullptr; |
| 1559 |
} |
| 1560 |
if (!AssignNumericComponent(cx, internals, cx->names().year, &bag.year)) { |
| 1561 |
return nullptr; |
| 1562 |
} |
| 1563 |
if (!AssignMonthComponent(cx, internals, cx->names().month, &bag.month)) { |
| 1564 |
return nullptr; |
| 1565 |
} |
| 1566 |
if (!AssignNumericComponent(cx, internals, cx->names().day, &bag.day)) { |
| 1567 |
return nullptr; |
| 1568 |
} |
| 1569 |
if (!AssignTextComponent(cx, internals, cx->names().weekday, |
| 1570 |
&bag.weekday)) { |
| 1571 |
return nullptr; |
| 1572 |
} |
| 1573 |
if (!AssignNumericComponent(cx, internals, cx->names().hour, &bag.hour)) { |
| 1574 |
return nullptr; |
| 1575 |
} |
| 1576 |
if (!AssignNumericComponent(cx, internals, cx->names().minute, |
| 1577 |
&bag.minute)) { |
| 1578 |
return nullptr; |
| 1579 |
} |
| 1580 |
if (!AssignNumericComponent(cx, internals, cx->names().second, |
| 1581 |
&bag.second)) { |
| 1582 |
return nullptr; |
| 1583 |
} |
| 1584 |
if (!AssignTimeZoneNameComponent(cx, internals, cx->names().timeZoneName, |
| 1585 |
&bag.timeZoneName)) { |
| 1586 |
return nullptr; |
| 1587 |
} |
| 1588 |
if (!AssignHourCycleComponent(cx, internals, cx->names().hourCycle, |
| 1589 |
&bag.hourCycle)) { |
| 1590 |
return nullptr; |
| 1591 |
} |
| 1592 |
if (!AssignTextComponent(cx, internals, cx->names().dayPeriod, |
| 1593 |
&bag.dayPeriod)) { |
| 1594 |
return nullptr; |
| 1595 |
} |
| 1596 |
if (!AssignHour12Component(cx, internals, &bag.hour12)) { |
| 1597 |
return nullptr; |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (!GetProperty(cx, internals, internals, |
| 1601 |
cx->names().fractionalSecondDigits, &value)) { |
| 1602 |
return nullptr; |
| 1603 |
} |
| 1604 |
if (value.isInt32()) { |
| 1605 |
bag.fractionalSecondDigits = mozilla::Some(value.toInt32()); |
| 1606 |
} else { |
| 1607 |
MOZ_ASSERT(value.isUndefined()); |
| 1608 |
} |
| 1609 |
|
| 1610 |
DateTimeFormatArgs dateTimeFormatArgs; |
| 1611 |
if (!GetDateTimeFormatArgs(cx, internals, kind, &dateTimeFormatArgs)) { |
| 1612 |
return nullptr; |
| 1613 |
} |
| 1614 |
auto [required, defaults, inherit] = dateTimeFormatArgs; |
| 1615 |
|
| 1616 |
auto resolvedBag = GetDateTimeFormat(bag, required, defaults, inherit); |
| 1617 |
if (!resolvedBag) { |
| 1618 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1619 |
JSMSG_INVALID_FORMAT_OPTIONS, |
| 1620 |
DateTimeValueKindToString(kind)); |
| 1621 |
return nullptr; |
| 1622 |
} |
| 1623 |
bag = *resolvedBag; |
| 1624 |
|
| 1625 |
SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 1626 |
auto* dtpg = sharedIntlData.getDateTimePatternGenerator(cx, locale.get()); |
| 1627 |
if (!dtpg) { |
| 1628 |
return nullptr; |
| 1629 |
} |
| 1630 |
|
| 1631 |
auto dfResult = mozilla::intl::DateTimeFormat::TryCreateFromComponents( |
| 1632 |
mozilla::MakeStringSpan(locale.get()), bag, dtpg, |
| 1633 |
mozilla::Some(timeZoneChars)); |
| 1634 |
if (dfResult.isErr()) { |
| 1635 |
intl::ReportInternalError(cx, dfResult.unwrapErr()); |
| 1636 |
return nullptr; |
| 1637 |
} |
| 1638 |
df = dfResult.unwrap(); |
| 1639 |
} |
| 1640 |
|
| 1641 |
return df.release(); |
| 1642 |
} |
| 1643 |
|
| 1644 |
void js::DateTimeFormatObject::maybeClearCache(DateTimeValueKind kind) { |
| 1645 |
if (getDateTimeValueKind() == kind) { |
| 1646 |
return; |
| 1647 |
} |
| 1648 |
setDateTimeValueKind(kind); |
| 1649 |
|
| 1650 |
if (auto* df = getDateFormat()) { |
| 1651 |
intl::RemoveICUCellMemory( |
| 1652 |
this, DateTimeFormatObject::UDateFormatEstimatedMemoryUse); |
| 1653 |
delete df; |
| 1654 |
|
| 1655 |
setDateFormat(nullptr); |
| 1656 |
} |
| 1657 |
|
| 1658 |
if (auto* dif = getDateIntervalFormat()) { |
| 1659 |
intl::RemoveICUCellMemory( |
| 1660 |
this, DateTimeFormatObject::UDateIntervalFormatEstimatedMemoryUse); |
| 1661 |
delete dif; |
| 1662 |
|
| 1663 |
setDateIntervalFormat(nullptr); |
| 1664 |
} |
| 1665 |
} |
| 1666 |
|
| 1667 |
static mozilla::intl::DateTimeFormat* GetOrCreateDateTimeFormat( |
| 1668 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 1669 |
DateTimeValueKind kind) { |
| 1670 |
// Clear previously created formatters if their type doesn't match. |
| 1671 |
dateTimeFormat->maybeClearCache(kind); |
| 1672 |
|
| 1673 |
// Obtain a cached mozilla::intl::DateTimeFormat object. |
| 1674 |
mozilla::intl::DateTimeFormat* df = dateTimeFormat->getDateFormat(); |
| 1675 |
if (df) { |
| 1676 |
return df; |
| 1677 |
} |
| 1678 |
|
| 1679 |
df = NewDateTimeFormat(cx, dateTimeFormat, kind); |
| 1680 |
if (!df) { |
| 1681 |
return nullptr; |
| 1682 |
} |
| 1683 |
dateTimeFormat->setDateFormat(df); |
| 1684 |
|
| 1685 |
intl::AddICUCellMemory(dateTimeFormat, |
| 1686 |
DateTimeFormatObject::UDateFormatEstimatedMemoryUse); |
| 1687 |
return df; |
| 1688 |
} |
| 1689 |
|
| 1690 |
template <typename T> |
| 1691 |
static bool SetResolvedProperty(JSContext* cx, HandleObject resolved, |
| 1692 |
Handle<PropertyName*> name, |
| 1693 |
mozilla::Maybe<T> intlProp) { |
| 1694 |
if (!intlProp) { |
| 1695 |
return true; |
| 1696 |
} |
| 1697 |
JSString* str = NewStringCopyZ<CanGC>( |
| 1698 |
cx, mozilla::intl::DateTimeFormat::ToString(*intlProp)); |
| 1699 |
if (!str) { |
| 1700 |
return false; |
| 1701 |
} |
| 1702 |
RootedValue value(cx, StringValue(str)); |
| 1703 |
return DefineDataProperty(cx, resolved, name, value); |
| 1704 |
} |
| 1705 |
|
| 1706 |
bool js::intl_resolveDateTimeFormatComponents(JSContext* cx, unsigned argc, |
| 1707 |
Value* vp) { |
| 1708 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1709 |
MOZ_ASSERT(args.length() == 3); |
| 1710 |
MOZ_ASSERT(args[0].isObject()); |
| 1711 |
MOZ_ASSERT(args[1].isObject()); |
| 1712 |
MOZ_ASSERT(args[2].isBoolean()); |
| 1713 |
|
| 1714 |
Rooted<DateTimeFormatObject*> dateTimeFormat(cx); |
| 1715 |
dateTimeFormat = &args[0].toObject().as<DateTimeFormatObject>(); |
| 1716 |
|
| 1717 |
RootedObject resolved(cx, &args[1].toObject()); |
| 1718 |
|
| 1719 |
bool includeDateTimeFields = args[2].toBoolean(); |
| 1720 |
|
| 1721 |
mozilla::intl::DateTimeFormat* df = |
| 1722 |
GetOrCreateDateTimeFormat(cx, dateTimeFormat, DateTimeValueKind::Number); |
| 1723 |
if (!df) { |
| 1724 |
return false; |
| 1725 |
} |
| 1726 |
|
| 1727 |
auto result = df->ResolveComponents(); |
| 1728 |
if (result.isErr()) { |
| 1729 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 1730 |
return false; |
| 1731 |
} |
| 1732 |
|
| 1733 |
mozilla::intl::DateTimeFormat::ComponentsBag components = result.unwrap(); |
| 1734 |
|
| 1735 |
// Map the resolved mozilla::intl::DateTimeFormat::ComponentsBag to the |
| 1736 |
// options object as returned by DateTimeFormat.prototype.resolvedOptions. |
| 1737 |
// |
| 1738 |
// Resolved options must match the ordering as defined in: |
| 1739 |
// https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype.resolvedoptions |
| 1740 |
|
| 1741 |
if (!SetResolvedProperty(cx, resolved, cx->names().hourCycle, |
| 1742 |
components.hourCycle)) { |
| 1743 |
return false; |
| 1744 |
} |
| 1745 |
|
| 1746 |
if (components.hour12) { |
| 1747 |
RootedValue value(cx, BooleanValue(*components.hour12)); |
| 1748 |
if (!DefineDataProperty(cx, resolved, cx->names().hour12, value)) { |
| 1749 |
return false; |
| 1750 |
} |
| 1751 |
} |
| 1752 |
|
| 1753 |
if (!includeDateTimeFields) { |
| 1754 |
args.rval().setUndefined(); |
| 1755 |
// Do not include date time fields. |
| 1756 |
return true; |
| 1757 |
} |
| 1758 |
|
| 1759 |
if (!SetResolvedProperty(cx, resolved, cx->names().weekday, |
| 1760 |
components.weekday)) { |
| 1761 |
return false; |
| 1762 |
} |
| 1763 |
if (!SetResolvedProperty(cx, resolved, cx->names().era, components.era)) { |
| 1764 |
return false; |
| 1765 |
} |
| 1766 |
if (!SetResolvedProperty(cx, resolved, cx->names().year, components.year)) { |
| 1767 |
return false; |
| 1768 |
} |
| 1769 |
if (!SetResolvedProperty(cx, resolved, cx->names().month, components.month)) { |
| 1770 |
return false; |
| 1771 |
} |
| 1772 |
if (!SetResolvedProperty(cx, resolved, cx->names().day, components.day)) { |
| 1773 |
return false; |
| 1774 |
} |
| 1775 |
if (!SetResolvedProperty(cx, resolved, cx->names().dayPeriod, |
| 1776 |
components.dayPeriod)) { |
| 1777 |
return false; |
| 1778 |
} |
| 1779 |
if (!SetResolvedProperty(cx, resolved, cx->names().hour, components.hour)) { |
| 1780 |
return false; |
| 1781 |
} |
| 1782 |
if (!SetResolvedProperty(cx, resolved, cx->names().minute, |
| 1783 |
components.minute)) { |
| 1784 |
return false; |
| 1785 |
} |
| 1786 |
if (!SetResolvedProperty(cx, resolved, cx->names().second, |
| 1787 |
components.second)) { |
| 1788 |
return false; |
| 1789 |
} |
| 1790 |
if (!SetResolvedProperty(cx, resolved, cx->names().timeZoneName, |
| 1791 |
components.timeZoneName)) { |
| 1792 |
return false; |
| 1793 |
} |
| 1794 |
|
| 1795 |
if (components.fractionalSecondDigits) { |
| 1796 |
RootedValue value(cx, Int32Value(*components.fractionalSecondDigits)); |
| 1797 |
if (!DefineDataProperty(cx, resolved, cx->names().fractionalSecondDigits, |
| 1798 |
value)) { |
| 1799 |
return false; |
| 1800 |
} |
| 1801 |
} |
| 1802 |
|
| 1803 |
args.rval().setUndefined(); |
| 1804 |
return true; |
| 1805 |
} |
| 1806 |
|
| 1807 |
/** |
| 1808 |
* ToDateTimeFormattable ( value ) |
| 1809 |
* |
| 1810 |
* https://tc39.es/proposal-temporal/#sec-todatetimeformattable |
| 1811 |
*/ |
| 1812 |
static auto ToDateTimeFormattable(const Value& value) { |
| 1813 |
// Step 1. (Inlined IsTemporalObject) |
| 1814 |
if (value.isObject()) { |
| 1815 |
auto* obj = CheckedUnwrapStatic(&value.toObject()); |
| 1816 |
if (obj) { |
| 1817 |
if (obj->is<PlainDateObject>()) { |
| 1818 |
return DateTimeValueKind::TemporalDate; |
| 1819 |
} |
| 1820 |
if (obj->is<PlainDateTimeObject>()) { |
| 1821 |
return DateTimeValueKind::TemporalDateTime; |
| 1822 |
} |
| 1823 |
if (obj->is<PlainTimeObject>()) { |
| 1824 |
return DateTimeValueKind::TemporalTime; |
| 1825 |
} |
| 1826 |
if (obj->is<PlainYearMonthObject>()) { |
| 1827 |
return DateTimeValueKind::TemporalYearMonth; |
| 1828 |
} |
| 1829 |
if (obj->is<PlainMonthDayObject>()) { |
| 1830 |
return DateTimeValueKind::TemporalMonthDay; |
| 1831 |
} |
| 1832 |
if (obj->is<ZonedDateTimeObject>()) { |
| 1833 |
return DateTimeValueKind::TemporalZonedDateTime; |
| 1834 |
} |
| 1835 |
if (obj->is<InstantObject>()) { |
| 1836 |
return DateTimeValueKind::TemporalInstant; |
| 1837 |
} |
| 1838 |
return DateTimeValueKind::Number; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
// Step 2. (ToNumber performed in caller) |
| 1843 |
return DateTimeValueKind::Number; |
| 1844 |
} |
| 1845 |
|
| 1846 |
static bool ResolveCalendarAndTimeZone( |
| 1847 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat) { |
| 1848 |
Rooted<JSObject*> internals(cx, intl::GetInternalsObject(cx, dateTimeFormat)); |
| 1849 |
if (!internals) { |
| 1850 |
return false; |
| 1851 |
} |
| 1852 |
|
| 1853 |
Rooted<Value> calendarValue(cx); |
| 1854 |
if (!GetProperty(cx, internals, internals, cx->names().calendar, |
| 1855 |
&calendarValue)) { |
| 1856 |
return false; |
| 1857 |
} |
| 1858 |
Rooted<JSString*> calendarString(cx, calendarValue.toString()); |
| 1859 |
|
| 1860 |
Rooted<CalendarValue> calendar(cx); |
| 1861 |
if (!CanonicalizeCalendar(cx, calendarString, &calendar)) { |
| 1862 |
return false; |
| 1863 |
} |
| 1864 |
|
| 1865 |
Rooted<Value> timeZoneValue(cx); |
| 1866 |
if (!GetProperty(cx, internals, internals, cx->names().timeZone, |
| 1867 |
&timeZoneValue)) { |
| 1868 |
return false; |
| 1869 |
} |
| 1870 |
Rooted<JSString*> timeZoneString(cx, timeZoneValue.toString()); |
| 1871 |
|
| 1872 |
Rooted<ParsedTimeZone> parsedTimeZone(cx); |
| 1873 |
Rooted<TimeZoneValue> timeZone(cx); |
| 1874 |
if (!ParseTemporalTimeZoneString(cx, timeZoneString, &parsedTimeZone) || |
| 1875 |
!ToTemporalTimeZone(cx, parsedTimeZone, &timeZone)) { |
| 1876 |
return false; |
| 1877 |
} |
| 1878 |
|
| 1879 |
dateTimeFormat->setCalendar(calendar); |
| 1880 |
dateTimeFormat->setTimeZone(timeZone); |
| 1881 |
return true; |
| 1882 |
} |
| 1883 |
|
| 1884 |
/** |
| 1885 |
* HandleDateTimeTemporalDate ( dateTimeFormat, temporalDate ) |
| 1886 |
* |
| 1887 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimetemporaldate |
| 1888 |
*/ |
| 1889 |
static bool HandleDateTimeTemporalDate( |
| 1890 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 1891 |
Handle<PlainDateObject*> unwrappedTemporalDate, ClippedTime* result) { |
| 1892 |
auto isoDate = unwrappedTemporalDate->date(); |
| 1893 |
auto calendarId = unwrappedTemporalDate->calendar().identifier(); |
| 1894 |
|
| 1895 |
Rooted<CalendarValue> calendar(cx, dateTimeFormat->getCalendar()); |
| 1896 |
Rooted<TimeZoneValue> timeZone(cx, dateTimeFormat->getTimeZone()); |
| 1897 |
if (!calendar || !timeZone) { |
| 1898 |
if (!ResolveCalendarAndTimeZone(cx, dateTimeFormat)) { |
| 1899 |
return false; |
| 1900 |
} |
| 1901 |
calendar.set(dateTimeFormat->getCalendar()); |
| 1902 |
timeZone.set(dateTimeFormat->getTimeZone()); |
| 1903 |
} |
| 1904 |
MOZ_ASSERT(calendar && timeZone); |
| 1905 |
|
| 1906 |
// Step 1. |
| 1907 |
if (calendarId != CalendarId::ISO8601 && |
| 1908 |
calendarId != calendar.identifier()) { |
| 1909 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1910 |
JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE, |
| 1911 |
CalendarIdentifier(calendarId).data(), |
| 1912 |
CalendarIdentifier(calendar).data()); |
| 1913 |
return false; |
| 1914 |
} |
| 1915 |
|
| 1916 |
// Step 2. |
| 1917 |
auto isoDateTime = ISODateTime{isoDate, {12, 0, 0}}; |
| 1918 |
|
| 1919 |
// Step 3. |
| 1920 |
EpochNanoseconds epochNs; |
| 1921 |
if (!GetEpochNanosecondsFor(cx, timeZone, isoDateTime, |
| 1922 |
TemporalDisambiguation::Compatible, &epochNs)) { |
| 1923 |
return false; |
| 1924 |
} |
| 1925 |
|
| 1926 |
// Steps 4-5. (Performed in NewDateTimeFormat) |
| 1927 |
|
| 1928 |
// Step 6. |
| 1929 |
int64_t milliseconds = epochNs.floorToMilliseconds(); |
| 1930 |
*result = JS::TimeClip(double(milliseconds)); |
| 1931 |
return true; |
| 1932 |
} |
| 1933 |
|
| 1934 |
/** |
| 1935 |
* HandleDateTimeTemporalYearMonth ( dateTimeFormat, temporalYearMonth ) |
| 1936 |
* |
| 1937 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimetemporalyearmonth |
| 1938 |
*/ |
| 1939 |
static bool HandleDateTimeTemporalYearMonth( |
| 1940 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 1941 |
Handle<PlainYearMonthObject*> unwrappedTemporalYearMonth, |
| 1942 |
ClippedTime* result) { |
| 1943 |
auto isoDate = unwrappedTemporalYearMonth->date(); |
| 1944 |
auto calendarId = unwrappedTemporalYearMonth->calendar().identifier(); |
| 1945 |
|
| 1946 |
Rooted<CalendarValue> calendar(cx, dateTimeFormat->getCalendar()); |
| 1947 |
Rooted<TimeZoneValue> timeZone(cx, dateTimeFormat->getTimeZone()); |
| 1948 |
if (!calendar || !timeZone) { |
| 1949 |
if (!ResolveCalendarAndTimeZone(cx, dateTimeFormat)) { |
| 1950 |
return false; |
| 1951 |
} |
| 1952 |
calendar.set(dateTimeFormat->getCalendar()); |
| 1953 |
timeZone.set(dateTimeFormat->getTimeZone()); |
| 1954 |
} |
| 1955 |
MOZ_ASSERT(calendar && timeZone); |
| 1956 |
|
| 1957 |
// Step 1. |
| 1958 |
if (calendarId != calendar.identifier()) { |
| 1959 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1960 |
JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE, |
| 1961 |
CalendarIdentifier(calendarId).data(), |
| 1962 |
CalendarIdentifier(calendar).data()); |
| 1963 |
return false; |
| 1964 |
} |
| 1965 |
|
| 1966 |
// Step 2. |
| 1967 |
auto isoDateTime = ISODateTime{isoDate, {12, 0, 0}}; |
| 1968 |
|
| 1969 |
// Step 3. |
| 1970 |
EpochNanoseconds epochNs; |
| 1971 |
if (!GetEpochNanosecondsFor(cx, timeZone, isoDateTime, |
| 1972 |
TemporalDisambiguation::Compatible, &epochNs)) { |
| 1973 |
return false; |
| 1974 |
} |
| 1975 |
|
| 1976 |
// Steps 4-5. (Performed in NewDateTimeFormat) |
| 1977 |
|
| 1978 |
// Step 6. |
| 1979 |
int64_t milliseconds = epochNs.floorToMilliseconds(); |
| 1980 |
*result = JS::TimeClip(double(milliseconds)); |
| 1981 |
return true; |
| 1982 |
} |
| 1983 |
|
| 1984 |
/** |
| 1985 |
* HandleDateTimeTemporalMonthDay ( dateTimeFormat, temporalMonthDay ) |
| 1986 |
* |
| 1987 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimetemporalmonthday |
| 1988 |
*/ |
| 1989 |
static bool HandleDateTimeTemporalMonthDay( |
| 1990 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 1991 |
Handle<PlainMonthDayObject*> unwrappedTemporalMonthDay, |
| 1992 |
ClippedTime* result) { |
| 1993 |
auto isoDate = unwrappedTemporalMonthDay->date(); |
| 1994 |
auto calendarId = unwrappedTemporalMonthDay->calendar().identifier(); |
| 1995 |
|
| 1996 |
Rooted<CalendarValue> calendar(cx, dateTimeFormat->getCalendar()); |
| 1997 |
Rooted<TimeZoneValue> timeZone(cx, dateTimeFormat->getTimeZone()); |
| 1998 |
if (!calendar || !timeZone) { |
| 1999 |
if (!ResolveCalendarAndTimeZone(cx, dateTimeFormat)) { |
| 2000 |
return false; |
| 2001 |
} |
| 2002 |
calendar.set(dateTimeFormat->getCalendar()); |
| 2003 |
timeZone.set(dateTimeFormat->getTimeZone()); |
| 2004 |
} |
| 2005 |
MOZ_ASSERT(calendar && timeZone); |
| 2006 |
|
| 2007 |
// Step 1. |
| 2008 |
if (calendarId != calendar.identifier()) { |
| 2009 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2010 |
JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE, |
| 2011 |
CalendarIdentifier(calendarId).data(), |
| 2012 |
CalendarIdentifier(calendar).data()); |
| 2013 |
return false; |
| 2014 |
} |
| 2015 |
|
| 2016 |
// Step 2. |
| 2017 |
auto isoDateTime = ISODateTime{isoDate, {12, 0, 0}}; |
| 2018 |
|
| 2019 |
// Step 3. |
| 2020 |
EpochNanoseconds epochNs; |
| 2021 |
if (!GetEpochNanosecondsFor(cx, timeZone, isoDateTime, |
| 2022 |
TemporalDisambiguation::Compatible, &epochNs)) { |
| 2023 |
return false; |
| 2024 |
} |
| 2025 |
|
| 2026 |
// Steps 4-5. (Performed in NewDateTimeFormat) |
| 2027 |
|
| 2028 |
// Step 6. |
| 2029 |
int64_t milliseconds = epochNs.floorToMilliseconds(); |
| 2030 |
*result = JS::TimeClip(double(milliseconds)); |
| 2031 |
return true; |
| 2032 |
} |
| 2033 |
|
| 2034 |
/** |
| 2035 |
* HandleDateTimeTemporalTime ( dateTimeFormat, temporalTime ) |
| 2036 |
* |
| 2037 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimetemporaltime |
| 2038 |
*/ |
| 2039 |
static bool HandleDateTimeTemporalTime( |
| 2040 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 2041 |
Handle<PlainTimeObject*> unwrappedTemporalTime, ClippedTime* result) { |
| 2042 |
auto time = unwrappedTemporalTime->time(); |
| 2043 |
|
| 2044 |
Rooted<TimeZoneValue> timeZone(cx, dateTimeFormat->getTimeZone()); |
| 2045 |
if (!timeZone) { |
| 2046 |
if (!ResolveCalendarAndTimeZone(cx, dateTimeFormat)) { |
| 2047 |
return false; |
| 2048 |
} |
| 2049 |
timeZone.set(dateTimeFormat->getTimeZone()); |
| 2050 |
} |
| 2051 |
MOZ_ASSERT(timeZone); |
| 2052 |
|
| 2053 |
// Steps 1-2. |
| 2054 |
auto isoDateTime = ISODateTime{{1970, 1, 1}, time}; |
| 2055 |
|
| 2056 |
// Step 3. |
| 2057 |
EpochNanoseconds epochNs; |
| 2058 |
if (!GetEpochNanosecondsFor(cx, timeZone, isoDateTime, |
| 2059 |
TemporalDisambiguation::Compatible, &epochNs)) { |
| 2060 |
return false; |
| 2061 |
} |
| 2062 |
|
| 2063 |
// Steps 4-5. (Performed in NewDateTimeFormat) |
| 2064 |
|
| 2065 |
// Step 6. |
| 2066 |
int64_t milliseconds = epochNs.floorToMilliseconds(); |
| 2067 |
*result = JS::TimeClip(double(milliseconds)); |
| 2068 |
return true; |
| 2069 |
} |
| 2070 |
|
| 2071 |
/** |
| 2072 |
* HandleDateTimeTemporalDateTime ( dateTimeFormat, dateTime ) |
| 2073 |
* |
| 2074 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimetemporaldatetime |
| 2075 |
*/ |
| 2076 |
static bool HandleDateTimeTemporalDateTime( |
| 2077 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 2078 |
Handle<PlainDateTimeObject*> unwrappedDateTime, ClippedTime* result) { |
| 2079 |
auto isoDateTime = unwrappedDateTime->dateTime(); |
| 2080 |
auto calendarId = unwrappedDateTime->calendar().identifier(); |
| 2081 |
|
| 2082 |
Rooted<CalendarValue> calendar(cx, dateTimeFormat->getCalendar()); |
| 2083 |
Rooted<TimeZoneValue> timeZone(cx, dateTimeFormat->getTimeZone()); |
| 2084 |
if (!calendar || !timeZone) { |
| 2085 |
if (!ResolveCalendarAndTimeZone(cx, dateTimeFormat)) { |
| 2086 |
return false; |
| 2087 |
} |
| 2088 |
calendar.set(dateTimeFormat->getCalendar()); |
| 2089 |
timeZone.set(dateTimeFormat->getTimeZone()); |
| 2090 |
} |
| 2091 |
MOZ_ASSERT(calendar && timeZone); |
| 2092 |
|
| 2093 |
// Step 1. |
| 2094 |
if (calendarId != CalendarId::ISO8601 && |
| 2095 |
calendarId != calendar.identifier()) { |
| 2096 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2097 |
JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE, |
| 2098 |
CalendarIdentifier(calendarId).data(), |
| 2099 |
CalendarIdentifier(calendar).data()); |
| 2100 |
return false; |
| 2101 |
} |
| 2102 |
|
| 2103 |
// Step 2. |
| 2104 |
EpochNanoseconds epochNs; |
| 2105 |
if (!GetEpochNanosecondsFor(cx, timeZone, isoDateTime, |
| 2106 |
TemporalDisambiguation::Compatible, &epochNs)) { |
| 2107 |
return false; |
| 2108 |
} |
| 2109 |
|
| 2110 |
// Step 3. (Performed in NewDateTimeFormat) |
| 2111 |
|
| 2112 |
// Step 4. |
| 2113 |
int64_t milliseconds = epochNs.floorToMilliseconds(); |
| 2114 |
*result = JS::TimeClip(double(milliseconds)); |
| 2115 |
return true; |
| 2116 |
} |
| 2117 |
|
| 2118 |
/** |
| 2119 |
* HandleDateTimeTemporalInstant ( dateTimeFormat, instant ) |
| 2120 |
* |
| 2121 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimetemporalinstant |
| 2122 |
*/ |
| 2123 |
static bool HandleDateTimeTemporalInstant(InstantObject* unwrappedInstant, |
| 2124 |
ClippedTime* result) { |
| 2125 |
// Step 1. (Performed in NewDateTimeFormat) |
| 2126 |
|
| 2127 |
// Step 2. |
| 2128 |
auto epochNs = unwrappedInstant->epochNanoseconds(); |
| 2129 |
int64_t milliseconds = epochNs.floorToMilliseconds(); |
| 2130 |
*result = JS::TimeClip(double(milliseconds)); |
| 2131 |
return true; |
| 2132 |
} |
| 2133 |
|
| 2134 |
/** |
| 2135 |
* Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ) |
| 2136 |
*/ |
| 2137 |
static bool HandleDateTimeTemporalZonedDateTime( |
| 2138 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 2139 |
Handle<ZonedDateTimeObject*> unwrappedZonedDateTime, ClippedTime* result) { |
| 2140 |
auto epochNs = unwrappedZonedDateTime->epochNanoseconds(); |
| 2141 |
auto calendarId = unwrappedZonedDateTime->calendar().identifier(); |
| 2142 |
|
| 2143 |
Rooted<CalendarValue> calendar(cx, dateTimeFormat->getCalendar()); |
| 2144 |
Rooted<TimeZoneValue> timeZone(cx, dateTimeFormat->getTimeZone()); |
| 2145 |
if (!calendar || !timeZone) { |
| 2146 |
if (!ResolveCalendarAndTimeZone(cx, dateTimeFormat)) { |
| 2147 |
return false; |
| 2148 |
} |
| 2149 |
calendar.set(dateTimeFormat->getCalendar()); |
| 2150 |
timeZone.set(dateTimeFormat->getTimeZone()); |
| 2151 |
} |
| 2152 |
MOZ_ASSERT(calendar && timeZone); |
| 2153 |
|
| 2154 |
// Step 4. |
| 2155 |
if (calendarId != CalendarId::ISO8601 && |
| 2156 |
calendarId != calendar.identifier()) { |
| 2157 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2158 |
JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE, |
| 2159 |
CalendarIdentifier(calendarId).data(), |
| 2160 |
CalendarIdentifier(calendar).data()); |
| 2161 |
return false; |
| 2162 |
} |
| 2163 |
|
| 2164 |
// Step 5. |
| 2165 |
int64_t milliseconds = epochNs.floorToMilliseconds(); |
| 2166 |
*result = JS::TimeClip(double(milliseconds)); |
| 2167 |
return true; |
| 2168 |
} |
| 2169 |
|
| 2170 |
/** |
| 2171 |
* HandleDateTimeOthers ( dateTimeFormat, x ) |
| 2172 |
* |
| 2173 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimeothers |
| 2174 |
*/ |
| 2175 |
static bool HandleDateTimeOthers(JSContext* cx, const char* method, double x, |
| 2176 |
ClippedTime* result) { |
| 2177 |
// Step 1. |
| 2178 |
auto clipped = JS::TimeClip(x); |
| 2179 |
|
| 2180 |
// Step 2. |
| 2181 |
if (!clipped.isValid()) { |
| 2182 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2183 |
JSMSG_DATE_NOT_FINITE, "DateTimeFormat", method); |
| 2184 |
return false; |
| 2185 |
} |
| 2186 |
|
| 2187 |
// Step 4. (Performed in NewDateTimeFormat) |
| 2188 |
|
| 2189 |
// Steps 3 and 5. |
| 2190 |
*result = clipped; |
| 2191 |
return true; |
| 2192 |
} |
| 2193 |
|
| 2194 |
/** |
| 2195 |
* HandleDateTimeValue ( dateTimeFormat, x ) |
| 2196 |
* |
| 2197 |
* https://tc39.es/proposal-temporal/#sec-temporal-handledatetimevalue |
| 2198 |
*/ |
| 2199 |
static bool HandleDateTimeValue(JSContext* cx, const char* method, |
| 2200 |
Handle<DateTimeFormatObject*> dateTimeFormat, |
| 2201 |
Handle<Value> x, ClippedTime* result) { |
| 2202 |
MOZ_ASSERT(x.isObject() || x.isNumber()); |
| 2203 |
|
| 2204 |
// Step 1. |
| 2205 |
if (x.isObject()) { |
| 2206 |
Rooted<JSObject*> unwrapped(cx, CheckedUnwrapStatic(&x.toObject())); |
| 2207 |
if (!unwrapped) { |
| 2208 |
ReportAccessDenied(cx); |
| 2209 |
return false; |
| 2210 |
} |
| 2211 |
|
| 2212 |
// Step 1.a. |
| 2213 |
if (unwrapped->is<PlainDateObject>()) { |
| 2214 |
return HandleDateTimeTemporalDate( |
| 2215 |
cx, dateTimeFormat, unwrapped.as<PlainDateObject>(), result); |
| 2216 |
} |
| 2217 |
|
| 2218 |
// Step 1.b. |
| 2219 |
if (unwrapped->is<PlainYearMonthObject>()) { |
| 2220 |
return HandleDateTimeTemporalYearMonth( |
| 2221 |
cx, dateTimeFormat, unwrapped.as<PlainYearMonthObject>(), result); |
| 2222 |
} |
| 2223 |
|
| 2224 |
// Step 1.c. |
| 2225 |
if (unwrapped->is<PlainMonthDayObject>()) { |
| 2226 |
return HandleDateTimeTemporalMonthDay( |
| 2227 |
cx, dateTimeFormat, unwrapped.as<PlainMonthDayObject>(), result); |
| 2228 |
} |
| 2229 |
|
| 2230 |
// Step 1.d. |
| 2231 |
if (unwrapped->is<PlainTimeObject>()) { |
| 2232 |
return HandleDateTimeTemporalTime( |
| 2233 |
cx, dateTimeFormat, unwrapped.as<PlainTimeObject>(), result); |
| 2234 |
} |
| 2235 |
|
| 2236 |
// Step 1.e. |
| 2237 |
if (unwrapped->is<PlainDateTimeObject>()) { |
| 2238 |
return HandleDateTimeTemporalDateTime( |
| 2239 |
cx, dateTimeFormat, unwrapped.as<PlainDateTimeObject>(), result); |
| 2240 |
} |
| 2241 |
|
| 2242 |
// Step 1.f. |
| 2243 |
if (unwrapped->is<InstantObject>()) { |
| 2244 |
return HandleDateTimeTemporalInstant(&unwrapped->as<InstantObject>(), |
| 2245 |
result); |
| 2246 |
} |
| 2247 |
|
| 2248 |
// Step 1.g. |
| 2249 |
MOZ_ASSERT(unwrapped->is<ZonedDateTimeObject>()); |
| 2250 |
|
| 2251 |
// Step 1.h. |
| 2252 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2253 |
JSMSG_UNEXPECTED_TYPE, "object", |
| 2254 |
unwrapped->getClass()->name); |
| 2255 |
return false; |
| 2256 |
} |
| 2257 |
|
| 2258 |
// Step 2. |
| 2259 |
return HandleDateTimeOthers(cx, method, x.toNumber(), result); |
| 2260 |
} |
| 2261 |
|
| 2262 |
static bool intl_FormatDateTime(JSContext* cx, |
| 2263 |
const mozilla::intl::DateTimeFormat* df, |
| 2264 |
ClippedTime x, MutableHandleValue result) { |
| 2265 |
MOZ_ASSERT(x.isValid()); |
| 2266 |
|
| 2267 |
FormatBuffer<char16_t, INITIAL_CHAR_BUFFER_SIZE> buffer(cx); |
| 2268 |
auto dfResult = df->TryFormat(x.toDouble(), buffer); |
| 2269 |
if (dfResult.isErr()) { |
| 2270 |
intl::ReportInternalError(cx, dfResult.unwrapErr()); |
| 2271 |
return false; |
| 2272 |
} |
| 2273 |
|
| 2274 |
JSString* str = buffer.toString(cx); |
| 2275 |
if (!str) { |
| 2276 |
return false; |
| 2277 |
} |
| 2278 |
|
| 2279 |
result.setString(str); |
| 2280 |
return true; |
| 2281 |
} |
| 2282 |
|
| 2283 |
using FieldType = js::ImmutableTenuredPtr<PropertyName*> JSAtomState::*; |
| 2284 |
|
| 2285 |
static FieldType GetFieldTypeForPartType(mozilla::intl::DateTimePartType type) { |
| 2286 |
switch (type) { |
| 2287 |
case mozilla::intl::DateTimePartType::Literal: |
| 2288 |
return &JSAtomState::literal; |
| 2289 |
case mozilla::intl::DateTimePartType::Era: |
| 2290 |
return &JSAtomState::era; |
| 2291 |
case mozilla::intl::DateTimePartType::Year: |
| 2292 |
return &JSAtomState::year; |
| 2293 |
case mozilla::intl::DateTimePartType::YearName: |
| 2294 |
return &JSAtomState::yearName; |
| 2295 |
case mozilla::intl::DateTimePartType::RelatedYear: |
| 2296 |
return &JSAtomState::relatedYear; |
| 2297 |
case mozilla::intl::DateTimePartType::Month: |
| 2298 |
return &JSAtomState::month; |
| 2299 |
case mozilla::intl::DateTimePartType::Day: |
| 2300 |
return &JSAtomState::day; |
| 2301 |
case mozilla::intl::DateTimePartType::Hour: |
| 2302 |
return &JSAtomState::hour; |
| 2303 |
case mozilla::intl::DateTimePartType::Minute: |
| 2304 |
return &JSAtomState::minute; |
| 2305 |
case mozilla::intl::DateTimePartType::Second: |
| 2306 |
return &JSAtomState::second; |
| 2307 |
case mozilla::intl::DateTimePartType::Weekday: |
| 2308 |
return &JSAtomState::weekday; |
| 2309 |
case mozilla::intl::DateTimePartType::DayPeriod: |
| 2310 |
return &JSAtomState::dayPeriod; |
| 2311 |
case mozilla::intl::DateTimePartType::TimeZoneName: |
| 2312 |
return &JSAtomState::timeZoneName; |
| 2313 |
case mozilla::intl::DateTimePartType::FractionalSecondDigits: |
| 2314 |
return &JSAtomState::fractionalSecond; |
| 2315 |
case mozilla::intl::DateTimePartType::Unknown: |
| 2316 |
return &JSAtomState::unknown; |
| 2317 |
} |
| 2318 |
|
| 2319 |
MOZ_CRASH( |
| 2320 |
"unenumerated, undocumented format field returned " |
| 2321 |
"by iterator"); |
| 2322 |
} |
| 2323 |
|
| 2324 |
static FieldType GetFieldTypeForPartSource( |
| 2325 |
mozilla::intl::DateTimePartSource source) { |
| 2326 |
switch (source) { |
| 2327 |
case mozilla::intl::DateTimePartSource::Shared: |
| 2328 |
return &JSAtomState::shared; |
| 2329 |
case mozilla::intl::DateTimePartSource::StartRange: |
| 2330 |
return &JSAtomState::startRange; |
| 2331 |
case mozilla::intl::DateTimePartSource::EndRange: |
| 2332 |
return &JSAtomState::endRange; |
| 2333 |
} |
| 2334 |
|
| 2335 |
MOZ_CRASH( |
| 2336 |
"unenumerated, undocumented format field returned " |
| 2337 |
"by iterator"); |
| 2338 |
} |
| 2339 |
|
| 2340 |
// A helper function to create an ArrayObject from DateTimePart objects. |
| 2341 |
// When hasNoSource is true, we don't need to create the ||Source|| property for |
| 2342 |
// the DateTimePart object. |
| 2343 |
static bool CreateDateTimePartArray( |
| 2344 |
JSContext* cx, mozilla::Span<const char16_t> formattedSpan, |
| 2345 |
bool hasNoSource, const mozilla::intl::DateTimePartVector& parts, |
| 2346 |
MutableHandleValue result) { |
| 2347 |
RootedString overallResult(cx, NewStringCopy<CanGC>(cx, formattedSpan)); |
| 2348 |
if (!overallResult) { |
| 2349 |
return false; |
| 2350 |
} |
| 2351 |
|
| 2352 |
Rooted<ArrayObject*> partsArray( |
| 2353 |
cx, NewDenseFullyAllocatedArray(cx, parts.length())); |
| 2354 |
if (!partsArray) { |
| 2355 |
return false; |
| 2356 |
} |
| 2357 |
partsArray->ensureDenseInitializedLength(0, parts.length()); |
| 2358 |
|
| 2359 |
if (overallResult->length() == 0) { |
| 2360 |
// An empty string contains no parts, so avoid extra work below. |
| 2361 |
result.setObject(*partsArray); |
| 2362 |
return true; |
| 2363 |
} |
| 2364 |
|
| 2365 |
RootedObject singlePart(cx); |
| 2366 |
RootedValue val(cx); |
| 2367 |
|
| 2368 |
size_t index = 0; |
| 2369 |
size_t beginIndex = 0; |
| 2370 |
for (const mozilla::intl::DateTimePart& part : parts) { |
| 2371 |
singlePart = NewPlainObject(cx); |
| 2372 |
if (!singlePart) { |
| 2373 |
return false; |
| 2374 |
} |
| 2375 |
|
| 2376 |
FieldType type = GetFieldTypeForPartType(part.mType); |
| 2377 |
val = StringValue(cx->names().*type); |
| 2378 |
if (!DefineDataProperty(cx, singlePart, cx->names().type, val)) { |
| 2379 |
return false; |
| 2380 |
} |
| 2381 |
|
| 2382 |
MOZ_ASSERT(part.mEndIndex > beginIndex); |
| 2383 |
JSLinearString* partStr = NewDependentString(cx, overallResult, beginIndex, |
| 2384 |
part.mEndIndex - beginIndex); |
| 2385 |
if (!partStr) { |
| 2386 |
return false; |
| 2387 |
} |
| 2388 |
val = StringValue(partStr); |
| 2389 |
if (!DefineDataProperty(cx, singlePart, cx->names().value, val)) { |
| 2390 |
return false; |
| 2391 |
} |
| 2392 |
|
| 2393 |
if (!hasNoSource) { |
| 2394 |
FieldType source = GetFieldTypeForPartSource(part.mSource); |
| 2395 |
val = StringValue(cx->names().*source); |
| 2396 |
if (!DefineDataProperty(cx, singlePart, cx->names().source, val)) { |
| 2397 |
return false; |
| 2398 |
} |
| 2399 |
} |
| 2400 |
|
| 2401 |
beginIndex = part.mEndIndex; |
| 2402 |
partsArray->initDenseElement(index++, ObjectValue(*singlePart)); |
| 2403 |
} |
| 2404 |
|
| 2405 |
MOZ_ASSERT(index == parts.length()); |
| 2406 |
MOZ_ASSERT(beginIndex == formattedSpan.size()); |
| 2407 |
result.setObject(*partsArray); |
| 2408 |
return true; |
| 2409 |
} |
| 2410 |
|
| 2411 |
static bool intl_FormatToPartsDateTime(JSContext* cx, |
| 2412 |
const mozilla::intl::DateTimeFormat* df, |
| 2413 |
ClippedTime x, bool hasNoSource, |
| 2414 |
MutableHandleValue result) { |
| 2415 |
MOZ_ASSERT(x.isValid()); |
| 2416 |
|
| 2417 |
FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> buffer(cx); |
| 2418 |
mozilla::intl::DateTimePartVector parts; |
| 2419 |
auto r = df->TryFormatToParts(x.toDouble(), buffer, parts); |
| 2420 |
if (r.isErr()) { |
| 2421 |
intl::ReportInternalError(cx, r.unwrapErr()); |
| 2422 |
return false; |
| 2423 |
} |
| 2424 |
|
| 2425 |
return CreateDateTimePartArray(cx, buffer, hasNoSource, parts, result); |
| 2426 |
} |
| 2427 |
|
| 2428 |
bool js::intl_FormatDateTime(JSContext* cx, unsigned argc, Value* vp) { |
| 2429 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2430 |
MOZ_ASSERT(args.length() == 3); |
| 2431 |
MOZ_ASSERT(args[0].isObject()); |
| 2432 |
MOZ_ASSERT(args[2].isBoolean()); |
| 2433 |
|
| 2434 |
Rooted<DateTimeFormatObject*> dateTimeFormat(cx); |
| 2435 |
dateTimeFormat = &args[0].toObject().as<DateTimeFormatObject>(); |
| 2436 |
|
| 2437 |
bool formatToParts = args[2].toBoolean(); |
| 2438 |
const char* method = formatToParts ? "formatToParts" : "format"; |
| 2439 |
|
| 2440 |
auto kind = ToDateTimeFormattable(args[1]); |
| 2441 |
|
| 2442 |
JS::ClippedTime x; |
| 2443 |
if (!args[1].isUndefined()) { |
| 2444 |
Rooted<Value> value(cx, args[1]); |
| 2445 |
if (kind == DateTimeValueKind::Number) { |
| 2446 |
if (!ToNumber(cx, &value)) { |
| 2447 |
return false; |
| 2448 |
} |
| 2449 |
} |
| 2450 |
MOZ_ASSERT(value.isNumber() || value.isObject()); |
| 2451 |
|
| 2452 |
if (!HandleDateTimeValue(cx, method, dateTimeFormat, value, &x)) { |
| 2453 |
return false; |
| 2454 |
} |
| 2455 |
} else { |
| 2456 |
x = DateNow(cx); |
| 2457 |
} |
| 2458 |
MOZ_ASSERT(x.isValid()); |
| 2459 |
|
| 2460 |
mozilla::intl::DateTimeFormat* df = |
| 2461 |
GetOrCreateDateTimeFormat(cx, dateTimeFormat, kind); |
| 2462 |
if (!df) { |
| 2463 |
return false; |
| 2464 |
} |
| 2465 |
|
| 2466 |
// Use the DateTimeFormat to actually format the time stamp. |
| 2467 |
return formatToParts ? intl_FormatToPartsDateTime( |
| 2468 |
cx, df, x, /* hasNoSource */ true, args.rval()) |
| 2469 |
: intl_FormatDateTime(cx, df, x, args.rval()); |
| 2470 |
} |
| 2471 |
|
| 2472 |
/** |
| 2473 |
* Returns a new DateIntervalFormat with the locale and date-time formatting |
| 2474 |
* options of the given DateTimeFormat. |
| 2475 |
*/ |
| 2476 |
static mozilla::intl::DateIntervalFormat* NewDateIntervalFormat( |
| 2477 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 2478 |
mozilla::intl::DateTimeFormat& mozDtf) { |
| 2479 |
RootedValue value(cx); |
| 2480 |
RootedObject internals(cx, intl::GetInternalsObject(cx, dateTimeFormat)); |
| 2481 |
if (!internals) { |
| 2482 |
return nullptr; |
| 2483 |
} |
| 2484 |
|
| 2485 |
FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> pattern(cx); |
| 2486 |
auto result = mozDtf.GetPattern(pattern); |
| 2487 |
if (result.isErr()) { |
| 2488 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 2489 |
return nullptr; |
| 2490 |
} |
| 2491 |
|
| 2492 |
// Determine the hour cycle used in the resolved pattern. |
| 2493 |
mozilla::Maybe<mozilla::intl::DateTimeFormat::HourCycle> hcPattern = |
| 2494 |
mozilla::intl::DateTimeFormat::HourCycleFromPattern(pattern); |
| 2495 |
|
| 2496 |
UniqueChars locale = DateTimeFormatLocale(cx, internals, hcPattern); |
| 2497 |
if (!locale) { |
| 2498 |
return nullptr; |
| 2499 |
} |
| 2500 |
|
| 2501 |
if (!GetProperty(cx, internals, internals, cx->names().timeZone, &value)) { |
| 2502 |
return nullptr; |
| 2503 |
} |
| 2504 |
|
| 2505 |
Rooted<JSLinearString*> timeZoneString(cx, |
| 2506 |
value.toString()->ensureLinear(cx)); |
| 2507 |
if (!timeZoneString) { |
| 2508 |
return nullptr; |
| 2509 |
} |
| 2510 |
|
| 2511 |
AutoStableStringChars timeZone(cx); |
| 2512 |
mozilla::Span<const char16_t> timeZoneChars{}; |
| 2513 |
|
| 2514 |
auto timeZoneOffset = TimeZoneOffsetString::from(timeZoneString); |
| 2515 |
if (timeZoneOffset) { |
| 2516 |
timeZoneChars = *timeZoneOffset; |
| 2517 |
} else { |
| 2518 |
if (!timeZone.initTwoByte(cx, timeZoneString)) { |
| 2519 |
return nullptr; |
| 2520 |
} |
| 2521 |
timeZoneChars = timeZone.twoByteRange(); |
| 2522 |
} |
| 2523 |
|
| 2524 |
FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> skeleton(cx); |
| 2525 |
auto skelResult = mozDtf.GetOriginalSkeleton(skeleton); |
| 2526 |
if (skelResult.isErr()) { |
| 2527 |
intl::ReportInternalError(cx, skelResult.unwrapErr()); |
| 2528 |
return nullptr; |
| 2529 |
} |
| 2530 |
|
| 2531 |
auto dif = mozilla::intl::DateIntervalFormat::TryCreate( |
| 2532 |
mozilla::MakeStringSpan(locale.get()), skeleton, timeZoneChars); |
| 2533 |
|
| 2534 |
if (dif.isErr()) { |
| 2535 |
js::intl::ReportInternalError(cx, dif.unwrapErr()); |
| 2536 |
return nullptr; |
| 2537 |
} |
| 2538 |
|
| 2539 |
return dif.unwrap().release(); |
| 2540 |
} |
| 2541 |
|
| 2542 |
static mozilla::intl::DateIntervalFormat* GetOrCreateDateIntervalFormat( |
| 2543 |
JSContext* cx, Handle<DateTimeFormatObject*> dateTimeFormat, |
| 2544 |
mozilla::intl::DateTimeFormat& mozDtf, DateTimeValueKind kind) { |
| 2545 |
dateTimeFormat->maybeClearCache(kind); |
| 2546 |
|
| 2547 |
// Obtain a cached DateIntervalFormat object. |
| 2548 |
mozilla::intl::DateIntervalFormat* dif = |
| 2549 |
dateTimeFormat->getDateIntervalFormat(); |
| 2550 |
if (dif) { |
| 2551 |
return dif; |
| 2552 |
} |
| 2553 |
|
| 2554 |
dif = NewDateIntervalFormat(cx, dateTimeFormat, mozDtf); |
| 2555 |
if (!dif) { |
| 2556 |
return nullptr; |
| 2557 |
} |
| 2558 |
dateTimeFormat->setDateIntervalFormat(dif); |
| 2559 |
|
| 2560 |
intl::AddICUCellMemory( |
| 2561 |
dateTimeFormat, |
| 2562 |
DateTimeFormatObject::UDateIntervalFormatEstimatedMemoryUse); |
| 2563 |
return dif; |
| 2564 |
} |
| 2565 |
|
| 2566 |
/** |
| 2567 |
* PartitionDateTimeRangePattern ( dateTimeFormat, x, y ) |
| 2568 |
*/ |
| 2569 |
static bool PartitionDateTimeRangePattern( |
| 2570 |
JSContext* cx, const mozilla::intl::DateTimeFormat* df, |
| 2571 |
const mozilla::intl::DateIntervalFormat* dif, |
| 2572 |
mozilla::intl::AutoFormattedDateInterval& formatted, ClippedTime x, |
| 2573 |
ClippedTime y, bool* equal) { |
| 2574 |
MOZ_ASSERT(x.isValid()); |
| 2575 |
MOZ_ASSERT(y.isValid()); |
| 2576 |
|
| 2577 |
auto result = |
| 2578 |
dif->TryFormatDateTime(x.toDouble(), y.toDouble(), df, formatted, equal); |
| 2579 |
if (result.isErr()) { |
| 2580 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 2581 |
return false; |
| 2582 |
} |
| 2583 |
return true; |
| 2584 |
} |
| 2585 |
|
| 2586 |
/** |
| 2587 |
* FormatDateTimeRange( dateTimeFormat, x, y ) |
| 2588 |
*/ |
| 2589 |
static bool FormatDateTimeRange(JSContext* cx, |
| 2590 |
const mozilla::intl::DateTimeFormat* df, |
| 2591 |
const mozilla::intl::DateIntervalFormat* dif, |
| 2592 |
ClippedTime x, ClippedTime y, |
| 2593 |
MutableHandleValue result) { |
| 2594 |
mozilla::intl::AutoFormattedDateInterval formatted; |
| 2595 |
if (!formatted.IsValid()) { |
| 2596 |
intl::ReportInternalError(cx, formatted.GetError()); |
| 2597 |
return false; |
| 2598 |
} |
| 2599 |
|
| 2600 |
bool equal; |
| 2601 |
if (!PartitionDateTimeRangePattern(cx, df, dif, formatted, x, y, &equal)) { |
| 2602 |
return false; |
| 2603 |
} |
| 2604 |
|
| 2605 |
// PartitionDateTimeRangePattern, step 12. |
| 2606 |
if (equal) { |
| 2607 |
return intl_FormatDateTime(cx, df, x, result); |
| 2608 |
} |
| 2609 |
|
| 2610 |
auto spanResult = formatted.ToSpan(); |
| 2611 |
if (spanResult.isErr()) { |
| 2612 |
intl::ReportInternalError(cx, spanResult.unwrapErr()); |
| 2613 |
return false; |
| 2614 |
} |
| 2615 |
JSString* resultStr = NewStringCopy<CanGC>(cx, spanResult.unwrap()); |
| 2616 |
if (!resultStr) { |
| 2617 |
return false; |
| 2618 |
} |
| 2619 |
|
| 2620 |
result.setString(resultStr); |
| 2621 |
return true; |
| 2622 |
} |
| 2623 |
|
| 2624 |
/** |
| 2625 |
* FormatDateTimeRangeToParts ( dateTimeFormat, x, y ) |
| 2626 |
*/ |
| 2627 |
static bool FormatDateTimeRangeToParts( |
| 2628 |
JSContext* cx, const mozilla::intl::DateTimeFormat* df, |
| 2629 |
const mozilla::intl::DateIntervalFormat* dif, ClippedTime x, ClippedTime y, |
| 2630 |
MutableHandleValue result) { |
| 2631 |
mozilla::intl::AutoFormattedDateInterval formatted; |
| 2632 |
if (!formatted.IsValid()) { |
| 2633 |
intl::ReportInternalError(cx, formatted.GetError()); |
| 2634 |
return false; |
| 2635 |
} |
| 2636 |
|
| 2637 |
bool equal; |
| 2638 |
if (!PartitionDateTimeRangePattern(cx, df, dif, formatted, x, y, &equal)) { |
| 2639 |
return false; |
| 2640 |
} |
| 2641 |
|
| 2642 |
// PartitionDateTimeRangePattern, step 12. |
| 2643 |
if (equal) { |
| 2644 |
return intl_FormatToPartsDateTime(cx, df, x, /* hasNoSource */ false, |
| 2645 |
result); |
| 2646 |
} |
| 2647 |
|
| 2648 |
mozilla::intl::DateTimePartVector parts; |
| 2649 |
auto r = dif->TryFormattedToParts(formatted, parts); |
| 2650 |
if (r.isErr()) { |
| 2651 |
intl::ReportInternalError(cx, r.unwrapErr()); |
| 2652 |
return false; |
| 2653 |
} |
| 2654 |
|
| 2655 |
auto spanResult = formatted.ToSpan(); |
| 2656 |
if (spanResult.isErr()) { |
| 2657 |
intl::ReportInternalError(cx, spanResult.unwrapErr()); |
| 2658 |
return false; |
| 2659 |
} |
| 2660 |
return CreateDateTimePartArray(cx, spanResult.unwrap(), |
| 2661 |
/* hasNoSource */ false, parts, result); |
| 2662 |
} |
| 2663 |
|
| 2664 |
bool js::intl_FormatDateTimeRange(JSContext* cx, unsigned argc, Value* vp) { |
| 2665 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2666 |
MOZ_ASSERT(args.length() == 4); |
| 2667 |
MOZ_ASSERT(args[0].isObject()); |
| 2668 |
MOZ_ASSERT(!args[1].isUndefined()); |
| 2669 |
MOZ_ASSERT(!args[2].isUndefined()); |
| 2670 |
MOZ_ASSERT(args[3].isBoolean()); |
| 2671 |
|
| 2672 |
Rooted<DateTimeFormatObject*> dateTimeFormat(cx); |
| 2673 |
dateTimeFormat = &args[0].toObject().as<DateTimeFormatObject>(); |
| 2674 |
|
| 2675 |
bool formatToParts = args[3].toBoolean(); |
| 2676 |
const char* method = formatToParts ? "formatRangeToParts" : "formatRange"; |
| 2677 |
|
| 2678 |
Rooted<Value> start(cx, args[1]); |
| 2679 |
auto startKind = ToDateTimeFormattable(start); |
| 2680 |
if (startKind == DateTimeValueKind::Number) { |
| 2681 |
if (!ToNumber(cx, &start)) { |
| 2682 |
return false; |
| 2683 |
} |
| 2684 |
} |
| 2685 |
MOZ_ASSERT(start.isNumber() || start.isObject()); |
| 2686 |
|
| 2687 |
Rooted<Value> end(cx, args[2]); |
| 2688 |
auto endKind = ToDateTimeFormattable(end); |
| 2689 |
if (endKind == DateTimeValueKind::Number) { |
| 2690 |
if (!ToNumber(cx, &end)) { |
| 2691 |
return false; |
| 2692 |
} |
| 2693 |
} |
| 2694 |
MOZ_ASSERT(end.isNumber() || end.isObject()); |
| 2695 |
|
| 2696 |
if (startKind != endKind) { |
| 2697 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2698 |
JSMSG_NOT_EXPECTED_TYPE, method, |
| 2699 |
DateTimeValueKindToString(startKind), |
| 2700 |
DateTimeValueKindToString(endKind)); |
| 2701 |
return false; |
| 2702 |
} |
| 2703 |
|
| 2704 |
// PartitionDateTimeRangePattern, steps 1-2. |
| 2705 |
JS::ClippedTime x; |
| 2706 |
if (!HandleDateTimeValue(cx, method, dateTimeFormat, start, &x)) { |
| 2707 |
return false; |
| 2708 |
} |
| 2709 |
MOZ_ASSERT(x.isValid()); |
| 2710 |
|
| 2711 |
// PartitionDateTimeRangePattern, steps 3-4. |
| 2712 |
JS::ClippedTime y; |
| 2713 |
if (!HandleDateTimeValue(cx, method, dateTimeFormat, end, &y)) { |
| 2714 |
return false; |
| 2715 |
} |
| 2716 |
MOZ_ASSERT(y.isValid()); |
| 2717 |
|
| 2718 |
mozilla::intl::DateTimeFormat* df = |
| 2719 |
GetOrCreateDateTimeFormat(cx, dateTimeFormat, startKind); |
| 2720 |
if (!df) { |
| 2721 |
return false; |
| 2722 |
} |
| 2723 |
|
| 2724 |
mozilla::intl::DateIntervalFormat* dif = |
| 2725 |
GetOrCreateDateIntervalFormat(cx, dateTimeFormat, *df, startKind); |
| 2726 |
if (!dif) { |
| 2727 |
return false; |
| 2728 |
} |
| 2729 |
|
| 2730 |
// Use the DateIntervalFormat to actually format the time range. |
| 2731 |
return formatToParts |
| 2732 |
? FormatDateTimeRangeToParts(cx, df, dif, x, y, args.rval()) |
| 2733 |
: FormatDateTimeRange(cx, df, dif, x, y, args.rval()); |
| 2734 |
} |
| 2735 |
|
| 2736 |
bool js::TemporalObjectToLocaleString(JSContext* cx, const CallArgs& args, |
| 2737 |
Handle<JSString*> required, |
| 2738 |
Handle<JSString*> defaults, |
| 2739 |
Handle<Value> toLocaleStringTimeZone) { |
| 2740 |
MOZ_ASSERT(args.thisv().isObject()); |
| 2741 |
|
| 2742 |
auto kind = ToDateTimeFormattable(args.thisv()); |
| 2743 |
MOZ_ASSERT(kind != DateTimeValueKind::Number); |
| 2744 |
MOZ_ASSERT_IF(kind != DateTimeValueKind::TemporalZonedDateTime, |
| 2745 |
toLocaleStringTimeZone.isUndefined()); |
| 2746 |
MOZ_ASSERT_IF(kind == DateTimeValueKind::TemporalZonedDateTime, |
| 2747 |
toLocaleStringTimeZone.isString()); |
| 2748 |
|
| 2749 |
Rooted<DateTimeFormatObject*> dateTimeFormat( |
| 2750 |
cx, NewBuiltinClassInstance<DateTimeFormatObject>(cx)); |
| 2751 |
if (!dateTimeFormat) { |
| 2752 |
return false; |
| 2753 |
} |
| 2754 |
|
| 2755 |
Rooted<Value> thisValue(cx, ObjectValue(*dateTimeFormat)); |
| 2756 |
Rooted<Value> ignored(cx); |
| 2757 |
if (!intl::InitializeDateTimeFormatObject( |
| 2758 |
cx, dateTimeFormat, thisValue, args.get(0), args.get(1), required, |
| 2759 |
defaults, toLocaleStringTimeZone, DateTimeFormatOptions::Standard, |
| 2760 |
&ignored)) { |
| 2761 |
return false; |
| 2762 |
} |
| 2763 |
MOZ_ASSERT(&ignored.toObject() == dateTimeFormat); |
| 2764 |
|
| 2765 |
JS::ClippedTime x; |
| 2766 |
if (kind == DateTimeValueKind::TemporalZonedDateTime) { |
| 2767 |
Rooted<ZonedDateTimeObject*> zonedDateTime( |
| 2768 |
cx, &args.thisv().toObject().as<ZonedDateTimeObject>()); |
| 2769 |
if (!HandleDateTimeTemporalZonedDateTime(cx, dateTimeFormat, zonedDateTime, |
| 2770 |
&x)) { |
| 2771 |
return false; |
| 2772 |
} |
| 2773 |
} else { |
| 2774 |
if (!HandleDateTimeValue(cx, "toLocaleString", dateTimeFormat, args.thisv(), |
| 2775 |
&x)) { |
| 2776 |
return false; |
| 2777 |
} |
| 2778 |
} |
| 2779 |
MOZ_ASSERT(x.isValid()); |
| 2780 |
|
| 2781 |
auto* df = GetOrCreateDateTimeFormat(cx, dateTimeFormat, kind); |
| 2782 |
if (!df) { |
| 2783 |
return false; |
| 2784 |
} |
| 2785 |
|
| 2786 |
return intl_FormatDateTime(cx, df, x, args.rval()); |
| 2787 |
} |