| 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.Collator implementation. */ |
| 8 |
|
| 9 |
#include "builtin/intl/Collator.h" |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#include "mozilla/intl/Collator.h" |
| 13 |
#include "mozilla/intl/Locale.h" |
| 14 |
#include "mozilla/Span.h" |
| 15 |
|
| 16 |
#include "builtin/Array.h" |
| 17 |
#include "builtin/intl/CommonFunctions.h" |
| 18 |
#include "builtin/intl/FormatBuffer.h" |
| 19 |
#include "builtin/intl/LanguageTag.h" |
| 20 |
#include "builtin/intl/SharedIntlData.h" |
| 21 |
#include "gc/GCContext.h" |
| 22 |
#include "js/PropertySpec.h" |
| 23 |
#include "js/StableStringChars.h" |
| 24 |
#include "js/TypeDecls.h" |
| 25 |
#include "vm/GlobalObject.h" |
| 26 |
#include "vm/JSContext.h" |
| 27 |
#include "vm/PlainObject.h" // js::PlainObject |
| 28 |
#include "vm/Runtime.h" |
| 29 |
#include "vm/StringType.h" |
| 30 |
|
| 31 |
#include "vm/GeckoProfiler-inl.h" |
| 32 |
#include "vm/JSObject-inl.h" |
| 33 |
|
| 34 |
using namespace js; |
| 35 |
|
| 36 |
using JS::AutoStableStringChars; |
| 37 |
|
| 38 |
using js::intl::ReportInternalError; |
| 39 |
using js::intl::SharedIntlData; |
| 40 |
|
| 41 |
const JSClassOps CollatorObject::classOps_ = { |
| 42 |
nullptr, // addProperty |
| 43 |
nullptr, // delProperty |
| 44 |
nullptr, // enumerate |
| 45 |
nullptr, // newEnumerate |
| 46 |
nullptr, // resolve |
| 47 |
nullptr, // mayResolve |
| 48 |
CollatorObject::finalize, // finalize |
| 49 |
nullptr, // call |
| 50 |
nullptr, // construct |
| 51 |
nullptr, // trace |
| 52 |
}; |
| 53 |
|
| 54 |
const JSClass CollatorObject::class_ = { |
| 55 |
"Intl.Collator", |
| 56 |
JSCLASS_HAS_RESERVED_SLOTS(CollatorObject::SLOT_COUNT) | |
| 57 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Collator) | |
| 58 |
JSCLASS_FOREGROUND_FINALIZE, |
| 59 |
&CollatorObject::classOps_, |
| 60 |
&CollatorObject::classSpec_, |
| 61 |
}; |
| 62 |
|
| 63 |
const JSClass& CollatorObject::protoClass_ = PlainObject::class_; |
| 64 |
|
| 65 |
static bool collator_toSource(JSContext* cx, unsigned argc, Value* vp) { |
| 66 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 67 |
args.rval().setString(cx->names().Collator); |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
static const JSFunctionSpec collator_static_methods[] = { |
| 72 |
JS_SELF_HOSTED_FN("supportedLocalesOf", "Intl_Collator_supportedLocalesOf", |
| 73 |
1, 0), |
| 74 |
JS_FS_END, |
| 75 |
}; |
| 76 |
|
| 77 |
static const JSFunctionSpec collator_methods[] = { |
| 78 |
JS_SELF_HOSTED_FN("resolvedOptions", "Intl_Collator_resolvedOptions", 0, 0), |
| 79 |
JS_FN("toSource", collator_toSource, 0, 0), |
| 80 |
JS_FS_END, |
| 81 |
}; |
| 82 |
|
| 83 |
static const JSPropertySpec collator_properties[] = { |
| 84 |
JS_SELF_HOSTED_GET("compare", "$Intl_Collator_compare_get", 0), |
| 85 |
JS_STRING_SYM_PS(toStringTag, "Intl.Collator", JSPROP_READONLY), |
| 86 |
JS_PS_END, |
| 87 |
}; |
| 88 |
|
| 89 |
static bool Collator(JSContext* cx, unsigned argc, Value* vp); |
| 90 |
|
| 91 |
const ClassSpec CollatorObject::classSpec_ = { |
| 92 |
GenericCreateConstructor<Collator, 0, gc::AllocKind::FUNCTION>, |
| 93 |
GenericCreatePrototype<CollatorObject>, |
| 94 |
collator_static_methods, |
| 95 |
nullptr, |
| 96 |
collator_methods, |
| 97 |
collator_properties, |
| 98 |
nullptr, |
| 99 |
ClassSpec::DontDefineConstructor, |
| 100 |
}; |
| 101 |
|
| 102 |
/** |
| 103 |
* 10.1.2 Intl.Collator([ locales [, options]]) |
| 104 |
* |
| 105 |
* ES2017 Intl draft rev 94045d234762ad107a3d09bb6f7381a65f1a2f9b |
| 106 |
*/ |
| 107 |
static bool Collator(JSContext* cx, const CallArgs& args) { |
| 108 |
AutoJSConstructorProfilerEntry pseudoFrame(cx, "Intl.Collator"); |
| 109 |
|
| 110 |
// Step 1 (Handled by OrdinaryCreateFromConstructor fallback code). |
| 111 |
|
| 112 |
// Steps 2-5 (Inlined 9.1.14, OrdinaryCreateFromConstructor). |
| 113 |
RootedObject proto(cx); |
| 114 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_Collator, &proto)) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
Rooted<CollatorObject*> collator( |
| 119 |
cx, NewObjectWithClassProto<CollatorObject>(cx, proto)); |
| 120 |
if (!collator) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
HandleValue locales = args.get(0); |
| 125 |
HandleValue options = args.get(1); |
| 126 |
|
| 127 |
// Step 6. |
| 128 |
if (!intl::InitializeObject(cx, collator, cx->names().InitializeCollator, |
| 129 |
locales, options)) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
args.rval().setObject(*collator); |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
static bool Collator(JSContext* cx, unsigned argc, Value* vp) { |
| 138 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 139 |
return Collator(cx, args); |
| 140 |
} |
| 141 |
|
| 142 |
bool js::intl_Collator(JSContext* cx, unsigned argc, Value* vp) { |
| 143 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 144 |
MOZ_ASSERT(args.length() == 2); |
| 145 |
MOZ_ASSERT(!args.isConstructing()); |
| 146 |
|
| 147 |
return Collator(cx, args); |
| 148 |
} |
| 149 |
|
| 150 |
void js::CollatorObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 151 |
MOZ_ASSERT(gcx->onMainThread()); |
| 152 |
|
| 153 |
if (mozilla::intl::Collator* coll = obj->as<CollatorObject>().getCollator()) { |
| 154 |
intl::RemoveICUCellMemory(gcx, obj, CollatorObject::EstimatedMemoryUse); |
| 155 |
delete coll; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
bool js::intl_availableCollations(JSContext* cx, unsigned argc, Value* vp) { |
| 160 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 161 |
MOZ_ASSERT(args.length() == 1); |
| 162 |
MOZ_ASSERT(args[0].isString()); |
| 163 |
|
| 164 |
UniqueChars locale = intl::EncodeLocale(cx, args[0].toString()); |
| 165 |
if (!locale) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
auto keywords = |
| 169 |
mozilla::intl::Collator::GetBcp47KeywordValuesForLocale(locale.get()); |
| 170 |
if (keywords.isErr()) { |
| 171 |
ReportInternalError(cx, keywords.unwrapErr()); |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
RootedObject collations(cx, NewDenseEmptyArray(cx)); |
| 176 |
if (!collations) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
// The first element of the collations array must be |null| per |
| 181 |
// ES2017 Intl, 10.2.3 Internal Slots. |
| 182 |
if (!NewbornArrayPush(cx, collations, NullValue())) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
for (auto result : keywords.unwrap()) { |
| 187 |
if (result.isErr()) { |
| 188 |
ReportInternalError(cx); |
| 189 |
return false; |
| 190 |
} |
| 191 |
mozilla::Span<const char> collation = result.unwrap(); |
| 192 |
|
| 193 |
// Per ECMA-402, 10.2.3, we don't include standard and search: |
| 194 |
// "The values 'standard' and 'search' must not be used as elements in |
| 195 |
// any [[sortLocaleData]][locale].co and [[searchLocaleData]][locale].co |
| 196 |
// array." |
| 197 |
static constexpr auto standard = mozilla::MakeStringSpan("standard"); |
| 198 |
static constexpr auto search = mozilla::MakeStringSpan("search"); |
| 199 |
if (collation == standard || collation == search) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
JSString* jscollation = NewStringCopy<CanGC>(cx, collation); |
| 204 |
if (!jscollation) { |
| 205 |
return false; |
| 206 |
} |
| 207 |
if (!NewbornArrayPush(cx, collations, StringValue(jscollation))) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
args.rval().setObject(*collations); |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Returns a new mozilla::intl::Collator with the locale and collation options |
| 218 |
* of the given Collator. |
| 219 |
*/ |
| 220 |
static mozilla::intl::Collator* NewIntlCollator( |
| 221 |
JSContext* cx, Handle<CollatorObject*> collator) { |
| 222 |
RootedValue value(cx); |
| 223 |
|
| 224 |
RootedObject internals(cx, intl::GetInternalsObject(cx, collator)); |
| 225 |
if (!internals) { |
| 226 |
return nullptr; |
| 227 |
} |
| 228 |
|
| 229 |
if (!GetProperty(cx, internals, internals, cx->names().locale, &value)) { |
| 230 |
return nullptr; |
| 231 |
} |
| 232 |
|
| 233 |
mozilla::intl::Locale tag; |
| 234 |
{ |
| 235 |
Rooted<JSLinearString*> locale(cx, value.toString()->ensureLinear(cx)); |
| 236 |
if (!locale) { |
| 237 |
return nullptr; |
| 238 |
} |
| 239 |
|
| 240 |
if (!intl::ParseLocale(cx, locale, tag)) { |
| 241 |
return nullptr; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
using mozilla::intl::Collator; |
| 246 |
|
| 247 |
Collator::Options options{}; |
| 248 |
|
| 249 |
if (!GetProperty(cx, internals, internals, cx->names().usage, &value)) { |
| 250 |
return nullptr; |
| 251 |
} |
| 252 |
|
| 253 |
enum class Usage { Search, Sort }; |
| 254 |
|
| 255 |
Usage usage; |
| 256 |
{ |
| 257 |
JSLinearString* str = value.toString()->ensureLinear(cx); |
| 258 |
if (!str) { |
| 259 |
return nullptr; |
| 260 |
} |
| 261 |
|
| 262 |
if (StringEqualsLiteral(str, "search")) { |
| 263 |
usage = Usage::Search; |
| 264 |
} else { |
| 265 |
MOZ_ASSERT(StringEqualsLiteral(str, "sort")); |
| 266 |
usage = Usage::Sort; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
JS::RootedVector<intl::UnicodeExtensionKeyword> keywords(cx); |
| 271 |
|
| 272 |
// ICU expects collation as Unicode locale extensions on locale. |
| 273 |
if (usage == Usage::Search) { |
| 274 |
if (!keywords.emplaceBack("co", cx->names().search)) { |
| 275 |
return nullptr; |
| 276 |
} |
| 277 |
|
| 278 |
// Search collations can't select a different collation, so the collation |
| 279 |
// property is guaranteed to be "default". |
| 280 |
#ifdef DEBUG |
| 281 |
if (!GetProperty(cx, internals, internals, cx->names().collation, &value)) { |
| 282 |
return nullptr; |
| 283 |
} |
| 284 |
|
| 285 |
JSLinearString* collation = value.toString()->ensureLinear(cx); |
| 286 |
if (!collation) { |
| 287 |
return nullptr; |
| 288 |
} |
| 289 |
|
| 290 |
MOZ_ASSERT(StringEqualsLiteral(collation, "default")); |
| 291 |
#endif |
| 292 |
} else { |
| 293 |
if (!GetProperty(cx, internals, internals, cx->names().collation, &value)) { |
| 294 |
return nullptr; |
| 295 |
} |
| 296 |
|
| 297 |
JSLinearString* collation = value.toString()->ensureLinear(cx); |
| 298 |
if (!collation) { |
| 299 |
return nullptr; |
| 300 |
} |
| 301 |
|
| 302 |
// Set collation as a Unicode locale extension when it was specified. |
| 303 |
if (!StringEqualsLiteral(collation, "default")) { |
| 304 |
if (!keywords.emplaceBack("co", collation)) { |
| 305 |
return nullptr; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// |ApplyUnicodeExtensionToTag| applies the new keywords to the front of the |
| 311 |
// Unicode extension subtag. We're then relying on ICU to follow RFC 6067, |
| 312 |
// which states that any trailing keywords using the same key should be |
| 313 |
// ignored. |
| 314 |
if (!intl::ApplyUnicodeExtensionToTag(cx, tag, keywords)) { |
| 315 |
return nullptr; |
| 316 |
} |
| 317 |
|
| 318 |
intl::FormatBuffer<char> buffer(cx); |
| 319 |
if (auto result = tag.ToString(buffer); result.isErr()) { |
| 320 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 321 |
return nullptr; |
| 322 |
} |
| 323 |
|
| 324 |
UniqueChars locale = buffer.extractStringZ(); |
| 325 |
if (!locale) { |
| 326 |
return nullptr; |
| 327 |
} |
| 328 |
|
| 329 |
if (!GetProperty(cx, internals, internals, cx->names().sensitivity, &value)) { |
| 330 |
return nullptr; |
| 331 |
} |
| 332 |
|
| 333 |
{ |
| 334 |
JSLinearString* sensitivity = value.toString()->ensureLinear(cx); |
| 335 |
if (!sensitivity) { |
| 336 |
return nullptr; |
| 337 |
} |
| 338 |
if (StringEqualsLiteral(sensitivity, "base")) { |
| 339 |
options.sensitivity = Collator::Sensitivity::Base; |
| 340 |
} else if (StringEqualsLiteral(sensitivity, "accent")) { |
| 341 |
options.sensitivity = Collator::Sensitivity::Accent; |
| 342 |
} else if (StringEqualsLiteral(sensitivity, "case")) { |
| 343 |
options.sensitivity = Collator::Sensitivity::Case; |
| 344 |
} else { |
| 345 |
MOZ_ASSERT(StringEqualsLiteral(sensitivity, "variant")); |
| 346 |
options.sensitivity = Collator::Sensitivity::Variant; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if (!GetProperty(cx, internals, internals, cx->names().ignorePunctuation, |
| 351 |
&value)) { |
| 352 |
return nullptr; |
| 353 |
} |
| 354 |
options.ignorePunctuation = value.toBoolean(); |
| 355 |
|
| 356 |
if (!GetProperty(cx, internals, internals, cx->names().numeric, &value)) { |
| 357 |
return nullptr; |
| 358 |
} |
| 359 |
if (!value.isUndefined()) { |
| 360 |
options.numeric = value.toBoolean(); |
| 361 |
} |
| 362 |
|
| 363 |
if (!GetProperty(cx, internals, internals, cx->names().caseFirst, &value)) { |
| 364 |
return nullptr; |
| 365 |
} |
| 366 |
if (!value.isUndefined()) { |
| 367 |
JSLinearString* caseFirst = value.toString()->ensureLinear(cx); |
| 368 |
if (!caseFirst) { |
| 369 |
return nullptr; |
| 370 |
} |
| 371 |
if (StringEqualsLiteral(caseFirst, "upper")) { |
| 372 |
options.caseFirst = Collator::CaseFirst::Upper; |
| 373 |
} else if (StringEqualsLiteral(caseFirst, "lower")) { |
| 374 |
options.caseFirst = Collator::CaseFirst::Lower; |
| 375 |
} else { |
| 376 |
MOZ_ASSERT(StringEqualsLiteral(caseFirst, "false")); |
| 377 |
options.caseFirst = Collator::CaseFirst::False; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
auto collResult = Collator::TryCreate(locale.get()); |
| 382 |
if (collResult.isErr()) { |
| 383 |
ReportInternalError(cx, collResult.unwrapErr()); |
| 384 |
return nullptr; |
| 385 |
} |
| 386 |
auto coll = collResult.unwrap(); |
| 387 |
|
| 388 |
auto optResult = coll->SetOptions(options); |
| 389 |
if (optResult.isErr()) { |
| 390 |
ReportInternalError(cx, optResult.unwrapErr()); |
| 391 |
return nullptr; |
| 392 |
} |
| 393 |
|
| 394 |
return coll.release(); |
| 395 |
} |
| 396 |
|
| 397 |
static mozilla::intl::Collator* GetOrCreateCollator( |
| 398 |
JSContext* cx, Handle<CollatorObject*> collator) { |
| 399 |
// Obtain a cached mozilla::intl::Collator object. |
| 400 |
mozilla::intl::Collator* coll = collator->getCollator(); |
| 401 |
if (coll) { |
| 402 |
return coll; |
| 403 |
} |
| 404 |
|
| 405 |
coll = NewIntlCollator(cx, collator); |
| 406 |
if (!coll) { |
| 407 |
return nullptr; |
| 408 |
} |
| 409 |
collator->setCollator(coll); |
| 410 |
|
| 411 |
intl::AddICUCellMemory(collator, CollatorObject::EstimatedMemoryUse); |
| 412 |
return coll; |
| 413 |
} |
| 414 |
|
| 415 |
static bool intl_CompareStrings(JSContext* cx, mozilla::intl::Collator* coll, |
| 416 |
HandleString str1, HandleString str2, |
| 417 |
MutableHandleValue result) { |
| 418 |
MOZ_ASSERT(str1); |
| 419 |
MOZ_ASSERT(str2); |
| 420 |
|
| 421 |
if (str1 == str2) { |
| 422 |
result.setInt32(0); |
| 423 |
return true; |
| 424 |
} |
| 425 |
|
| 426 |
AutoStableStringChars stableChars1(cx); |
| 427 |
if (!stableChars1.initTwoByte(cx, str1)) { |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
AutoStableStringChars stableChars2(cx); |
| 432 |
if (!stableChars2.initTwoByte(cx, str2)) { |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
mozilla::Range<const char16_t> chars1 = stableChars1.twoByteRange(); |
| 437 |
mozilla::Range<const char16_t> chars2 = stableChars2.twoByteRange(); |
| 438 |
|
| 439 |
result.setInt32(coll->CompareStrings(chars1, chars2)); |
| 440 |
return true; |
| 441 |
} |
| 442 |
|
| 443 |
bool js::intl_CompareStrings(JSContext* cx, unsigned argc, Value* vp) { |
| 444 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 445 |
MOZ_ASSERT(args.length() == 3); |
| 446 |
MOZ_ASSERT(args[0].isObject()); |
| 447 |
MOZ_ASSERT(args[1].isString()); |
| 448 |
MOZ_ASSERT(args[2].isString()); |
| 449 |
|
| 450 |
Rooted<CollatorObject*> collator(cx, |
| 451 |
&args[0].toObject().as<CollatorObject>()); |
| 452 |
|
| 453 |
mozilla::intl::Collator* coll = GetOrCreateCollator(cx, collator); |
| 454 |
if (!coll) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
// Use the UCollator to actually compare the strings. |
| 459 |
RootedString str1(cx, args[1].toString()); |
| 460 |
RootedString str2(cx, args[2].toString()); |
| 461 |
return intl_CompareStrings(cx, coll, str1, str2, args.rval()); |
| 462 |
} |
| 463 |
|
| 464 |
bool js::intl_isUpperCaseFirst(JSContext* cx, unsigned argc, Value* vp) { |
| 465 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 466 |
MOZ_ASSERT(args.length() == 1); |
| 467 |
MOZ_ASSERT(args[0].isString()); |
| 468 |
|
| 469 |
SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 470 |
|
| 471 |
RootedString locale(cx, args[0].toString()); |
| 472 |
bool isUpperFirst; |
| 473 |
if (!sharedIntlData.isUpperCaseFirst(cx, locale, &isUpperFirst)) { |
| 474 |
return false; |
| 475 |
} |
| 476 |
|
| 477 |
args.rval().setBoolean(isUpperFirst); |
| 478 |
return true; |
| 479 |
} |
| 480 |
|
| 481 |
bool js::intl_isIgnorePunctuation(JSContext* cx, unsigned argc, Value* vp) { |
| 482 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 483 |
MOZ_ASSERT(args.length() == 1); |
| 484 |
MOZ_ASSERT(args[0].isString()); |
| 485 |
|
| 486 |
SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 487 |
|
| 488 |
RootedString locale(cx, args[0].toString()); |
| 489 |
bool isIgnorePunctuation; |
| 490 |
if (!sharedIntlData.isIgnorePunctuation(cx, locale, &isIgnorePunctuation)) { |
| 491 |
return false; |
| 492 |
} |
| 493 |
|
| 494 |
args.rval().setBoolean(isIgnorePunctuation); |
| 495 |
return true; |
| 496 |
} |