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

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

Issue 1986763003: Use stricter type checks in Intl's bound methods (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 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 | src/js/intl-extra.js » ('j') | 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 %CheckIsBootstrapping(); 80 %CheckIsBootstrapping();
81 SetFunctionName(func, name); 81 SetFunctionName(func, name);
82 %AddNamedProperty(object, name, func, DONT_ENUM); 82 %AddNamedProperty(object, name, func, DONT_ENUM);
83 %SetNativeFlag(func); 83 %SetNativeFlag(func);
84 %ToFastProperties(object); 84 %ToFastProperties(object);
85 } 85 }
86 86
87 /** 87 /**
88 * Adds bound method to the prototype of the given object. 88 * Adds bound method to the prototype of the given object.
89 */ 89 */
90 function AddBoundMethod(obj, methodName, implementation, length) { 90 function AddBoundMethod(obj, methodName, implementation, length, type) {
91 %CheckIsBootstrapping(); 91 %CheckIsBootstrapping();
92 var internalName = %CreatePrivateSymbol(methodName); 92 var internalName = %CreatePrivateSymbol(methodName);
93 var getter = function() { 93 var getter = function() {
94 if (!%IsInitializedIntlObject(this)) { 94 if (!%IsInitializedIntlObjectOfType(this, type)) {
95 throw MakeTypeError(kMethodCalledOnWrongObject, methodName); 95 throw MakeTypeError(kMethodCalledOnWrongObject, methodName);
96 } 96 }
97 if (IS_UNDEFINED(this[internalName])) { 97 if (IS_UNDEFINED(this[internalName])) {
98 var boundMethod; 98 var boundMethod;
99 if (IS_UNDEFINED(length) || length === 2) { 99 if (IS_UNDEFINED(length) || length === 2) {
100 boundMethod = (x, y) => implementation(this, x, y); 100 boundMethod = (x, y) => implementation(this, x, y);
101 } else if (length === 1) { 101 } else if (length === 1) {
102 boundMethod = x => implementation(this, x); 102 boundMethod = x => implementation(this, x);
103 } else { 103 } else {
104 boundMethod = (...args) => { 104 boundMethod = (...args) => {
(...skipping 970 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 * of this Collator object, and will be negative, zero, or positive, depending 1075 * of this Collator object, and will be negative, zero, or positive, depending
1076 * on whether x comes before y in the sort order, the Strings are equal under 1076 * on whether x comes before y in the sort order, the Strings are equal under
1077 * the sort order, or x comes after y in the sort order, respectively. 1077 * the sort order, or x comes after y in the sort order, respectively.
1078 */ 1078 */
1079 function compare(collator, x, y) { 1079 function compare(collator, x, y) {
1080 return %InternalCompare(%GetImplFromInitializedIntlObject(collator), 1080 return %InternalCompare(%GetImplFromInitializedIntlObject(collator),
1081 GlobalString(x), GlobalString(y)); 1081 GlobalString(x), GlobalString(y));
1082 }; 1082 };
1083 1083
1084 1084
1085 AddBoundMethod(Intl.Collator, 'compare', compare, 2); 1085 AddBoundMethod(Intl.Collator, 'compare', compare, 2, 'collator');
1086 1086
1087 /** 1087 /**
1088 * Verifies that the input is a well-formed ISO 4217 currency code. 1088 * Verifies that the input is a well-formed ISO 4217 currency code.
1089 * Don't uppercase to test. It could convert invalid code into a valid one. 1089 * Don't uppercase to test. It could convert invalid code into a valid one.
1090 * For example \u00DFP (Eszett+P) becomes SSP. 1090 * For example \u00DFP (Eszett+P) becomes SSP.
1091 */ 1091 */
1092 function isWellFormedCurrencyCode(currency) { 1092 function isWellFormedCurrencyCode(currency) {
1093 return typeof currency == "string" && currency.length == 3 && 1093 return typeof currency == "string" && currency.length == 3 &&
1094 IS_NULL(InternalRegExpMatch(/[^A-Za-z]/, currency)); 1094 IS_NULL(InternalRegExpMatch(/[^A-Za-z]/, currency));
1095 } 1095 }
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 1344
1345 1345
1346 /** 1346 /**
1347 * Returns a Number that represents string value that was passed in. 1347 * Returns a Number that represents string value that was passed in.
1348 */ 1348 */
1349 function IntlParseNumber(formatter, value) { 1349 function IntlParseNumber(formatter, value) {
1350 return %InternalNumberParse(%GetImplFromInitializedIntlObject(formatter), 1350 return %InternalNumberParse(%GetImplFromInitializedIntlObject(formatter),
1351 GlobalString(value)); 1351 GlobalString(value));
1352 } 1352 }
1353 1353
1354 AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1); 1354 AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1, 'numberformat');
1355 1355
1356 /** 1356 /**
1357 * Returns a string that matches LDML representation of the options object. 1357 * Returns a string that matches LDML representation of the options object.
1358 */ 1358 */
1359 function toLDMLString(options) { 1359 function toLDMLString(options) {
1360 var getOption = getGetOption(options, 'dateformat'); 1360 var getOption = getGetOption(options, 'dateformat');
1361 1361
1362 var ldmlString = ''; 1362 var ldmlString = '';
1363 1363
1364 var option = getOption('weekday', 'string', ['narrow', 'short', 'long']); 1364 var option = getOption('weekday', 'string', ['narrow', 'short', 'long']);
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
1768 * DateTimeFormat. 1768 * DateTimeFormat.
1769 * Returns undefined if date string cannot be parsed. 1769 * Returns undefined if date string cannot be parsed.
1770 */ 1770 */
1771 function IntlParseDate(formatter, value) { 1771 function IntlParseDate(formatter, value) {
1772 return %InternalDateParse(%GetImplFromInitializedIntlObject(formatter), 1772 return %InternalDateParse(%GetImplFromInitializedIntlObject(formatter),
1773 GlobalString(value)); 1773 GlobalString(value));
1774 } 1774 }
1775 1775
1776 1776
1777 // 0 because date is optional argument. 1777 // 0 because date is optional argument.
1778 AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0); 1778 AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0, 'dateformat');
1779 1779
1780 1780
1781 /** 1781 /**
1782 * Returns canonical Area/Location(/Location) name, or throws an exception 1782 * Returns canonical Area/Location(/Location) name, or throws an exception
1783 * if the zone name is invalid IANA name. 1783 * if the zone name is invalid IANA name.
1784 */ 1784 */
1785 function canonicalizeTimeZoneID(tzID) { 1785 function canonicalizeTimeZoneID(tzID) {
1786 // Skip undefined zones. 1786 // Skip undefined zones.
1787 if (IS_UNDEFINED(tzID)) { 1787 if (IS_UNDEFINED(tzID)) {
1788 return tzID; 1788 return tzID;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1956 1956
1957 1957
1958 /** 1958 /**
1959 * Returns type of the current break. 1959 * Returns type of the current break.
1960 */ 1960 */
1961 function breakType(iterator) { 1961 function breakType(iterator) {
1962 return %BreakIteratorBreakType(%GetImplFromInitializedIntlObject(iterator)); 1962 return %BreakIteratorBreakType(%GetImplFromInitializedIntlObject(iterator));
1963 } 1963 }
1964 1964
1965 1965
1966 AddBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1); 1966 AddBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1,
1967 AddBoundMethod(Intl.v8BreakIterator, 'first', first, 0); 1967 'breakiterator');
1968 AddBoundMethod(Intl.v8BreakIterator, 'next', next, 0); 1968 AddBoundMethod(Intl.v8BreakIterator, 'first', first, 0, 'breakiterator');
1969 AddBoundMethod(Intl.v8BreakIterator, 'current', current, 0); 1969 AddBoundMethod(Intl.v8BreakIterator, 'next', next, 0, 'breakiterator');
1970 AddBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0); 1970 AddBoundMethod(Intl.v8BreakIterator, 'current', current, 0, 'breakiterator');
1971 AddBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0,
1972 'breakiterator');
1971 1973
1972 // Save references to Intl objects and methods we use, for added security. 1974 // Save references to Intl objects and methods we use, for added security.
1973 var savedObjects = { 1975 var savedObjects = {
1974 'collator': Intl.Collator, 1976 'collator': Intl.Collator,
1975 'numberformat': Intl.NumberFormat, 1977 'numberformat': Intl.NumberFormat,
1976 'dateformatall': Intl.DateTimeFormat, 1978 'dateformatall': Intl.DateTimeFormat,
1977 'dateformatdate': Intl.DateTimeFormat, 1979 'dateformatdate': Intl.DateTimeFormat,
1978 'dateformattime': Intl.DateTimeFormat 1980 'dateformattime': Intl.DateTimeFormat
1979 }; 1981 };
1980 1982
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
2232 } 2234 }
2233 ); 2235 );
2234 2236
2235 utils.Export(function(to) { 2237 utils.Export(function(to) {
2236 to.AddBoundMethod = AddBoundMethod; 2238 to.AddBoundMethod = AddBoundMethod;
2237 to.IntlParseDate = IntlParseDate; 2239 to.IntlParseDate = IntlParseDate;
2238 to.IntlParseNumber = IntlParseNumber; 2240 to.IntlParseNumber = IntlParseNumber;
2239 }); 2241 });
2240 2242
2241 }) 2243 })
OLDNEW
« no previous file with comments | « no previous file | src/js/intl-extra.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698