Debian Codegraph @

js/src/builtin/intl/DisplayNames.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 /**
6 * Intl.DisplayNames internal properties.
7 */
8 function displayNamesLocaleData() {
9 // Intl.DisplayNames doesn't support any extension keys.
10 return {};
11 }
12 var displayNamesInternalProperties = {
13 localeData: displayNamesLocaleData,
14 relevantExtensionKeys: [],
15 };
16
17 function mozDisplayNamesLocaleData() {
18 return {
19 ca: intl_availableCalendars,
20 default: {
21 ca: intl_defaultCalendar,
22 },
23 };
24 }
25 var mozDisplayNamesInternalProperties = {
26 localeData: mozDisplayNamesLocaleData,
27 relevantExtensionKeys: ["ca"],
28 };
29
30 /**
31 * Intl.DisplayNames ( [ locales [ , options ] ] )
32 *
33 * Compute an internal properties object from |lazyDisplayNamesData|.
34 */
35 function resolveDisplayNamesInternals(lazyDisplayNamesData) {
36 assert(IsObject(lazyDisplayNamesData), "lazy data not an object?");
37
38 var internalProps = std_Object_create(null);
39
40 var mozExtensions = lazyDisplayNamesData.mozExtensions;
41
42 var DisplayNames = mozExtensions
43 ? mozDisplayNamesInternalProperties
44 : displayNamesInternalProperties;
45
46 // Compute effective locale.
47
48 // Step 7.
49 var localeData = DisplayNames.localeData;
50
51 // Step 10.
52 var r = ResolveLocale(
53 "DisplayNames",
54 lazyDisplayNamesData.requestedLocales,
55 lazyDisplayNamesData.opt,
56 DisplayNames.relevantExtensionKeys,
57 localeData
58 );
59
60 // Step 12.
61 internalProps.style = lazyDisplayNamesData.style;
62
63 // Step 14.
64 var type = lazyDisplayNamesData.type;
65 internalProps.type = type;
66
67 // Step 16.
68 internalProps.fallback = lazyDisplayNamesData.fallback;
69
70 // Step 17.
71 internalProps.locale = r.locale;
72
73 // Step 25.
74 if (type === "language") {
75 internalProps.languageDisplay = lazyDisplayNamesData.languageDisplay;
76 }
77
78 if (mozExtensions) {
79 internalProps.calendar = r.ca;
80 }
81
82 // The caller is responsible for associating |internalProps| with the right
83 // object using |setInternalProperties|.
84 return internalProps;
85 }
86
87 /**
88 * Returns an object containing the DisplayNames internal properties of |obj|.
89 */
90 function getDisplayNamesInternals(obj) {
91 assert(IsObject(obj), "getDisplayNamesInternals called with non-object");
92 assert(
93 intl_GuardToDisplayNames(obj) !== null,
94 "getDisplayNamesInternals called with non-DisplayNames"
95 );
96
97 var internals = getIntlObjectInternals(obj);
98 assert(
99 internals.type === "DisplayNames",
100 "bad type escaped getIntlObjectInternals"
101 );
102
103 // If internal properties have already been computed, use them.
104 var internalProps = maybeInternalProperties(internals);
105 if (internalProps) {
106 return internalProps;
107 }
108
109 // Otherwise it's time to fully create them.
110 internalProps = resolveDisplayNamesInternals(internals.lazyData);
111 setInternalProperties(internals, internalProps);
112 return internalProps;
113 }
114
115 /**
116 * Intl.DisplayNames ( [ locales [ , options ] ] )
117 *
118 * Initializes an object as a DisplayNames.
119 *
120 * This method is complicated a moderate bit by its implementing initialization
121 * as a *lazy* concept. Everything that must happen now, does -- but we defer
122 * all the work we can until the object is actually used as a DisplayNames.
123 * This later work occurs in |resolveDisplayNamesInternals|; steps not noted
124 * here occur there.
125 */
126 function InitializeDisplayNames(displayNames, locales, options, mozExtensions) {
127 assert(
128 IsObject(displayNames),
129 "InitializeDisplayNames called with non-object"
130 );
131 assert(
132 intl_GuardToDisplayNames(displayNames) !== null,
133 "InitializeDisplayNames called with non-DisplayNames"
134 );
135
136 // Lazy DisplayNames data has the following structure:
137 //
138 // {
139 // requestedLocales: List of locales,
140 //
141 // opt: // opt object computed in InitializeDisplayNames
142 // {
143 // localeMatcher: "lookup" / "best fit",
144 //
145 // ca: string matching a Unicode extension type, // optional
146 // }
147 //
148 // localeMatcher: "lookup" / "best fit",
149 //
150 // style: "narrow" / "short" / "abbreviated" / "long",
151 //
152 // type: "language" / "region" / "script" / "currency" / "weekday" /
153 // "month" / "quarter" / "dayPeriod" / "dateTimeField"
154 //
155 // fallback: "code" / "none",
156 //
157 // // field present only if type === "language":
158 // languageDisplay: "dialect" / "standard",
159 //
160 // mozExtensions: true / false,
161 // }
162 //
163 // Note that lazy data is only installed as a final step of initialization,
164 // so every DisplayNames lazy data object has *all* these properties, never a
165 // subset of them.
166 var lazyDisplayNamesData = std_Object_create(null);
167
168 // Step 3.
169 var requestedLocales = CanonicalizeLocaleList(locales);
170 lazyDisplayNamesData.requestedLocales = requestedLocales;
171
172 // Step 4.
173 if (!IsObject(options)) {
174 ThrowTypeError(
175 JSMSG_OBJECT_REQUIRED,
176 options === null ? "null" : typeof options
177 );
178 }
179
180 // Step 5.
181 var opt = new_Record();
182 lazyDisplayNamesData.opt = opt;
183 lazyDisplayNamesData.mozExtensions = mozExtensions;
184
185 // Steps 7-8.
186 var matcher = GetOption(
187 options,
188 "localeMatcher",
189 "string",
190 ["lookup", "best fit"],
191 "best fit"
192 );
193 opt.localeMatcher = matcher;
194
195 if (mozExtensions) {
196 var calendar = GetOption(
197 options,
198 "calendar",
199 "string",
200 undefined,
201 undefined
202 );
203
204 if (calendar !== undefined) {
205 calendar = intl_ValidateAndCanonicalizeUnicodeExtensionType(
206 calendar,
207 "calendar",
208 "ca"
209 );
210 }
211
212 opt.ca = calendar;
213 }
214
215 // Step 10.
216 var style;
217 if (mozExtensions) {
218 style = GetOption(
219 options,
220 "style",
221 "string",
222 ["narrow", "short", "abbreviated", "long"],
223 "long"
224 );
225 } else {
226 style = GetOption(
227 options,
228 "style",
229 "string",
230 ["narrow", "short", "long"],
231 "long"
232 );
233 }
234
235 // Step 11.
236 lazyDisplayNamesData.style = style;
237
238 // Step 12.
239 var type;
240 if (mozExtensions) {
241 type = GetOption(
242 options,
243 "type",
244 "string",
245 [
246 "language",
247 "region",
248 "script",
249 "currency",
250 "calendar",
251 "dateTimeField",
252 "weekday",
253 "month",
254 "quarter",
255 "dayPeriod",
256 ],
257 undefined
258 );
259 } else {
260 type = GetOption(
261 options,
262 "type",
263 "string",
264 ["language", "region", "script", "currency", "calendar", "dateTimeField"],
265 undefined
266 );
267 }
268
269 // Step 13.
270 if (type === undefined) {
271 ThrowTypeError(JSMSG_UNDEFINED_TYPE);
272 }
273
274 // Step 14.
275 lazyDisplayNamesData.type = type;
276
277 // Step 15.
278 var fallback = GetOption(
279 options,
280 "fallback",
281 "string",
282 ["code", "none"],
283 "code"
284 );
285
286 // Step 16.
287 lazyDisplayNamesData.fallback = fallback;
288
289 // Step 24.
290 var languageDisplay = GetOption(
291 options,
292 "languageDisplay",
293 "string",
294 ["dialect", "standard"],
295 "dialect"
296 );
297
298 // Step 25.
299 if (type === "language") {
300 lazyDisplayNamesData.languageDisplay = languageDisplay;
301 }
302
303 // We've done everything that must be done now: mark the lazy data as fully
304 // computed and install it.
305 initializeIntlObject(displayNames, "DisplayNames", lazyDisplayNamesData);
306 }
307
308 /**
309 * Returns the subset of the given locale list for which this locale list has a
310 * matching (possibly fallback) locale. Locales appear in the same order in the
311 * returned list as in the input list.
312 */
313 function Intl_DisplayNames_supportedLocalesOf(locales /*, options*/) {
314 var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
315
316 // Step 1.
317 var availableLocales = "DisplayNames";
318
319 // Step 2.
320 var requestedLocales = CanonicalizeLocaleList(locales);
321
322 // Step 3.
323 return SupportedLocales(availableLocales, requestedLocales, options);
324 }
325
326 /**
327 * Returns the resolved options for a DisplayNames object.
328 */
329 function Intl_DisplayNames_of(code) {
330 // Step 1.
331 var displayNames = this;
332
333 // Steps 2-3.
334 if (
335 !IsObject(displayNames) ||
336 (displayNames = intl_GuardToDisplayNames(displayNames)) === null
337 ) {
338 return callFunction(
339 intl_CallDisplayNamesMethodIfWrapped,
340 this,
341 "Intl_DisplayNames_of"
342 );
343 }
344
345 code = ToString(code);
346
347 var internals = getDisplayNamesInternals(displayNames);
348
349 // Unpack the internals object to avoid a slow runtime to selfhosted JS call
350 // in |intl_ComputeDisplayName()|.
351 var {
352 locale,
353 calendar = "",
354 style,
355 type,
356 languageDisplay = "",
357 fallback,
358 } = internals;
359
360 // Steps 5-10.
361 return intl_ComputeDisplayName(
362 displayNames,
363 locale,
364 calendar,
365 style,
366 languageDisplay,
367 fallback,
368 type,
369 code
370 );
371 }
372
373 /**
374 * Returns the resolved options for a DisplayNames object.
375 */
376 function Intl_DisplayNames_resolvedOptions() {
377 // Step 1.
378 var displayNames = this;
379
380 // Steps 2-3.
381 if (
382 !IsObject(displayNames) ||
383 (displayNames = intl_GuardToDisplayNames(displayNames)) === null
384 ) {
385 return callFunction(
386 intl_CallDisplayNamesMethodIfWrapped,
387 this,
388 "Intl_DisplayNames_resolvedOptions"
389 );
390 }
391
392 var internals = getDisplayNamesInternals(displayNames);
393
394 // Steps 4-5.
395 var options = {
396 locale: internals.locale,
397 style: internals.style,
398 type: internals.type,
399 fallback: internals.fallback,
400 };
401
402 // languageDisplay is only present for language display names.
403 assert(
404 hasOwn("languageDisplay", internals) === (internals.type === "language"),
405 "languageDisplay is present iff type is 'language'"
406 );
407
408 if (hasOwn("languageDisplay", internals)) {
409 DefineDataProperty(options, "languageDisplay", internals.languageDisplay);
410 }
411
412 if (hasOwn("calendar", internals)) {
413 DefineDataProperty(options, "calendar", internals.calendar);
414 }
415
416 // Step 6.
417 return options;
418 }