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 */ |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 'numberformat': UNDEFINED, | 137 'numberformat': UNDEFINED, |
138 'dateformat': UNDEFINED, | 138 'dateformat': UNDEFINED, |
139 'breakiterator': UNDEFINED | 139 'breakiterator': UNDEFINED |
140 }; | 140 }; |
141 | 141 |
142 /** | 142 /** |
143 * Caches default ICU locale. | 143 * Caches default ICU locale. |
144 */ | 144 */ |
145 var DEFAULT_ICU_LOCALE = UNDEFINED; | 145 var DEFAULT_ICU_LOCALE = UNDEFINED; |
146 | 146 |
| 147 function GetDefaultICULocaleJS() { |
| 148 if (IS_UNDEFINED(DEFAULT_ICU_LOCALE)) { |
| 149 DEFAULT_ICU_LOCALE = %GetDefaultICULocale(); |
| 150 } |
| 151 return DEFAULT_ICU_LOCALE; |
| 152 } |
| 153 |
147 /** | 154 /** |
148 * Unicode extension regular expression. | 155 * Unicode extension regular expression. |
149 */ | 156 */ |
150 var UNICODE_EXTENSION_RE = UNDEFINED; | 157 var UNICODE_EXTENSION_RE = UNDEFINED; |
151 | 158 |
152 function GetUnicodeExtensionRE() { | 159 function GetUnicodeExtensionRE() { |
153 if (IS_UNDEFINED(UNDEFINED)) { | 160 if (IS_UNDEFINED(UNDEFINED)) { |
154 UNICODE_EXTENSION_RE = new GlobalRegExp('-u(-[a-z0-9]{2,8})+', 'g'); | 161 UNICODE_EXTENSION_RE = new GlobalRegExp('-u(-[a-z0-9]{2,8})+', 'g'); |
155 } | 162 } |
156 return UNICODE_EXTENSION_RE; | 163 return UNICODE_EXTENSION_RE; |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 // Truncate locale if possible. | 448 // Truncate locale if possible. |
442 var pos = %_Call(StringLastIndexOf, locale, '-'); | 449 var pos = %_Call(StringLastIndexOf, locale, '-'); |
443 if (pos === -1) { | 450 if (pos === -1) { |
444 break; | 451 break; |
445 } | 452 } |
446 locale = %_Call(StringSubstring, locale, 0, pos); | 453 locale = %_Call(StringSubstring, locale, 0, pos); |
447 } while (true); | 454 } while (true); |
448 } | 455 } |
449 | 456 |
450 // Didn't find a match, return default. | 457 // Didn't find a match, return default. |
451 if (IS_UNDEFINED(DEFAULT_ICU_LOCALE)) { | 458 return {'locale': GetDefaultICULocaleJS(), 'extension': '', 'position': -1}; |
452 DEFAULT_ICU_LOCALE = %GetDefaultICULocale(); | |
453 } | |
454 | |
455 return {'locale': DEFAULT_ICU_LOCALE, 'extension': '', 'position': -1}; | |
456 } | 459 } |
457 | 460 |
458 | 461 |
459 /** | 462 /** |
460 * Returns best matched supported locale and extension info using | 463 * Returns best matched supported locale and extension info using |
461 * implementation dependend algorithm. | 464 * implementation dependend algorithm. |
462 */ | 465 */ |
463 function bestFitMatcher(service, requestedLocales) { | 466 function bestFitMatcher(service, requestedLocales) { |
464 // TODO(cira): implement better best fit algorithm. | 467 // TODO(cira): implement better best fit algorithm. |
465 return lookupMatcher(service, requestedLocales); | 468 return lookupMatcher(service, requestedLocales); |
(...skipping 1519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1985 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults; | 1988 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults; |
1986 if (IS_UNDEFINED(locales) && IS_UNDEFINED(options)) { | 1989 if (IS_UNDEFINED(locales) && IS_UNDEFINED(options)) { |
1987 if (IS_UNDEFINED(defaultObjects[service])) { | 1990 if (IS_UNDEFINED(defaultObjects[service])) { |
1988 defaultObjects[service] = new savedObjects[service](locales, useOptions); | 1991 defaultObjects[service] = new savedObjects[service](locales, useOptions); |
1989 } | 1992 } |
1990 return defaultObjects[service]; | 1993 return defaultObjects[service]; |
1991 } | 1994 } |
1992 return new savedObjects[service](locales, useOptions); | 1995 return new savedObjects[service](locales, useOptions); |
1993 } | 1996 } |
1994 | 1997 |
| 1998 function getCaseConversionLanguageId(locales) { |
| 1999 var locales = initializeLocaleList(locales); |
| 2000 var language = locales.length > 0 ? locales[0] : GetDefaultICULocaleJS(); |
| 2001 var pos = %_Call(StringIndexOf, language, '-'); |
| 2002 if (pos != -1) |
| 2003 language = %_Call(StringSubstring, language, 0, pos); |
| 2004 var CUSTOM_CASE_LANGUAGES = ['az', 'el', 'lt', 'tr']; |
| 2005 return %_Call(ArrayIndexOf, CUSTOM_CASE_LANGUAGES, language); |
| 2006 } |
| 2007 |
| 2008 function localeConvertCase(s, locales, isToUpper) { |
| 2009 var caseConversionLanguageId = getCaseConversionLanguageId(locales); |
| 2010 if (caseConversionLanguageId == -1) |
| 2011 return isToUpper ? %StringToUpperCaseI18N(s) : %StringToLowerCaseI18N(s); |
| 2012 return %StringLocaleConvertCase(s, isToUpper, caseConversionLanguageId); |
| 2013 } |
| 2014 |
1995 /** | 2015 /** |
1996 * Compares this and that, and returns less than 0, 0 or greater than 0 value. | 2016 * Compares this and that, and returns less than 0, 0 or greater than 0 value. |
1997 * Overrides the built-in method. | 2017 * Overrides the built-in method. |
1998 */ | 2018 */ |
1999 OverrideFunction(GlobalString.prototype, 'localeCompare', function(that) { | 2019 OverrideFunction(GlobalString.prototype, 'localeCompare', function(that) { |
2000 if (!IS_UNDEFINED(new.target)) { | 2020 if (!IS_UNDEFINED(new.target)) { |
2001 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); | 2021 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); |
2002 } | 2022 } |
2003 | 2023 |
2004 if (IS_NULL_OR_UNDEFINED(this)) { | 2024 if (IS_NULL_OR_UNDEFINED(this)) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2037 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form); | 2057 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form); |
2038 if (normalizationForm === -1) { | 2058 if (normalizationForm === -1) { |
2039 throw MakeRangeError(kNormalizationForm, | 2059 throw MakeRangeError(kNormalizationForm, |
2040 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', ')); | 2060 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', ')); |
2041 } | 2061 } |
2042 | 2062 |
2043 return %StringNormalize(s, normalizationForm); | 2063 return %StringNormalize(s, normalizationForm); |
2044 } | 2064 } |
2045 ); | 2065 ); |
2046 | 2066 |
| 2067 OverrideFunction(GlobalString.prototype, 'toLowerCase', function() { |
| 2068 if (!IS_UNDEFINED(new.target)) { |
| 2069 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); |
| 2070 } |
| 2071 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLowerCase"); |
| 2072 var s = TO_STRING(this); |
| 2073 return %StringToLowerCaseI18N(s); |
| 2074 } |
| 2075 ); |
| 2076 |
| 2077 OverrideFunction(GlobalString.prototype, 'toUpperCase', function() { |
| 2078 if (!IS_UNDEFINED(new.target)) { |
| 2079 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); |
| 2080 } |
| 2081 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLowerCase"); |
| 2082 var s = TO_STRING(this); |
| 2083 return %StringToUpperCaseI18N(s); |
| 2084 } |
| 2085 ); |
| 2086 |
| 2087 OverrideFunction(GlobalString.prototype, 'toLocaleLowerCase', function() { |
| 2088 if (!IS_UNDEFINED(new.target)) { |
| 2089 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); |
| 2090 } |
| 2091 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLocaleLowerCase"); |
| 2092 return localeConvertCase(TO_STRING(this), arguments[0], false); |
| 2093 } |
| 2094 ); |
| 2095 |
| 2096 |
| 2097 OverrideFunction(GlobalString.prototype, 'toLocaleUpperCase', function() { |
| 2098 if (!IS_UNDEFINED(new.target)) { |
| 2099 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); |
| 2100 } |
| 2101 CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLocaleUpperCase"); |
| 2102 return localeConvertCase(TO_STRING(this), arguments[0], true); |
| 2103 } |
| 2104 ); |
| 2105 |
2047 | 2106 |
2048 /** | 2107 /** |
2049 * Formats a Number object (this) using locale and options values. | 2108 * Formats a Number object (this) using locale and options values. |
2050 * If locale or options are omitted, defaults are used. | 2109 * If locale or options are omitted, defaults are used. |
2051 */ | 2110 */ |
2052 OverrideFunction(GlobalNumber.prototype, 'toLocaleString', function() { | 2111 OverrideFunction(GlobalNumber.prototype, 'toLocaleString', function() { |
2053 if (!IS_UNDEFINED(new.target)) { | 2112 if (!IS_UNDEFINED(new.target)) { |
2054 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); | 2113 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); |
2055 } | 2114 } |
2056 | 2115 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2132 } | 2191 } |
2133 | 2192 |
2134 var locales = arguments[0]; | 2193 var locales = arguments[0]; |
2135 var options = arguments[1]; | 2194 var options = arguments[1]; |
2136 return toLocaleDateTime( | 2195 return toLocaleDateTime( |
2137 this, locales, options, 'time', 'time', 'dateformattime'); | 2196 this, locales, options, 'time', 'time', 'dateformattime'); |
2138 } | 2197 } |
2139 ); | 2198 ); |
2140 | 2199 |
2141 }) | 2200 }) |
OLD | NEW |