| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <time.h> | |
| 6 | |
| 7 #include "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
| 8 | 6 |
| 9 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 10 #include "vm/native_entry.h" | 8 #include "vm/native_entry.h" |
| 11 #include "vm/object.h" | 9 #include "vm/object.h" |
| 12 #include "vm/os.h" | 10 #include "vm/os.h" |
| 13 | 11 |
| 14 namespace dart { | 12 namespace dart { |
| 15 | 13 |
| 16 typedef struct BrokenDownDate { | |
| 17 intptr_t year; | |
| 18 intptr_t month; // [1..12] | |
| 19 intptr_t day; // [1..31] | |
| 20 intptr_t hours; | |
| 21 intptr_t minutes; | |
| 22 intptr_t seconds; | |
| 23 } BrokenDownDate; | |
| 24 | |
| 25 | |
| 26 // Takes the seconds since epoch (midnight, January 1, 1970 UTC) and breaks it | |
| 27 // down into date and time. | |
| 28 // If 'dart_is_utc', then the broken down date and time are in the UTC timezone, | |
| 29 // otherwise the local timezone is used. | |
| 30 // The returned year is offset by 1900. The returned month is 0-based. | |
| 31 // Returns true if the conversion succeeds, false otherwise. | |
| 32 static bool BreakDownSecondsSinceEpoch(const Integer& dart_seconds, | 14 static bool BreakDownSecondsSinceEpoch(const Integer& dart_seconds, |
| 33 const Bool& dart_is_utc, | 15 const Bool& dart_is_utc, |
| 34 BrokenDownDate* result) { | 16 OS::BrokenDownDate* result) { |
| 35 // Always fill the result to avoid unitialized use warnings. | |
| 36 result->year = 0; | |
| 37 result->month = 0; | |
| 38 result->day = 0; | |
| 39 result->hours = 0; | |
| 40 result->minutes = 0; | |
| 41 result->seconds = 0; | |
| 42 | |
| 43 bool is_utc = dart_is_utc.value(); | 17 bool is_utc = dart_is_utc.value(); |
| 44 int64_t seconds = dart_seconds.AsInt64Value(); | 18 int64_t value = dart_seconds.AsInt64Value(); |
| 45 | 19 time_t seconds = static_cast<time_t>(value); |
| 46 struct tm tm_result; | 20 return OS::BreakDownSecondsSinceEpoch(seconds, is_utc, result); |
| 47 bool succeeded; | |
| 48 if (is_utc) { | |
| 49 succeeded = OS::GmTime(seconds, &tm_result); | |
| 50 } else { | |
| 51 succeeded = OS::LocalTime(seconds, &tm_result); | |
| 52 } | |
| 53 if (succeeded) { | |
| 54 result->year = tm_result.tm_year; | |
| 55 // C uses years since 1900, and not full years. | |
| 56 // Adding 1900 could overflow the intptr_t. | |
| 57 if (tm_result.tm_year > kIntptrMax - 1900) return false; | |
| 58 result->year += 1900; | |
| 59 // Dart has 1-based months (contrary to C's 0-based). | |
| 60 result->month= tm_result.tm_mon + 1; | |
| 61 result->day = tm_result.tm_mday; | |
| 62 result->hours = tm_result.tm_hour; | |
| 63 result->minutes = tm_result.tm_min; | |
| 64 result->seconds = tm_result.tm_sec; | |
| 65 } | |
| 66 return succeeded; | |
| 67 } | 21 } |
| 68 | 22 |
| 69 | 23 |
| 70 static bool BrokenDownToSecondsSinceEpoch(const BrokenDownDate& broken_down, | |
| 71 bool in_utc, | |
| 72 int64_t* result) { | |
| 73 // Always set the result to avoid unitialized use warnings. | |
| 74 *result = 0; | |
| 75 | |
| 76 struct tm tm_broken_down; | |
| 77 intptr_t year = broken_down.year; | |
| 78 // C works with years since 1900. | |
| 79 // Removing 1900 could underflow the intptr_t. | |
| 80 if (year < kIntptrMin + 1900) return false; | |
| 81 year -= 1900; | |
| 82 intptr_t month = broken_down.month; | |
| 83 // C works with 0-based months. | |
| 84 // Avoid underflows (even though they should not matter since the date would | |
| 85 // be invalid anyways. | |
| 86 if (month < 0) return false; | |
| 87 month--; | |
| 88 tm_broken_down.tm_year = static_cast<int>(year); | |
| 89 tm_broken_down.tm_mon = static_cast<int>(month); | |
| 90 tm_broken_down.tm_mday = static_cast<int>(broken_down.day); | |
| 91 tm_broken_down.tm_hour = static_cast<int>(broken_down.hours); | |
| 92 tm_broken_down.tm_min = static_cast<int>(broken_down.minutes); | |
| 93 tm_broken_down.tm_sec = static_cast<int>(broken_down.seconds); | |
| 94 // Verify that casting to int did not change the value. | |
| 95 if (tm_broken_down.tm_year != year | |
| 96 || tm_broken_down.tm_mon != month | |
| 97 || tm_broken_down.tm_mday != broken_down.day | |
| 98 || tm_broken_down.tm_hour != broken_down.hours | |
| 99 || tm_broken_down.tm_min != broken_down.minutes | |
| 100 || tm_broken_down.tm_sec != broken_down.seconds) { | |
| 101 return false; | |
| 102 } | |
| 103 if (in_utc) { | |
| 104 return OS::MkGmTime(&tm_broken_down, result); | |
| 105 } else { | |
| 106 return OS::MkTime(&tm_broken_down, result); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 | |
| 111 DEFINE_NATIVE_ENTRY(DateNatives_brokenDownToSecondsSinceEpoch, 7) { | 24 DEFINE_NATIVE_ENTRY(DateNatives_brokenDownToSecondsSinceEpoch, 7) { |
| 112 GET_NATIVE_ARGUMENT(Integer, dart_years, arguments->At(0)); | 25 GET_NATIVE_ARGUMENT(Integer, dart_years, arguments->At(0)); |
| 113 GET_NATIVE_ARGUMENT(Smi, dart_month, arguments->At(1)); | 26 GET_NATIVE_ARGUMENT(Smi, dart_month, arguments->At(1)); |
| 114 GET_NATIVE_ARGUMENT(Smi, dart_day, arguments->At(2)); | 27 GET_NATIVE_ARGUMENT(Smi, dart_day, arguments->At(2)); |
| 115 GET_NATIVE_ARGUMENT(Smi, dart_hours, arguments->At(3)); | 28 GET_NATIVE_ARGUMENT(Smi, dart_hours, arguments->At(3)); |
| 116 GET_NATIVE_ARGUMENT(Smi, dart_minutes, arguments->At(4)); | 29 GET_NATIVE_ARGUMENT(Smi, dart_minutes, arguments->At(4)); |
| 117 GET_NATIVE_ARGUMENT(Smi, dart_seconds, arguments->At(5)); | 30 GET_NATIVE_ARGUMENT(Smi, dart_seconds, arguments->At(5)); |
| 118 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(6)); | 31 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(6)); |
| 119 if (!dart_years.IsSmi()) { | 32 if (!dart_years.IsSmi()) { |
| 120 UNIMPLEMENTED(); | 33 UNIMPLEMENTED(); |
| 121 } | 34 } |
| 122 Smi& smi_years = Smi::Handle(); | 35 Smi& smi_years = Smi::Handle(); |
| 123 smi_years ^= dart_years.raw(); | 36 smi_years ^= dart_years.raw(); |
| 124 BrokenDownDate broken_down; | 37 OS::BrokenDownDate broken_down; |
| 125 broken_down.year = smi_years.Value(); | 38 // mktime takes the years since 1900. |
| 126 broken_down.month = dart_month.Value(); | 39 // TODO(floitsch): Removing 1900 could underflow the intptr_t. |
| 40 intptr_t year = smi_years.Value() - 1900; |
| 41 // TODO(1143): We don't handle the case yet where intptr_t and int have |
| 42 // different sizes. |
| 43 // ASSERT(sizeof(year) <= sizeof(broken_down.year)); |
| 44 broken_down.year = static_cast<int>(year); |
| 45 // libc months are 0-based (contrary to Dart' 1-based months). |
| 46 broken_down.month = dart_month.Value() - 1; |
| 127 broken_down.day = dart_day.Value(); | 47 broken_down.day = dart_day.Value(); |
| 128 broken_down.hours = dart_hours.Value(); | 48 broken_down.hours = dart_hours.Value(); |
| 129 broken_down.minutes = dart_minutes.Value(); | 49 broken_down.minutes = dart_minutes.Value(); |
| 130 broken_down.seconds = dart_seconds.Value(); | 50 broken_down.seconds = dart_seconds.Value(); |
| 131 int64_t value; | 51 time_t value; |
| 132 bool succeeded = BrokenDownToSecondsSinceEpoch(broken_down, | 52 bool succeeded = OS::BrokenDownToSecondsSinceEpoch(broken_down, |
| 133 dart_is_utc.value(), | 53 dart_is_utc.value(), |
| 134 &value); | 54 &value); |
| 135 if (!succeeded) { | 55 if (!succeeded) { |
| 136 UNIMPLEMENTED(); | 56 UNIMPLEMENTED(); |
| 137 } | 57 } |
| 138 arguments->SetReturn(Integer::Handle(Integer::New(value))); | 58 arguments->SetReturn(Integer::Handle(Integer::New(value))); |
| 139 } | 59 } |
| 140 | 60 |
| 141 | 61 |
| 142 DEFINE_NATIVE_ENTRY(DateNatives_currentTimeMillis, 0) { | 62 DEFINE_NATIVE_ENTRY(DateNatives_currentTimeMillis, 0) { |
| 143 const Integer& time = Integer::Handle( | 63 const Integer& time = Integer::Handle( |
| 144 Integer::New(OS::GetCurrentTimeMillis())); | 64 Integer::New(OS::GetCurrentTimeMillis())); |
| 145 arguments->SetReturn(time); | 65 arguments->SetReturn(time); |
| 146 } | 66 } |
| 147 | 67 |
| 148 | 68 |
| 149 DEFINE_NATIVE_ENTRY(DateNatives_getYear, 2) { | 69 DEFINE_NATIVE_ENTRY(DateNatives_getYear, 2) { |
| 150 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 70 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
| 151 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | 71 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); |
| 152 BrokenDownDate broken_down; | 72 OS::BrokenDownDate broken_down; |
| 153 bool succeeded = | 73 bool succeeded = |
| 154 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | 74 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); |
| 155 if (!succeeded) { | 75 if (!succeeded) { |
| 156 UNIMPLEMENTED(); | 76 UNIMPLEMENTED(); |
| 157 } | 77 } |
| 158 intptr_t year = broken_down.year; | 78 // C uses years since 1900, and not full years. |
| 79 // TODO(floitsch): adding 1900 could overflow the intptr_t. |
| 80 intptr_t year = broken_down.year + 1900; |
| 159 arguments->SetReturn(Integer::Handle(Integer::New(year))); | 81 arguments->SetReturn(Integer::Handle(Integer::New(year))); |
| 160 } | 82 } |
| 161 | 83 |
| 162 | 84 |
| 163 DEFINE_NATIVE_ENTRY(DateNatives_getMonth, 2) { | 85 DEFINE_NATIVE_ENTRY(DateNatives_getMonth, 2) { |
| 164 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 86 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
| 165 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | 87 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); |
| 166 BrokenDownDate broken_down; | 88 OS::BrokenDownDate broken_down; |
| 167 bool succeeded = | 89 bool succeeded = |
| 168 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | 90 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); |
| 169 if (!succeeded) { | 91 if (!succeeded) { |
| 170 UNIMPLEMENTED(); | 92 UNIMPLEMENTED(); |
| 171 } | 93 } |
| 172 const Smi& result = Smi::Handle(Smi::New(broken_down.month)); | 94 // Dart has 1-based months (contrary to C's 0-based). |
| 95 const Smi& result = Smi::Handle(Smi::New(broken_down.month + 1)); |
| 173 arguments->SetReturn(result); | 96 arguments->SetReturn(result); |
| 174 } | 97 } |
| 175 | 98 |
| 176 | 99 |
| 177 DEFINE_NATIVE_ENTRY(DateNatives_getDay, 2) { | 100 DEFINE_NATIVE_ENTRY(DateNatives_getDay, 2) { |
| 178 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 101 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
| 179 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | 102 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); |
| 180 BrokenDownDate broken_down; | 103 OS::BrokenDownDate broken_down; |
| 181 bool succeeded = | 104 bool succeeded = |
| 182 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | 105 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); |
| 183 if (!succeeded) { | 106 if (!succeeded) { |
| 184 UNIMPLEMENTED(); | 107 UNIMPLEMENTED(); |
| 185 } | 108 } |
| 186 const Smi& result = Smi::Handle(Smi::New(broken_down.day)); | 109 const Smi& result = Smi::Handle(Smi::New(broken_down.day)); |
| 187 arguments->SetReturn(result); | 110 arguments->SetReturn(result); |
| 188 } | 111 } |
| 189 | 112 |
| 190 | 113 |
| 191 DEFINE_NATIVE_ENTRY(DateNatives_getHours, 2) { | 114 DEFINE_NATIVE_ENTRY(DateNatives_getHours, 2) { |
| 192 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 115 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
| 193 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | 116 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); |
| 194 BrokenDownDate broken_down; | 117 OS::BrokenDownDate broken_down; |
| 195 bool succeeded = | 118 bool succeeded = |
| 196 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | 119 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); |
| 197 if (!succeeded) { | 120 if (!succeeded) { |
| 198 UNIMPLEMENTED(); | 121 UNIMPLEMENTED(); |
| 199 } | 122 } |
| 200 const Smi& result = Smi::Handle(Smi::New(broken_down.hours)); | 123 const Smi& result = Smi::Handle(Smi::New(broken_down.hours)); |
| 201 arguments->SetReturn(result); | 124 arguments->SetReturn(result); |
| 202 } | 125 } |
| 203 | 126 |
| 204 | 127 |
| 205 DEFINE_NATIVE_ENTRY(DateNatives_getMinutes, 2) { | 128 DEFINE_NATIVE_ENTRY(DateNatives_getMinutes, 2) { |
| 206 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 129 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
| 207 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | 130 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); |
| 208 BrokenDownDate broken_down; | 131 OS::BrokenDownDate broken_down; |
| 209 bool succeeded = | 132 bool succeeded = |
| 210 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | 133 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); |
| 211 if (!succeeded) { | 134 if (!succeeded) { |
| 212 UNIMPLEMENTED(); | 135 UNIMPLEMENTED(); |
| 213 } | 136 } |
| 214 const Smi& result = Smi::Handle(Smi::New(broken_down.minutes)); | 137 const Smi& result = Smi::Handle(Smi::New(broken_down.minutes)); |
| 215 arguments->SetReturn(result); | 138 arguments->SetReturn(result); |
| 216 } | 139 } |
| 217 | 140 |
| 218 | 141 |
| 219 DEFINE_NATIVE_ENTRY(DateNatives_getSeconds, 2) { | 142 DEFINE_NATIVE_ENTRY(DateNatives_getSeconds, 2) { |
| 220 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 143 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
| 221 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | 144 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); |
| 222 BrokenDownDate broken_down; | 145 OS::BrokenDownDate broken_down; |
| 223 bool succeeded = | 146 bool succeeded = |
| 224 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | 147 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); |
| 225 if (!succeeded) { | 148 if (!succeeded) { |
| 226 UNIMPLEMENTED(); | 149 UNIMPLEMENTED(); |
| 227 } | 150 } |
| 228 const Smi& result = Smi::Handle(Smi::New(broken_down.seconds)); | 151 const Smi& result = Smi::Handle(Smi::New(broken_down.seconds)); |
| 229 arguments->SetReturn(result); | 152 arguments->SetReturn(result); |
| 230 } | 153 } |
| 231 | 154 |
| 232 } // namespace dart | 155 } // namespace dart |
| OLD | NEW |