|
| 1 | +// #5835 — Intl service constructor mechanics: `new.target` requirement and |
| 2 | +// `Reflect.construct`-driven prototype selection. |
| 3 | +// |
| 4 | +// ECMA-402 step 1 for ListFormat/RelativeTimeFormat/Segmenter/PluralRules/ |
| 5 | +// Locale is "If NewTarget is undefined, throw a TypeError exception" — unlike |
| 6 | +// the legacy factory-pattern NumberFormat/DateTimeFormat/Collator, which |
| 7 | +// silently `new`-ed on a bare call. And OrdinaryCreateFromConstructor means |
| 8 | +// `Reflect.construct(Intl.X, args, Custom)` must install `Custom.prototype` |
| 9 | +// on the result, not `Intl.X.prototype`. Both must match |
| 10 | +// `node --experimental-strip-types` byte-for-byte. |
| 11 | +function probe(name: string, fn: () => unknown): string { |
| 12 | + try { |
| 13 | + fn(); |
| 14 | + return "no throw"; |
| 15 | + } catch (e) { |
| 16 | + return (e as Error).constructor.name; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const I: any = (globalThis as any).Intl; |
| 21 | + |
| 22 | +console.log("ListFormat()", probe("ListFormat", () => I.ListFormat())); |
| 23 | +console.log("RelativeTimeFormat()", probe("RelativeTimeFormat", () => I.RelativeTimeFormat())); |
| 24 | +console.log("Segmenter()", probe("Segmenter", () => I.Segmenter())); |
| 25 | +console.log("PluralRules()", probe("PluralRules", () => I.PluralRules())); |
| 26 | +console.log("PluralRules.call(undefined)", probe("PluralRules.call", () => I.PluralRules.call(undefined))); |
| 27 | +console.log("Locale()", probe("Locale", () => I.Locale())); |
| 28 | +console.log("Locale('en')", probe("Locale('en')", () => I.Locale("en"))); |
| 29 | + |
| 30 | +// Legacy factory-pattern constructors still allow a bare call (no `new`). |
| 31 | +console.log("NumberFormat bare call", probe("NumberFormat", () => I.NumberFormat("en"))); |
| 32 | +console.log("DateTimeFormat bare call", probe("DateTimeFormat", () => I.DateTimeFormat("en"))); |
| 33 | +console.log("Collator bare call", probe("Collator", () => I.Collator("en"))); |
| 34 | + |
| 35 | +// `Reflect.construct(Intl.X, args, Custom)` installs `Custom.prototype`. |
| 36 | +{ |
| 37 | + const custom: any = new Function(); |
| 38 | + custom.prototype = {}; |
| 39 | + const obj = Reflect.construct(I.DisplayNames, [undefined, { type: "language" }], custom); |
| 40 | + console.log("DisplayNames custom prototype", Object.getPrototypeOf(obj) === custom.prototype); |
| 41 | +} |
| 42 | +{ |
| 43 | + const custom: any = new Function(); |
| 44 | + custom.prototype = {}; |
| 45 | + const obj = Reflect.construct(I.Segmenter, [], custom); |
| 46 | + console.log("Segmenter custom prototype", Object.getPrototypeOf(obj) === custom.prototype); |
| 47 | +} |
| 48 | + |
| 49 | +// Ordinary (non-Reflect.construct) instances still resolve the default prototype. |
| 50 | +console.log("plain ListFormat prototype", Object.getPrototypeOf(new I.ListFormat("en")) === I.ListFormat.prototype); |
| 51 | +console.log("plain Locale prototype", Object.getPrototypeOf(new I.Locale("en")) === I.Locale.prototype); |
0 commit comments