1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use boa_gc::{custom_trace, Finalize, Trace};
use boa_profiler::Profiler;
use icu_collator::{
provider::CollationMetadataV1Marker, AlternateHandling, CaseFirst, MaxVariable, Numeric,
};
use icu_locid::{
extensions::unicode::Value, extensions_unicode_key as key, extensions_unicode_value as value,
Locale,
};
use icu_provider::DataLocale;
use crate::{
builtins::{BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
context::{
intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
BoaProvider,
},
native_function::NativeFunction,
object::{
internal_methods::get_prototype_from_constructor, FunctionObjectBuilder, JsFunction,
JsObject, ObjectData,
},
property::Attribute,
realm::Realm,
string::utf16,
symbol::JsSymbol,
Context, JsArgs, JsNativeError, JsResult, JsValue,
};
use super::{
locale::{canonicalize_locale_list, resolve_locale, supported_locales, validate_extension},
options::{coerce_options_to_object, get_option, IntlOptions, LocaleMatcher},
Service,
};
mod options;
pub(crate) use options::*;
pub struct Collator {
locale: Locale,
collation: Value,
numeric: bool,
case_first: Option<CaseFirst>,
usage: Usage,
sensitivity: Sensitivity,
ignore_punctuation: bool,
collator: icu_collator::Collator,
bound_compare: Option<JsFunction>,
}
impl Finalize for Collator {}
// SAFETY: only `bound_compare` is a traceable object.
unsafe impl Trace for Collator {
custom_trace!(this, mark(&this.bound_compare));
}
impl Collator {
/// Gets the inner [`icu_collator::Collator`] comparator.
pub(crate) const fn collator(&self) -> &icu_collator::Collator {
&self.collator
}
}
impl std::fmt::Debug for Collator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Collator")
.field("locale", &self.locale)
.field("collation", &self.collation)
.field("numeric", &self.numeric)
.field("case_first", &self.case_first)
.field("usage", &self.usage)
.field("sensitivity", &self.sensitivity)
.field("ignore_punctuation", &self.ignore_punctuation)
.field("collator", &"ICUCollator")
.field("bound_compare", &self.bound_compare)
.finish()
}
}
#[derive(Debug, Clone)]
pub(in crate::builtins::intl) struct CollatorLocaleOptions {
collation: Option<Value>,
numeric: Option<bool>,
case_first: Option<CaseFirst>,
}
impl Service for Collator {
type LangMarker = CollationMetadataV1Marker;
type LocaleOptions = CollatorLocaleOptions;
fn resolve(locale: &mut Locale, options: &mut Self::LocaleOptions, provider: BoaProvider<'_>) {
let collation = options
.collation
.take()
.filter(|co| {
validate_extension::<Self::LangMarker>(locale.id.clone(), key!("co"), co, &provider)
})
.or_else(|| {
locale
.extensions
.unicode
.keywords
.get(&key!("co"))
.cloned()
.filter(|co| {
validate_extension::<Self::LangMarker>(
locale.id.clone(),
key!("co"),
co,
&provider,
)
})
})
.filter(|co| co != &value!("search"));
let numeric =
options.numeric.or_else(
|| match locale.extensions.unicode.keywords.get(&key!("kn")) {
Some(a) if a == &value!("true") => Some(true),
Some(_) => Some(false),
_ => None,
},
);
let case_first = options.case_first.or_else(|| {
match locale.extensions.unicode.keywords.get(&key!("kf")) {
Some(a) if a == &value!("upper") => Some(CaseFirst::UpperFirst),
Some(a) if a == &value!("lower") => Some(CaseFirst::LowerFirst),
Some(_) => Some(CaseFirst::Off),
_ => None,
}
});
locale.extensions.unicode.clear();
if let Some(co) = collation.clone() {
locale.extensions.unicode.keywords.set(key!("co"), co);
}
if let Some(kn) = numeric.map(|kn| if kn { value!("true") } else { value!("false") }) {
locale.extensions.unicode.keywords.set(key!("kn"), kn);
}
if let Some(kf) = case_first.map(|kf| match kf {
CaseFirst::Off => value!("false"),
CaseFirst::LowerFirst => value!("lower"),
CaseFirst::UpperFirst => value!("upper"),
_ => unreachable!(),
}) {
locale.extensions.unicode.keywords.set(key!("kf"), kf);
}
options.collation = collation;
options.numeric = numeric;
options.case_first = case_first;
}
}
impl IntrinsicObject for Collator {
fn init(realm: &Realm) {
let _timer = Profiler::global().start_event(Self::NAME, "init");
let compare = BuiltInBuilder::callable(realm, Self::compare)
.name("get compare")
.build();
BuiltInBuilder::from_standard_constructor::<Self>(realm)
.static_method(Self::supported_locales_of, "supportedLocalesOf", 1)
.property(
JsSymbol::to_string_tag(),
"Intl.Collator",
Attribute::CONFIGURABLE,
)
.accessor(
utf16!("compare"),
Some(compare),
None,
Attribute::CONFIGURABLE,
)
.method(Self::resolved_options, "resolvedOptions", 0)
.build();
}
fn get(intrinsics: &Intrinsics) -> JsObject {
Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
}
}
impl BuiltInObject for Collator {
const NAME: &'static str = "Collator";
}
impl BuiltInConstructor for Collator {
const LENGTH: usize = 0;
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::collator;
/// Constructor [`Intl.Collator ( [ locales [ , options ] ] )`][spec].
///
/// Constructor for `Collator` objects.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma402/#sec-intl.collator
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator
fn constructor(
new_target: &JsValue,
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
// 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
let new_target = &if new_target.is_undefined() {
context
.vm
.active_function
.clone()
.unwrap_or_else(|| context.intrinsics().constructors().collator().constructor())
.into()
} else {
new_target.clone()
};
// 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
// 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then
// a. Append [[Numeric]] as the last element of internalSlotsList.
// 4. If %Collator%.[[RelevantExtensionKeys]] contains "kf", then
// a. Append [[CaseFirst]] as the last element of internalSlotsList.
// 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Collator.prototype%", internalSlotsList).
// 6. Return ? InitializeCollator(collator, locales, options).
let locales = args.get_or_undefined(0);
let options = args.get_or_undefined(1);
// Abstract operation `InitializeCollator ( collator, locales, options )`
// https://tc39.es/ecma402/#sec-initializecollator
// 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
let requested_locales = canonicalize_locale_list(locales, context)?;
// 2. Set options to ? CoerceOptionsToObject(options).
let options = coerce_options_to_object(options, context)?;
// 3. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort").
// 4. Set collator.[[Usage]] to usage.
// 5. If usage is "sort", then
// a. Let localeData be %Collator%.[[SortLocaleData]].
// 6. Else,
// a. Let localeData be %Collator%.[[SearchLocaleData]].
let usage =
get_option::<Usage>(&options, utf16!("usage"), false, context)?.unwrap_or_default();
// 7. Let opt be a new Record.
// 8. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
// 9. Set opt.[[localeMatcher]] to matcher.
let matcher =
get_option::<LocaleMatcher>(&options, utf16!("localeMatcher"), false, context)?
.unwrap_or_default();
// 10. Let collation be ? GetOption(options, "collation", string, empty, undefined).
// 11. If collation is not undefined, then
// a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
// 12. Set opt.[[co]] to collation.
let collation = get_option::<Value>(&options, utf16!("collation"), false, context)?;
// 13. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined).
// 14. If numeric is not undefined, then
// a. Let numeric be ! ToString(numeric).
// 15. Set opt.[[kn]] to numeric.
let numeric = get_option::<bool>(&options, utf16!("numeric"), false, context)?;
// 16. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined).
// 17. Set opt.[[kf]] to caseFirst.
let case_first = get_option::<CaseFirst>(&options, utf16!("caseFirst"), false, context)?;
let mut intl_options = IntlOptions {
matcher,
service_options: CollatorLocaleOptions {
collation,
numeric,
case_first,
},
};
// 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]].
// 19. Let r be ResolveLocale(%Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
let mut locale =
resolve_locale::<Self>(&requested_locales, &mut intl_options, context.icu());
let collator_locale = {
// `collator_locale` needs to be different from the resolved locale because ECMA402 doesn't
// define `search` as a resolvable extension of a locale, so we need to add that extension
// only to the locale passed to the collator.
let mut col_loc = DataLocale::from(&locale);
if usage == Usage::Search {
intl_options.service_options.collation = None;
locale.extensions.unicode.keywords.remove(key!("co"));
col_loc.set_unicode_ext(key!("co"), value!("search"));
}
col_loc
};
// 20. Set collator.[[Locale]] to r.[[locale]].
// 21. Let collation be r.[[co]].
// 22. If collation is null, let collation be "default".
// 23. Set collator.[[Collation]] to collation.
let collation = intl_options
.service_options
.collation
.unwrap_or(value!("default"));
// 24. If relevantExtensionKeys contains "kn", then
// a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
let numeric = intl_options.service_options.numeric.unwrap_or_default();
// 25. If relevantExtensionKeys contains "kf", then
// a. Set collator.[[CaseFirst]] to r.[[kf]].
let case_first = intl_options.service_options.case_first;
// 26. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined).
// 28. Set collator.[[Sensitivity]] to sensitivity.
let sensitivity =
get_option::<Sensitivity>(&options, utf16!("sensitivity"), false, context)?
// 27. If sensitivity is undefined, then
// a. If usage is "sort", then
// i. Let sensitivity be "variant".
// b. Else,
// i. Let dataLocale be r.[[dataLocale]].
// ii. Let dataLocaleData be localeData.[[<dataLocale>]].
// iii. Let sensitivity be dataLocaleData.[[sensitivity]].
.or_else(|| (usage == Usage::Sort).then_some(Sensitivity::Variant));
// 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, false).
// 30. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
let ignore_punctuation =
get_option::<bool>(&options, utf16!("ignorePunctuation"), false, context)?
.unwrap_or_default();
let (strength, case_level) = sensitivity.map(Sensitivity::to_collator_options).unzip();
let (alternate_handling, max_variable) = ignore_punctuation
.then_some((AlternateHandling::Shifted, MaxVariable::Punctuation))
.unzip();
let collator = context
.icu()
.provider()
.try_new_collator(&collator_locale, {
let mut options = icu_collator::CollatorOptions::new();
options.strength = strength;
options.case_level = case_level;
options.case_first = case_first;
options.numeric = Some(if numeric { Numeric::On } else { Numeric::Off });
options.alternate_handling = alternate_handling;
options.max_variable = max_variable;
options
})
.map_err(|e| JsNativeError::typ().with_message(e.to_string()))?;
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::collator, context)?;
let collator = JsObject::from_proto_and_data_with_shared_shape(
context.root_shape(),
prototype,
ObjectData::collator(Self {
locale,
collation,
numeric,
case_first,
usage,
sensitivity: sensitivity.unwrap_or(Sensitivity::Variant),
ignore_punctuation,
collator,
bound_compare: None,
}),
);
// 31. Return collator.
Ok(collator.into())
}
}
impl Collator {
/// [`Intl.Collator.supportedLocalesOf ( locales [ , options ] )`][spec].
///
/// Returns an array containing those of the provided locales that are supported in collation
/// without having to fall back to the runtime's default locale.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma402/#sec-intl.collator.supportedlocalesof
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/supportedLocalesOf
fn supported_locales_of(
_: &JsValue,
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
let locales = args.get_or_undefined(0);
let options = args.get_or_undefined(1);
// 1. Let availableLocales be %Collator%.[[AvailableLocales]].
// 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
let requested_locales = canonicalize_locale_list(locales, context)?;
// 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
supported_locales::<<Self as Service>::LangMarker>(&requested_locales, options, context)
.map(JsValue::from)
}
/// [`get Intl.Collator.prototype.compare`][spec].
///
/// Compares two strings according to the sort order of this Intl.Collator object.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma402/#sec-intl.collator.prototype.compare
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/compare
fn compare(this: &JsValue, _: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
// 1. Let collator be the this value.
// 2. Perform ? RequireInternalSlot(collator, [[InitializedCollator]]).
let this = this.as_object().ok_or_else(|| {
JsNativeError::typ()
.with_message("`resolvedOptions` can only be called on a `Collator` object")
})?;
let collator_obj = this.clone();
let mut collator = this.borrow_mut();
let collator = collator.as_collator_mut().ok_or_else(|| {
JsNativeError::typ()
.with_message("`resolvedOptions` can only be called on a `Collator` object")
})?;
// 3. If collator.[[BoundCompare]] is undefined, then
// a. Let F be a new built-in function object as defined in 10.3.3.1.
// b. Set F.[[Collator]] to collator.
// c. Set collator.[[BoundCompare]] to F.
let bound_compare = if let Some(f) = collator.bound_compare.clone() {
f
} else {
let bound_compare = FunctionObjectBuilder::new(
context,
// 10.3.3.1. Collator Compare Functions
// https://tc39.es/ecma402/#sec-collator-compare-functions
NativeFunction::from_copy_closure_with_captures(
|_, args, collator, context| {
// 1. Let collator be F.[[Collator]].
// 2. Assert: Type(collator) is Object and collator has an [[InitializedCollator]] internal slot.
let collator = collator.borrow();
let collator = collator
.as_collator()
.expect("checked above that the object was a collator object");
// 3. If x is not provided, let x be undefined.
// 5. Let X be ? ToString(x).
let x = args.get_or_undefined(0).to_string(context)?;
// 4. If y is not provided, let y be undefined.
// 6. Let Y be ? ToString(y).
let y = args.get_or_undefined(1).to_string(context)?;
// 7. Return CompareStrings(collator, X, Y).
let result = collator.collator.compare_utf16(&x, &y) as i32;
Ok(result.into())
},
collator_obj,
),
)
.length(2)
.build();
collator.bound_compare = Some(bound_compare.clone());
bound_compare
};
// 4. Return collator.[[BoundCompare]].
Ok(bound_compare.into())
}
/// [`Intl.Collator.prototype.resolvedOptions ( )`][spec].
///
/// Returns a new object with properties reflecting the locale and collation options computed
/// during initialization of this `Intl.Collator` object.
///
/// More information:
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma402/#sec-intl.collator.prototype.resolvedoptions
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/resolvedOptions
fn resolved_options(
this: &JsValue,
_: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
// 1. Let collator be the this value.
// 2. Perform ? RequireInternalSlot(collator, [[InitializedCollator]]).
let collator = this.as_object().map(JsObject::borrow).ok_or_else(|| {
JsNativeError::typ()
.with_message("`resolvedOptions` can only be called on a `Collator` object")
})?;
let collator = collator.as_collator().ok_or_else(|| {
JsNativeError::typ()
.with_message("`resolvedOptions` can only be called on a `Collator` object")
})?;
// 3. Let options be OrdinaryObjectCreate(%Object.prototype%).
let options = context
.intrinsics()
.templates()
.ordinary_object()
.create(ObjectData::ordinary(), vec![]);
// 4. For each row of Table 4, except the header row, in table order, do
// a. Let p be the Property value of the current row.
// b. Let v be the value of collator's internal slot whose name is the Internal Slot value of the current row.
// c. If the current row has an Extension Key value, then
// i. Let extensionKey be the Extension Key value of the current row.
// ii. If %Collator%.[[RelevantExtensionKeys]] does not contain extensionKey, then
// 1. Let v be undefined.
// d. If v is not undefined, then
// i. Perform ! CreateDataPropertyOrThrow(options, p, v).
// 5. Return options.
options
.create_data_property_or_throw(utf16!("locale"), collator.locale.to_string(), context)
.expect("operation must not fail per the spec");
options
.create_data_property_or_throw(
utf16!("usage"),
match collator.usage {
Usage::Search => "search",
Usage::Sort => "sort",
},
context,
)
.expect("operation must not fail per the spec");
options
.create_data_property_or_throw(
utf16!("sensitivity"),
match collator.sensitivity {
Sensitivity::Base => "base",
Sensitivity::Accent => "accent",
Sensitivity::Case => "case",
Sensitivity::Variant => "variant",
},
context,
)
.expect("operation must not fail per the spec");
options
.create_data_property_or_throw(
utf16!("ignorePunctuation"),
collator.ignore_punctuation,
context,
)
.expect("operation must not fail per the spec");
options
.create_data_property_or_throw(
utf16!("collation"),
collator.collation.to_string(),
context,
)
.expect("operation must not fail per the spec");
options
.create_data_property_or_throw(utf16!("numeric"), collator.numeric, context)
.expect("operation must not fail per the spec");
if let Some(kf) = collator.case_first {
options
.create_data_property_or_throw(
utf16!("caseFirst"),
match kf {
CaseFirst::Off => "false",
CaseFirst::LowerFirst => "lower",
CaseFirst::UpperFirst => "upper",
_ => unreachable!(),
},
context,
)
.expect("operation must not fail per the spec");
}
// 5. Return options.
Ok(options.into())
}
}