Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: src/js/i18n.js

Issue 1733293003: Make Intl install properties more like how other builtins do (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 RegExpTest = from.RegExpTest; 57 RegExpTest = from.RegExpTest;
58 StringIndexOf = from.StringIndexOf; 58 StringIndexOf = from.StringIndexOf;
59 StringLastIndexOf = from.StringLastIndexOf; 59 StringLastIndexOf = from.StringLastIndexOf;
60 StringMatch = from.StringMatch; 60 StringMatch = from.StringMatch;
61 StringReplace = from.StringReplace; 61 StringReplace = from.StringReplace;
62 StringSplit = from.StringSplit; 62 StringSplit = from.StringSplit;
63 StringSubstr = from.StringSubstr; 63 StringSubstr = from.StringSubstr;
64 StringSubstring = from.StringSubstring; 64 StringSubstring = from.StringSubstring;
65 }); 65 });
66 66
67 // Utilities for definitions
68
69 function OverrideFunction(object, name, f) {
70 %CheckIsBootstrapping();
71 ObjectDefineProperty(object, name, { value: f,
72 writeable: true,
73 configurable: true,
74 enumerable: false });
75 %FunctionSetName(f, name);
76 %FunctionRemovePrototype(f);
77 %SetNativeFlag(f);
78 }
79
80 function InstallFunction(object, name, func) {
81 utils.InstallFunctions(object, DONT_ENUM, [name, func]);
82 }
83
84
85 function InstallConstructor(object, name, func) {
86 %CheckIsBootstrapping();
87 utils.SetFunctionName(func, name);
88 %AddNamedProperty(object, name, func, DONT_ENUM);
89 %SetNativeFlag(func);
90 %ToFastProperties(object);
91 }
92
93 /**
94 * Adds bound method to the prototype of the given object.
95 */
96 function AddBoundMethod(obj, methodName, implementation, length) {
97 %CheckIsBootstrapping();
98 var internalName = %CreatePrivateSymbol(methodName);
99 var getter = function() {
100 if (!%IsInitializedIntlObject(this)) {
101 throw MakeTypeError(kMethodCalledOnWrongObject, methodName);
102 }
103 if (IS_UNDEFINED(this[internalName])) {
104 var boundMethod;
105 if (IS_UNDEFINED(length) || length === 2) {
106 boundMethod = (x, y) => implementation(this, x, y);
107 } else if (length === 1) {
108 boundMethod = x => implementation(this, x);
109 } else {
110 boundMethod = (...args) => {
111 // DateTimeFormat.format needs to be 0 arg method, but can stil
112 // receive optional dateValue param. If one was provided, pass it
113 // along.
114 if (args.length > 0) {
115 return implementation(this, args[0]);
116 } else {
117 return implementation(this);
118 }
119 }
120 }
121 // TODO(littledan): Once function name reform is shipped, remove the
122 // following line and wrap the boundMethod definition in an anonymous
123 // function macro.
124 %FunctionSetName(boundMethod, '__bound' + methodName + '__');
125 %FunctionRemovePrototype(boundMethod);
126 %SetNativeFlag(boundMethod);
127 this[internalName] = boundMethod;
128 }
129 return this[internalName];
130 };
131
132 utils.InstallGetter(obj.prototype, methodName, getter, DONT_ENUM);
133 }
134
67 // ------------------------------------------------------------------- 135 // -------------------------------------------------------------------
68 136
69 var Intl = {}; 137 var Intl = {};
70 138
71 %AddNamedProperty(global, "Intl", Intl, DONT_ENUM); 139 %AddNamedProperty(global, "Intl", Intl, DONT_ENUM);
72 140
73 /** 141 /**
74 * Caches available locales for each service. 142 * Caches available locales for each service.
75 */ 143 */
76 var AVAILABLE_LOCALES = { 144 var AVAILABLE_LOCALES = {
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 var TIMEZONE_NAME_LOCATION_PART_RE = UNDEFINED; 258 var TIMEZONE_NAME_LOCATION_PART_RE = UNDEFINED;
191 259
192 function GetTimezoneNameLocationPartRE() { 260 function GetTimezoneNameLocationPartRE() {
193 if (IS_UNDEFINED(TIMEZONE_NAME_LOCATION_PART_RE)) { 261 if (IS_UNDEFINED(TIMEZONE_NAME_LOCATION_PART_RE)) {
194 TIMEZONE_NAME_LOCATION_PART_RE = 262 TIMEZONE_NAME_LOCATION_PART_RE =
195 new GlobalRegExp('^([A-Za-z]+)((?:[_-][A-Za-z]+)+)*$'); 263 new GlobalRegExp('^([A-Za-z]+)((?:[_-][A-Za-z]+)+)*$');
196 } 264 }
197 return TIMEZONE_NAME_LOCATION_PART_RE; 265 return TIMEZONE_NAME_LOCATION_PART_RE;
198 } 266 }
199 267
200 /**
201 * Adds bound method to the prototype of the given object.
202 */
203 function addBoundMethod(obj, methodName, implementation, length) {
204 %CheckIsBootstrapping();
205 var internalName = %CreatePrivateSymbol(methodName);
206 function getter() {
207 if (!%IsInitializedIntlObject(this)) {
208 throw MakeTypeError(kMethodCalledOnWrongObject, methodName);
209 }
210 if (IS_UNDEFINED(this[internalName])) {
211 var boundMethod;
212 if (IS_UNDEFINED(length) || length === 2) {
213 boundMethod = (x, y) => implementation(this, x, y);
214 } else if (length === 1) {
215 boundMethod = x => implementation(this, x);
216 } else {
217 boundMethod = (...args) => {
218 // DateTimeFormat.format needs to be 0 arg method, but can stil
219 // receive optional dateValue param. If one was provided, pass it
220 // along.
221 if (args.length > 0) {
222 return implementation(this, args[0]);
223 } else {
224 return implementation(this);
225 }
226 }
227 }
228 // TODO(littledan): Once function name reform is shipped, remove the
229 // following line and wrap the boundMethod definition in an anonymous
230 // function macro.
231 %FunctionSetName(boundMethod, '__bound' + methodName + '__');
232 %FunctionRemovePrototype(boundMethod);
233 %SetNativeFlag(boundMethod);
234 this[internalName] = boundMethod;
235 }
236 return this[internalName];
237 }
238
239 %FunctionSetName(getter, methodName);
240 %FunctionRemovePrototype(getter);
241 %SetNativeFlag(getter);
242
243 ObjectDefineProperty(obj.prototype, methodName, {
244 get: getter,
245 enumerable: false,
246 configurable: true
247 });
248 }
249
250 268
251 /** 269 /**
252 * Returns an intersection of locales and service supported locales. 270 * Returns an intersection of locales and service supported locales.
253 * Parameter locales is treated as a priority list. 271 * Parameter locales is treated as a priority list.
254 */ 272 */
255 function supportedLocalesOf(service, locales, options) { 273 function supportedLocalesOf(service, locales, options) {
256 if (IS_NULL(%_Call(StringMatch, service, GetServiceRE()))) { 274 if (IS_NULL(%_Call(StringMatch, service, GetServiceRE()))) {
257 throw MakeError(kWrongServiceType, service); 275 throw MakeError(kWrongServiceType, service);
258 } 276 }
259 277
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 return collator; 1001 return collator;
984 } 1002 }
985 1003
986 1004
987 /** 1005 /**
988 * Constructs Intl.Collator object given optional locales and options 1006 * Constructs Intl.Collator object given optional locales and options
989 * parameters. 1007 * parameters.
990 * 1008 *
991 * @constructor 1009 * @constructor
992 */ 1010 */
993 %AddNamedProperty(Intl, 'Collator', function() { 1011 InstallConstructor(Intl, 'Collator', function() {
994 var locales = arguments[0]; 1012 var locales = arguments[0];
995 var options = arguments[1]; 1013 var options = arguments[1];
996 1014
997 if (!this || this === Intl) { 1015 if (!this || this === Intl) {
998 // Constructor is called as a function. 1016 // Constructor is called as a function.
999 return new Intl.Collator(locales, options); 1017 return new Intl.Collator(locales, options);
1000 } 1018 }
1001 1019
1002 return initializeCollator(TO_OBJECT(this), locales, options); 1020 return initializeCollator(TO_OBJECT(this), locales, options);
1003 }, 1021 }
1004 DONT_ENUM
1005 ); 1022 );
1006 1023
1007 1024
1008 /** 1025 /**
1009 * Collator resolvedOptions method. 1026 * Collator resolvedOptions method.
1010 */ 1027 */
1011 %AddNamedProperty(Intl.Collator.prototype, 'resolvedOptions', function() { 1028 InstallFunction(Intl.Collator.prototype, 'resolvedOptions', function() {
1012 if (!IS_UNDEFINED(new.target)) { 1029 if (!IS_UNDEFINED(new.target)) {
1013 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1030 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1014 } 1031 }
1015 1032
1016 if (!%IsInitializedIntlObjectOfType(this, 'collator')) { 1033 if (!%IsInitializedIntlObjectOfType(this, 'collator')) {
1017 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "Collator"); 1034 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "Collator");
1018 } 1035 }
1019 1036
1020 var coll = this; 1037 var coll = this;
1021 var locale = getOptimalLanguageTag(coll[resolvedSymbol].requestedLocale, 1038 var locale = getOptimalLanguageTag(coll[resolvedSymbol].requestedLocale,
1022 coll[resolvedSymbol].locale); 1039 coll[resolvedSymbol].locale);
1023 1040
1024 return { 1041 return {
1025 locale: locale, 1042 locale: locale,
1026 usage: coll[resolvedSymbol].usage, 1043 usage: coll[resolvedSymbol].usage,
1027 sensitivity: coll[resolvedSymbol].sensitivity, 1044 sensitivity: coll[resolvedSymbol].sensitivity,
1028 ignorePunctuation: coll[resolvedSymbol].ignorePunctuation, 1045 ignorePunctuation: coll[resolvedSymbol].ignorePunctuation,
1029 numeric: coll[resolvedSymbol].numeric, 1046 numeric: coll[resolvedSymbol].numeric,
1030 caseFirst: coll[resolvedSymbol].caseFirst, 1047 caseFirst: coll[resolvedSymbol].caseFirst,
1031 collation: coll[resolvedSymbol].collation 1048 collation: coll[resolvedSymbol].collation
1032 }; 1049 };
1033 }, 1050 }
1034 DONT_ENUM
1035 ); 1051 );
1036 %FunctionSetName(Intl.Collator.prototype.resolvedOptions, 'resolvedOptions');
1037 %FunctionRemovePrototype(Intl.Collator.prototype.resolvedOptions);
1038 %SetNativeFlag(Intl.Collator.prototype.resolvedOptions);
1039 1052
1040 1053
1041 /** 1054 /**
1042 * Returns the subset of the given locale list for which this locale list 1055 * Returns the subset of the given locale list for which this locale list
1043 * has a matching (possibly fallback) locale. Locales appear in the same 1056 * has a matching (possibly fallback) locale. Locales appear in the same
1044 * order in the returned list as in the input list. 1057 * order in the returned list as in the input list.
1045 * Options are optional parameter. 1058 * Options are optional parameter.
1046 */ 1059 */
1047 %AddNamedProperty(Intl.Collator, 'supportedLocalesOf', function(locales) { 1060 InstallFunction(Intl.Collator, 'supportedLocalesOf', function(locales) {
1048 if (!IS_UNDEFINED(new.target)) { 1061 if (!IS_UNDEFINED(new.target)) {
1049 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1062 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1050 } 1063 }
1051 1064
1052 return supportedLocalesOf('collator', locales, arguments[1]); 1065 return supportedLocalesOf('collator', locales, arguments[1]);
1053 }, 1066 }
1054 DONT_ENUM
1055 ); 1067 );
1056 %FunctionSetName(Intl.Collator.supportedLocalesOf, 'supportedLocalesOf');
1057 %FunctionRemovePrototype(Intl.Collator.supportedLocalesOf);
1058 %SetNativeFlag(Intl.Collator.supportedLocalesOf);
1059 1068
1060 1069
1061 /** 1070 /**
1062 * When the compare method is called with two arguments x and y, it returns a 1071 * When the compare method is called with two arguments x and y, it returns a
1063 * Number other than NaN that represents the result of a locale-sensitive 1072 * Number other than NaN that represents the result of a locale-sensitive
1064 * String comparison of x with y. 1073 * String comparison of x with y.
1065 * The result is intended to order String values in the sort order specified 1074 * The result is intended to order String values in the sort order specified
1066 * by the effective locale and collation options computed during construction 1075 * by the effective locale and collation options computed during construction
1067 * of this Collator object, and will be negative, zero, or positive, depending 1076 * of this Collator object, and will be negative, zero, or positive, depending
1068 * on whether x comes before y in the sort order, the Strings are equal under 1077 * on whether x comes before y in the sort order, the Strings are equal under
1069 * the sort order, or x comes after y in the sort order, respectively. 1078 * the sort order, or x comes after y in the sort order, respectively.
1070 */ 1079 */
1071 function compare(collator, x, y) { 1080 function compare(collator, x, y) {
1072 return %InternalCompare(%GetImplFromInitializedIntlObject(collator), 1081 return %InternalCompare(%GetImplFromInitializedIntlObject(collator),
1073 GlobalString(x), GlobalString(y)); 1082 GlobalString(x), GlobalString(y));
1074 }; 1083 };
1075 1084
1076 1085
1077 addBoundMethod(Intl.Collator, 'compare', compare, 2); 1086 AddBoundMethod(Intl.Collator, 'compare', compare, 2);
1078 1087
1079 /** 1088 /**
1080 * Verifies that the input is a well-formed ISO 4217 currency code. 1089 * Verifies that the input is a well-formed ISO 4217 currency code.
1081 * Don't uppercase to test. It could convert invalid code into a valid one. 1090 * Don't uppercase to test. It could convert invalid code into a valid one.
1082 * For example \u00DFP (Eszett+P) becomes SSP. 1091 * For example \u00DFP (Eszett+P) becomes SSP.
1083 */ 1092 */
1084 function isWellFormedCurrencyCode(currency) { 1093 function isWellFormedCurrencyCode(currency) {
1085 return typeof currency == "string" && 1094 return typeof currency == "string" &&
1086 currency.length == 3 && 1095 currency.length == 3 &&
1087 %_Call(StringMatch, currency, /[^A-Za-z]/) == null; 1096 %_Call(StringMatch, currency, /[^A-Za-z]/) == null;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
1245 */ 1254 */
1246 %AddNamedProperty(Intl, 'NumberFormat', function() { 1255 InstallConstructor(Intl, 'NumberFormat', function() {
1247 var locales = arguments[0]; 1256 var locales = arguments[0];
1248 var options = arguments[1]; 1257 var options = arguments[1];
1249 1258
1250 if (!this || this === Intl) { 1259 if (!this || this === Intl) {
1251 // Constructor is called as a function. 1260 // Constructor is called as a function.
1252 return new Intl.NumberFormat(locales, options); 1261 return new Intl.NumberFormat(locales, options);
1253 } 1262 }
1254 1263
1255 return initializeNumberFormat(TO_OBJECT(this), locales, options); 1264 return initializeNumberFormat(TO_OBJECT(this), locales, options);
1256 }, 1265 }
1257 DONT_ENUM
1258 ); 1266 );
1259 1267
1260 1268
1261 /** 1269 /**
1262 * NumberFormat resolvedOptions method. 1270 * NumberFormat resolvedOptions method.
1263 */ 1271 */
1264 %AddNamedProperty(Intl.NumberFormat.prototype, 'resolvedOptions', function() { 1272 InstallFunction(Intl.NumberFormat.prototype, 'resolvedOptions', function() {
1265 if (!IS_UNDEFINED(new.target)) { 1273 if (!IS_UNDEFINED(new.target)) {
1266 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1274 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1267 } 1275 }
1268 1276
1269 if (!%IsInitializedIntlObjectOfType(this, 'numberformat')) { 1277 if (!%IsInitializedIntlObjectOfType(this, 'numberformat')) {
1270 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "NumberFormat"); 1278 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "NumberFormat");
1271 } 1279 }
1272 1280
1273 var format = this; 1281 var format = this;
1274 var locale = getOptimalLanguageTag(format[resolvedSymbol].requestedLocale, 1282 var locale = getOptimalLanguageTag(format[resolvedSymbol].requestedLocale,
(...skipping 19 matching lines...) Expand all
1294 defineWECProperty(result, 'minimumSignificantDigits', 1302 defineWECProperty(result, 'minimumSignificantDigits',
1295 format[resolvedSymbol].minimumSignificantDigits); 1303 format[resolvedSymbol].minimumSignificantDigits);
1296 } 1304 }
1297 1305
1298 if (%HasOwnProperty(format[resolvedSymbol], 'maximumSignificantDigits')) { 1306 if (%HasOwnProperty(format[resolvedSymbol], 'maximumSignificantDigits')) {
1299 defineWECProperty(result, 'maximumSignificantDigits', 1307 defineWECProperty(result, 'maximumSignificantDigits',
1300 format[resolvedSymbol].maximumSignificantDigits); 1308 format[resolvedSymbol].maximumSignificantDigits);
1301 } 1309 }
1302 1310
1303 return result; 1311 return result;
1304 }, 1312 }
1305 DONT_ENUM
1306 ); 1313 );
1307 %FunctionSetName(Intl.NumberFormat.prototype.resolvedOptions,
1308 'resolvedOptions');
1309 %FunctionRemovePrototype(Intl.NumberFormat.prototype.resolvedOptions);
1310 %SetNativeFlag(Intl.NumberFormat.prototype.resolvedOptions);
1311 1314
1312 1315
1313 /** 1316 /**
1314 * Returns the subset of the given locale list for which this locale list 1317 * Returns the subset of the given locale list for which this locale list
1315 * has a matching (possibly fallback) locale. Locales appear in the same 1318 * has a matching (possibly fallback) locale. Locales appear in the same
1316 * order in the returned list as in the input list. 1319 * order in the returned list as in the input list.
1317 * Options are optional parameter. 1320 * Options are optional parameter.
1318 */ 1321 */
1319 %AddNamedProperty(Intl.NumberFormat, 'supportedLocalesOf', function(locales) { 1322 InstallFunction(Intl.NumberFormat, 'supportedLocalesOf', function(locales) {
1320 if (!IS_UNDEFINED(new.target)) { 1323 if (!IS_UNDEFINED(new.target)) {
1321 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1324 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1322 } 1325 }
1323 1326
1324 return supportedLocalesOf('numberformat', locales, arguments[1]); 1327 return supportedLocalesOf('numberformat', locales, arguments[1]);
1325 }, 1328 }
1326 DONT_ENUM
1327 ); 1329 );
1328 %FunctionSetName(Intl.NumberFormat.supportedLocalesOf, 'supportedLocalesOf');
1329 %FunctionRemovePrototype(Intl.NumberFormat.supportedLocalesOf);
1330 %SetNativeFlag(Intl.NumberFormat.supportedLocalesOf);
1331 1330
1332 1331
1333 /** 1332 /**
1334 * Returns a String value representing the result of calling ToNumber(value) 1333 * Returns a String value representing the result of calling ToNumber(value)
1335 * according to the effective locale and the formatting options of this 1334 * according to the effective locale and the formatting options of this
1336 * NumberFormat. 1335 * NumberFormat.
1337 */ 1336 */
1338 function formatNumber(formatter, value) { 1337 function formatNumber(formatter, value) {
1339 // Spec treats -0 and +0 as 0. 1338 // Spec treats -0 and +0 as 0.
1340 var number = TO_NUMBER(value) + 0; 1339 var number = TO_NUMBER(value) + 0;
1341 1340
1342 return %InternalNumberFormat(%GetImplFromInitializedIntlObject(formatter), 1341 return %InternalNumberFormat(%GetImplFromInitializedIntlObject(formatter),
1343 number); 1342 number);
1344 } 1343 }
1345 1344
1346 1345
1347 /** 1346 /**
1348 * Returns a Number that represents string value that was passed in. 1347 * Returns a Number that represents string value that was passed in.
1349 */ 1348 */
1350 function parseNumber(formatter, value) { 1349 function parseNumber(formatter, value) {
1351 return %InternalNumberParse(%GetImplFromInitializedIntlObject(formatter), 1350 return %InternalNumberParse(%GetImplFromInitializedIntlObject(formatter),
1352 GlobalString(value)); 1351 GlobalString(value));
1353 } 1352 }
1354 1353
1355 1354
1356 addBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1); 1355 AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1);
1357 addBoundMethod(Intl.NumberFormat, 'v8Parse', parseNumber, 1); 1356 AddBoundMethod(Intl.NumberFormat, 'v8Parse', parseNumber, 1);
1358 1357
1359 /** 1358 /**
1360 * Returns a string that matches LDML representation of the options object. 1359 * Returns a string that matches LDML representation of the options object.
1361 */ 1360 */
1362 function toLDMLString(options) { 1361 function toLDMLString(options) {
1363 var getOption = getGetOption(options, 'dateformat'); 1362 var getOption = getGetOption(options, 'dateformat');
1364 1363
1365 var ldmlString = ''; 1364 var ldmlString = '';
1366 1365
1367 var option = getOption('weekday', 'string', ['narrow', 'short', 'long']); 1366 var option = getOption('weekday', 'string', ['narrow', 'short', 'long']);
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
1640 return dateFormat; 1639 return dateFormat;
1641 } 1640 }
1642 1641
1643 1642
1644 /** 1643 /**
1645 * Constructs Intl.DateTimeFormat object given optional locales and options 1644 * Constructs Intl.DateTimeFormat object given optional locales and options
1646 * parameters. 1645 * parameters.
1647 * 1646 *
1648 * @constructor 1647 * @constructor
1649 */ 1648 */
1650 %AddNamedProperty(Intl, 'DateTimeFormat', function() { 1649 InstallConstructor(Intl, 'DateTimeFormat', function() {
1651 var locales = arguments[0]; 1650 var locales = arguments[0];
1652 var options = arguments[1]; 1651 var options = arguments[1];
1653 1652
1654 if (!this || this === Intl) { 1653 if (!this || this === Intl) {
1655 // Constructor is called as a function. 1654 // Constructor is called as a function.
1656 return new Intl.DateTimeFormat(locales, options); 1655 return new Intl.DateTimeFormat(locales, options);
1657 } 1656 }
1658 1657
1659 return initializeDateTimeFormat(TO_OBJECT(this), locales, options); 1658 return initializeDateTimeFormat(TO_OBJECT(this), locales, options);
1660 }, 1659 }
1661 DONT_ENUM
1662 ); 1660 );
1663 1661
1664 1662
1665 /** 1663 /**
1666 * DateTimeFormat resolvedOptions method. 1664 * DateTimeFormat resolvedOptions method.
1667 */ 1665 */
1668 %AddNamedProperty(Intl.DateTimeFormat.prototype, 'resolvedOptions', function() { 1666 InstallFunction(Intl.DateTimeFormat.prototype, 'resolvedOptions', function() {
1669 if (!IS_UNDEFINED(new.target)) { 1667 if (!IS_UNDEFINED(new.target)) {
1670 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1668 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1671 } 1669 }
1672 1670
1673 if (!%IsInitializedIntlObjectOfType(this, 'dateformat')) { 1671 if (!%IsInitializedIntlObjectOfType(this, 'dateformat')) {
1674 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "DateTimeFormat"); 1672 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "DateTimeFormat");
1675 } 1673 }
1676 1674
1677 /** 1675 /**
1678 * Maps ICU calendar names into LDML type. 1676 * Maps ICU calendar names into LDML type.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1717 addWECPropertyIfDefined(result, 'year', fromPattern.year); 1715 addWECPropertyIfDefined(result, 'year', fromPattern.year);
1718 addWECPropertyIfDefined(result, 'month', fromPattern.month); 1716 addWECPropertyIfDefined(result, 'month', fromPattern.month);
1719 addWECPropertyIfDefined(result, 'day', fromPattern.day); 1717 addWECPropertyIfDefined(result, 'day', fromPattern.day);
1720 addWECPropertyIfDefined(result, 'weekday', fromPattern.weekday); 1718 addWECPropertyIfDefined(result, 'weekday', fromPattern.weekday);
1721 addWECPropertyIfDefined(result, 'hour12', fromPattern.hour12); 1719 addWECPropertyIfDefined(result, 'hour12', fromPattern.hour12);
1722 addWECPropertyIfDefined(result, 'hour', fromPattern.hour); 1720 addWECPropertyIfDefined(result, 'hour', fromPattern.hour);
1723 addWECPropertyIfDefined(result, 'minute', fromPattern.minute); 1721 addWECPropertyIfDefined(result, 'minute', fromPattern.minute);
1724 addWECPropertyIfDefined(result, 'second', fromPattern.second); 1722 addWECPropertyIfDefined(result, 'second', fromPattern.second);
1725 1723
1726 return result; 1724 return result;
1727 }, 1725 }
1728 DONT_ENUM
1729 ); 1726 );
1730 %FunctionSetName(Intl.DateTimeFormat.prototype.resolvedOptions,
1731 'resolvedOptions');
1732 %FunctionRemovePrototype(Intl.DateTimeFormat.prototype.resolvedOptions);
1733 %SetNativeFlag(Intl.DateTimeFormat.prototype.resolvedOptions);
1734 1727
1735 1728
1736 /** 1729 /**
1737 * Returns the subset of the given locale list for which this locale list 1730 * Returns the subset of the given locale list for which this locale list
1738 * has a matching (possibly fallback) locale. Locales appear in the same 1731 * has a matching (possibly fallback) locale. Locales appear in the same
1739 * order in the returned list as in the input list. 1732 * order in the returned list as in the input list.
1740 * Options are optional parameter. 1733 * Options are optional parameter.
1741 */ 1734 */
1742 %AddNamedProperty(Intl.DateTimeFormat, 'supportedLocalesOf', function(locales) { 1735 InstallFunction(Intl.DateTimeFormat, 'supportedLocalesOf', function(locales) {
1743 if (!IS_UNDEFINED(new.target)) { 1736 if (!IS_UNDEFINED(new.target)) {
1744 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1737 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1745 } 1738 }
1746 1739
1747 return supportedLocalesOf('dateformat', locales, arguments[1]); 1740 return supportedLocalesOf('dateformat', locales, arguments[1]);
1748 }, 1741 }
1749 DONT_ENUM
1750 ); 1742 );
1751 %FunctionSetName(Intl.DateTimeFormat.supportedLocalesOf, 'supportedLocalesOf');
1752 %FunctionRemovePrototype(Intl.DateTimeFormat.supportedLocalesOf);
1753 %SetNativeFlag(Intl.DateTimeFormat.supportedLocalesOf);
1754 1743
1755 1744
1756 /** 1745 /**
1757 * Returns a String value representing the result of calling ToNumber(date) 1746 * Returns a String value representing the result of calling ToNumber(date)
1758 * according to the effective locale and the formatting options of this 1747 * according to the effective locale and the formatting options of this
1759 * DateTimeFormat. 1748 * DateTimeFormat.
1760 */ 1749 */
1761 function formatDate(formatter, dateValue) { 1750 function formatDate(formatter, dateValue) {
1762 var dateMs; 1751 var dateMs;
1763 if (IS_UNDEFINED(dateValue)) { 1752 if (IS_UNDEFINED(dateValue)) {
(...skipping 15 matching lines...) Expand all
1779 * DateTimeFormat. 1768 * DateTimeFormat.
1780 * Returns undefined if date string cannot be parsed. 1769 * Returns undefined if date string cannot be parsed.
1781 */ 1770 */
1782 function parseDate(formatter, value) { 1771 function parseDate(formatter, value) {
1783 return %InternalDateParse(%GetImplFromInitializedIntlObject(formatter), 1772 return %InternalDateParse(%GetImplFromInitializedIntlObject(formatter),
1784 GlobalString(value)); 1773 GlobalString(value));
1785 } 1774 }
1786 1775
1787 1776
1788 // 0 because date is optional argument. 1777 // 0 because date is optional argument.
1789 addBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0); 1778 AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0);
1790 addBoundMethod(Intl.DateTimeFormat, 'v8Parse', parseDate, 1); 1779 AddBoundMethod(Intl.DateTimeFormat, 'v8Parse', parseDate, 1);
1791 1780
1792 1781
1793 /** 1782 /**
1794 * Returns canonical Area/Location(/Location) name, or throws an exception 1783 * Returns canonical Area/Location(/Location) name, or throws an exception
1795 * if the zone name is invalid IANA name. 1784 * if the zone name is invalid IANA name.
1796 */ 1785 */
1797 function canonicalizeTimeZoneID(tzID) { 1786 function canonicalizeTimeZoneID(tzID) {
1798 // Skip undefined zones. 1787 // Skip undefined zones.
1799 if (IS_UNDEFINED(tzID)) { 1788 if (IS_UNDEFINED(tzID)) {
1800 return tzID; 1789 return tzID;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1867 return iterator; 1856 return iterator;
1868 } 1857 }
1869 1858
1870 1859
1871 /** 1860 /**
1872 * Constructs Intl.v8BreakIterator object given optional locales and options 1861 * Constructs Intl.v8BreakIterator object given optional locales and options
1873 * parameters. 1862 * parameters.
1874 * 1863 *
1875 * @constructor 1864 * @constructor
1876 */ 1865 */
1877 %AddNamedProperty(Intl, 'v8BreakIterator', function() { 1866 InstallConstructor(Intl, 'v8BreakIterator', function() {
1878 var locales = arguments[0]; 1867 var locales = arguments[0];
1879 var options = arguments[1]; 1868 var options = arguments[1];
1880 1869
1881 if (!this || this === Intl) { 1870 if (!this || this === Intl) {
1882 // Constructor is called as a function. 1871 // Constructor is called as a function.
1883 return new Intl.v8BreakIterator(locales, options); 1872 return new Intl.v8BreakIterator(locales, options);
1884 } 1873 }
1885 1874
1886 return initializeBreakIterator(TO_OBJECT(this), locales, options); 1875 return initializeBreakIterator(TO_OBJECT(this), locales, options);
1887 }, 1876 }
1888 DONT_ENUM
1889 ); 1877 );
1890 1878
1891 1879
1892 /** 1880 /**
1893 * BreakIterator resolvedOptions method. 1881 * BreakIterator resolvedOptions method.
1894 */ 1882 */
1895 %AddNamedProperty(Intl.v8BreakIterator.prototype, 'resolvedOptions', 1883 InstallFunction(Intl.v8BreakIterator.prototype, 'resolvedOptions',
1896 function() { 1884 function() {
1897 if (!IS_UNDEFINED(new.target)) { 1885 if (!IS_UNDEFINED(new.target)) {
1898 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1886 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1899 } 1887 }
1900 1888
1901 if (!%IsInitializedIntlObjectOfType(this, 'breakiterator')) { 1889 if (!%IsInitializedIntlObjectOfType(this, 'breakiterator')) {
1902 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "v8BreakIterator"); 1890 throw MakeTypeError(kResolvedOptionsCalledOnNonObject, "v8BreakIterator");
1903 } 1891 }
1904 1892
1905 var segmenter = this; 1893 var segmenter = this;
1906 var locale = 1894 var locale =
1907 getOptimalLanguageTag(segmenter[resolvedSymbol].requestedLocale, 1895 getOptimalLanguageTag(segmenter[resolvedSymbol].requestedLocale,
1908 segmenter[resolvedSymbol].locale); 1896 segmenter[resolvedSymbol].locale);
1909 1897
1910 return { 1898 return {
1911 locale: locale, 1899 locale: locale,
1912 type: segmenter[resolvedSymbol].type 1900 type: segmenter[resolvedSymbol].type
1913 }; 1901 };
1914 }, 1902 }
1915 DONT_ENUM
1916 ); 1903 );
1917 %FunctionSetName(Intl.v8BreakIterator.prototype.resolvedOptions,
1918 'resolvedOptions');
1919 %FunctionRemovePrototype(Intl.v8BreakIterator.prototype.resolvedOptions);
1920 %SetNativeFlag(Intl.v8BreakIterator.prototype.resolvedOptions);
1921 1904
1922 1905
1923 /** 1906 /**
1924 * Returns the subset of the given locale list for which this locale list 1907 * Returns the subset of the given locale list for which this locale list
1925 * has a matching (possibly fallback) locale. Locales appear in the same 1908 * has a matching (possibly fallback) locale. Locales appear in the same
1926 * order in the returned list as in the input list. 1909 * order in the returned list as in the input list.
1927 * Options are optional parameter. 1910 * Options are optional parameter.
1928 */ 1911 */
1929 %AddNamedProperty(Intl.v8BreakIterator, 'supportedLocalesOf', 1912 InstallFunction(Intl.v8BreakIterator, 'supportedLocalesOf',
1930 function(locales) { 1913 function(locales) {
1931 if (!IS_UNDEFINED(new.target)) { 1914 if (!IS_UNDEFINED(new.target)) {
1932 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 1915 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
1933 } 1916 }
1934 1917
1935 return supportedLocalesOf('breakiterator', locales, arguments[1]); 1918 return supportedLocalesOf('breakiterator', locales, arguments[1]);
1936 }, 1919 }
1937 DONT_ENUM
1938 ); 1920 );
1939 %FunctionSetName(Intl.v8BreakIterator.supportedLocalesOf, 'supportedLocalesOf');
1940 %FunctionRemovePrototype(Intl.v8BreakIterator.supportedLocalesOf);
1941 %SetNativeFlag(Intl.v8BreakIterator.supportedLocalesOf);
1942 1921
1943 1922
1944 /** 1923 /**
1945 * Adopts text to segment using the iterator. Old text, if present, 1924 * Adopts text to segment using the iterator. Old text, if present,
1946 * gets discarded. 1925 * gets discarded.
1947 */ 1926 */
1948 function adoptText(iterator, text) { 1927 function adoptText(iterator, text) {
1949 %BreakIteratorAdoptText(%GetImplFromInitializedIntlObject(iterator), 1928 %BreakIteratorAdoptText(%GetImplFromInitializedIntlObject(iterator),
1950 GlobalString(text)); 1929 GlobalString(text));
1951 } 1930 }
(...skipping 24 matching lines...) Expand all
1976 1955
1977 1956
1978 /** 1957 /**
1979 * Returns type of the current break. 1958 * Returns type of the current break.
1980 */ 1959 */
1981 function breakType(iterator) { 1960 function breakType(iterator) {
1982 return %BreakIteratorBreakType(%GetImplFromInitializedIntlObject(iterator)); 1961 return %BreakIteratorBreakType(%GetImplFromInitializedIntlObject(iterator));
1983 } 1962 }
1984 1963
1985 1964
1986 addBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1); 1965 AddBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1);
1987 addBoundMethod(Intl.v8BreakIterator, 'first', first, 0); 1966 AddBoundMethod(Intl.v8BreakIterator, 'first', first, 0);
1988 addBoundMethod(Intl.v8BreakIterator, 'next', next, 0); 1967 AddBoundMethod(Intl.v8BreakIterator, 'next', next, 0);
1989 addBoundMethod(Intl.v8BreakIterator, 'current', current, 0); 1968 AddBoundMethod(Intl.v8BreakIterator, 'current', current, 0);
1990 addBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0); 1969 AddBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0);
1991 1970
1992 // Save references to Intl objects and methods we use, for added security. 1971 // Save references to Intl objects and methods we use, for added security.
1993 var savedObjects = { 1972 var savedObjects = {
1994 'collator': Intl.Collator, 1973 'collator': Intl.Collator,
1995 'numberformat': Intl.NumberFormat, 1974 'numberformat': Intl.NumberFormat,
1996 'dateformatall': Intl.DateTimeFormat, 1975 'dateformatall': Intl.DateTimeFormat,
1997 'dateformatdate': Intl.DateTimeFormat, 1976 'dateformatdate': Intl.DateTimeFormat,
1998 'dateformattime': Intl.DateTimeFormat 1977 'dateformattime': Intl.DateTimeFormat
1999 }; 1978 };
2000 1979
(...skipping 17 matching lines...) Expand all
2018 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults; 1997 var useOptions = (IS_UNDEFINED(defaults)) ? options : defaults;
2019 if (IS_UNDEFINED(locales) && IS_UNDEFINED(options)) { 1998 if (IS_UNDEFINED(locales) && IS_UNDEFINED(options)) {
2020 if (IS_UNDEFINED(defaultObjects[service])) { 1999 if (IS_UNDEFINED(defaultObjects[service])) {
2021 defaultObjects[service] = new savedObjects[service](locales, useOptions); 2000 defaultObjects[service] = new savedObjects[service](locales, useOptions);
2022 } 2001 }
2023 return defaultObjects[service]; 2002 return defaultObjects[service];
2024 } 2003 }
2025 return new savedObjects[service](locales, useOptions); 2004 return new savedObjects[service](locales, useOptions);
2026 } 2005 }
2027 2006
2028
2029 function OverrideFunction(object, name, f) {
2030 %CheckIsBootstrapping();
2031 ObjectDefineProperty(object, name, { value: f,
2032 writeable: true,
2033 configurable: true,
2034 enumerable: false });
2035 %FunctionSetName(f, name);
2036 %FunctionRemovePrototype(f);
2037 %SetNativeFlag(f);
2038 }
2039
2040 /** 2007 /**
2041 * Compares this and that, and returns less than 0, 0 or greater than 0 value. 2008 * Compares this and that, and returns less than 0, 0 or greater than 0 value.
2042 * Overrides the built-in method. 2009 * Overrides the built-in method.
2043 */ 2010 */
2044 OverrideFunction(GlobalString.prototype, 'localeCompare', function(that) { 2011 OverrideFunction(GlobalString.prototype, 'localeCompare', function(that) {
2045 if (!IS_UNDEFINED(new.target)) { 2012 if (!IS_UNDEFINED(new.target)) {
2046 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor); 2013 throw MakeTypeError(kOrdinaryFunctionCalledAsConstructor);
2047 } 2014 }
2048 2015
2049 if (IS_NULL_OR_UNDEFINED(this)) { 2016 if (IS_NULL_OR_UNDEFINED(this)) {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
2177 } 2144 }
2178 2145
2179 var locales = arguments[0]; 2146 var locales = arguments[0];
2180 var options = arguments[1]; 2147 var options = arguments[1];
2181 return toLocaleDateTime( 2148 return toLocaleDateTime(
2182 this, locales, options, 'time', 'time', 'dateformattime'); 2149 this, locales, options, 'time', 'time', 'dateformattime');
2183 } 2150 }
2184 ); 2151 );
2185 2152
2186 }) 2153 })
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698