v8/test/intl/general/constructor.js
littledan b0a09d7809 [intl] Add new semantics + compat fallback to Intl constructor
ECMA 402 v2 made Intl constructors more strict in terms of how they would
initialize objects, refusing to initialize objects which have already
been constructed. However, when Chrome tried to ship these semantics,
we ran into web compatibility issues.

This patch tries to square the circle and implement the simpler v2 object
semantics while including a compatibility workaround to allow objects to
sort of be initialized later, storing the real underlying Intl object
in a symbol-named property.

The new semantics are described in this PR against the ECMA 402 spec:
https://github.com/tc39/ecma402/pull/84

BUG=v8:4360, v8:4870
LOG=Y

Review-Url: https://codereview.chromium.org/2582993002
Cr-Commit-Position: refs/heads/master@{#41943}
2016-12-23 14:32:16 +00:00

45 lines
1.2 KiB
JavaScript

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
let compatConstructors = [
{c: Intl.DateTimeFormat, m: "format"},
{c: Intl.NumberFormat, m: "format"},
];
for (let {c, m} of compatConstructors) {
let i = Object.create(c.prototype);
assertTrue(i instanceof c);
assertThrows(() => i[m], TypeError);
assertEquals(i, c.call(i));
assertEquals(i[m], i[m]);
assertTrue(i instanceof c);
for ({c: c2, m: m2} of compatConstructors) {
if (c2 === c) {
assertThrows(() => c2.call(i), TypeError);
} else {
let i2 = c2.call(i);
assertTrue(i2 != i);
assertFalse(i2 instanceof c);
assertTrue(i2 instanceof c2);
assertEquals(i2[m2], i2[m2]);
}
}
}
let noCompatConstructors = [
{c: Intl.Collator, m: "compare"},
{c: Intl.v8BreakIterator, m: "next"},
];
for (let {c, m} of noCompatConstructors) {
let i = Object.create(c.prototype);
assertTrue(i instanceof c);
assertThrows(() => i[m], TypeError);
let i2 = c.call(i);
assertTrue(i2 != i);
assertEquals('function', typeof i2[m]);
assertTrue(i2 instanceof c);
}