summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/DateTimeFormat/unwrapping.js
blob: d2fc991cb4d0e05dffc175bbf0c98dbf717d91cd (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
// |reftest| skip-if(!this.hasOwnProperty("Intl"))

// Test UnwrapDateTimeFormat operation.

const dateTimeFormatFunctions = [];
dateTimeFormatFunctions.push({
    function: Intl.DateTimeFormat.prototype.resolvedOptions,
    unwrap: true,
});
dateTimeFormatFunctions.push({
    function: Object.getOwnPropertyDescriptor(Intl.DateTimeFormat.prototype, "format").get,
    unwrap: true,
});
dateTimeFormatFunctions.push({
    function: Intl.DateTimeFormat.prototype.formatToParts,
    unwrap: false,
});

function IsIntlService(c) {
    return typeof c === "function" &&
           c.hasOwnProperty("prototype") &&
           c.prototype.hasOwnProperty("resolvedOptions");
}

function IsObject(o) {
    return Object(o) === o;
}

function IsPrimitive(o) {
    return Object(o) !== o;
}

function intlObjects(ctor) {
    let args = [];
    if (ctor === Intl.DisplayNames) {
        // Intl.DisplayNames can't be constructed without any arguments.
        args = [undefined, {type: "language"}];
    }

    return [
        // Instance of an Intl constructor.
        new ctor(...args),

        // Instance of a subclassed Intl constructor.
        new class extends ctor {}(...args),

        // Intl object not inheriting from its default prototype.
        Object.setPrototypeOf(new ctor(...args), Object.prototype),
    ];
}

function thisValues(C) {
    const intlConstructors = Object.getOwnPropertyNames(Intl).map(name => Intl[name]).filter(IsIntlService);

    return [
        // Primitive values.
        ...[undefined, null, true, "abc", Symbol(), 123],

        // Object values.
        ...[{}, [], /(?:)/, function(){}, new Proxy({}, {})],

        // Intl objects.
        ...[].concat(...intlConstructors.filter(ctor => ctor !== C).map(intlObjects)),

        // Object inheriting from an Intl constructor prototype.
        ...intlConstructors.map(ctor => Object.create(ctor.prototype)),
    ];
}

const intlFallbackSymbol = Object.getOwnPropertySymbols(Intl.DateTimeFormat.call(Object.create(Intl.DateTimeFormat.prototype)))[0];

// Test Intl.DateTimeFormat.prototype methods.
for (let {function: dateTimeFormatFunction, unwrap} of dateTimeFormatFunctions) {
    // Test a TypeError is thrown when the this-value isn't an initialized
    // Intl.DateTimeFormat instance.
    for (let thisValue of thisValues(Intl.DateTimeFormat)) {
        assertThrowsInstanceOf(() => dateTimeFormatFunction.call(thisValue), TypeError);
    }

    // And test no error is thrown for initialized Intl.DateTimeFormat instances.
    for (let thisValue of intlObjects(Intl.DateTimeFormat)) {
        dateTimeFormatFunction.call(thisValue);
    }

    // Manually add [[FallbackSymbol]] to objects and then repeat the tests from above.
    for (let thisValue of thisValues(Intl.DateTimeFormat)) {
        assertThrowsInstanceOf(() => dateTimeFormatFunction.call({
            __proto__: Intl.DateTimeFormat.prototype,
            [intlFallbackSymbol]: thisValue,
        }), TypeError);
    }

    for (let thisValue of intlObjects(Intl.DateTimeFormat)) {
        let obj = {
            __proto__: Intl.DateTimeFormat.prototype,
            [intlFallbackSymbol]: thisValue,
        };
        if (unwrap) {
            dateTimeFormatFunction.call(obj);
        } else {
            assertThrowsInstanceOf(() => dateTimeFormatFunction.call(obj), TypeError);
        }
    }

    // Ensure [[FallbackSymbol]] isn't retrieved for Intl.DateTimeFormat instances.
    for (let thisValue of intlObjects(Intl.DateTimeFormat)) {
        Object.defineProperty(thisValue, intlFallbackSymbol, {
            get() { assertEq(false, true); }
        });
        dateTimeFormatFunction.call(thisValue);
    }

    // Ensure [[FallbackSymbol]] is only retrieved for objects inheriting from Intl.DateTimeFormat.prototype.
    for (let thisValue of thisValues(Intl.DateTimeFormat).filter(IsObject)) {
        if (Intl.DateTimeFormat.prototype.isPrototypeOf(thisValue))
            continue;
        Object.defineProperty(thisValue, intlFallbackSymbol, {
            get() { assertEq(false, true); }
        });
        assertThrowsInstanceOf(() => dateTimeFormatFunction.call(thisValue), TypeError);
    }

    // Repeat the test from above, but also change Intl.DateTimeFormat[@@hasInstance]
    // so it always returns |true|.
    for (let thisValue of thisValues(Intl.DateTimeFormat).filter(IsObject)) {
        let isPrototypeOf = Intl.DateTimeFormat.prototype.isPrototypeOf(thisValue);
        let hasInstanceCalled = false, symbolGetterCalled = false;
        Object.defineProperty(Intl.DateTimeFormat, Symbol.hasInstance, {
            value() {
                assertEq(hasInstanceCalled, false);
                hasInstanceCalled = true;
                return true;
            }, configurable: true
        });
        Object.defineProperty(thisValue, intlFallbackSymbol, {
            get() {
                assertEq(symbolGetterCalled, false);
                symbolGetterCalled = true;
                return null;
            }, configurable: true
        });

        assertThrowsInstanceOf(() => dateTimeFormatFunction.call(thisValue), TypeError);

        delete Intl.DateTimeFormat[Symbol.hasInstance];

        assertEq(hasInstanceCalled, false);
        assertEq(symbolGetterCalled, unwrap && isPrototypeOf);
    }

    // Test with primitive values.
    for (let thisValue of thisValues(Intl.DateTimeFormat).filter(IsPrimitive)) {
        // Ensure @@hasInstance is not called.
        Object.defineProperty(Intl.DateTimeFormat, Symbol.hasInstance, {
            value() { assertEq(true, false); }, configurable: true
        });
        let isUndefinedOrNull = thisValue === undefined || thisValue === null;
        let symbolHolder;
        if (!isUndefinedOrNull) {
            // Ensure the fallback symbol isn't retrieved from the primitive wrapper prototype.
            symbolHolder = Object.getPrototypeOf(thisValue);
            Object.defineProperty(symbolHolder, intlFallbackSymbol, {
                get() { assertEq(true, false); }, configurable: true
            });
        }

        assertThrowsInstanceOf(() => dateTimeFormatFunction.call(thisValue), TypeError);

        delete Intl.DateTimeFormat[Symbol.hasInstance];
        if (!isUndefinedOrNull)
            delete symbolHolder[intlFallbackSymbol];
    }
}

// Test format() returns the correct result for objects initialized as Intl.DateTimeFormat instances.
{
    // An actual Intl.DateTimeFormat instance.
    let dateTimeFormat = new Intl.DateTimeFormat();

    // An object initialized as a DateTimeFormat instance.
    let thisValue = Object.create(Intl.DateTimeFormat.prototype);
    Intl.DateTimeFormat.call(thisValue);

    // Object with [[FallbackSymbol]] set to DateTimeFormat instance.
    let fakeObj = {
        __proto__: Intl.DateTimeFormat.prototype,
        [intlFallbackSymbol]: dateTimeFormat,
    };

    for (let number of [0, Date.now(), -Date.now()]) {
        let expected = dateTimeFormat.format(number);
        assertEq(thisValue.format(number), expected);
        assertEq(thisValue[intlFallbackSymbol].format(number), expected);
        assertEq(fakeObj.format(number), expected);
    }
}

// Ensure formatToParts() doesn't use the fallback semantics.
{
    let formatToParts = Intl.DateTimeFormat.prototype.formatToParts;

    // An object initialized as a DateTimeFormat instance.
    let thisValue = Object.create(Intl.DateTimeFormat.prototype);
    Intl.DateTimeFormat.call(thisValue);
    assertThrowsInstanceOf(() => formatToParts.call(thisValue), TypeError);

    // Object with [[FallbackSymbol]] set to DateTimeFormat instance.
    let fakeObj = {
        __proto__: Intl.DateTimeFormat.prototype,
        [intlFallbackSymbol]: new Intl.DateTimeFormat(),
    };
    assertThrowsInstanceOf(() => formatToParts.call(fakeObj), TypeError);
}

// Test resolvedOptions() returns the same results.
{
    // An actual Intl.DateTimeFormat instance.
    let dateTimeFormat = new Intl.DateTimeFormat();

    // An object initialized as a DateTimeFormat instance.
    let thisValue = Object.create(Intl.DateTimeFormat.prototype);
    Intl.DateTimeFormat.call(thisValue);

    // Object with [[FallbackSymbol]] set to DateTimeFormat instance.
    let fakeObj = {
        __proto__: Intl.DateTimeFormat.prototype,
        [intlFallbackSymbol]: dateTimeFormat,
    };

    function assertEqOptions(actual, expected) {
        actual = Object.entries(actual);
        expected = Object.entries(expected);

        assertEq(actual.length, expected.length, "options count mismatch");
        for (var i = 0; i < expected.length; i++) {
            assertEq(actual[i][0], expected[i][0], "key mismatch at " + i);
            assertEq(actual[i][1], expected[i][1], "value mismatch at " + i);
        }
    }

    let expected = dateTimeFormat.resolvedOptions();
    assertEqOptions(thisValue.resolvedOptions(), expected);
    assertEqOptions(thisValue[intlFallbackSymbol].resolvedOptions(), expected);
    assertEqOptions(fakeObj.resolvedOptions(), expected);
}

if (typeof reportCompare === "function")
    reportCompare(true, true);