| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // ECMAScript 402 API implementation. | 5 // ECMAScript 402 API implementation. |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Intl object is a single object that has some named properties, | 8 * Intl object is a single object that has some named properties, |
| 9 * all of which are constructors. | 9 * all of which are constructors. |
| 10 */ | 10 */ |
| 11 (function(global, utils) { | 11 (function(global, utils) { |
| 12 | 12 |
| 13 "use strict"; | 13 "use strict"; |
| 14 | 14 |
| 15 %CheckIsBootstrapping(); | 15 %CheckIsBootstrapping(); |
| 16 | 16 |
| 17 // ------------------------------------------------------------------- | 17 // ------------------------------------------------------------------- |
| 18 // Imports | 18 // Imports |
| 19 | 19 |
| 20 var ArrayJoin; | 20 var ArrayJoin; |
| 21 var ArrayPush; | 21 var ArrayPush; |
| 22 var FLAG_intl_extra; | |
| 23 var GlobalDate = global.Date; | 22 var GlobalDate = global.Date; |
| 24 var GlobalNumber = global.Number; | 23 var GlobalNumber = global.Number; |
| 25 var GlobalRegExp = global.RegExp; | 24 var GlobalRegExp = global.RegExp; |
| 26 var GlobalString = global.String; | 25 var GlobalString = global.String; |
| 27 var InstallFunctions = utils.InstallFunctions; | 26 var InstallFunctions = utils.InstallFunctions; |
| 28 var InstallGetter = utils.InstallGetter; | 27 var InstallGetter = utils.InstallGetter; |
| 29 var InternalArray = utils.InternalArray; | 28 var InternalArray = utils.InternalArray; |
| 30 var InternalRegExpMatch; | 29 var InternalRegExpMatch; |
| 31 var InternalRegExpReplace | 30 var InternalRegExpReplace |
| 32 var ObjectHasOwnProperty = utils.ImportNow("ObjectHasOwnProperty"); | 31 var ObjectHasOwnProperty = utils.ImportNow("ObjectHasOwnProperty"); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 43 ArrayJoin = from.ArrayJoin; | 42 ArrayJoin = from.ArrayJoin; |
| 44 ArrayPush = from.ArrayPush; | 43 ArrayPush = from.ArrayPush; |
| 45 InternalRegExpMatch = from.InternalRegExpMatch; | 44 InternalRegExpMatch = from.InternalRegExpMatch; |
| 46 InternalRegExpReplace = from.InternalRegExpReplace; | 45 InternalRegExpReplace = from.InternalRegExpReplace; |
| 47 StringIndexOf = from.StringIndexOf; | 46 StringIndexOf = from.StringIndexOf; |
| 48 StringLastIndexOf = from.StringLastIndexOf; | 47 StringLastIndexOf = from.StringLastIndexOf; |
| 49 StringSubstr = from.StringSubstr; | 48 StringSubstr = from.StringSubstr; |
| 50 StringSubstring = from.StringSubstring; | 49 StringSubstring = from.StringSubstring; |
| 51 }); | 50 }); |
| 52 | 51 |
| 53 utils.ImportFromExperimental(function(from) { | |
| 54 FLAG_intl_extra = from.FLAG_intl_extra; | |
| 55 }); | |
| 56 | |
| 57 // Utilities for definitions | 52 // Utilities for definitions |
| 58 | 53 |
| 59 function InstallFunction(object, name, func) { | 54 function InstallFunction(object, name, func) { |
| 60 InstallFunctions(object, DONT_ENUM, [name, func]); | 55 InstallFunctions(object, DONT_ENUM, [name, func]); |
| 61 } | 56 } |
| 62 | 57 |
| 63 | 58 |
| 64 function InstallConstructor(object, name, func) { | 59 function InstallConstructor(object, name, func) { |
| 65 %CheckIsBootstrapping(); | 60 %CheckIsBootstrapping(); |
| 66 SetFunctionName(func, name); | 61 SetFunctionName(func, name); |
| (...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1029 usage: {value: internalOptions.usage, writable: true} | 1024 usage: {value: internalOptions.usage, writable: true} |
| 1030 }); | 1025 }); |
| 1031 | 1026 |
| 1032 var internalCollator = %CreateCollator(requestedLocale, | 1027 var internalCollator = %CreateCollator(requestedLocale, |
| 1033 internalOptions, | 1028 internalOptions, |
| 1034 resolved); | 1029 resolved); |
| 1035 | 1030 |
| 1036 // Writable, configurable and enumerable are set to false by default. | 1031 // Writable, configurable and enumerable are set to false by default. |
| 1037 %MarkAsInitializedIntlObjectOfType(collator, 'collator', internalCollator); | 1032 %MarkAsInitializedIntlObjectOfType(collator, 'collator', internalCollator); |
| 1038 collator[resolvedSymbol] = resolved; | 1033 collator[resolvedSymbol] = resolved; |
| 1039 if (FLAG_intl_extra) { | |
| 1040 %object_define_property(collator, 'resolved', resolvedAccessor); | |
| 1041 } | |
| 1042 | 1034 |
| 1043 return collator; | 1035 return collator; |
| 1044 } | 1036 } |
| 1045 | 1037 |
| 1046 | 1038 |
| 1047 /** | 1039 /** |
| 1048 * Constructs Intl.Collator object given optional locales and options | 1040 * Constructs Intl.Collator object given optional locales and options |
| 1049 * parameters. | 1041 * parameters. |
| 1050 * | 1042 * |
| 1051 * @constructor | 1043 * @constructor |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1273 internalOptions, | 1265 internalOptions, |
| 1274 resolved); | 1266 resolved); |
| 1275 | 1267 |
| 1276 if (internalOptions.style === 'currency') { | 1268 if (internalOptions.style === 'currency') { |
| 1277 %object_define_property(resolved, 'currencyDisplay', | 1269 %object_define_property(resolved, 'currencyDisplay', |
| 1278 {value: currencyDisplay, writable: true}); | 1270 {value: currencyDisplay, writable: true}); |
| 1279 } | 1271 } |
| 1280 | 1272 |
| 1281 %MarkAsInitializedIntlObjectOfType(numberFormat, 'numberformat', formatter); | 1273 %MarkAsInitializedIntlObjectOfType(numberFormat, 'numberformat', formatter); |
| 1282 numberFormat[resolvedSymbol] = resolved; | 1274 numberFormat[resolvedSymbol] = resolved; |
| 1283 if (FLAG_intl_extra) { | |
| 1284 %object_define_property(resolved, 'pattern', patternAccessor); | |
| 1285 %object_define_property(numberFormat, 'resolved', resolvedAccessor); | |
| 1286 } | |
| 1287 | 1275 |
| 1288 return numberFormat; | 1276 return numberFormat; |
| 1289 } | 1277 } |
| 1290 | 1278 |
| 1291 | 1279 |
| 1292 /** | 1280 /** |
| 1293 * Constructs Intl.NumberFormat object given optional locales and options | 1281 * Constructs Intl.NumberFormat object given optional locales and options |
| 1294 * parameters. | 1282 * parameters. |
| 1295 * | 1283 * |
| 1296 * @constructor | 1284 * @constructor |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1379 */ | 1367 */ |
| 1380 function formatNumber(formatter, value) { | 1368 function formatNumber(formatter, value) { |
| 1381 // Spec treats -0 and +0 as 0. | 1369 // Spec treats -0 and +0 as 0. |
| 1382 var number = TO_NUMBER(value) + 0; | 1370 var number = TO_NUMBER(value) + 0; |
| 1383 | 1371 |
| 1384 return %InternalNumberFormat(%GetImplFromInitializedIntlObject(formatter), | 1372 return %InternalNumberFormat(%GetImplFromInitializedIntlObject(formatter), |
| 1385 number); | 1373 number); |
| 1386 } | 1374 } |
| 1387 | 1375 |
| 1388 | 1376 |
| 1389 /** | |
| 1390 * Returns a Number that represents string value that was passed in. | |
| 1391 */ | |
| 1392 function IntlParseNumber(formatter, value) { | |
| 1393 return %InternalNumberParse(%GetImplFromInitializedIntlObject(formatter), | |
| 1394 TO_STRING(value)); | |
| 1395 } | |
| 1396 | |
| 1397 AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1, 'numberformat'); | 1377 AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1, 'numberformat'); |
| 1398 | 1378 |
| 1399 /** | 1379 /** |
| 1400 * Returns a string that matches LDML representation of the options object. | 1380 * Returns a string that matches LDML representation of the options object. |
| 1401 */ | 1381 */ |
| 1402 function toLDMLString(options) { | 1382 function toLDMLString(options) { |
| 1403 var getOption = getGetOption(options, 'dateformat'); | 1383 var getOption = getGetOption(options, 'dateformat'); |
| 1404 | 1384 |
| 1405 var ldmlString = ''; | 1385 var ldmlString = ''; |
| 1406 | 1386 |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1667 | 1647 |
| 1668 var formatter = %CreateDateTimeFormat( | 1648 var formatter = %CreateDateTimeFormat( |
| 1669 requestedLocale, {skeleton: ldmlString, timeZone: tz}, resolved); | 1649 requestedLocale, {skeleton: ldmlString, timeZone: tz}, resolved); |
| 1670 | 1650 |
| 1671 if (resolved.timeZone === "Etc/Unknown") { | 1651 if (resolved.timeZone === "Etc/Unknown") { |
| 1672 throw %make_range_error(kUnsupportedTimeZone, tz); | 1652 throw %make_range_error(kUnsupportedTimeZone, tz); |
| 1673 } | 1653 } |
| 1674 | 1654 |
| 1675 %MarkAsInitializedIntlObjectOfType(dateFormat, 'dateformat', formatter); | 1655 %MarkAsInitializedIntlObjectOfType(dateFormat, 'dateformat', formatter); |
| 1676 dateFormat[resolvedSymbol] = resolved; | 1656 dateFormat[resolvedSymbol] = resolved; |
| 1677 if (FLAG_intl_extra) { | |
| 1678 %object_define_property(resolved, 'pattern', patternAccessor); | |
| 1679 %object_define_property(dateFormat, 'resolved', resolvedAccessor); | |
| 1680 } | |
| 1681 | 1657 |
| 1682 return dateFormat; | 1658 return dateFormat; |
| 1683 } | 1659 } |
| 1684 | 1660 |
| 1685 | 1661 |
| 1686 /** | 1662 /** |
| 1687 * Constructs Intl.DateTimeFormat object given optional locales and options | 1663 * Constructs Intl.DateTimeFormat object given optional locales and options |
| 1688 * parameters. | 1664 * parameters. |
| 1689 * | 1665 * |
| 1690 * @constructor | 1666 * @constructor |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1812 | 1788 |
| 1813 if (!NUMBER_IS_FINITE(dateMs)) throw %make_range_error(kDateRange); | 1789 if (!NUMBER_IS_FINITE(dateMs)) throw %make_range_error(kDateRange); |
| 1814 | 1790 |
| 1815 return %InternalDateFormatToParts( | 1791 return %InternalDateFormatToParts( |
| 1816 %GetImplFromInitializedIntlObject(this), new GlobalDate(dateMs)); | 1792 %GetImplFromInitializedIntlObject(this), new GlobalDate(dateMs)); |
| 1817 } | 1793 } |
| 1818 | 1794 |
| 1819 %FunctionSetLength(FormatDateToParts, 0); | 1795 %FunctionSetLength(FormatDateToParts, 0); |
| 1820 | 1796 |
| 1821 | 1797 |
| 1822 /** | |
| 1823 * Returns a Date object representing the result of calling ToString(value) | |
| 1824 * according to the effective locale and the formatting options of this | |
| 1825 * DateTimeFormat. | |
| 1826 * Returns undefined if date string cannot be parsed. | |
| 1827 */ | |
| 1828 function IntlParseDate(formatter, value) { | |
| 1829 return %InternalDateParse(%GetImplFromInitializedIntlObject(formatter), | |
| 1830 TO_STRING(value)); | |
| 1831 } | |
| 1832 | |
| 1833 | |
| 1834 // 0 because date is optional argument. | 1798 // 0 because date is optional argument. |
| 1835 AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0, 'dateformat'); | 1799 AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0, 'dateformat'); |
| 1836 | 1800 |
| 1837 | 1801 |
| 1838 /** | 1802 /** |
| 1839 * Returns canonical Area/Location(/Location) name, or throws an exception | 1803 * Returns canonical Area/Location(/Location) name, or throws an exception |
| 1840 * if the zone name is invalid IANA name. | 1804 * if the zone name is invalid IANA name. |
| 1841 */ | 1805 */ |
| 1842 function canonicalizeTimeZoneID(tzID) { | 1806 function canonicalizeTimeZoneID(tzID) { |
| 1843 // Skip undefined zones. | 1807 // Skip undefined zones. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1903 locale: {writable: true} | 1867 locale: {writable: true} |
| 1904 }); | 1868 }); |
| 1905 | 1869 |
| 1906 var internalIterator = %CreateBreakIterator(locale.locale, | 1870 var internalIterator = %CreateBreakIterator(locale.locale, |
| 1907 internalOptions, | 1871 internalOptions, |
| 1908 resolved); | 1872 resolved); |
| 1909 | 1873 |
| 1910 %MarkAsInitializedIntlObjectOfType(iterator, 'breakiterator', | 1874 %MarkAsInitializedIntlObjectOfType(iterator, 'breakiterator', |
| 1911 internalIterator); | 1875 internalIterator); |
| 1912 iterator[resolvedSymbol] = resolved; | 1876 iterator[resolvedSymbol] = resolved; |
| 1913 if (FLAG_intl_extra) { | |
| 1914 %object_define_property(iterator, 'resolved', resolvedAccessor); | |
| 1915 } | |
| 1916 | 1877 |
| 1917 return iterator; | 1878 return iterator; |
| 1918 } | 1879 } |
| 1919 | 1880 |
| 1920 | 1881 |
| 1921 /** | 1882 /** |
| 1922 * Constructs Intl.v8BreakIterator object given optional locales and options | 1883 * Constructs Intl.v8BreakIterator object given optional locales and options |
| 1923 * parameters. | 1884 * parameters. |
| 1924 * | 1885 * |
| 1925 * @constructor | 1886 * @constructor |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2309 var locales = arguments[0]; | 2270 var locales = arguments[0]; |
| 2310 var options = arguments[1]; | 2271 var options = arguments[1]; |
| 2311 return toLocaleDateTime( | 2272 return toLocaleDateTime( |
| 2312 this, locales, options, 'time', 'time', 'dateformattime'); | 2273 this, locales, options, 'time', 'time', 'dateformattime'); |
| 2313 } | 2274 } |
| 2314 ); | 2275 ); |
| 2315 | 2276 |
| 2316 %FunctionRemovePrototype(FormatDateToParts); | 2277 %FunctionRemovePrototype(FormatDateToParts); |
| 2317 | 2278 |
| 2318 utils.Export(function(to) { | 2279 utils.Export(function(to) { |
| 2319 to.AddBoundMethod = AddBoundMethod; | |
| 2320 to.FormatDateToParts = FormatDateToParts; | 2280 to.FormatDateToParts = FormatDateToParts; |
| 2321 to.IntlParseDate = IntlParseDate; | |
| 2322 to.IntlParseNumber = IntlParseNumber; | |
| 2323 }); | 2281 }); |
| 2324 | 2282 |
| 2325 }) | 2283 }) |
| OLD | NEW |