| 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 ArrayIndexOf; | 20 var ArrayIndexOf; |
| 21 var ArrayJoin; | 21 var ArrayJoin; |
| 22 var ArrayPush; | 22 var ArrayPush; |
| 23 var FLAG_intl_extra; |
| 23 var GlobalBoolean = global.Boolean; | 24 var GlobalBoolean = global.Boolean; |
| 24 var GlobalDate = global.Date; | 25 var GlobalDate = global.Date; |
| 25 var GlobalNumber = global.Number; | 26 var GlobalNumber = global.Number; |
| 26 var GlobalRegExp = global.RegExp; | 27 var GlobalRegExp = global.RegExp; |
| 27 var GlobalString = global.String; | 28 var GlobalString = global.String; |
| 28 var InstallFunctions = utils.InstallFunctions; | 29 var InstallFunctions = utils.InstallFunctions; |
| 29 var InstallGetter = utils.InstallGetter; | 30 var InstallGetter = utils.InstallGetter; |
| 30 var InternalArray = utils.InternalArray; | 31 var InternalArray = utils.InternalArray; |
| 31 var InternalRegExpMatch; | 32 var InternalRegExpMatch; |
| 32 var InternalRegExpReplace | 33 var InternalRegExpReplace |
| (...skipping 24 matching lines...) Expand all Loading... |
| 57 MakeTypeError = from.MakeTypeError; | 58 MakeTypeError = from.MakeTypeError; |
| 58 InternalRegExpMatch = from.InternalRegExpMatch; | 59 InternalRegExpMatch = from.InternalRegExpMatch; |
| 59 InternalRegExpReplace = from.InternalRegExpReplace; | 60 InternalRegExpReplace = from.InternalRegExpReplace; |
| 60 StringIndexOf = from.StringIndexOf; | 61 StringIndexOf = from.StringIndexOf; |
| 61 StringLastIndexOf = from.StringLastIndexOf; | 62 StringLastIndexOf = from.StringLastIndexOf; |
| 62 StringSplit = from.StringSplit; | 63 StringSplit = from.StringSplit; |
| 63 StringSubstr = from.StringSubstr; | 64 StringSubstr = from.StringSubstr; |
| 64 StringSubstring = from.StringSubstring; | 65 StringSubstring = from.StringSubstring; |
| 65 }); | 66 }); |
| 66 | 67 |
| 68 utils.ImportFromExperimental(function(from) { |
| 69 FLAG_intl_extra = from.FLAG_intl_extra; |
| 70 }); |
| 71 |
| 67 // Utilities for definitions | 72 // Utilities for definitions |
| 68 | 73 |
| 69 function InstallFunction(object, name, func) { | 74 function InstallFunction(object, name, func) { |
| 70 InstallFunctions(object, DONT_ENUM, [name, func]); | 75 InstallFunctions(object, DONT_ENUM, [name, func]); |
| 71 } | 76 } |
| 72 | 77 |
| 73 | 78 |
| 74 function InstallConstructor(object, name, func) { | 79 function InstallConstructor(object, name, func) { |
| 75 %CheckIsBootstrapping(); | 80 %CheckIsBootstrapping(); |
| 76 SetFunctionName(func, name); | 81 SetFunctionName(func, name); |
| (...skipping 904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 981 usage: {value: internalOptions.usage, writable: true} | 986 usage: {value: internalOptions.usage, writable: true} |
| 982 }); | 987 }); |
| 983 | 988 |
| 984 var internalCollator = %CreateCollator(requestedLocale, | 989 var internalCollator = %CreateCollator(requestedLocale, |
| 985 internalOptions, | 990 internalOptions, |
| 986 resolved); | 991 resolved); |
| 987 | 992 |
| 988 // Writable, configurable and enumerable are set to false by default. | 993 // Writable, configurable and enumerable are set to false by default. |
| 989 %MarkAsInitializedIntlObjectOfType(collator, 'collator', internalCollator); | 994 %MarkAsInitializedIntlObjectOfType(collator, 'collator', internalCollator); |
| 990 collator[resolvedSymbol] = resolved; | 995 collator[resolvedSymbol] = resolved; |
| 991 %object_define_property(collator, 'resolved', resolvedAccessor); | 996 if (FLAG_intl_extra) { |
| 997 %object_define_property(collator, 'resolved', resolvedAccessor); |
| 998 } |
| 992 | 999 |
| 993 return collator; | 1000 return collator; |
| 994 } | 1001 } |
| 995 | 1002 |
| 996 | 1003 |
| 997 /** | 1004 /** |
| 998 * Constructs Intl.Collator object given optional locales and options | 1005 * Constructs Intl.Collator object given optional locales and options |
| 999 * parameters. | 1006 * parameters. |
| 1000 * | 1007 * |
| 1001 * @constructor | 1008 * @constructor |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1202 | 1209 |
| 1203 var requestedLocale = locale.locale + extension; | 1210 var requestedLocale = locale.locale + extension; |
| 1204 var resolved = %object_define_properties({}, { | 1211 var resolved = %object_define_properties({}, { |
| 1205 currency: {writable: true}, | 1212 currency: {writable: true}, |
| 1206 currencyDisplay: {writable: true}, | 1213 currencyDisplay: {writable: true}, |
| 1207 locale: {writable: true}, | 1214 locale: {writable: true}, |
| 1208 maximumFractionDigits: {writable: true}, | 1215 maximumFractionDigits: {writable: true}, |
| 1209 minimumFractionDigits: {writable: true}, | 1216 minimumFractionDigits: {writable: true}, |
| 1210 minimumIntegerDigits: {writable: true}, | 1217 minimumIntegerDigits: {writable: true}, |
| 1211 numberingSystem: {writable: true}, | 1218 numberingSystem: {writable: true}, |
| 1212 pattern: patternAccessor, | |
| 1213 requestedLocale: {value: requestedLocale, writable: true}, | 1219 requestedLocale: {value: requestedLocale, writable: true}, |
| 1214 style: {value: internalOptions.style, writable: true}, | 1220 style: {value: internalOptions.style, writable: true}, |
| 1215 useGrouping: {writable: true} | 1221 useGrouping: {writable: true} |
| 1216 }); | 1222 }); |
| 1217 if (HAS_OWN_PROPERTY(internalOptions, 'minimumSignificantDigits')) { | 1223 if (HAS_OWN_PROPERTY(internalOptions, 'minimumSignificantDigits')) { |
| 1218 defineWEProperty(resolved, 'minimumSignificantDigits', UNDEFINED); | 1224 defineWEProperty(resolved, 'minimumSignificantDigits', UNDEFINED); |
| 1219 } | 1225 } |
| 1220 if (HAS_OWN_PROPERTY(internalOptions, 'maximumSignificantDigits')) { | 1226 if (HAS_OWN_PROPERTY(internalOptions, 'maximumSignificantDigits')) { |
| 1221 defineWEProperty(resolved, 'maximumSignificantDigits', UNDEFINED); | 1227 defineWEProperty(resolved, 'maximumSignificantDigits', UNDEFINED); |
| 1222 } | 1228 } |
| 1223 var formatter = %CreateNumberFormat(requestedLocale, | 1229 var formatter = %CreateNumberFormat(requestedLocale, |
| 1224 internalOptions, | 1230 internalOptions, |
| 1225 resolved); | 1231 resolved); |
| 1226 | 1232 |
| 1227 if (internalOptions.style === 'currency') { | 1233 if (internalOptions.style === 'currency') { |
| 1228 %object_define_property(resolved, 'currencyDisplay', | 1234 %object_define_property(resolved, 'currencyDisplay', |
| 1229 {value: currencyDisplay, writable: true}); | 1235 {value: currencyDisplay, writable: true}); |
| 1230 } | 1236 } |
| 1231 | 1237 |
| 1232 %MarkAsInitializedIntlObjectOfType(numberFormat, 'numberformat', formatter); | 1238 %MarkAsInitializedIntlObjectOfType(numberFormat, 'numberformat', formatter); |
| 1233 numberFormat[resolvedSymbol] = resolved; | 1239 numberFormat[resolvedSymbol] = resolved; |
| 1234 %object_define_property(numberFormat, 'resolved', resolvedAccessor); | 1240 if (FLAG_intl_extra) { |
| 1241 %object_define_property(resolved, 'pattern', patternAccessor); |
| 1242 %object_define_property(numberFormat, 'resolved', resolvedAccessor); |
| 1243 } |
| 1235 | 1244 |
| 1236 return numberFormat; | 1245 return numberFormat; |
| 1237 } | 1246 } |
| 1238 | 1247 |
| 1239 | 1248 |
| 1240 /** | 1249 /** |
| 1241 * Constructs Intl.NumberFormat object given optional locales and options | 1250 * Constructs Intl.NumberFormat object given optional locales and options |
| 1242 * parameters. | 1251 * parameters. |
| 1243 * | 1252 * |
| 1244 * @constructor | 1253 * @constructor |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1330 var number = TO_NUMBER(value) + 0; | 1339 var number = TO_NUMBER(value) + 0; |
| 1331 | 1340 |
| 1332 return %InternalNumberFormat(%GetImplFromInitializedIntlObject(formatter), | 1341 return %InternalNumberFormat(%GetImplFromInitializedIntlObject(formatter), |
| 1333 number); | 1342 number); |
| 1334 } | 1343 } |
| 1335 | 1344 |
| 1336 | 1345 |
| 1337 /** | 1346 /** |
| 1338 * Returns a Number that represents string value that was passed in. | 1347 * Returns a Number that represents string value that was passed in. |
| 1339 */ | 1348 */ |
| 1340 function parseNumber(formatter, value) { | 1349 function IntlParseNumber(formatter, value) { |
| 1341 return %InternalNumberParse(%GetImplFromInitializedIntlObject(formatter), | 1350 return %InternalNumberParse(%GetImplFromInitializedIntlObject(formatter), |
| 1342 GlobalString(value)); | 1351 GlobalString(value)); |
| 1343 } | 1352 } |
| 1344 | 1353 |
| 1345 | |
| 1346 AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1); | 1354 AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1); |
| 1347 AddBoundMethod(Intl.NumberFormat, 'v8Parse', parseNumber, 1); | |
| 1348 | 1355 |
| 1349 /** | 1356 /** |
| 1350 * Returns a string that matches LDML representation of the options object. | 1357 * Returns a string that matches LDML representation of the options object. |
| 1351 */ | 1358 */ |
| 1352 function toLDMLString(options) { | 1359 function toLDMLString(options) { |
| 1353 var getOption = getGetOption(options, 'dateformat'); | 1360 var getOption = getGetOption(options, 'dateformat'); |
| 1354 | 1361 |
| 1355 var ldmlString = ''; | 1362 var ldmlString = ''; |
| 1356 | 1363 |
| 1357 var option = getOption('weekday', 'string', ['narrow', 'short', 'long']); | 1364 var option = getOption('weekday', 'string', ['narrow', 'short', 'long']); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1599 calendar: {writable: true}, | 1606 calendar: {writable: true}, |
| 1600 day: {writable: true}, | 1607 day: {writable: true}, |
| 1601 era: {writable: true}, | 1608 era: {writable: true}, |
| 1602 hour12: {writable: true}, | 1609 hour12: {writable: true}, |
| 1603 hour: {writable: true}, | 1610 hour: {writable: true}, |
| 1604 locale: {writable: true}, | 1611 locale: {writable: true}, |
| 1605 minute: {writable: true}, | 1612 minute: {writable: true}, |
| 1606 month: {writable: true}, | 1613 month: {writable: true}, |
| 1607 numberingSystem: {writable: true}, | 1614 numberingSystem: {writable: true}, |
| 1608 [patternSymbol]: {writable: true}, | 1615 [patternSymbol]: {writable: true}, |
| 1609 pattern: patternAccessor, | |
| 1610 requestedLocale: {value: requestedLocale, writable: true}, | 1616 requestedLocale: {value: requestedLocale, writable: true}, |
| 1611 second: {writable: true}, | 1617 second: {writable: true}, |
| 1612 timeZone: {writable: true}, | 1618 timeZone: {writable: true}, |
| 1613 timeZoneName: {writable: true}, | 1619 timeZoneName: {writable: true}, |
| 1614 tz: {value: tz, writable: true}, | 1620 tz: {value: tz, writable: true}, |
| 1615 weekday: {writable: true}, | 1621 weekday: {writable: true}, |
| 1616 year: {writable: true} | 1622 year: {writable: true} |
| 1617 }); | 1623 }); |
| 1618 | 1624 |
| 1619 var formatter = %CreateDateTimeFormat( | 1625 var formatter = %CreateDateTimeFormat( |
| 1620 requestedLocale, {skeleton: ldmlString, timeZone: tz}, resolved); | 1626 requestedLocale, {skeleton: ldmlString, timeZone: tz}, resolved); |
| 1621 | 1627 |
| 1622 if (resolved.timeZone === "Etc/Unknown") { | 1628 if (resolved.timeZone === "Etc/Unknown") { |
| 1623 throw MakeRangeError(kUnsupportedTimeZone, tz); | 1629 throw MakeRangeError(kUnsupportedTimeZone, tz); |
| 1624 } | 1630 } |
| 1625 | 1631 |
| 1626 %MarkAsInitializedIntlObjectOfType(dateFormat, 'dateformat', formatter); | 1632 %MarkAsInitializedIntlObjectOfType(dateFormat, 'dateformat', formatter); |
| 1627 dateFormat[resolvedSymbol] = resolved; | 1633 dateFormat[resolvedSymbol] = resolved; |
| 1628 %object_define_property(dateFormat, 'resolved', resolvedAccessor); | 1634 if (FLAG_intl_extra) { |
| 1635 %object_define_property(resolved, 'pattern', patternAccessor); |
| 1636 %object_define_property(dateFormat, 'resolved', resolvedAccessor); |
| 1637 } |
| 1629 | 1638 |
| 1630 return dateFormat; | 1639 return dateFormat; |
| 1631 } | 1640 } |
| 1632 | 1641 |
| 1633 | 1642 |
| 1634 /** | 1643 /** |
| 1635 * Constructs Intl.DateTimeFormat object given optional locales and options | 1644 * Constructs Intl.DateTimeFormat object given optional locales and options |
| 1636 * parameters. | 1645 * parameters. |
| 1637 * | 1646 * |
| 1638 * @constructor | 1647 * @constructor |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1752 new GlobalDate(dateMs)); | 1761 new GlobalDate(dateMs)); |
| 1753 } | 1762 } |
| 1754 | 1763 |
| 1755 | 1764 |
| 1756 /** | 1765 /** |
| 1757 * Returns a Date object representing the result of calling ToString(value) | 1766 * Returns a Date object representing the result of calling ToString(value) |
| 1758 * according to the effective locale and the formatting options of this | 1767 * according to the effective locale and the formatting options of this |
| 1759 * DateTimeFormat. | 1768 * DateTimeFormat. |
| 1760 * Returns undefined if date string cannot be parsed. | 1769 * Returns undefined if date string cannot be parsed. |
| 1761 */ | 1770 */ |
| 1762 function parseDate(formatter, value) { | 1771 function IntlParseDate(formatter, value) { |
| 1763 return %InternalDateParse(%GetImplFromInitializedIntlObject(formatter), | 1772 return %InternalDateParse(%GetImplFromInitializedIntlObject(formatter), |
| 1764 GlobalString(value)); | 1773 GlobalString(value)); |
| 1765 } | 1774 } |
| 1766 | 1775 |
| 1767 | 1776 |
| 1768 // 0 because date is optional argument. | 1777 // 0 because date is optional argument. |
| 1769 AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0); | 1778 AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0); |
| 1770 AddBoundMethod(Intl.DateTimeFormat, 'v8Parse', parseDate, 1); | |
| 1771 | 1779 |
| 1772 | 1780 |
| 1773 /** | 1781 /** |
| 1774 * Returns canonical Area/Location(/Location) name, or throws an exception | 1782 * Returns canonical Area/Location(/Location) name, or throws an exception |
| 1775 * if the zone name is invalid IANA name. | 1783 * if the zone name is invalid IANA name. |
| 1776 */ | 1784 */ |
| 1777 function canonicalizeTimeZoneID(tzID) { | 1785 function canonicalizeTimeZoneID(tzID) { |
| 1778 // Skip undefined zones. | 1786 // Skip undefined zones. |
| 1779 if (IS_UNDEFINED(tzID)) { | 1787 if (IS_UNDEFINED(tzID)) { |
| 1780 return tzID; | 1788 return tzID; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1835 locale: {writable: true} | 1843 locale: {writable: true} |
| 1836 }); | 1844 }); |
| 1837 | 1845 |
| 1838 var internalIterator = %CreateBreakIterator(locale.locale, | 1846 var internalIterator = %CreateBreakIterator(locale.locale, |
| 1839 internalOptions, | 1847 internalOptions, |
| 1840 resolved); | 1848 resolved); |
| 1841 | 1849 |
| 1842 %MarkAsInitializedIntlObjectOfType(iterator, 'breakiterator', | 1850 %MarkAsInitializedIntlObjectOfType(iterator, 'breakiterator', |
| 1843 internalIterator); | 1851 internalIterator); |
| 1844 iterator[resolvedSymbol] = resolved; | 1852 iterator[resolvedSymbol] = resolved; |
| 1845 %object_define_property(iterator, 'resolved', resolvedAccessor); | 1853 if (FLAG_intl_extra) { |
| 1854 %object_define_property(iterator, 'resolved', resolvedAccessor); |
| 1855 } |
| 1846 | 1856 |
| 1847 return iterator; | 1857 return iterator; |
| 1848 } | 1858 } |
| 1849 | 1859 |
| 1850 | 1860 |
| 1851 /** | 1861 /** |
| 1852 * Constructs Intl.v8BreakIterator object given optional locales and options | 1862 * Constructs Intl.v8BreakIterator object given optional locales and options |
| 1853 * parameters. | 1863 * parameters. |
| 1854 * | 1864 * |
| 1855 * @constructor | 1865 * @constructor |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2215 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); | 2225 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); |
| 2216 } | 2226 } |
| 2217 | 2227 |
| 2218 var locales = arguments[0]; | 2228 var locales = arguments[0]; |
| 2219 var options = arguments[1]; | 2229 var options = arguments[1]; |
| 2220 return toLocaleDateTime( | 2230 return toLocaleDateTime( |
| 2221 this, locales, options, 'time', 'time', 'dateformattime'); | 2231 this, locales, options, 'time', 'time', 'dateformattime'); |
| 2222 } | 2232 } |
| 2223 ); | 2233 ); |
| 2224 | 2234 |
| 2235 utils.Export(function(to) { |
| 2236 to.AddBoundMethod = AddBoundMethod; |
| 2237 to.IntlParseDate = IntlParseDate; |
| 2238 to.IntlParseNumber = IntlParseNumber; |
| 2239 }); |
| 2240 |
| 2225 }) | 2241 }) |
| OLD | NEW |