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

Side by Side Diff: src/i18n.js

Issue 996213003: Hide native Date implementation in function context. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: padded formatting Created 5 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 | « src/date.js ('k') | src/math.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 "use strict";
6
7 // ECMAScript 402 API implementation. 5 // ECMAScript 402 API implementation.
8 6
9 /** 7 /**
10 * Intl object is a single object that has some named properties, 8 * Intl object is a single object that has some named properties,
11 * all of which are constructors. 9 * all of which are constructors.
12 */ 10 */
13 $Object.defineProperty(global, "Intl", { enumerable: false, value: (function() { 11 (function() {
12
13 "use strict";
14
15 %CheckIsBootstrapping();
16
17 var GlobalDate = global.Date;
18
19 var undefined = global.undefined;
Jakob Kummerow 2015/03/11 15:46:55 How about just dropping this? Shouldn't make a dif
14 20
15 var Intl = {}; 21 var Intl = {};
16 22
17 var undefined = global.undefined; 23 %AddNamedProperty(global, "Intl", Intl, DONT_ENUM);
18 24
19 var AVAILABLE_SERVICES = ['collator', 25 var AVAILABLE_SERVICES = ['collator',
20 'numberformat', 26 'numberformat',
21 'dateformat', 27 'dateformat',
22 'breakiterator']; 28 'breakiterator'];
23 29
24 var NORMALIZATION_FORMS = ['NFC', 30 var NORMALIZATION_FORMS = ['NFC',
25 'NFD', 31 'NFD',
26 'NFKC', 32 'NFKC',
27 'NFKD']; 33 'NFKD'];
(...skipping 1623 matching lines...) Expand 10 before | Expand all | Expand 10 after
1651 1657
1652 1658
1653 /** 1659 /**
1654 * Returns a String value representing the result of calling ToNumber(date) 1660 * Returns a String value representing the result of calling ToNumber(date)
1655 * according to the effective locale and the formatting options of this 1661 * according to the effective locale and the formatting options of this
1656 * DateTimeFormat. 1662 * DateTimeFormat.
1657 */ 1663 */
1658 function formatDate(formatter, dateValue) { 1664 function formatDate(formatter, dateValue) {
1659 var dateMs; 1665 var dateMs;
1660 if (dateValue === undefined) { 1666 if (dateValue === undefined) {
1661 dateMs = $Date.now(); 1667 dateMs = GlobalDate.now();
1662 } else { 1668 } else {
1663 dateMs = $Number(dateValue); 1669 dateMs = $Number(dateValue);
1664 } 1670 }
1665 1671
1666 if (!$isFinite(dateMs)) { 1672 if (!$isFinite(dateMs)) {
1667 throw new $RangeError('Provided date is not in valid range.'); 1673 throw new $RangeError('Provided date is not in valid range.');
1668 } 1674 }
1669 1675
1670 return %InternalDateFormat(%GetImplFromInitializedIntlObject(formatter), 1676 return %InternalDateFormat(%GetImplFromInitializedIntlObject(formatter),
1671 new $Date(dateMs)); 1677 new GlobalDate(dateMs));
1672 } 1678 }
1673 1679
1674 1680
1675 /** 1681 /**
1676 * Returns a Date object representing the result of calling ToString(value) 1682 * Returns a Date object representing the result of calling ToString(value)
1677 * according to the effective locale and the formatting options of this 1683 * according to the effective locale and the formatting options of this
1678 * DateTimeFormat. 1684 * DateTimeFormat.
1679 * Returns undefined if date string cannot be parsed. 1685 * Returns undefined if date string cannot be parsed.
1680 */ 1686 */
1681 function parseDate(formatter, value) { 1687 function parseDate(formatter, value) {
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
1917 return defaultObjects[service]; 1923 return defaultObjects[service];
1918 } 1924 }
1919 return new savedObjects[service](locales, useOptions); 1925 return new savedObjects[service](locales, useOptions);
1920 } 1926 }
1921 1927
1922 1928
1923 /** 1929 /**
1924 * Compares this and that, and returns less than 0, 0 or greater than 0 value. 1930 * Compares this and that, and returns less than 0, 0 or greater than 0 value.
1925 * Overrides the built-in method. 1931 * Overrides the built-in method.
1926 */ 1932 */
1927 ObjectDefineProperty($String.prototype, 'localeCompare', { 1933 OverrideFunction($String.prototype, 'localeCompare', function(that) {
1928 value: function(that) {
1929 if (%_IsConstructCall()) { 1934 if (%_IsConstructCall()) {
1930 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); 1935 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
1931 } 1936 }
1932 1937
1933 if (IS_NULL_OR_UNDEFINED(this)) { 1938 if (IS_NULL_OR_UNDEFINED(this)) {
1934 throw new $TypeError('Method invoked on undefined or null value.'); 1939 throw new $TypeError('Method invoked on undefined or null value.');
1935 } 1940 }
1936 1941
1937 var locales = %_Arguments(1); 1942 var locales = %_Arguments(1);
1938 var options = %_Arguments(2); 1943 var options = %_Arguments(2);
1939 var collator = cachedOrNewService('collator', locales, options); 1944 var collator = cachedOrNewService('collator', locales, options);
1940 return compare(collator, this, that); 1945 return compare(collator, this, that);
1941 }, 1946 }
1942 writable: true, 1947 );
1943 configurable: true,
1944 enumerable: false
1945 });
1946 %FunctionSetName($String.prototype.localeCompare, 'localeCompare');
1947 %FunctionRemovePrototype($String.prototype.localeCompare);
1948 %SetNativeFlag($String.prototype.localeCompare);
1949 1948
1950 1949
1951 /** 1950 /**
1952 * Unicode normalization. This method is called with one argument that 1951 * Unicode normalization. This method is called with one argument that
1953 * specifies the normalization form. 1952 * specifies the normalization form.
1954 * If none is specified, "NFC" is assumed. 1953 * If none is specified, "NFC" is assumed.
1955 * If the form is not one of "NFC", "NFD", "NFKC", or "NFKD", then throw 1954 * If the form is not one of "NFC", "NFD", "NFKC", or "NFKD", then throw
1956 * a RangeError Exception. 1955 * a RangeError Exception.
1957 */ 1956 */
1958 ObjectDefineProperty($String.prototype, 'normalize', { 1957 OverrideFunction($String.prototype, 'normalize', function(that) {
1959 value: function(that) {
1960 if (%_IsConstructCall()) { 1958 if (%_IsConstructCall()) {
1961 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); 1959 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
1962 } 1960 }
1963 1961
1964 CHECK_OBJECT_COERCIBLE(this, "String.prototype.normalize"); 1962 CHECK_OBJECT_COERCIBLE(this, "String.prototype.normalize");
1965 1963
1966 var form = $String(%_Arguments(0) || 'NFC'); 1964 var form = $String(%_Arguments(0) || 'NFC');
1967 1965
1968 var normalizationForm = NORMALIZATION_FORMS.indexOf(form); 1966 var normalizationForm = NORMALIZATION_FORMS.indexOf(form);
1969 if (normalizationForm === -1) { 1967 if (normalizationForm === -1) {
1970 throw new $RangeError('The normalization form should be one of ' 1968 throw new $RangeError('The normalization form should be one of '
1971 + NORMALIZATION_FORMS.join(', ') + '.'); 1969 + NORMALIZATION_FORMS.join(', ') + '.');
1972 } 1970 }
1973 1971
1974 return %StringNormalize(this, normalizationForm); 1972 return %StringNormalize(this, normalizationForm);
1975 }, 1973 }
1976 writable: true, 1974 );
1977 configurable: true,
1978 enumerable: false
1979 });
1980 %FunctionSetName($String.prototype.normalize, 'normalize');
1981 %FunctionRemovePrototype($String.prototype.normalize);
1982 %SetNativeFlag($String.prototype.normalize);
1983 1975
1984 1976
1985 /** 1977 /**
1986 * Formats a Number object (this) using locale and options values. 1978 * Formats a Number object (this) using locale and options values.
1987 * If locale or options are omitted, defaults are used. 1979 * If locale or options are omitted, defaults are used.
1988 */ 1980 */
1989 ObjectDefineProperty($Number.prototype, 'toLocaleString', { 1981 OverrideFunction($Number.prototype, 'toLocaleString', function() {
1990 value: function() {
1991 if (%_IsConstructCall()) { 1982 if (%_IsConstructCall()) {
1992 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); 1983 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
1993 } 1984 }
1994 1985
1995 if (!(this instanceof $Number) && typeof(this) !== 'number') { 1986 if (!(this instanceof $Number) && typeof(this) !== 'number') {
1996 throw new $TypeError('Method invoked on an object that is not Number.'); 1987 throw new $TypeError('Method invoked on an object that is not Number.');
1997 } 1988 }
1998 1989
1999 var locales = %_Arguments(0); 1990 var locales = %_Arguments(0);
2000 var options = %_Arguments(1); 1991 var options = %_Arguments(1);
2001 var numberFormat = cachedOrNewService('numberformat', locales, options); 1992 var numberFormat = cachedOrNewService('numberformat', locales, options);
2002 return formatNumber(numberFormat, this); 1993 return formatNumber(numberFormat, this);
2003 }, 1994 }
2004 writable: true, 1995 );
2005 configurable: true,
2006 enumerable: false
2007 });
2008 %FunctionSetName($Number.prototype.toLocaleString, 'toLocaleString');
2009 %FunctionRemovePrototype($Number.prototype.toLocaleString);
2010 %SetNativeFlag($Number.prototype.toLocaleString);
2011 1996
2012 1997
2013 /** 1998 /**
2014 * Returns actual formatted date or fails if date parameter is invalid. 1999 * Returns actual formatted date or fails if date parameter is invalid.
2015 */ 2000 */
2016 function toLocaleDateTime(date, locales, options, required, defaults, service) { 2001 function toLocaleDateTime(date, locales, options, required, defaults, service) {
2017 if (!(date instanceof $Date)) { 2002 if (!(date instanceof GlobalDate)) {
2018 throw new $TypeError('Method invoked on an object that is not Date.'); 2003 throw new $TypeError('Method invoked on an object that is not Date.');
2019 } 2004 }
2020 2005
2021 if ($isNaN(date)) { 2006 if ($isNaN(date)) {
2022 return 'Invalid Date'; 2007 return 'Invalid Date';
2023 } 2008 }
2024 2009
2025 var internalOptions = toDateTimeOptions(options, required, defaults); 2010 var internalOptions = toDateTimeOptions(options, required, defaults);
2026 2011
2027 var dateFormat = 2012 var dateFormat =
2028 cachedOrNewService(service, locales, options, internalOptions); 2013 cachedOrNewService(service, locales, options, internalOptions);
2029 2014
2030 return formatDate(dateFormat, date); 2015 return formatDate(dateFormat, date);
2031 } 2016 }
2032 2017
2033 2018
2034 /** 2019 /**
2035 * Formats a Date object (this) using locale and options values. 2020 * Formats a Date object (this) using locale and options values.
2036 * If locale or options are omitted, defaults are used - both date and time are 2021 * If locale or options are omitted, defaults are used - both date and time are
2037 * present in the output. 2022 * present in the output.
2038 */ 2023 */
2039 ObjectDefineProperty($Date.prototype, 'toLocaleString', { 2024 OverrideFunction(GlobalDate.prototype, 'toLocaleString', function() {
2040 value: function() {
2041 if (%_IsConstructCall()) { 2025 if (%_IsConstructCall()) {
2042 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); 2026 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
2043 } 2027 }
2044 2028
2045 var locales = %_Arguments(0); 2029 var locales = %_Arguments(0);
2046 var options = %_Arguments(1); 2030 var options = %_Arguments(1);
2047 return toLocaleDateTime( 2031 return toLocaleDateTime(
2048 this, locales, options, 'any', 'all', 'dateformatall'); 2032 this, locales, options, 'any', 'all', 'dateformatall');
2049 }, 2033 }
2050 writable: true, 2034 );
2051 configurable: true,
2052 enumerable: false
2053 });
2054 %FunctionSetName($Date.prototype.toLocaleString, 'toLocaleString');
2055 %FunctionRemovePrototype($Date.prototype.toLocaleString);
2056 %SetNativeFlag($Date.prototype.toLocaleString);
2057 2035
2058 2036
2059 /** 2037 /**
2060 * Formats a Date object (this) using locale and options values. 2038 * Formats a Date object (this) using locale and options values.
2061 * If locale or options are omitted, defaults are used - only date is present 2039 * If locale or options are omitted, defaults are used - only date is present
2062 * in the output. 2040 * in the output.
2063 */ 2041 */
2064 ObjectDefineProperty($Date.prototype, 'toLocaleDateString', { 2042 OverrideFunction(GlobalDate.prototype, 'toLocaleDateString', function() {
2065 value: function() {
2066 if (%_IsConstructCall()) { 2043 if (%_IsConstructCall()) {
2067 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); 2044 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
2068 } 2045 }
2069 2046
2070 var locales = %_Arguments(0); 2047 var locales = %_Arguments(0);
2071 var options = %_Arguments(1); 2048 var options = %_Arguments(1);
2072 return toLocaleDateTime( 2049 return toLocaleDateTime(
2073 this, locales, options, 'date', 'date', 'dateformatdate'); 2050 this, locales, options, 'date', 'date', 'dateformatdate');
2074 }, 2051 }
2075 writable: true, 2052 );
2076 configurable: true,
2077 enumerable: false
2078 });
2079 %FunctionSetName($Date.prototype.toLocaleDateString, 'toLocaleDateString');
2080 %FunctionRemovePrototype($Date.prototype.toLocaleDateString);
2081 %SetNativeFlag($Date.prototype.toLocaleDateString);
2082 2053
2083 2054
2084 /** 2055 /**
2085 * Formats a Date object (this) using locale and options values. 2056 * Formats a Date object (this) using locale and options values.
2086 * If locale or options are omitted, defaults are used - only time is present 2057 * If locale or options are omitted, defaults are used - only time is present
2087 * in the output. 2058 * in the output.
2088 */ 2059 */
2089 ObjectDefineProperty($Date.prototype, 'toLocaleTimeString', { 2060 OverrideFunction(GlobalDate.prototype, 'toLocaleTimeString', function() {
2090 value: function() {
2091 if (%_IsConstructCall()) { 2061 if (%_IsConstructCall()) {
2092 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); 2062 throw new $TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
2093 } 2063 }
2094 2064
2095 var locales = %_Arguments(0); 2065 var locales = %_Arguments(0);
2096 var options = %_Arguments(1); 2066 var options = %_Arguments(1);
2097 return toLocaleDateTime( 2067 return toLocaleDateTime(
2098 this, locales, options, 'time', 'time', 'dateformattime'); 2068 this, locales, options, 'time', 'time', 'dateformattime');
2099 }, 2069 }
2100 writable: true, 2070 );
2101 configurable: true,
2102 enumerable: false
2103 });
2104 %FunctionSetName($Date.prototype.toLocaleTimeString, 'toLocaleTimeString');
2105 %FunctionRemovePrototype($Date.prototype.toLocaleTimeString);
2106 %SetNativeFlag($Date.prototype.toLocaleTimeString);
2107 2071
2108 return Intl; 2072 })();
2109 }())});
OLDNEW
« no previous file with comments | « src/date.js ('k') | src/math.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698