Debian Codegraph @

js/src/builtin/intl/CommonFunctions.js

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /* Portions Copyright Norbert Lindenberg 2011-2012. */
6
7 #ifdef DEBUG
8 #define JS_CONCAT2(x, y) x##y
9 #define JS_CONCAT(x, y) JS_CONCAT2(x, y)
10 #define assertIsValidAndCanonicalLanguageTag(locale, desc) \
11 do { \
12 var JS_CONCAT(canonical, __LINE__) = intl_TryValidateAndCanonicalizeLanguageTag(locale); \
13 assert(JS_CONCAT(canonical, __LINE__) !== null, \
14 `${desc} is a structurally valid language tag`); \
15 assert(JS_CONCAT(canonical, __LINE__) === locale, \
16 `${desc} is a canonicalized language tag`); \
17 } while (false)
18 #else
19 #define assertIsValidAndCanonicalLanguageTag(locale, desc) ; // Elided assertion.
20 #endif
21
22 /**
23 * Returns the start index of a "Unicode locale extension sequence", which the
24 * specification defines as: "any substring of a language tag that starts with
25 * a separator '-' and the singleton 'u' and includes the maximum sequence of
26 * following non-singleton subtags and their preceding '-' separators."
27 *
28 * Alternatively, this may be defined as: the components of a language tag that
29 * match the `unicode_locale_extensions` production in UTS 35.
30 *
31 * Spec: ECMAScript Internationalization API Specification, 6.2.1.
32 */
33 function startOfUnicodeExtensions(locale) {
34 assert(typeof locale === "string", "locale is a string");
35
36 // Search for "-u-" marking the start of a Unicode extension sequence.
37 var start = callFunction(std_String_indexOf, locale, "-u-");
38 if (start < 0) {
39 return -1;
40 }
41
42 // And search for "-x-" marking the start of any privateuse component to
43 // handle the case when "-u-" was only found within a privateuse subtag.
44 var privateExt = callFunction(std_String_indexOf, locale, "-x-");
45 if (privateExt >= 0 && privateExt < start) {
46 return -1;
47 }
48
49 return start;
50 }
51
52 /**
53 * Returns the end index of a Unicode locale extension sequence.
54 */
55 function endOfUnicodeExtensions(locale, start) {
56 assert(typeof locale === "string", "locale is a string");
57 assert(0 <= start && start < locale.length, "start is an index into locale");
58 assert(
59 Substring(locale, start, 3) === "-u-",
60 "start points to Unicode extension sequence"
61 );
62
63 // Search for the start of the next singleton or privateuse subtag.
64 //
65 // Begin searching after the smallest possible Unicode locale extension
66 // sequence, namely |"-u-" 2alphanum|. End searching once the remaining
67 // characters can't fit the smallest possible singleton or privateuse
68 // subtag, namely |"-x-" alphanum|. Note the reduced end-limit means
69 // indexing inside the loop is always in-range.
70 for (var i = start + 5, end = locale.length - 4; i <= end; i++) {
71 if (locale[i] !== "-") {
72 continue;
73 }
74 if (locale[i + 2] === "-") {
75 return i;
76 }
77
78 // Skip over (i + 1) and (i + 2) because we've just verified they
79 // aren't "-", so the next possible delimiter can only be at (i + 3).
80 i += 2;
81 }
82
83 // If no singleton or privateuse subtag was found, the Unicode extension
84 // sequence extends until the end of the string.
85 return locale.length;
86 }
87
88 /**
89 * Removes Unicode locale extension sequences from the given language tag.
90 */
91 function removeUnicodeExtensions(locale) {
92 assertIsValidAndCanonicalLanguageTag(
93 locale,
94 "locale with possible Unicode extension"
95 );
96
97 var start = startOfUnicodeExtensions(locale);
98 if (start < 0) {
99 return locale;
100 }
101
102 var end = endOfUnicodeExtensions(locale, start);
103
104 var left = Substring(locale, 0, start);
105 var right = Substring(locale, end, locale.length - end);
106 var combined = left + right;
107
108 assertIsValidAndCanonicalLanguageTag(combined, "the recombined locale");
109 assert(
110 startOfUnicodeExtensions(combined) < 0,
111 "recombination failed to remove all Unicode locale extension sequences"
112 );
113
114 return combined;
115 }
116
117 /**
118 * Returns Unicode locale extension sequences from the given language tag.
119 */
120 function getUnicodeExtensions(locale) {
121 assertIsValidAndCanonicalLanguageTag(locale, "locale with Unicode extension");
122
123 var start = startOfUnicodeExtensions(locale);
124 assert(start >= 0, "start of Unicode extension sequence not found");
125 var end = endOfUnicodeExtensions(locale, start);
126
127 return Substring(locale, start, end - start);
128 }
129
130 /**
131 * Returns true if the input contains only ASCII alphabetical characters.
132 */
133 function IsASCIIAlphaString(s) {
134 assert(typeof s === "string", "IsASCIIAlphaString");
135
136 for (var i = 0; i < s.length; i++) {
137 var c = callFunction(std_String_charCodeAt, s, i);
138 if (!((0x41 <= c && c <= 0x5a) || (0x61 <= c && c <= 0x7a))) {
139 return false;
140 }
141 }
142 return true;
143 }
144
145 var localeCache = {
146 runtimeDefaultLocale: undefined,
147 defaultLocale: undefined,
148 };
149
150 /**
151 * Returns the BCP 47 language tag for the host environment's current locale.
152 *
153 * Spec: ECMAScript Internationalization API Specification, 6.2.4.
154 */
155 function DefaultLocale() {
156 if (intl_IsRuntimeDefaultLocale(localeCache.runtimeDefaultLocale)) {
157 return localeCache.defaultLocale;
158 }
159
160 // If we didn't have a cache hit, compute the candidate default locale.
161 var runtimeDefaultLocale = intl_RuntimeDefaultLocale();
162 var locale = intl_supportedLocaleOrFallback(runtimeDefaultLocale);
163
164 assertIsValidAndCanonicalLanguageTag(locale, "the computed default locale");
165 assert(
166 startOfUnicodeExtensions(locale) < 0,
167 "the computed default locale must not contain a Unicode extension sequence"
168 );
169
170 // Cache the computed locale until the runtime default locale changes.
171 localeCache.defaultLocale = locale;
172 localeCache.runtimeDefaultLocale = runtimeDefaultLocale;
173
174 return locale;
175 }
176
177 /**
178 * Canonicalizes a locale list.
179 *
180 * Spec: ECMAScript Internationalization API Specification, 9.2.1.
181 */
182 function CanonicalizeLocaleList(locales) {
183 // Step 1.
184 if (locales === undefined) {
185 return [];
186 }
187
188 // Step 3 (and the remaining steps).
189 var tag = intl_ValidateAndCanonicalizeLanguageTag(locales, false);
190 if (tag !== null) {
191 assert(
192 typeof tag === "string",
193 "intl_ValidateAndCanonicalizeLanguageTag returns a string value"
194 );
195 return [tag];
196 }
197
198 // Step 2.
199 var seen = [];
200
201 // Step 4.
202 var O = ToObject(locales);
203
204 // Step 5.
205 var len = ToLength(O.length);
206
207 // Step 6.
208 var k = 0;
209
210 // Step 7.
211 while (k < len) {
212 // Steps 7.a-c.
213 if (k in O) {
214 // Step 7.c.i.
215 var kValue = O[k];
216
217 // Step 7.c.ii.
218 if (!(typeof kValue === "string" || IsObject(kValue))) {
219 ThrowTypeError(JSMSG_INVALID_LOCALES_ELEMENT);
220 }
221
222 // Steps 7.c.iii-iv.
223 var tag = intl_ValidateAndCanonicalizeLanguageTag(kValue, true);
224 assert(
225 typeof tag === "string",
226 "ValidateAndCanonicalizeLanguageTag returns a string value"
227 );
228
229 // Step 7.c.v.
230 if (callFunction(std_Array_indexOf, seen, tag) === -1) {
231 DefineDataProperty(seen, seen.length, tag);
232 }
233 }
234
235 // Step 7.d.
236 k++;
237 }
238
239 // Step 8.
240 return seen;
241 }
242
243 /**
244 * Compares a BCP 47 language tag against the locales in availableLocales
245 * and returns the best available match. Uses the fallback
246 * mechanism of RFC 4647, section 3.4.
247 *
248 * Spec: ECMAScript Internationalization API Specification, 9.2.2.
249 * Spec: RFC 4647, section 3.4.
250 */
251 function BestAvailableLocale(availableLocales, locale) {
252 return intl_BestAvailableLocale(availableLocales, locale, DefaultLocale());
253 }
254
255 /**
256 * Identical to BestAvailableLocale, but does not consider the default locale
257 * during computation.
258 */
259 function BestAvailableLocaleIgnoringDefault(availableLocales, locale) {
260 return intl_BestAvailableLocale(availableLocales, locale, null);
261 }
262
263 /**
264 * Compares a BCP 47 language priority list against the set of locales in
265 * availableLocales and determines the best available language to meet the
266 * request. Options specified through Unicode extension subsequences are
267 * ignored in the lookup, but information about such subsequences is returned
268 * separately.
269 *
270 * This variant is based on the Lookup algorithm of RFC 4647 section 3.4.
271 *
272 * Spec: ECMAScript Internationalization API Specification, 9.2.3.
273 * Spec: RFC 4647, section 3.4.
274 */
275 function LookupMatcher(availableLocales, requestedLocales) {
276 // Step 1.
277 var result = new_Record();
278
279 // Step 2.
280 for (var i = 0; i < requestedLocales.length; i++) {
281 var locale = requestedLocales[i];
282
283 // Step 2.a.
284 var noExtensionsLocale = removeUnicodeExtensions(locale);
285
286 // Step 2.b.
287 var availableLocale = BestAvailableLocale(
288 availableLocales,
289 noExtensionsLocale
290 );
291
292 // Step 2.c.
293 if (availableLocale !== undefined) {
294 // Step 2.c.i.
295 result.locale = availableLocale;
296
297 // Step 2.c.ii.
298 if (locale !== noExtensionsLocale) {
299 result.extension = getUnicodeExtensions(locale);
300 }
301
302 // Step 2.c.iii.
303 return result;
304 }
305 }
306
307 // Steps 3-4.
308 result.locale = DefaultLocale();
309
310 // Step 5.
311 return result;
312 }
313
314 /**
315 * Compares a BCP 47 language priority list against the set of locales in
316 * availableLocales and determines the best available language to meet the
317 * request. Options specified through Unicode extension subsequences are
318 * ignored in the lookup, but information about such subsequences is returned
319 * separately.
320 *
321 * Spec: ECMAScript Internationalization API Specification, 9.2.4.
322 */
323 function BestFitMatcher(availableLocales, requestedLocales) {
324 // this implementation doesn't have anything better
325 return LookupMatcher(availableLocales, requestedLocales);
326 }
327
328 /**
329 * Returns the Unicode extension value subtags for the requested key subtag.
330 *
331 * Spec: ECMAScript Internationalization API Specification, 9.2.5.
332 */
333 function UnicodeExtensionValue(extension, key) {
334 assert(typeof extension === "string", "extension is a string value");
335 assert(
336 callFunction(std_String_startsWith, extension, "-u-") &&
337 getUnicodeExtensions("und" + extension) === extension,
338 "extension is a Unicode extension subtag"
339 );
340 assert(typeof key === "string", "key is a string value");
341
342 // Step 1.
343 assert(key.length === 2, "key is a Unicode extension key subtag");
344
345 // Step 2.
346 var size = extension.length;
347
348 // Step 3.
349 var searchValue = "-" + key + "-";
350
351 // Step 4.
352 var pos = callFunction(std_String_indexOf, extension, searchValue);
353
354 // Step 5.
355 if (pos !== -1) {
356 // Step 5.a.
357 var start = pos + 4;
358
359 // Step 5.b.
360 var end = start;
361
362 // Step 5.c.
363 var k = start;
364
365 // Steps 5.d-e.
366 while (true) {
367 // Step 5.e.i.
368 var e = callFunction(std_String_indexOf, extension, "-", k);
369
370 // Step 5.e.ii.
371 var len = e === -1 ? size - k : e - k;
372
373 // Step 5.e.iii.
374 if (len === 2) {
375 break;
376 }
377
378 // Step 5.e.iv.
379 if (e === -1) {
380 end = size;
381 break;
382 }
383
384 // Step 5.e.v.
385 end = e;
386 k = e + 1;
387 }
388
389 // Step 5.f.
390 return callFunction(String_substring, extension, start, end);
391 }
392
393 // Step 6.
394 searchValue = "-" + key;
395
396 // Steps 7-8.
397 if (callFunction(std_String_endsWith, extension, searchValue)) {
398 return "";
399 }
400
401 // Step 9 (implicit).
402 }
403
404 /**
405 * Compares a BCP 47 language priority list against availableLocales and
406 * determines the best available language to meet the request. Options specified
407 * through Unicode extension subsequences are negotiated separately, taking the
408 * caller's relevant extensions and locale data as well as client-provided
409 * options into consideration.
410 *
411 * Spec: ECMAScript Internationalization API Specification, 9.2.6.
412 */
413 function ResolveLocale(
414 availableLocales,
415 requestedLocales,
416 options,
417 relevantExtensionKeys,
418 localeData
419 ) {
420 // Steps 1-3.
421 var matcher = options.localeMatcher;
422 var r =
423 matcher === "lookup"
424 ? LookupMatcher(availableLocales, requestedLocales)
425 : BestFitMatcher(availableLocales, requestedLocales);
426
427 // Step 4.
428 var foundLocale = r.locale;
429 var extension = r.extension;
430
431 // Step 5.
432 var result = new_Record();
433
434 // Step 6.
435 result.dataLocale = foundLocale;
436
437 // Step 7.
438 var supportedExtension = "-u";
439
440 // In this implementation, localeData is a function, not an object.
441 var localeDataProvider = localeData();
442
443 // Step 8.
444 for (var i = 0; i < relevantExtensionKeys.length; i++) {
445 var key = relevantExtensionKeys[i];
446
447 // Steps 8.a-h (The locale data is only computed when needed).
448 var keyLocaleData = undefined;
449 var value = undefined;
450
451 // Locale tag may override.
452
453 // Step 8.g.
454 var supportedExtensionAddition = "";
455
456 // Step 8.h.
457 if (extension !== undefined) {
458 // Step 8.h.i.
459 var requestedValue = UnicodeExtensionValue(extension, key);
460
461 // Step 8.h.ii.
462 if (requestedValue !== undefined) {
463 // Steps 8.a-d.
464 keyLocaleData = callFunction(
465 localeDataProvider[key],
466 null,
467 foundLocale
468 );
469
470 // Step 8.h.ii.1.
471 if (requestedValue !== "") {
472 // Step 8.h.ii.1.a.
473 if (
474 callFunction(std_Array_indexOf, keyLocaleData, requestedValue) !==
475 -1
476 ) {
477 value = requestedValue;
478 supportedExtensionAddition = "-" + key + "-" + value;
479 }
480 } else {
481 // Step 8.h.ii.2.
482
483 // According to the LDML spec, if there's no type value,
484 // and true is an allowed value, it's used.
485 if (callFunction(std_Array_indexOf, keyLocaleData, "true") !== -1) {
486 value = "true";
487 supportedExtensionAddition = "-" + key;
488 }
489 }
490 }
491 }
492
493 // Options override all.
494
495 // Step 8.i.i.
496 var optionsValue = options[key];
497
498 // Step 8.i.ii.
499 assert(
500 typeof optionsValue === "string" ||
501 optionsValue === undefined ||
502 optionsValue === null,
503 "unexpected type for options value"
504 );
505
506 // Steps 8.i, 8.i.iii.1.
507 if (optionsValue !== undefined && optionsValue !== value) {
508 // Steps 8.a-d.
509 if (keyLocaleData === undefined) {
510 keyLocaleData = callFunction(
511 localeDataProvider[key],
512 null,
513 foundLocale
514 );
515 }
516
517 // Step 8.i.iii.
518 if (callFunction(std_Array_indexOf, keyLocaleData, optionsValue) !== -1) {
519 value = optionsValue;
520 supportedExtensionAddition = "";
521 }
522 }
523
524 // Locale data provides default value.
525 if (value === undefined) {
526 // Steps 8.a-f.
527 value =
528 keyLocaleData === undefined
529 ? callFunction(localeDataProvider.default[key], null, foundLocale)
530 : keyLocaleData[0];
531 }
532
533 // Step 8.j.
534 assert(
535 typeof value === "string" || value === null,
536 "unexpected locale data value"
537 );
538 result[key] = value;
539
540 // Step 8.k.
541 supportedExtension += supportedExtensionAddition;
542 }
543
544 // Step 9.
545 if (supportedExtension.length > 2) {
546 foundLocale = addUnicodeExtension(foundLocale, supportedExtension);
547 }
548
549 // Step 10.
550 result.locale = foundLocale;
551
552 // Step 11.
553 return result;
554 }
555
556 /**
557 * Adds a Unicode extension subtag to a locale.
558 *
559 * Spec: ECMAScript Internationalization API Specification, 9.2.6.
560 */
561 function addUnicodeExtension(locale, extension) {
562 assert(typeof locale === "string", "locale is a string value");
563 assert(
564 !callFunction(std_String_startsWith, locale, "x-"),
565 "unexpected privateuse-only locale"
566 );
567 assert(
568 startOfUnicodeExtensions(locale) < 0,
569 "Unicode extension subtag already present in locale"
570 );
571
572 assert(typeof extension === "string", "extension is a string value");
573 assert(
574 callFunction(std_String_startsWith, extension, "-u-") &&
575 getUnicodeExtensions("und" + extension) === extension,
576 "extension is a Unicode extension subtag"
577 );
578
579 // Step 9.a.
580 var privateIndex = callFunction(std_String_indexOf, locale, "-x-");
581
582 // Steps 9.b-c.
583 if (privateIndex === -1) {
584 locale += extension;
585 } else {
586 var preExtension = callFunction(String_substring, locale, 0, privateIndex);
587 var postExtension = callFunction(String_substring, locale, privateIndex);
588 locale = preExtension + extension + postExtension;
589 }
590
591 // Steps 9.d-e (Step 9.e is not required in this implementation, because we don't canonicalize
592 // Unicode extension subtags).
593 assertIsValidAndCanonicalLanguageTag(locale, "locale after concatenation");
594
595 return locale;
596 }
597
598 /**
599 * Returns the subset of requestedLocales for which availableLocales has a
600 * matching (possibly fallback) locale. Locales appear in the same order in the
601 * returned list as in the input list.
602 *
603 * Spec: ECMAScript Internationalization API Specification, 9.2.7.
604 */
605 function LookupSupportedLocales(availableLocales, requestedLocales) {
606 // Step 1.
607 var subset = [];
608
609 // Step 2.
610 for (var i = 0; i < requestedLocales.length; i++) {
611 var locale = requestedLocales[i];
612
613 // Step 2.a.
614 var noExtensionsLocale = removeUnicodeExtensions(locale);
615
616 // Step 2.b.
617 var availableLocale = BestAvailableLocale(
618 availableLocales,
619 noExtensionsLocale
620 );
621
622 // Step 2.c.
623 if (availableLocale !== undefined) {
624 DefineDataProperty(subset, subset.length, locale);
625 }
626 }
627
628 // Step 3.
629 return subset;
630 }
631
632 /**
633 * Returns the subset of requestedLocales for which availableLocales has a
634 * matching (possibly fallback) locale. Locales appear in the same order in the
635 * returned list as in the input list.
636 *
637 * Spec: ECMAScript Internationalization API Specification, 9.2.8.
638 */
639 function BestFitSupportedLocales(availableLocales, requestedLocales) {
640 // don't have anything better
641 return LookupSupportedLocales(availableLocales, requestedLocales);
642 }
643
644 /**
645 * Returns the subset of requestedLocales for which availableLocales has a
646 * matching (possibly fallback) locale. Locales appear in the same order in the
647 * returned list as in the input list.
648 *
649 * Spec: ECMAScript Internationalization API Specification, 9.2.9.
650 */
651 function SupportedLocales(availableLocales, requestedLocales, options) {
652 // Step 1.
653 var matcher;
654 if (options !== undefined) {
655 // Step 1.a.
656 options = ToObject(options);
657
658 // Step 1.b
659 matcher = options.localeMatcher;
660 if (matcher !== undefined) {
661 matcher = ToString(matcher);
662 if (matcher !== "lookup" && matcher !== "best fit") {
663 ThrowRangeError(JSMSG_INVALID_LOCALE_MATCHER, matcher);
664 }
665 }
666 }
667
668 // Steps 2-5.
669 return matcher === undefined || matcher === "best fit"
670 ? BestFitSupportedLocales(availableLocales, requestedLocales)
671 : LookupSupportedLocales(availableLocales, requestedLocales);
672 }
673
674 /**
675 * Extracts a property value from the provided options object, converts it to
676 * the required type, checks whether it is one of a list of allowed values,
677 * and fills in a fallback value if necessary.
678 *
679 * Spec: ECMAScript Internationalization API Specification, 9.2.10.
680 */
681 function GetOption(options, property, type, values, fallback) {
682 // Step 1.
683 var value = options[property];
684
685 // Step 2.
686 if (value !== undefined) {
687 // Steps 2.a-c.
688 if (type === "boolean") {
689 value = ToBoolean(value);
690 } else if (type === "string") {
691 value = ToString(value);
692 } else {
693 assert(false, "GetOption");
694 }
695
696 // Step 2.d.
697 if (
698 values !== undefined &&
699 callFunction(std_Array_indexOf, values, value) === -1
700 ) {
701 ThrowRangeError(JSMSG_INVALID_OPTION_VALUE, property, `"${value}"`);
702 }
703
704 // Step 2.e.
705 return value;
706 }
707
708 // Step 3.
709 return fallback;
710 }
711
712 /**
713 * Extracts a property value from the provided options object, converts it to
714 * a boolean or string, checks whether it is one of a list of allowed values,
715 * and fills in a fallback value if necessary.
716 */
717 function GetStringOrBooleanOption(
718 options,
719 property,
720 stringValues,
721 fallback
722 ) {
723 assert(IsObject(stringValues), "GetStringOrBooleanOption");
724
725 // Step 1.
726 var value = options[property];
727
728 // Step 2.
729 if (value === undefined) {
730 return fallback;
731 }
732
733 // Step 3.
734 if (value === true) {
735 return true;
736 }
737
738 // Steps 4-5.
739 if (!value) {
740 return false;
741 }
742
743 // Step 6.
744 value = ToString(value);
745
746 // Step 7.
747 if (callFunction(std_Array_indexOf, stringValues, value) === -1) {
748 ThrowRangeError(JSMSG_INVALID_OPTION_VALUE, property, `"${value}"`);
749 }
750
751 // Step 8.
752 return value;
753 }
754
755 /**
756 * The abstract operation DefaultNumberOption converts value to a Number value,
757 * checks whether it is in the allowed range, and fills in a fallback value if
758 * necessary.
759 *
760 * Spec: ECMAScript Internationalization API Specification, 9.2.11.
761 */
762 function DefaultNumberOption(value, minimum, maximum, fallback) {
763 assert(
764 typeof minimum === "number" && (minimum | 0) === minimum,
765 "DefaultNumberOption"
766 );
767 assert(
768 typeof maximum === "number" && (maximum | 0) === maximum,
769 "DefaultNumberOption"
770 );
771 assert(
772 fallback === undefined ||
773 (typeof fallback === "number" && (fallback | 0) === fallback),
774 "DefaultNumberOption"
775 );
776 assert(
777 fallback === undefined || (minimum <= fallback && fallback <= maximum),
778 "DefaultNumberOption"
779 );
780
781 // Step 1.
782 if (value === undefined) {
783 return fallback;
784 }
785
786 // Step 2.
787 value = ToNumber(value);
788
789 // Step 3.
790 if (Number_isNaN(value) || value < minimum || value > maximum) {
791 ThrowRangeError(JSMSG_INVALID_DIGITS_VALUE, value);
792 }
793
794 // Step 4.
795 // Apply bitwise-or to convert -0 to +0 per ES2017, 5.2 and to ensure the
796 // result is an int32 value.
797 return std_Math_floor(value) | 0;
798 }
799
800 /**
801 * Extracts a property value from the provided options object, converts it to a
802 * Number value, checks whether it is in the allowed range, and fills in a
803 * fallback value if necessary.
804 *
805 * Spec: ECMAScript Internationalization API Specification, 9.2.12.
806 */
807 function GetNumberOption(options, property, minimum, maximum, fallback) {
808 // Steps 1-2.
809 return DefaultNumberOption(options[property], minimum, maximum, fallback);
810 }
811
812 // Symbols in the self-hosting compartment can't be cloned, use a separate
813 // object to hold the actual symbol value.
814 // TODO: Can we add support to clone symbols?
815 var intlFallbackSymbolHolder = { value: undefined };
816
817 /**
818 * The [[FallbackSymbol]] symbol of the %Intl% intrinsic object.
819 *
820 * This symbol is used to implement the legacy constructor semantics for
821 * Intl.DateTimeFormat and Intl.NumberFormat.
822 */
823 function intlFallbackSymbol() {
824 var fallbackSymbol = intlFallbackSymbolHolder.value;
825 if (!fallbackSymbol) {
826 var Symbol = GetBuiltinConstructor("Symbol");
827 fallbackSymbol = Symbol("IntlLegacyConstructedSymbol");
828 intlFallbackSymbolHolder.value = fallbackSymbol;
829 }
830 return fallbackSymbol;
831 }
832
833 /**
834 * Initializes the INTL_INTERNALS_OBJECT_SLOT of the given object.
835 */
836 function initializeIntlObject(obj, type, lazyData) {
837 assert(IsObject(obj), "Non-object passed to initializeIntlObject");
838 assert(
839 (type === "Collator" && intl_GuardToCollator(obj) !== null) ||
840 (type === "DateTimeFormat" && intl_GuardToDateTimeFormat(obj) !== null) ||
841 (type === "DisplayNames" && intl_GuardToDisplayNames(obj) !== null) ||
842 (type === "DurationFormat" && intl_GuardToDurationFormat(obj) !== null) ||
843 (type === "ListFormat" && intl_GuardToListFormat(obj) !== null) ||
844 (type === "NumberFormat" && intl_GuardToNumberFormat(obj) !== null) ||
845 (type === "PluralRules" && intl_GuardToPluralRules(obj) !== null) ||
846 (type === "RelativeTimeFormat" &&
847 intl_GuardToRelativeTimeFormat(obj) !== null) ||
848 (type === "Segmenter" && intl_GuardToSegmenter(obj) !== null),
849 "type must match the object's class"
850 );
851 assert(IsObject(lazyData), "non-object lazy data");
852
853 // The meaning of an internals object for an object |obj| is as follows.
854 //
855 // The .type property indicates the type of Intl object that |obj| is. It
856 // must be one of:
857 // - Collator
858 // - DateTimeFormat
859 // - DisplayNames
860 // - DurationFormat
861 // - ListFormat
862 // - NumberFormat
863 // - PluralRules
864 // - RelativeTimeFormat
865 // - Segmenter
866 //
867 // The .lazyData property stores information needed to compute -- without
868 // observable side effects -- the actual internal Intl properties of
869 // |obj|. If it is non-null, then the actual internal properties haven't
870 // been computed, and .lazyData must be processed by
871 // |setInternalProperties| before internal Intl property values are
872 // available. If it is null, then the .internalProps property contains an
873 // object whose properties are the internal Intl properties of |obj|.
874
875 var internals = std_Object_create(null);
876 internals.type = type;
877 internals.lazyData = lazyData;
878 internals.internalProps = null;
879
880 assert(
881 UnsafeGetReservedSlot(obj, INTL_INTERNALS_OBJECT_SLOT) === undefined,
882 "Internal slot already initialized?"
883 );
884 UnsafeSetReservedSlot(obj, INTL_INTERNALS_OBJECT_SLOT, internals);
885 }
886
887 /**
888 * Set the internal properties object for an |internals| object previously
889 * associated with lazy data.
890 */
891 function setInternalProperties(internals, internalProps) {
892 assert(IsObject(internals.lazyData), "lazy data must exist already");
893 assert(IsObject(internalProps), "internalProps argument should be an object");
894
895 // Set in reverse order so that the .lazyData nulling is a barrier.
896 internals.internalProps = internalProps;
897 internals.lazyData = null;
898 }
899
900 /**
901 * Get the existing internal properties out of a non-newborn |internals|, or
902 * null if none have been computed.
903 */
904 function maybeInternalProperties(internals) {
905 assert(IsObject(internals), "non-object passed to maybeInternalProperties");
906 var lazyData = internals.lazyData;
907 if (lazyData) {
908 return null;
909 }
910 assert(
911 IsObject(internals.internalProps),
912 "missing lazy data and computed internals"
913 );
914 return internals.internalProps;
915 }
916
917 /**
918 * Return |obj|'s internals object (*not* the object holding its internal
919 * properties!), with structure specified above.
920 *
921 * Spec: ECMAScript Internationalization API Specification, 10.3.
922 * Spec: ECMAScript Internationalization API Specification, 11.3.
923 * Spec: ECMAScript Internationalization API Specification, 12.3.
924 */
925 function getIntlObjectInternals(obj) {
926 assert(IsObject(obj), "getIntlObjectInternals called with non-Object");
927 assert(
928 intl_GuardToCollator(obj) !== null ||
929 intl_GuardToDateTimeFormat(obj) !== null ||
930 intl_GuardToDisplayNames(obj) !== null ||
931 intl_GuardToDurationFormat(obj) !== null ||
932 intl_GuardToListFormat(obj) !== null ||
933 intl_GuardToNumberFormat(obj) !== null ||
934 intl_GuardToPluralRules(obj) !== null ||
935 intl_GuardToRelativeTimeFormat(obj) !== null ||
936 intl_GuardToSegmenter(obj) !== null,
937 "getIntlObjectInternals called with non-Intl object"
938 );
939
940 var internals = UnsafeGetReservedSlot(obj, INTL_INTERNALS_OBJECT_SLOT);
941
942 assert(IsObject(internals), "internals not an object");
943 assert(hasOwn("type", internals), "missing type");
944 assert(
945 (internals.type === "Collator" && intl_GuardToCollator(obj) !== null) ||
946 (internals.type === "DateTimeFormat" &&
947 intl_GuardToDateTimeFormat(obj) !== null) ||
948 (internals.type === "DisplayNames" &&
949 intl_GuardToDisplayNames(obj) !== null) ||
950 (internals.type === "DurationFormat" &&
951 intl_GuardToDurationFormat(obj) !== null) ||
952 (internals.type === "ListFormat" &&
953 intl_GuardToListFormat(obj) !== null) ||
954 (internals.type === "NumberFormat" &&
955 intl_GuardToNumberFormat(obj) !== null) ||
956 (internals.type === "PluralRules" &&
957 intl_GuardToPluralRules(obj) !== null) ||
958 (internals.type === "RelativeTimeFormat" &&
959 intl_GuardToRelativeTimeFormat(obj) !== null) ||
960 (internals.type === "Segmenter" &&
961 intl_GuardToSegmenter(obj) !== null),
962 "type must match the object's class"
963 );
964 assert(hasOwn("lazyData", internals), "missing lazyData");
965 assert(hasOwn("internalProps", internals), "missing internalProps");
966
967 return internals;
968 }
969
970 /**
971 * Get the internal properties of known-Intl object |obj|. For use only by
972 * C++ code that knows what it's doing!
973 */
974 function getInternals(obj) {
975 var internals = getIntlObjectInternals(obj);
976
977 // If internal properties have already been computed, use them.
978 var internalProps = maybeInternalProperties(internals);
979 if (internalProps) {
980 return internalProps;
981 }
982
983 // Otherwise it's time to fully create them.
984 var type = internals.type;
985 if (type === "Collator") {
986 internalProps = resolveCollatorInternals(internals.lazyData);
987 } else if (type === "DateTimeFormat") {
988 internalProps = resolveDateTimeFormatInternals(internals.lazyData);
989 } else if (type === "DisplayNames") {
990 internalProps = resolveDisplayNamesInternals(internals.lazyData);
991 } else if (type === "DurationFormat") {
992 internalProps = resolveDurationFormatInternals(internals.lazyData);
993 } else if (type === "ListFormat") {
994 internalProps = resolveListFormatInternals(internals.lazyData);
995 } else if (type === "NumberFormat") {
996 internalProps = resolveNumberFormatInternals(internals.lazyData);
997 } else if (type === "PluralRules") {
998 internalProps = resolvePluralRulesInternals(internals.lazyData);
999 } else if (type === "RelativeTimeFormat") {
1000 internalProps = resolveRelativeTimeFormatInternals(internals.lazyData);
1001 } else {
1002 assert(type === "Segmenter", "unexpected Intl type");
1003 internalProps = resolveSegmenterInternals(internals.lazyData);
1004 }
1005 setInternalProperties(internals, internalProps);
1006 return internalProps;
1007 }