summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/intl/DisplayNames.js
blob: 00ba2301aaf16cdc933a4ba3f3f090719369a8e9 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Intl.DisplayNames internal properties.
 */
function displayNamesLocaleData() {
  // Intl.DisplayNames doesn't support any extension keys.
  return {};
}
var displayNamesInternalProperties = {
  localeData: displayNamesLocaleData,
  relevantExtensionKeys: [],
};

function mozDisplayNamesLocaleData() {
  return {
    ca: intl_availableCalendars,
    default: {
      ca: intl_defaultCalendar,
    },
  };
}
var mozDisplayNamesInternalProperties = {
  localeData: mozDisplayNamesLocaleData,
  relevantExtensionKeys: ["ca"],
};

/**
 * Intl.DisplayNames ( [ locales [ , options ] ] )
 *
 * Compute an internal properties object from |lazyDisplayNamesData|.
 */
function resolveDisplayNamesInternals(lazyDisplayNamesData) {
  assert(IsObject(lazyDisplayNamesData), "lazy data not an object?");

  var internalProps = std_Object_create(null);

  var mozExtensions = lazyDisplayNamesData.mozExtensions;

  var DisplayNames = mozExtensions
    ? mozDisplayNamesInternalProperties
    : displayNamesInternalProperties;

  // Compute effective locale.

  // Step 7.
  var localeData = DisplayNames.localeData;

  // Step 10.
  var r = ResolveLocale(
    "DisplayNames",
    lazyDisplayNamesData.requestedLocales,
    lazyDisplayNamesData.opt,
    DisplayNames.relevantExtensionKeys,
    localeData
  );

  // Step 12.
  internalProps.style = lazyDisplayNamesData.style;

  // Step 14.
  var type = lazyDisplayNamesData.type;
  internalProps.type = type;

  // Step 16.
  internalProps.fallback = lazyDisplayNamesData.fallback;

  // Step 17.
  internalProps.locale = r.locale;

  // Step 25.
  if (type === "language") {
    internalProps.languageDisplay = lazyDisplayNamesData.languageDisplay;
  }

  if (mozExtensions) {
    internalProps.calendar = r.ca;
  }

  // The caller is responsible for associating |internalProps| with the right
  // object using |setInternalProperties|.
  return internalProps;
}

/**
 * Returns an object containing the DisplayNames internal properties of |obj|.
 */
function getDisplayNamesInternals(obj) {
  assert(IsObject(obj), "getDisplayNamesInternals called with non-object");
  assert(
    intl_GuardToDisplayNames(obj) !== null,
    "getDisplayNamesInternals called with non-DisplayNames"
  );

  var internals = getIntlObjectInternals(obj);
  assert(
    internals.type === "DisplayNames",
    "bad type escaped getIntlObjectInternals"
  );

  // If internal properties have already been computed, use them.
  var internalProps = maybeInternalProperties(internals);
  if (internalProps) {
    return internalProps;
  }

  // Otherwise it's time to fully create them.
  internalProps = resolveDisplayNamesInternals(internals.lazyData);
  setInternalProperties(internals, internalProps);
  return internalProps;
}

/**
 * Intl.DisplayNames ( [ locales [ , options ] ] )
 *
 * Initializes an object as a DisplayNames.
 *
 * This method is complicated a moderate bit by its implementing initialization
 * as a *lazy* concept.  Everything that must happen now, does -- but we defer
 * all the work we can until the object is actually used as a DisplayNames.
 * This later work occurs in |resolveDisplayNamesInternals|; steps not noted
 * here occur there.
 */
function InitializeDisplayNames(displayNames, locales, options, mozExtensions) {
  assert(
    IsObject(displayNames),
    "InitializeDisplayNames called with non-object"
  );
  assert(
    intl_GuardToDisplayNames(displayNames) !== null,
    "InitializeDisplayNames called with non-DisplayNames"
  );

  // Lazy DisplayNames data has the following structure:
  //
  //   {
  //     requestedLocales: List of locales,
  //
  //     opt: // opt object computed in InitializeDisplayNames
  //       {
  //         localeMatcher: "lookup" / "best fit",
  //
  //         ca: string matching a Unicode extension type, // optional
  //       }
  //
  //     localeMatcher: "lookup" / "best fit",
  //
  //     style: "narrow" / "short" / "abbreviated" / "long",
  //
  //     type: "language" / "region" / "script" / "currency" / "weekday" /
  //           "month" / "quarter" / "dayPeriod" / "dateTimeField"
  //
  //     fallback: "code" / "none",
  //
  //     // field present only if type === "language":
  //     languageDisplay: "dialect" / "standard",
  //
  //     mozExtensions: true / false,
  //   }
  //
  // Note that lazy data is only installed as a final step of initialization,
  // so every DisplayNames lazy data object has *all* these properties, never a
  // subset of them.
  var lazyDisplayNamesData = std_Object_create(null);

  // Step 3.
  var requestedLocales = CanonicalizeLocaleList(locales);
  lazyDisplayNamesData.requestedLocales = requestedLocales;

  // Step 4.
  if (!IsObject(options)) {
    ThrowTypeError(
      JSMSG_OBJECT_REQUIRED,
      options === null ? "null" : typeof options
    );
  }

  // Step 5.
  var opt = new_Record();
  lazyDisplayNamesData.opt = opt;
  lazyDisplayNamesData.mozExtensions = mozExtensions;

  // Steps 7-8.
  var matcher = GetOption(
    options,
    "localeMatcher",
    "string",
    ["lookup", "best fit"],
    "best fit"
  );
  opt.localeMatcher = matcher;

  if (mozExtensions) {
    var calendar = GetOption(
      options,
      "calendar",
      "string",
      undefined,
      undefined
    );

    if (calendar !== undefined) {
      calendar = intl_ValidateAndCanonicalizeUnicodeExtensionType(
        calendar,
        "calendar",
        "ca"
      );
    }

    opt.ca = calendar;
  }

  // Step 10.
  var style;
  if (mozExtensions) {
    style = GetOption(
      options,
      "style",
      "string",
      ["narrow", "short", "abbreviated", "long"],
      "long"
    );
  } else {
    style = GetOption(
      options,
      "style",
      "string",
      ["narrow", "short", "long"],
      "long"
    );
  }

  // Step 11.
  lazyDisplayNamesData.style = style;

  // Step 12.
  var type;
  if (mozExtensions) {
    type = GetOption(
      options,
      "type",
      "string",
      [
        "language",
        "region",
        "script",
        "currency",
        "calendar",
        "dateTimeField",
        "weekday",
        "month",
        "quarter",
        "dayPeriod",
      ],
      undefined
    );
  } else {
    type = GetOption(
      options,
      "type",
      "string",
      ["language", "region", "script", "currency", "calendar", "dateTimeField"],
      undefined
    );
  }

  // Step 13.
  if (type === undefined) {
    ThrowTypeError(JSMSG_UNDEFINED_TYPE);
  }

  // Step 14.
  lazyDisplayNamesData.type = type;

  // Step 15.
  var fallback = GetOption(
    options,
    "fallback",
    "string",
    ["code", "none"],
    "code"
  );

  // Step 16.
  lazyDisplayNamesData.fallback = fallback;

  // Step 24.
  var languageDisplay = GetOption(
    options,
    "languageDisplay",
    "string",
    ["dialect", "standard"],
    "dialect"
  );

  // Step 25.
  if (type === "language") {
    lazyDisplayNamesData.languageDisplay = languageDisplay;
  }

  // We've done everything that must be done now: mark the lazy data as fully
  // computed and install it.
  initializeIntlObject(displayNames, "DisplayNames", lazyDisplayNamesData);
}

/**
 * Returns the subset of the given locale list for which this locale list has a
 * matching (possibly fallback) locale. Locales appear in the same order in the
 * returned list as in the input list.
 */
function Intl_DisplayNames_supportedLocalesOf(locales /*, options*/) {
  var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined;

  // Step 1.
  var availableLocales = "DisplayNames";

  // Step 2.
  var requestedLocales = CanonicalizeLocaleList(locales);

  // Step 3.
  return SupportedLocales(availableLocales, requestedLocales, options);
}

/**
 * Returns the resolved options for a DisplayNames object.
 */
function Intl_DisplayNames_of(code) {
  // Step 1.
  var displayNames = this;

  // Steps 2-3.
  if (
    !IsObject(displayNames) ||
    (displayNames = intl_GuardToDisplayNames(displayNames)) === null
  ) {
    return callFunction(
      intl_CallDisplayNamesMethodIfWrapped,
      this,
      "Intl_DisplayNames_of"
    );
  }

  code = ToString(code);

  var internals = getDisplayNamesInternals(displayNames);

  // Unpack the internals object to avoid a slow runtime to selfhosted JS call
  // in |intl_ComputeDisplayName()|.
  var {
    locale,
    calendar = "",
    style,
    type,
    languageDisplay = "",
    fallback,
  } = internals;

  // Steps 5-10.
  return intl_ComputeDisplayName(
    displayNames,
    locale,
    calendar,
    style,
    languageDisplay,
    fallback,
    type,
    code
  );
}

/**
 * Returns the resolved options for a DisplayNames object.
 */
function Intl_DisplayNames_resolvedOptions() {
  // Step 1.
  var displayNames = this;

  // Steps 2-3.
  if (
    !IsObject(displayNames) ||
    (displayNames = intl_GuardToDisplayNames(displayNames)) === null
  ) {
    return callFunction(
      intl_CallDisplayNamesMethodIfWrapped,
      this,
      "Intl_DisplayNames_resolvedOptions"
    );
  }

  var internals = getDisplayNamesInternals(displayNames);

  // Steps 4-5.
  var options = {
    locale: internals.locale,
    style: internals.style,
    type: internals.type,
    fallback: internals.fallback,
  };

  // languageDisplay is only present for language display names.
  assert(
    hasOwn("languageDisplay", internals) === (internals.type === "language"),
    "languageDisplay is present iff type is 'language'"
  );

  if (hasOwn("languageDisplay", internals)) {
    DefineDataProperty(options, "languageDisplay", internals.languageDisplay);
  }

  if (hasOwn("calendar", internals)) {
    DefineDataProperty(options, "calendar", internals.calendar);
  }

  // Step 6.
  return options;
}