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

Side by Side Diff: src/date.js

Issue 27491002: Cosmetic: Add macros for NaN, undefined and Infinity to native js code. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « src/d8.js ('k') | src/debug-debugger.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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 23 matching lines...) Expand all
34 // ------------------------------------------------------------------- 34 // -------------------------------------------------------------------
35 35
36 // This file contains date support implemented in JavaScript. 36 // This file contains date support implemented in JavaScript.
37 37
38 // Helper function to throw error. 38 // Helper function to throw error.
39 function ThrowDateTypeError() { 39 function ThrowDateTypeError() {
40 throw new $TypeError('this is not a Date object.'); 40 throw new $TypeError('this is not a Date object.');
41 } 41 }
42 42
43 43
44 var timezone_cache_time = $NaN; 44 var timezone_cache_time = NAN;
45 var timezone_cache_timezone; 45 var timezone_cache_timezone;
46 46
47 function LocalTimezone(t) { 47 function LocalTimezone(t) {
48 if (NUMBER_IS_NAN(t)) return ""; 48 if (NUMBER_IS_NAN(t)) return "";
49 if (t == timezone_cache_time) { 49 if (t == timezone_cache_time) {
50 return timezone_cache_timezone; 50 return timezone_cache_timezone;
51 } 51 }
52 var timezone = %DateLocalTimezone(t); 52 var timezone = %DateLocalTimezone(t);
53 timezone_cache_time = t; 53 timezone_cache_time = t;
54 timezone_cache_timezone = timezone; 54 timezone_cache_timezone = timezone;
55 return timezone; 55 return timezone;
56 } 56 }
57 57
58 58
59 function UTC(time) { 59 function UTC(time) {
60 if (NUMBER_IS_NAN(time)) return time; 60 if (NUMBER_IS_NAN(time)) return time;
61 // local_time_offset is needed before the call to DaylightSavingsOffset, 61 // local_time_offset is needed before the call to DaylightSavingsOffset,
62 // so it may be uninitialized. 62 // so it may be uninitialized.
63 return %DateToUTC(time); 63 return %DateToUTC(time);
64 } 64 }
65 65
66 66
67 // ECMA 262 - 15.9.1.11 67 // ECMA 262 - 15.9.1.11
68 function MakeTime(hour, min, sec, ms) { 68 function MakeTime(hour, min, sec, ms) {
69 if (!$isFinite(hour)) return $NaN; 69 if (!$isFinite(hour)) return NAN;
70 if (!$isFinite(min)) return $NaN; 70 if (!$isFinite(min)) return NAN;
71 if (!$isFinite(sec)) return $NaN; 71 if (!$isFinite(sec)) return NAN;
72 if (!$isFinite(ms)) return $NaN; 72 if (!$isFinite(ms)) return NAN;
73 return TO_INTEGER(hour) * msPerHour 73 return TO_INTEGER(hour) * msPerHour
74 + TO_INTEGER(min) * msPerMinute 74 + TO_INTEGER(min) * msPerMinute
75 + TO_INTEGER(sec) * msPerSecond 75 + TO_INTEGER(sec) * msPerSecond
76 + TO_INTEGER(ms); 76 + TO_INTEGER(ms);
77 } 77 }
78 78
79 79
80 // ECMA 262 - 15.9.1.12 80 // ECMA 262 - 15.9.1.12
81 function TimeInYear(year) { 81 function TimeInYear(year) {
82 return DaysInYear(year) * msPerDay; 82 return DaysInYear(year) * msPerDay;
83 } 83 }
84 84
85 85
86 // Compute number of days given a year, month, date. 86 // Compute number of days given a year, month, date.
87 // Note that month and date can lie outside the normal range. 87 // Note that month and date can lie outside the normal range.
88 // For example: 88 // For example:
89 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20) 89 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20)
90 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1) 90 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1)
91 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11) 91 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11)
92 function MakeDay(year, month, date) { 92 function MakeDay(year, month, date) {
93 if (!$isFinite(year) || !$isFinite(month) || !$isFinite(date)) return $NaN; 93 if (!$isFinite(year) || !$isFinite(month) || !$isFinite(date)) return NAN;
94 94
95 // Convert to integer and map -0 to 0. 95 // Convert to integer and map -0 to 0.
96 year = TO_INTEGER_MAP_MINUS_ZERO(year); 96 year = TO_INTEGER_MAP_MINUS_ZERO(year);
97 month = TO_INTEGER_MAP_MINUS_ZERO(month); 97 month = TO_INTEGER_MAP_MINUS_ZERO(month);
98 date = TO_INTEGER_MAP_MINUS_ZERO(date); 98 date = TO_INTEGER_MAP_MINUS_ZERO(date);
99 99
100 if (year < kMinYear || year > kMaxYear || 100 if (year < kMinYear || year > kMaxYear ||
101 month < kMinMonth || month > kMaxMonth) { 101 month < kMinMonth || month > kMaxMonth) {
102 return $NaN; 102 return NAN;
103 } 103 }
104 104
105 // Now we rely on year and month being SMIs. 105 // Now we rely on year and month being SMIs.
106 return %DateMakeDay(year | 0, month | 0) + date - 1; 106 return %DateMakeDay(year | 0, month | 0) + date - 1;
107 } 107 }
108 108
109 109
110 // ECMA 262 - 15.9.1.13 110 // ECMA 262 - 15.9.1.13
111 function MakeDate(day, time) { 111 function MakeDate(day, time) {
112 var time = day * msPerDay + time; 112 var time = day * msPerDay + time;
113 // Some of our runtime funtions for computing UTC(time) rely on 113 // Some of our runtime funtions for computing UTC(time) rely on
114 // times not being significantly larger than MAX_TIME_MS. If there 114 // times not being significantly larger than MAX_TIME_MS. If there
115 // is no way that the time can be within range even after UTC 115 // is no way that the time can be within range even after UTC
116 // conversion we return NaN immediately instead of relying on 116 // conversion we return NaN immediately instead of relying on
117 // TimeClip to do it. 117 // TimeClip to do it.
118 if ($abs(time) > MAX_TIME_BEFORE_UTC) return $NaN; 118 if ($abs(time) > MAX_TIME_BEFORE_UTC) return NAN;
119 return time; 119 return time;
120 } 120 }
121 121
122 122
123 // ECMA 262 - 15.9.1.14 123 // ECMA 262 - 15.9.1.14
124 function TimeClip(time) { 124 function TimeClip(time) {
125 if (!$isFinite(time)) return $NaN; 125 if (!$isFinite(time)) return NAN;
126 if ($abs(time) > MAX_TIME_MS) return $NaN; 126 if ($abs(time) > MAX_TIME_MS) return NAN;
127 return TO_INTEGER(time); 127 return TO_INTEGER(time);
128 } 128 }
129 129
130 130
131 // The Date cache is used to limit the cost of parsing the same Date 131 // The Date cache is used to limit the cost of parsing the same Date
132 // strings over and over again. 132 // strings over and over again.
133 var Date_cache = { 133 var Date_cache = {
134 // Cached time value. 134 // Cached time value.
135 time: $NaN, 135 time: NAN,
136 // String input for which the cached time is valid. 136 // String input for which the cached time is valid.
137 string: null 137 string: null
138 }; 138 };
139 139
140 140
141 function DateConstructor(year, month, date, hours, minutes, seconds, ms) { 141 function DateConstructor(year, month, date, hours, minutes, seconds, ms) {
142 if (!%_IsConstructCall()) { 142 if (!%_IsConstructCall()) {
143 // ECMA 262 - 15.9.2 143 // ECMA 262 - 15.9.2
144 return (new $Date()).toString(); 144 return (new $Date()).toString();
145 } 145 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 } 262 }
263 263
264 // ------------------------------------------------------------------- 264 // -------------------------------------------------------------------
265 265
266 // Reused output buffer. Used when parsing date strings. 266 // Reused output buffer. Used when parsing date strings.
267 var parse_buffer = $Array(8); 267 var parse_buffer = $Array(8);
268 268
269 // ECMA 262 - 15.9.4.2 269 // ECMA 262 - 15.9.4.2
270 function DateParse(string) { 270 function DateParse(string) {
271 var arr = %DateParseString(ToString(string), parse_buffer); 271 var arr = %DateParseString(ToString(string), parse_buffer);
272 if (IS_NULL(arr)) return $NaN; 272 if (IS_NULL(arr)) return NAN;
273 273
274 var day = MakeDay(arr[0], arr[1], arr[2]); 274 var day = MakeDay(arr[0], arr[1], arr[2]);
275 var time = MakeTime(arr[3], arr[4], arr[5], arr[6]); 275 var time = MakeTime(arr[3], arr[4], arr[5], arr[6]);
276 var date = MakeDate(day, time); 276 var date = MakeDate(day, time);
277 277
278 if (IS_NULL(arr[7])) { 278 if (IS_NULL(arr[7])) {
279 return TimeClip(UTC(date)); 279 return TimeClip(UTC(date));
280 } else { 280 } else {
281 return TimeClip(date - arr[7] * 1000); 281 return TimeClip(date - arr[7] * 1000);
282 } 282 }
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 // ECMA 262 - B.2.4 664 // ECMA 262 - B.2.4
665 function DateGetYear() { 665 function DateGetYear() {
666 return LOCAL_YEAR(this) - 1900; 666 return LOCAL_YEAR(this) - 1900;
667 } 667 }
668 668
669 669
670 // ECMA 262 - B.2.5 670 // ECMA 262 - B.2.5
671 function DateSetYear(year) { 671 function DateSetYear(year) {
672 CHECK_DATE(this); 672 CHECK_DATE(this);
673 year = ToNumber(year); 673 year = ToNumber(year);
674 if (NUMBER_IS_NAN(year)) return SET_UTC_DATE_VALUE(this, $NaN); 674 if (NUMBER_IS_NAN(year)) return SET_UTC_DATE_VALUE(this, NAN);
675 year = (0 <= TO_INTEGER(year) && TO_INTEGER(year) <= 99) 675 year = (0 <= TO_INTEGER(year) && TO_INTEGER(year) <= 99)
676 ? 1900 + TO_INTEGER(year) : year; 676 ? 1900 + TO_INTEGER(year) : year;
677 var t = LOCAL_DATE_VALUE(this); 677 var t = LOCAL_DATE_VALUE(this);
678 var month, date, time; 678 var month, date, time;
679 if (NUMBER_IS_NAN(t)) { 679 if (NUMBER_IS_NAN(t)) {
680 month = 0; 680 month = 0;
681 date = 1; 681 date = 1;
682 time = 0; 682 time = 0;
683 } else { 683 } else {
684 month = LOCAL_MONTH(this); 684 month = LOCAL_MONTH(this);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 var tv = DefaultNumber(o); 739 var tv = DefaultNumber(o);
740 if (IS_NUMBER(tv) && !NUMBER_IS_FINITE(tv)) { 740 if (IS_NUMBER(tv) && !NUMBER_IS_FINITE(tv)) {
741 return null; 741 return null;
742 } 742 }
743 return o.toISOString(); 743 return o.toISOString();
744 } 744 }
745 745
746 746
747 function ResetDateCache() { 747 function ResetDateCache() {
748 // Reset the timezone cache: 748 // Reset the timezone cache:
749 timezone_cache_time = $NaN; 749 timezone_cache_time = NAN;
750 timezone_cache_timezone = undefined; 750 timezone_cache_timezone = undefined;
751 751
752 // Reset the date cache: 752 // Reset the date cache:
753 cache = Date_cache; 753 cache = Date_cache;
754 cache.time = $NaN; 754 cache.time = NAN;
755 cache.string = null; 755 cache.string = null;
756 } 756 }
757 757
758 758
759 // ------------------------------------------------------------------- 759 // -------------------------------------------------------------------
760 760
761 function SetUpDate() { 761 function SetUpDate() {
762 %CheckIsBootstrapping(); 762 %CheckIsBootstrapping();
763 763
764 %SetCode($Date, DateConstructor); 764 %SetCode($Date, DateConstructor);
765 %FunctionSetPrototype($Date, new $Date($NaN)); 765 %FunctionSetPrototype($Date, new $Date(NAN));
766 766
767 // Set up non-enumerable properties of the Date object itself. 767 // Set up non-enumerable properties of the Date object itself.
768 InstallFunctions($Date, DONT_ENUM, $Array( 768 InstallFunctions($Date, DONT_ENUM, $Array(
769 "UTC", DateUTC, 769 "UTC", DateUTC,
770 "parse", DateParse, 770 "parse", DateParse,
771 "now", DateNow 771 "now", DateNow
772 )); 772 ));
773 773
774 // Set up non-enumerable constructor property of the Date prototype object. 774 // Set up non-enumerable constructor property of the Date prototype object.
775 %SetProperty($Date.prototype, "constructor", $Date, DONT_ENUM); 775 %SetProperty($Date.prototype, "constructor", $Date, DONT_ENUM);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 "toGMTString", DateToGMTString, 820 "toGMTString", DateToGMTString,
821 "toUTCString", DateToUTCString, 821 "toUTCString", DateToUTCString,
822 "getYear", DateGetYear, 822 "getYear", DateGetYear,
823 "setYear", DateSetYear, 823 "setYear", DateSetYear,
824 "toISOString", DateToISOString, 824 "toISOString", DateToISOString,
825 "toJSON", DateToJSON 825 "toJSON", DateToJSON
826 )); 826 ));
827 } 827 }
828 828
829 SetUpDate(); 829 SetUpDate();
OLDNEW
« no previous file with comments | « src/d8.js ('k') | src/debug-debugger.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698