cross-compartment.js (2953B)
1 // |reftest| skip-if(!this.hasOwnProperty("Intl")) 2 3 var otherGlobal = newGlobal(); 4 5 var numberFormat = new Intl.NumberFormat(); 6 var ccwNumberFormat = new otherGlobal.Intl.NumberFormat(); 7 8 // Test Intl.NumberFormat.prototype.format with a CCW object. 9 var Intl_NumberFormat_format_get = Object.getOwnPropertyDescriptor(Intl.NumberFormat.prototype, "format").get; 10 11 assertEq(Intl_NumberFormat_format_get.call(ccwNumberFormat)(0), 12 Intl_NumberFormat_format_get.call(numberFormat)(0)); 13 14 // Test Intl.NumberFormat.prototype.formatToParts with a CCW object. 15 var Intl_NumberFormat_formatToParts = Intl.NumberFormat.prototype.formatToParts; 16 17 assertEq(deepEqual(Intl_NumberFormat_formatToParts.call(ccwNumberFormat, 0), 18 Intl_NumberFormat_formatToParts.call(numberFormat, 0)), 19 true); 20 21 // Test Intl.NumberFormat.prototype.resolvedOptions with a CCW object. 22 var Intl_NumberFormat_resolvedOptions = Intl.NumberFormat.prototype.resolvedOptions; 23 24 assertEq(deepEqual(Intl_NumberFormat_resolvedOptions.call(ccwNumberFormat), 25 Intl_NumberFormat_resolvedOptions.call(numberFormat)), 26 true); 27 28 // Special case for Intl.NumberFormat: The Intl fallback symbol. 29 30 function fallbackSymbol(global) { 31 var NF = global.Intl.NumberFormat; 32 return Object.getOwnPropertySymbols(NF.call(Object.create(NF.prototype)))[0]; 33 } 34 35 const intlFallbackSymbol = fallbackSymbol(this); 36 const otherIntlFallbackSymbol = fallbackSymbol(otherGlobal); 37 assertEq(intlFallbackSymbol === otherIntlFallbackSymbol, false); 38 39 // Test when the fallback symbol points to a CCW NumberFormat object. 40 var objWithFallbackCCWNumberFormat = { 41 __proto__: Intl.NumberFormat.prototype, 42 [intlFallbackSymbol]: ccwNumberFormat, 43 }; 44 45 assertEq(Intl_NumberFormat_format_get.call(objWithFallbackCCWNumberFormat)(0), 46 Intl_NumberFormat_format_get.call(numberFormat)(0)); 47 48 assertEq(deepEqual(Intl_NumberFormat_resolvedOptions.call(objWithFallbackCCWNumberFormat), 49 Intl_NumberFormat_resolvedOptions.call(numberFormat)), 50 true); 51 52 // Ensure the fallback symbol(s) are not accessed for CCW NumberFormat objects. 53 var ccwNumberFormatWithPoisonedFallback = new otherGlobal.Intl.NumberFormat(); 54 Object.setPrototypeOf(ccwNumberFormatWithPoisonedFallback, Intl.NumberFormat.prototype); 55 Object.defineProperty(ccwNumberFormatWithPoisonedFallback, intlFallbackSymbol, { 56 get() { throw new Error(); } 57 }); 58 Object.defineProperty(ccwNumberFormatWithPoisonedFallback, otherIntlFallbackSymbol, { 59 get() { throw new Error(); } 60 }); 61 62 assertEq(Intl_NumberFormat_format_get.call(ccwNumberFormatWithPoisonedFallback)(0), 63 Intl_NumberFormat_format_get.call(numberFormat)(0)); 64 65 assertEq(deepEqual(Intl_NumberFormat_resolvedOptions.call(ccwNumberFormatWithPoisonedFallback), 66 Intl_NumberFormat_resolvedOptions.call(numberFormat)), 67 true); 68 69 if (typeof reportCompare === "function") 70 reportCompare(true, true);