| 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 |
/* Implementation of the Intl.RelativeTimeFormat proposal. */ |
| 8 |
|
| 9 |
#include "builtin/intl/RelativeTimeFormat.h" |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#include "mozilla/FloatingPoint.h" |
| 13 |
#include "mozilla/intl/RelativeTimeFormat.h" |
| 14 |
|
| 15 |
#include "builtin/intl/CommonFunctions.h" |
| 16 |
#include "builtin/intl/FormatBuffer.h" |
| 17 |
#include "builtin/intl/LanguageTag.h" |
| 18 |
#include "gc/GCContext.h" |
| 19 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 20 |
#include "js/Printer.h" |
| 21 |
#include "js/PropertySpec.h" |
| 22 |
#include "vm/GlobalObject.h" |
| 23 |
#include "vm/JSContext.h" |
| 24 |
#include "vm/PlainObject.h" // js::PlainObject |
| 25 |
#include "vm/StringType.h" |
| 26 |
|
| 27 |
#include "vm/NativeObject-inl.h" |
| 28 |
|
| 29 |
using namespace js; |
| 30 |
|
| 31 |
/**************** RelativeTimeFormat *****************/ |
| 32 |
|
| 33 |
const JSClassOps RelativeTimeFormatObject::classOps_ = { |
| 34 |
nullptr, // addProperty |
| 35 |
nullptr, // delProperty |
| 36 |
nullptr, // enumerate |
| 37 |
nullptr, // newEnumerate |
| 38 |
nullptr, // resolve |
| 39 |
nullptr, // mayResolve |
| 40 |
RelativeTimeFormatObject::finalize, // finalize |
| 41 |
nullptr, // call |
| 42 |
nullptr, // construct |
| 43 |
nullptr, // trace |
| 44 |
}; |
| 45 |
|
| 46 |
const JSClass RelativeTimeFormatObject::class_ = { |
| 47 |
"Intl.RelativeTimeFormat", |
| 48 |
JSCLASS_HAS_RESERVED_SLOTS(RelativeTimeFormatObject::SLOT_COUNT) | |
| 49 |
JSCLASS_HAS_CACHED_PROTO(JSProto_RelativeTimeFormat) | |
| 50 |
JSCLASS_FOREGROUND_FINALIZE, |
| 51 |
&RelativeTimeFormatObject::classOps_, |
| 52 |
&RelativeTimeFormatObject::classSpec_, |
| 53 |
}; |
| 54 |
|
| 55 |
const JSClass& RelativeTimeFormatObject::protoClass_ = PlainObject::class_; |
| 56 |
|
| 57 |
static bool relativeTimeFormat_toSource(JSContext* cx, unsigned argc, |
| 58 |
Value* vp) { |
| 59 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 60 |
args.rval().setString(cx->names().RelativeTimeFormat); |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
static const JSFunctionSpec relativeTimeFormat_static_methods[] = { |
| 65 |
JS_SELF_HOSTED_FN("supportedLocalesOf", |
| 66 |
"Intl_RelativeTimeFormat_supportedLocalesOf", 1, 0), |
| 67 |
JS_FS_END, |
| 68 |
}; |
| 69 |
|
| 70 |
static const JSFunctionSpec relativeTimeFormat_methods[] = { |
| 71 |
JS_SELF_HOSTED_FN("resolvedOptions", |
| 72 |
"Intl_RelativeTimeFormat_resolvedOptions", 0, 0), |
| 73 |
JS_SELF_HOSTED_FN("format", "Intl_RelativeTimeFormat_format", 2, 0), |
| 74 |
JS_SELF_HOSTED_FN("formatToParts", "Intl_RelativeTimeFormat_formatToParts", |
| 75 |
2, 0), |
| 76 |
JS_FN("toSource", relativeTimeFormat_toSource, 0, 0), |
| 77 |
JS_FS_END, |
| 78 |
}; |
| 79 |
|
| 80 |
static const JSPropertySpec relativeTimeFormat_properties[] = { |
| 81 |
JS_STRING_SYM_PS(toStringTag, "Intl.RelativeTimeFormat", JSPROP_READONLY), |
| 82 |
JS_PS_END, |
| 83 |
}; |
| 84 |
|
| 85 |
static bool RelativeTimeFormat(JSContext* cx, unsigned argc, Value* vp); |
| 86 |
|
| 87 |
const ClassSpec RelativeTimeFormatObject::classSpec_ = { |
| 88 |
GenericCreateConstructor<RelativeTimeFormat, 0, gc::AllocKind::FUNCTION>, |
| 89 |
GenericCreatePrototype<RelativeTimeFormatObject>, |
| 90 |
relativeTimeFormat_static_methods, |
| 91 |
nullptr, |
| 92 |
relativeTimeFormat_methods, |
| 93 |
relativeTimeFormat_properties, |
| 94 |
nullptr, |
| 95 |
ClassSpec::DontDefineConstructor, |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* RelativeTimeFormat constructor. |
| 100 |
* Spec: ECMAScript 402 API, RelativeTimeFormat, 1.1 |
| 101 |
*/ |
| 102 |
static bool RelativeTimeFormat(JSContext* cx, unsigned argc, Value* vp) { |
| 103 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 104 |
|
| 105 |
// Step 1. |
| 106 |
if (!ThrowIfNotConstructing(cx, args, "Intl.RelativeTimeFormat")) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
// Step 2 (Inlined 9.1.14, OrdinaryCreateFromConstructor). |
| 111 |
RootedObject proto(cx); |
| 112 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_RelativeTimeFormat, |
| 113 |
&proto)) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
Rooted<RelativeTimeFormatObject*> relativeTimeFormat(cx); |
| 118 |
relativeTimeFormat = |
| 119 |
NewObjectWithClassProto<RelativeTimeFormatObject>(cx, proto); |
| 120 |
if (!relativeTimeFormat) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
HandleValue locales = args.get(0); |
| 125 |
HandleValue options = args.get(1); |
| 126 |
|
| 127 |
// Step 3. |
| 128 |
if (!intl::InitializeObject(cx, relativeTimeFormat, |
| 129 |
cx->names().InitializeRelativeTimeFormat, locales, |
| 130 |
options)) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
args.rval().setObject(*relativeTimeFormat); |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
void js::RelativeTimeFormatObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 139 |
MOZ_ASSERT(gcx->onMainThread()); |
| 140 |
|
| 141 |
if (mozilla::intl::RelativeTimeFormat* rtf = |
| 142 |
obj->as<RelativeTimeFormatObject>().getRelativeTimeFormatter()) { |
| 143 |
intl::RemoveICUCellMemory(gcx, obj, |
| 144 |
RelativeTimeFormatObject::EstimatedMemoryUse); |
| 145 |
|
| 146 |
// This was allocated using `new` in mozilla::intl::RelativeTimeFormat, |
| 147 |
// so we delete here. |
| 148 |
delete rtf; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns a new URelativeDateTimeFormatter with the locale and options of the |
| 154 |
* given RelativeTimeFormatObject. |
| 155 |
*/ |
| 156 |
static mozilla::intl::RelativeTimeFormat* NewRelativeTimeFormatter( |
| 157 |
JSContext* cx, Handle<RelativeTimeFormatObject*> relativeTimeFormat) { |
| 158 |
RootedObject internals(cx, intl::GetInternalsObject(cx, relativeTimeFormat)); |
| 159 |
if (!internals) { |
| 160 |
return nullptr; |
| 161 |
} |
| 162 |
|
| 163 |
RootedValue value(cx); |
| 164 |
|
| 165 |
if (!GetProperty(cx, internals, internals, cx->names().locale, &value)) { |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
|
| 169 |
// ICU expects numberingSystem as a Unicode locale extensions on locale. |
| 170 |
|
| 171 |
mozilla::intl::Locale tag; |
| 172 |
{ |
| 173 |
Rooted<JSLinearString*> locale(cx, value.toString()->ensureLinear(cx)); |
| 174 |
if (!locale) { |
| 175 |
return nullptr; |
| 176 |
} |
| 177 |
|
| 178 |
if (!intl::ParseLocale(cx, locale, tag)) { |
| 179 |
return nullptr; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
JS::RootedVector<intl::UnicodeExtensionKeyword> keywords(cx); |
| 184 |
|
| 185 |
if (!GetProperty(cx, internals, internals, cx->names().numberingSystem, |
| 186 |
&value)) { |
| 187 |
return nullptr; |
| 188 |
} |
| 189 |
|
| 190 |
{ |
| 191 |
JSLinearString* numberingSystem = value.toString()->ensureLinear(cx); |
| 192 |
if (!numberingSystem) { |
| 193 |
return nullptr; |
| 194 |
} |
| 195 |
|
| 196 |
if (!keywords.emplaceBack("nu", numberingSystem)) { |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// |ApplyUnicodeExtensionToTag| applies the new keywords to the front of the |
| 202 |
// Unicode extension subtag. We're then relying on ICU to follow RFC 6067, |
| 203 |
// which states that any trailing keywords using the same key should be |
| 204 |
// ignored. |
| 205 |
if (!intl::ApplyUnicodeExtensionToTag(cx, tag, keywords)) { |
| 206 |
return nullptr; |
| 207 |
} |
| 208 |
|
| 209 |
intl::FormatBuffer<char> buffer(cx); |
| 210 |
if (auto result = tag.ToString(buffer); result.isErr()) { |
| 211 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 212 |
return nullptr; |
| 213 |
} |
| 214 |
|
| 215 |
UniqueChars locale = buffer.extractStringZ(); |
| 216 |
if (!locale) { |
| 217 |
return nullptr; |
| 218 |
} |
| 219 |
|
| 220 |
if (!GetProperty(cx, internals, internals, cx->names().style, &value)) { |
| 221 |
return nullptr; |
| 222 |
} |
| 223 |
|
| 224 |
using RelativeTimeFormatOptions = mozilla::intl::RelativeTimeFormatOptions; |
| 225 |
RelativeTimeFormatOptions options; |
| 226 |
{ |
| 227 |
JSLinearString* style = value.toString()->ensureLinear(cx); |
| 228 |
if (!style) { |
| 229 |
return nullptr; |
| 230 |
} |
| 231 |
|
| 232 |
if (StringEqualsLiteral(style, "short")) { |
| 233 |
options.style = RelativeTimeFormatOptions::Style::Short; |
| 234 |
} else if (StringEqualsLiteral(style, "narrow")) { |
| 235 |
options.style = RelativeTimeFormatOptions::Style::Narrow; |
| 236 |
} else { |
| 237 |
MOZ_ASSERT(StringEqualsLiteral(style, "long")); |
| 238 |
options.style = RelativeTimeFormatOptions::Style::Long; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
if (!GetProperty(cx, internals, internals, cx->names().numeric, &value)) { |
| 243 |
return nullptr; |
| 244 |
} |
| 245 |
|
| 246 |
{ |
| 247 |
JSLinearString* numeric = value.toString()->ensureLinear(cx); |
| 248 |
if (!numeric) { |
| 249 |
return nullptr; |
| 250 |
} |
| 251 |
|
| 252 |
if (StringEqualsLiteral(numeric, "auto")) { |
| 253 |
options.numeric = RelativeTimeFormatOptions::Numeric::Auto; |
| 254 |
} else { |
| 255 |
MOZ_ASSERT(StringEqualsLiteral(numeric, "always")); |
| 256 |
options.numeric = RelativeTimeFormatOptions::Numeric::Always; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
using RelativeTimeFormat = mozilla::intl::RelativeTimeFormat; |
| 261 |
mozilla::Result<mozilla::UniquePtr<RelativeTimeFormat>, |
| 262 |
mozilla::intl::ICUError> |
| 263 |
result = RelativeTimeFormat::TryCreate(locale.get(), options); |
| 264 |
|
| 265 |
if (result.isOk()) { |
| 266 |
return result.unwrap().release(); |
| 267 |
} |
| 268 |
|
| 269 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 270 |
return nullptr; |
| 271 |
} |
| 272 |
|
| 273 |
static mozilla::intl::RelativeTimeFormat* GetOrCreateRelativeTimeFormat( |
| 274 |
JSContext* cx, Handle<RelativeTimeFormatObject*> relativeTimeFormat) { |
| 275 |
// Obtain a cached RelativeDateTimeFormatter object. |
| 276 |
mozilla::intl::RelativeTimeFormat* rtf = |
| 277 |
relativeTimeFormat->getRelativeTimeFormatter(); |
| 278 |
if (rtf) { |
| 279 |
return rtf; |
| 280 |
} |
| 281 |
|
| 282 |
rtf = NewRelativeTimeFormatter(cx, relativeTimeFormat); |
| 283 |
if (!rtf) { |
| 284 |
return nullptr; |
| 285 |
} |
| 286 |
relativeTimeFormat->setRelativeTimeFormatter(rtf); |
| 287 |
|
| 288 |
intl::AddICUCellMemory(relativeTimeFormat, |
| 289 |
RelativeTimeFormatObject::EstimatedMemoryUse); |
| 290 |
return rtf; |
| 291 |
} |
| 292 |
|
| 293 |
bool js::intl_FormatRelativeTime(JSContext* cx, unsigned argc, Value* vp) { |
| 294 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 295 |
MOZ_ASSERT(args.length() == 4); |
| 296 |
MOZ_ASSERT(args[0].isObject()); |
| 297 |
MOZ_ASSERT(args[1].isNumber()); |
| 298 |
MOZ_ASSERT(args[2].isString()); |
| 299 |
MOZ_ASSERT(args[3].isBoolean()); |
| 300 |
|
| 301 |
Rooted<RelativeTimeFormatObject*> relativeTimeFormat(cx); |
| 302 |
relativeTimeFormat = &args[0].toObject().as<RelativeTimeFormatObject>(); |
| 303 |
|
| 304 |
bool formatToParts = args[3].toBoolean(); |
| 305 |
|
| 306 |
// PartitionRelativeTimePattern, step 4. |
| 307 |
double t = args[1].toNumber(); |
| 308 |
if (!std::isfinite(t)) { |
| 309 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 310 |
JSMSG_DATE_NOT_FINITE, "RelativeTimeFormat", |
| 311 |
formatToParts ? "formatToParts" : "format"); |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
mozilla::intl::RelativeTimeFormat* rtf = |
| 316 |
GetOrCreateRelativeTimeFormat(cx, relativeTimeFormat); |
| 317 |
if (!rtf) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
intl::FieldType jsUnitType; |
| 322 |
using FormatUnit = mozilla::intl::RelativeTimeFormat::FormatUnit; |
| 323 |
FormatUnit relTimeUnit; |
| 324 |
{ |
| 325 |
JSLinearString* unit = args[2].toString()->ensureLinear(cx); |
| 326 |
if (!unit) { |
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
// PartitionRelativeTimePattern, step 5. |
| 331 |
if (StringEqualsLiteral(unit, "second") || |
| 332 |
StringEqualsLiteral(unit, "seconds")) { |
| 333 |
jsUnitType = &JSAtomState::second; |
| 334 |
relTimeUnit = FormatUnit::Second; |
| 335 |
} else if (StringEqualsLiteral(unit, "minute") || |
| 336 |
StringEqualsLiteral(unit, "minutes")) { |
| 337 |
jsUnitType = &JSAtomState::minute; |
| 338 |
relTimeUnit = FormatUnit::Minute; |
| 339 |
} else if (StringEqualsLiteral(unit, "hour") || |
| 340 |
StringEqualsLiteral(unit, "hours")) { |
| 341 |
jsUnitType = &JSAtomState::hour; |
| 342 |
relTimeUnit = FormatUnit::Hour; |
| 343 |
} else if (StringEqualsLiteral(unit, "day") || |
| 344 |
StringEqualsLiteral(unit, "days")) { |
| 345 |
jsUnitType = &JSAtomState::day; |
| 346 |
relTimeUnit = FormatUnit::Day; |
| 347 |
} else if (StringEqualsLiteral(unit, "week") || |
| 348 |
StringEqualsLiteral(unit, "weeks")) { |
| 349 |
jsUnitType = &JSAtomState::week; |
| 350 |
relTimeUnit = FormatUnit::Week; |
| 351 |
} else if (StringEqualsLiteral(unit, "month") || |
| 352 |
StringEqualsLiteral(unit, "months")) { |
| 353 |
jsUnitType = &JSAtomState::month; |
| 354 |
relTimeUnit = FormatUnit::Month; |
| 355 |
} else if (StringEqualsLiteral(unit, "quarter") || |
| 356 |
StringEqualsLiteral(unit, "quarters")) { |
| 357 |
jsUnitType = &JSAtomState::quarter; |
| 358 |
relTimeUnit = FormatUnit::Quarter; |
| 359 |
} else if (StringEqualsLiteral(unit, "year") || |
| 360 |
StringEqualsLiteral(unit, "years")) { |
| 361 |
jsUnitType = &JSAtomState::year; |
| 362 |
relTimeUnit = FormatUnit::Year; |
| 363 |
} else { |
| 364 |
if (auto unitChars = QuoteString(cx, unit, '"')) { |
| 365 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 366 |
JSMSG_INVALID_OPTION_VALUE, "unit", |
| 367 |
unitChars.get()); |
| 368 |
} |
| 369 |
return false; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
using ICUError = mozilla::intl::ICUError; |
| 374 |
if (formatToParts) { |
| 375 |
mozilla::intl::NumberPartVector parts; |
| 376 |
mozilla::Result<mozilla::Span<const char16_t>, ICUError> result = |
| 377 |
rtf->formatToParts(t, relTimeUnit, parts); |
| 378 |
|
| 379 |
if (result.isErr()) { |
| 380 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
RootedString str(cx, NewStringCopy<CanGC>(cx, result.unwrap())); |
| 385 |
if (!str) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
return js::intl::FormattedRelativeTimeToParts(cx, str, parts, jsUnitType, |
| 390 |
args.rval()); |
| 391 |
} |
| 392 |
|
| 393 |
js::intl::FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> buffer(cx); |
| 394 |
mozilla::Result<Ok, ICUError> result = rtf->format(t, relTimeUnit, buffer); |
| 395 |
|
| 396 |
if (result.isErr()) { |
| 397 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 398 |
return false; |
| 399 |
} |
| 400 |
|
| 401 |
JSString* str = buffer.toString(cx); |
| 402 |
if (!str) { |
| 403 |
return false; |
| 404 |
} |
| 405 |
|
| 406 |
args.rval().setString(str); |
| 407 |
return true; |
| 408 |
} |