summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/intl/NumberFormat.js
blob: 83e306123c7dfee4f100cfcaf93d02b1f2dba586 (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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/* 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/. */

/* Portions Copyright Norbert Lindenberg 2011-2012. */

#include "NumberingSystemsGenerated.h"

/**
 * NumberFormat internal properties.
 *
 * Spec: ECMAScript Internationalization API Specification, 9.1 and 11.3.3.
 */
var numberFormatInternalProperties = {
    localeData: numberFormatLocaleData,
    relevantExtensionKeys: ["nu"],
};

/**
 * Compute an internal properties object from |lazyNumberFormatData|.
 */
function resolveNumberFormatInternals(lazyNumberFormatData) {
    assert(IsObject(lazyNumberFormatData), "lazy data not an object?");

    var internalProps = std_Object_create(null);

    var NumberFormat = numberFormatInternalProperties;

    // Compute effective locale.

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

    // Step 8.
    var r = ResolveLocale("NumberFormat",
                          lazyNumberFormatData.requestedLocales,
                          lazyNumberFormatData.opt,
                          NumberFormat.relevantExtensionKeys,
                          localeData);

    // Steps 9-10. (Step 11 is not relevant to our implementation.)
    internalProps.locale = r.locale;
    internalProps.numberingSystem = r.nu;

    // Compute formatting options.
    // Step 13.
    var style = lazyNumberFormatData.style;
    internalProps.style = style;

    // Steps 17, 19.
    if (style === "currency") {
        internalProps.currency = lazyNumberFormatData.currency;
        internalProps.currencyDisplay = lazyNumberFormatData.currencyDisplay;
        internalProps.currencySign = lazyNumberFormatData.currencySign;
    }

    // Intl.NumberFormat Unified API Proposal
    if (style === "unit") {
        internalProps.unit = lazyNumberFormatData.unit;
        internalProps.unitDisplay = lazyNumberFormatData.unitDisplay;
    }

    // Intl.NumberFormat Unified API Proposal
    var notation = lazyNumberFormatData.notation;
    internalProps.notation = notation;

    // Step 22.
    internalProps.minimumIntegerDigits = lazyNumberFormatData.minimumIntegerDigits;

    if ("minimumFractionDigits" in lazyNumberFormatData) {
        // Note: Intl.NumberFormat.prototype.resolvedOptions() exposes the
        // actual presence (versus undefined-ness) of these properties.
        assert("maximumFractionDigits" in lazyNumberFormatData, "min/max frac digits mismatch");
        internalProps.minimumFractionDigits = lazyNumberFormatData.minimumFractionDigits;
        internalProps.maximumFractionDigits = lazyNumberFormatData.maximumFractionDigits;
    }

    if ("minimumSignificantDigits" in lazyNumberFormatData) {
        // Note: Intl.NumberFormat.prototype.resolvedOptions() exposes the
        // actual presence (versus undefined-ness) of these properties.
        assert("maximumSignificantDigits" in lazyNumberFormatData, "min/max sig digits mismatch");
        internalProps.minimumSignificantDigits = lazyNumberFormatData.minimumSignificantDigits;
        internalProps.maximumSignificantDigits = lazyNumberFormatData.maximumSignificantDigits;
    }

    // Intl.NumberFormat Unified API Proposal
    if (notation === "compact")
        internalProps.compactDisplay = lazyNumberFormatData.compactDisplay;

    // Step 24.
    internalProps.useGrouping = lazyNumberFormatData.useGrouping;

    // Intl.NumberFormat Unified API Proposal
    internalProps.signDisplay = lazyNumberFormatData.signDisplay;

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

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

    var internals = getIntlObjectInternals(obj);
    assert(internals.type === "NumberFormat", "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 = resolveNumberFormatInternals(internals.lazyData);
    setInternalProperties(internals, internalProps);
    return internalProps;
}

/**
 * 11.1.11 UnwrapNumberFormat( nf )
 */
function UnwrapNumberFormat(nf) {
    // Steps 2 and 4 (error handling moved to caller).
    if (IsObject(nf) &&
        GuardToNumberFormat(nf) === null &&
        !IsWrappedNumberFormat(nf) &&
        nf instanceof GetBuiltinConstructor("NumberFormat"))
    {
        nf = nf[intlFallbackSymbol()];
    }
    return nf;
}

/**
 * Applies digit options used for number formatting onto the intl object.
 *
 * Spec: ECMAScript Internationalization API Specification, 11.1.1.
 */
function SetNumberFormatDigitOptions(lazyData, options, mnfdDefault, mxfdDefault, notation) {
    // We skip step 1 because we set the properties on a lazyData object.

    // Steps 2-4.
    assert(IsObject(options), "SetNumberFormatDigitOptions");
    assert(typeof mnfdDefault === "number", "SetNumberFormatDigitOptions");
    assert(typeof mxfdDefault === "number", "SetNumberFormatDigitOptions");
    assert(mnfdDefault <= mxfdDefault, "SetNumberFormatDigitOptions");
    assert(typeof notation === "string", "SetNumberFormatDigitOptions");

    // Steps 5-9.
    const mnid = GetNumberOption(options, "minimumIntegerDigits", 1, 21, 1);
    let mnfd = options.minimumFractionDigits;
    let mxfd = options.maximumFractionDigits;
    let mnsd = options.minimumSignificantDigits;
    let mxsd = options.maximumSignificantDigits;

    // Step 10.
    lazyData.minimumIntegerDigits = mnid;

    // Step 11.
    if (mnsd !== undefined || mxsd !== undefined) {
        // Step 11.a (Omitted).

        // Step 11.b.
        mnsd = DefaultNumberOption(mnsd, 1, 21, 1);

        // Step 11.c.
        mxsd = DefaultNumberOption(mxsd, mnsd, 21, 21);

        // Step 11.d.
        lazyData.minimumSignificantDigits = mnsd;

        // Step 11.e.
        lazyData.maximumSignificantDigits = mxsd;
    }

    // Step 12.
    else if (mnfd !== undefined || mxfd !== undefined) {
        // Step 12.a (Omitted).

        // Step 12.b.
        mnfd = DefaultNumberOption(mnfd, 0, 20, undefined);

        // Step 12.c.
        mxfd = DefaultNumberOption(mxfd, 0, 20, undefined);

        // Steps 12.d-e.
        // Inlined DefaultNumberOption, only the fallback case applies here.
        if (mnfd === undefined) {
            assert(mxfd !== undefined, "mxfd isn't undefined when mnfd is undefined");
            mnfd = std_Math_min(mnfdDefault, mxfd);
        }

        // Step 12.f.
        // Inlined DefaultNumberOption, only the fallback case applies here.
        else if (mxfd === undefined) {
            mxfd = std_Math_max(mxfdDefault, mnfd);
        }

        // Step 12.g.
        else if (mnfd > mxfd) {
            ThrowRangeError(JSMSG_INVALID_DIGITS_VALUE, mxfd);
        }

        // Step 12.h.
        lazyData.minimumFractionDigits = mnfd;

        // Step 12.i.
        lazyData.maximumFractionDigits = mxfd;
    }

    // Step 13.
    else if (notation === "compact") {
        // Step 13.a (Omitted).
    }

    // Step 14.
    else {
        // Step 14.a (Omitted).

        // Step 14.b.
        lazyData.minimumFractionDigits = mnfdDefault;

        // Step 14.c.
        lazyData.maximumFractionDigits = mxfdDefault;
    }
}

/**
 * Convert s to upper case, but limited to characters a-z.
 *
 * Spec: ECMAScript Internationalization API Specification, 6.1.
 */
function toASCIIUpperCase(s) {
    assert(typeof s === "string", "toASCIIUpperCase");

    // String.prototype.toUpperCase may map non-ASCII characters into ASCII,
    // so go character by character (actually code unit by code unit, but
    // since we only care about ASCII characters here, that's OK).
    var result = "";
    for (var i = 0; i < s.length; i++) {
        var c = callFunction(std_String_charCodeAt, s, i);
        result += (0x61 <= c && c <= 0x7A)
                  ? callFunction(std_String_fromCharCode, null, c & ~0x20)
                  : s[i];
    }
    return result;
}

/**
 * Verifies that the given string is a well-formed ISO 4217 currency code.
 *
 * Spec: ECMAScript Internationalization API Specification, 6.3.1.
 */
function IsWellFormedCurrencyCode(currency) {
    assert(typeof currency === "string", "currency is a string value");

    return currency.length === 3 && IsASCIIAlphaString(currency);
}

/**
 * Verifies that the given string is a well-formed core unit identifier as
 * defined in UTS #35, Part 2, Section 6. In addition to obeying the UTS #35
 * core unit identifier syntax, |unitIdentifier| must be one of the identifiers
 * sanctioned by UTS #35 or be a compound unit composed of two sanctioned simple
 * units.
 *
 * Intl.NumberFormat Unified API Proposal
 */
function IsWellFormedUnitIdentifier(unitIdentifier) {
    assert(typeof unitIdentifier === "string", "unitIdentifier is a string value");

    // Step 1.
    if (IsSanctionedSimpleUnitIdentifier(unitIdentifier))
        return true;

    // Step 2.
    var pos = callFunction(std_String_indexOf, unitIdentifier, "-per-");
    if (pos < 0)
        return false;

    var next = pos + "-per-".length;

    // Steps 3 and 5.
    var numerator = Substring(unitIdentifier, 0, pos);
    var denominator = Substring(unitIdentifier, next, unitIdentifier.length - next);

    // Steps 4 and 6.
    return IsSanctionedSimpleUnitIdentifier(numerator) &&
           IsSanctionedSimpleUnitIdentifier(denominator);
}

#if DEBUG || MOZ_SYSTEM_ICU
var availableMeasurementUnits = {
    value: null
};
#endif

/**
 * Verifies that the given string is a sanctioned simple core unit identifier.
 *
 * Intl.NumberFormat Unified API Proposal
 *
 * Also see: https://unicode.org/reports/tr35/tr35-general.html#Unit_Elements
 */
function IsSanctionedSimpleUnitIdentifier(unitIdentifier) {
    assert(typeof unitIdentifier === "string", "unitIdentifier is a string value");

    var isSanctioned = hasOwn(unitIdentifier, sanctionedSimpleUnitIdentifiers);

#if DEBUG || MOZ_SYSTEM_ICU
    if (isSanctioned) {
        if (availableMeasurementUnits.value === null)
            availableMeasurementUnits.value = intl_availableMeasurementUnits();

        var isSupported = hasOwn(unitIdentifier, availableMeasurementUnits.value);

#if MOZ_SYSTEM_ICU
        // A system ICU may support fewer measurement units, so we need to make
        // sure the unit is actually supported.
        isSanctioned = isSupported;
#else
        // Otherwise just assert that the sanctioned unit is also supported.
        assert(isSupported,
`"${unitIdentifier}" is sanctioned but not supported. Did you forget to update
intl/icu/data_filter.json to include the unit (and any implicit compound units)?
For example "speed/kilometer-per-hour" is implied by "length/kilometer" and
"duration/hour" and must therefore also be present.`);
#endif
    }
#endif

    return isSanctioned;
}

/**
 * Initializes an object as a NumberFormat.
 *
 * 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 NumberFormat.
 * This later work occurs in |resolveNumberFormatInternals|; steps not noted
 * here occur there.
 *
 * Spec: ECMAScript Internationalization API Specification, 11.1.2.
 */
function InitializeNumberFormat(numberFormat, thisValue, locales, options) {
    assert(IsObject(numberFormat), "InitializeNumberFormat called with non-object");
    assert(GuardToNumberFormat(numberFormat) !== null, "InitializeNumberFormat called with non-NumberFormat");

    // Lazy NumberFormat data has the following structure:
    //
    //   {
    //     requestedLocales: List of locales,
    //     style: "decimal" / "percent" / "currency" / "unit",
    //
    //     // fields present only if style === "currency":
    //     currency: a well-formed currency code (IsWellFormedCurrencyCode),
    //     currencyDisplay: "code" / "symbol" / "narrowSymbol" / "name",
    //     currencySign: "standard" / "accounting",
    //
    //     // fields present only if style === "unit":
    //     unit: a well-formed unit identifier (IsWellFormedUnitIdentifier),
    //     unitDisplay: "short" / "narrow" / "long",
    //
    //     opt: // opt object computed in InitializeNumberFormat
    //       {
    //         localeMatcher: "lookup" / "best fit",
    //
    //         nu: string matching a Unicode extension type, // optional
    //       }
    //
    //     minimumIntegerDigits: integer ∈ [1, 21],
    //
    //     // optional, mutually exclusive with the significant-digits option
    //     minimumFractionDigits: integer ∈ [0, 20],
    //     maximumFractionDigits: integer ∈ [0, 20],
    //
    //     // optional, mutually exclusive with the fraction-digits option
    //     minimumSignificantDigits: integer ∈ [1, 21],
    //     maximumSignificantDigits: integer ∈ [1, 21],
    //
    //     useGrouping: true / false,
    //
    //     notation: "standard" / "scientific" / "engineering" / "compact",
    //
    //     // optional, if notation is "compact"
    //     compactDisplay: "short" / "long",
    //
    //     signDisplay: "auto" / "never" / "always" / "exceptZero",
    //   }
    //
    // Note that lazy data is only installed as a final step of initialization,
    // so every NumberFormat lazy data object has *all* these properties, never a
    // subset of them.
    var lazyNumberFormatData = std_Object_create(null);

    // Step 1.
    var requestedLocales = CanonicalizeLocaleList(locales);
    lazyNumberFormatData.requestedLocales = requestedLocales;

    // Steps 2-3.
    //
    // If we ever need more speed here at startup, we should try to detect the
    // case where |options === undefined| and then directly use the default
    // value for each option.  For now, just keep it simple.
    if (options === undefined)
        options = std_Object_create(null);
    else
        options = ToObject(options);

    // Compute options that impact interpretation of locale.
    // Step 4.
    var opt = new Record();
    lazyNumberFormatData.opt = opt;

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

    var numberingSystem = GetOption(options, "numberingSystem", "string", undefined, undefined);

    if (numberingSystem !== undefined) {
        numberingSystem = intl_ValidateAndCanonicalizeUnicodeExtensionType(numberingSystem,
                                                                           "numberingSystem",
                                                                           "nu");
    }

    opt.nu = numberingSystem;

    // Compute formatting options.
    // Step 12.
    var style = GetOption(options, "style", "string", ["decimal", "percent", "currency", "unit"],
                          "decimal");
    lazyNumberFormatData.style = style;

    // Steps 14-17.
    var currency = GetOption(options, "currency", "string", undefined, undefined);

    // Per the Intl.NumberFormat Unified API Proposal, this check should only
    // happen for |style === "currency"|, which seems inconsistent, given that
    // we normally validate all options when present, even the ones which are
    // unused.
    // TODO: File issue at <https://github.com/tc39/proposal-unified-intl-numberformat>.
    if (currency !== undefined && !IsWellFormedCurrencyCode(currency))
        ThrowRangeError(JSMSG_INVALID_CURRENCY_CODE, currency);

    var cDigits;
    if (style === "currency") {
        if (currency === undefined)
            ThrowTypeError(JSMSG_UNDEFINED_CURRENCY);

        // Steps 19.a-c.
        currency = toASCIIUpperCase(currency);
        lazyNumberFormatData.currency = currency;
        cDigits = CurrencyDigits(currency);
    }

    // Step 18.
    var currencyDisplay = GetOption(options, "currencyDisplay", "string",
                                    ["code", "symbol", "narrowSymbol", "name"], "symbol");
    if (style === "currency")
        lazyNumberFormatData.currencyDisplay = currencyDisplay;

    // Intl.NumberFormat Unified API Proposal
    var currencySign = GetOption(options, "currencySign", "string", ["standard", "accounting"],
                                 "standard");
    if (style === "currency")
        lazyNumberFormatData.currencySign = currencySign;

    // Intl.NumberFormat Unified API Proposal
    var unit = GetOption(options, "unit", "string", undefined, undefined);

    // Aligned with |currency| check from above, see note about spec issue there.
    if (unit !== undefined && !IsWellFormedUnitIdentifier(unit))
        ThrowRangeError(JSMSG_INVALID_UNIT_IDENTIFIER, unit);

    var unitDisplay = GetOption(options, "unitDisplay", "string",
                                ["short", "narrow", "long"], "short");

    if (style === "unit") {
        if (unit === undefined)
            ThrowTypeError(JSMSG_UNDEFINED_UNIT);

        lazyNumberFormatData.unit = unit;
        lazyNumberFormatData.unitDisplay = unitDisplay;
    }

    // Steps 20-21.
    var mnfdDefault, mxfdDefault;
    if (style === "currency") {
        mnfdDefault = cDigits;
        mxfdDefault = cDigits;
    } else {
        mnfdDefault = 0;
        mxfdDefault = style === "percent" ? 0 : 3;
    }

    // Intl.NumberFormat Unified API Proposal
    var notation = GetOption(options, "notation", "string",
                             ["standard", "scientific", "engineering", "compact"], "standard");
    lazyNumberFormatData.notation = notation;

    // Step 22.
    SetNumberFormatDigitOptions(lazyNumberFormatData, options, mnfdDefault, mxfdDefault, notation);

    // Intl.NumberFormat Unified API Proposal
    var compactDisplay = GetOption(options, "compactDisplay", "string",
                                   ["short", "long"], "short");
    if (notation === "compact")
        lazyNumberFormatData.compactDisplay = compactDisplay;

    // Steps 23.
    var useGrouping = GetOption(options, "useGrouping", "boolean", undefined, true);
    lazyNumberFormatData.useGrouping = useGrouping;

    // Intl.NumberFormat Unified API Proposal
    var signDisplay = GetOption(options, "signDisplay", "string",
                                ["auto", "never", "always", "exceptZero"], "auto");
    lazyNumberFormatData.signDisplay = signDisplay;

    // Step 31.
    //
    // We've done everything that must be done now: mark the lazy data as fully
    // computed and install it.
    initializeIntlObject(numberFormat, "NumberFormat", lazyNumberFormatData);

    // 11.2.1, steps 4-5.
    // TODO: spec issue - The current spec doesn't have the IsObject check,
    // which means |Intl.NumberFormat.call(null)| is supposed to throw here.
    if (numberFormat !== thisValue && IsObject(thisValue) &&
        thisValue instanceof GetBuiltinConstructor("NumberFormat"))
    {
        _DefineDataProperty(thisValue, intlFallbackSymbol(), numberFormat,
                            ATTR_NONENUMERABLE | ATTR_NONCONFIGURABLE | ATTR_NONWRITABLE);

        return thisValue;
    }

    // 11.2.1, step 6.
    return numberFormat;
}

/**
 * Returns the number of decimal digits to be used for the given currency.
 *
 * Spec: ECMAScript Internationalization API Specification, 11.1.3.
 */
function CurrencyDigits(currency) {
    assert(typeof currency === "string", "currency is a string value");
    assert(IsWellFormedCurrencyCode(currency), "currency is well-formed");
    assert(currency == toASCIIUpperCase(currency), "currency is all upper-case");

    if (hasOwn(currency, currencyDigits))
        return currencyDigits[currency];
    return 2;
}

/**
 * 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.
 *
 * Spec: ECMAScript Internationalization API Specification, 11.3.2.
 */
function Intl_NumberFormat_supportedLocalesOf(locales /*, options*/) {
    var options = arguments.length > 1 ? arguments[1] : undefined;

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

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

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

function getNumberingSystems(locale) {
    // ICU doesn't have an API to determine the set of numbering systems
    // supported for a locale; it generally pretends that any numbering system
    // can be used with any locale. Supporting a decimal numbering system
    // (where only the digits are replaced) is easy, so we offer them all here.
    // Algorithmic numbering systems are typically tied to one locale, so for
    // lack of information we don't offer them.
    // The one thing we can find out from ICU is the default numbering system
    // for a locale.
    var defaultNumberingSystem = intl_numberingSystem(locale);
    return [
        defaultNumberingSystem,
        NUMBERING_SYSTEMS_WITH_SIMPLE_DIGIT_MAPPINGS
    ];
}

function numberFormatLocaleData() {
    return {
        nu: getNumberingSystems,
        default: {
            nu: intl_numberingSystem,
        },
    };
}

/**
 * Create function to be cached and returned by Intl.NumberFormat.prototype.format.
 *
 * Spec: ECMAScript Internationalization API Specification, 11.1.4.
 */
function createNumberFormatFormat(nf) {
    // This function is not inlined in $Intl_NumberFormat_format_get to avoid
    // creating a call-object on each call to $Intl_NumberFormat_format_get.
    return function(value) {
        // Step 1 (implicit).

        // Step 2.
        assert(IsObject(nf), "InitializeNumberFormat called with non-object");
        assert(GuardToNumberFormat(nf) !== null, "InitializeNumberFormat called with non-NumberFormat");

        // Steps 3-4.
        var x = ToNumeric(value);

        // Step 5.
        return intl_FormatNumber(nf, x, /* formatToParts = */ false, /* unitStyle = */ false);
    };
}

/**
 * Returns a function bound to this NumberFormat that returns a String value
 * representing the result of calling ToNumber(value) according to the
 * effective locale and the formatting options of this NumberFormat.
 *
 * Spec: ECMAScript Internationalization API Specification, 11.4.3.
 */
// Uncloned functions with `$` prefix are allocated as extended function
// to store the original name in `_SetCanonicalName`.
function $Intl_NumberFormat_format_get() {
    // Steps 1-3.
    var thisArg = UnwrapNumberFormat(this);
    var nf = thisArg;
    if (!IsObject(nf) || (nf = GuardToNumberFormat(nf)) === null) {
        return callFunction(CallNumberFormatMethodIfWrapped, thisArg,
                            "$Intl_NumberFormat_format_get");
    }

    var internals = getNumberFormatInternals(nf);

    // Step 4.
    if (internals.boundFormat === undefined) {
        // Steps 4.a-c.
        internals.boundFormat = createNumberFormatFormat(nf);
    }

    // Step 5.
    return internals.boundFormat;
}
_SetCanonicalName($Intl_NumberFormat_format_get, "get format");

/**
 * 11.4.4 Intl.NumberFormat.prototype.formatToParts ( value )
 */
function Intl_NumberFormat_formatToParts(value) {
    // Step 1.
    var nf = this;

    // Steps 2-3.
    if (!IsObject(nf) || (nf = GuardToNumberFormat(nf)) === null) {
        return callFunction(CallNumberFormatMethodIfWrapped, this, value,
                            "Intl_NumberFormat_formatToParts");
    }

    // Step 4.
    var x = ToNumeric(value);

    var internals = getNumberFormatInternals(nf);
    var unitStyle = internals.style === "unit";

    // Step 5.
    return intl_FormatNumber(nf, x, /* formatToParts = */ true, unitStyle);
}

/**
 * Returns the resolved options for a NumberFormat object.
 *
 * Spec: ECMAScript Internationalization API Specification, 11.4.5.
 */
function Intl_NumberFormat_resolvedOptions() {
    // Steps 1-3.
    var thisArg = UnwrapNumberFormat(this);
    var nf = thisArg;
    if (!IsObject(nf) || (nf = GuardToNumberFormat(nf)) === null) {
        return callFunction(CallNumberFormatMethodIfWrapped, thisArg,
                            "Intl_NumberFormat_resolvedOptions");
    }

    var internals = getNumberFormatInternals(nf);

    // Steps 4-5.
    var result = {
        locale: internals.locale,
        numberingSystem: internals.numberingSystem,
        style: internals.style,
    };

    // currency, currencyDisplay, and currencySign are only present for currency
    // formatters.
    assert(hasOwn("currency", internals) === (internals.style === "currency"),
           "currency is present iff style is 'currency'");
    assert(hasOwn("currencyDisplay", internals) === (internals.style === "currency"),
           "currencyDisplay is present iff style is 'currency'");
    assert(hasOwn("currencySign", internals) === (internals.style === "currency"),
           "currencySign is present iff style is 'currency'");

    if (hasOwn("currency", internals)) {
        _DefineDataProperty(result, "currency", internals.currency);
        _DefineDataProperty(result, "currencyDisplay", internals.currencyDisplay);
        _DefineDataProperty(result, "currencySign", internals.currencySign);
    }

    // unit and unitDisplay are only present for unit formatters.
    assert(hasOwn("unit", internals) === (internals.style === "unit"),
           "unit is present iff style is 'unit'");
    assert(hasOwn("unitDisplay", internals) === (internals.style === "unit"),
           "unitDisplay is present iff style is 'unit'");

    if (hasOwn("unit", internals)) {
        _DefineDataProperty(result, "unit", internals.unit);
        _DefineDataProperty(result, "unitDisplay", internals.unitDisplay);
    }

    _DefineDataProperty(result, "minimumIntegerDigits", internals.minimumIntegerDigits);

    // Min/Max fraction digits are either both present or not present at all.
    assert(hasOwn("minimumFractionDigits", internals) ===
           hasOwn("maximumFractionDigits", internals),
           "minimumFractionDigits is present iff maximumFractionDigits is present");

    if (hasOwn("minimumFractionDigits", internals)) {
        _DefineDataProperty(result, "minimumFractionDigits", internals.minimumFractionDigits);
        _DefineDataProperty(result, "maximumFractionDigits", internals.maximumFractionDigits);
    }

    // Min/Max significant digits are either both present or not present at all.
    assert(hasOwn("minimumSignificantDigits", internals) ===
           hasOwn("maximumSignificantDigits", internals),
           "minimumSignificantDigits is present iff maximumSignificantDigits is present");

    if (hasOwn("minimumSignificantDigits", internals)) {
        _DefineDataProperty(result, "minimumSignificantDigits",
                            internals.minimumSignificantDigits);
        _DefineDataProperty(result, "maximumSignificantDigits",
                            internals.maximumSignificantDigits);
    }

    _DefineDataProperty(result, "useGrouping", internals.useGrouping);

    var notation = internals.notation;
    _DefineDataProperty(result, "notation", notation);

    if (notation === "compact")
        _DefineDataProperty(result, "compactDisplay", internals.compactDisplay);

    _DefineDataProperty(result, "signDisplay", internals.signDisplay);

    // Step 6.
    return result;
}