1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "src/arguments.h"
9 #include "src/dateparser-inl.h"
10 #include "src/runtime/runtime-utils.h"
15 RUNTIME_FUNCTION(Runtime_DateMakeDay) {
16 SealHandleScope shs(isolate);
17 DCHECK(args.length() == 2);
19 CONVERT_SMI_ARG_CHECKED(year, 0);
20 CONVERT_SMI_ARG_CHECKED(month, 1);
22 int days = isolate->date_cache()->DaysFromYearMonth(year, month);
23 RUNTIME_ASSERT(Smi::IsValid(days));
24 return Smi::FromInt(days);
28 RUNTIME_FUNCTION(Runtime_DateSetValue) {
29 HandleScope scope(isolate);
30 DCHECK(args.length() == 3);
32 CONVERT_ARG_HANDLE_CHECKED(JSDate, date, 0);
33 CONVERT_DOUBLE_ARG_CHECKED(time, 1);
34 CONVERT_SMI_ARG_CHECKED(is_utc, 2);
36 DateCache* date_cache = isolate->date_cache();
40 bool is_value_nan = false;
41 if (std::isnan(time)) {
42 value = isolate->factory()->nan_value();
44 } else if (!is_utc && (time < -DateCache::kMaxTimeBeforeUTCInMs ||
45 time > DateCache::kMaxTimeBeforeUTCInMs)) {
46 value = isolate->factory()->nan_value();
49 time = is_utc ? time : date_cache->ToUTC(static_cast<int64_t>(time));
50 if (time < -DateCache::kMaxTimeInMs || time > DateCache::kMaxTimeInMs) {
51 value = isolate->factory()->nan_value();
54 value = isolate->factory()->NewNumber(DoubleToInteger(time));
57 date->SetValue(*value, is_value_nan);
62 RUNTIME_FUNCTION(Runtime_ThrowNotDateError) {
63 HandleScope scope(isolate);
64 DCHECK(args.length() == 0);
65 THROW_NEW_ERROR_RETURN_FAILURE(
66 isolate, NewTypeError("not_date_object", HandleVector<Object>(NULL, 0)));
70 RUNTIME_FUNCTION(Runtime_DateCurrentTime) {
71 HandleScope scope(isolate);
72 DCHECK(args.length() == 0);
73 if (FLAG_log_timer_events || FLAG_prof_cpp) LOG(isolate, CurrentTimeEvent());
75 // According to ECMA-262, section 15.9.1, page 117, the precision of
76 // the number in a Date object representing a particular instant in
77 // time is milliseconds. Therefore, we floor the result of getting
80 if (FLAG_verify_predictable) {
81 millis = 1388534400000.0; // Jan 1 2014 00:00:00 GMT+0000
82 millis += Floor(isolate->heap()->synthetic_time());
84 millis = Floor(base::OS::TimeCurrentMillis());
86 return *isolate->factory()->NewNumber(millis);
90 RUNTIME_FUNCTION(Runtime_DateParseString) {
91 HandleScope scope(isolate);
92 DCHECK(args.length() == 2);
93 CONVERT_ARG_HANDLE_CHECKED(String, str, 0);
94 CONVERT_ARG_HANDLE_CHECKED(JSArray, output, 1);
96 RUNTIME_ASSERT(output->HasFastElements());
97 JSObject::EnsureCanContainHeapObjectElements(output);
98 RUNTIME_ASSERT(output->HasFastObjectElements());
99 Handle<FixedArray> output_array(FixedArray::cast(output->elements()));
100 RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE);
102 str = String::Flatten(str);
103 DisallowHeapAllocation no_gc;
106 String::FlatContent str_content = str->GetFlatContent();
107 if (str_content.IsOneByte()) {
108 result = DateParser::Parse(str_content.ToOneByteVector(), *output_array,
109 isolate->unicode_cache());
111 DCHECK(str_content.IsTwoByte());
112 result = DateParser::Parse(str_content.ToUC16Vector(), *output_array,
113 isolate->unicode_cache());
119 return isolate->heap()->null_value();
124 RUNTIME_FUNCTION(Runtime_DateLocalTimezone) {
125 HandleScope scope(isolate);
126 DCHECK(args.length() == 1);
128 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
129 RUNTIME_ASSERT(x >= -DateCache::kMaxTimeBeforeUTCInMs &&
130 x <= DateCache::kMaxTimeBeforeUTCInMs);
132 isolate->date_cache()->LocalTimezone(static_cast<int64_t>(x));
133 Handle<String> result =
134 isolate->factory()->NewStringFromUtf8(CStrVector(zone)).ToHandleChecked();
139 RUNTIME_FUNCTION(Runtime_DateToUTC) {
140 HandleScope scope(isolate);
141 DCHECK(args.length() == 1);
143 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
144 RUNTIME_ASSERT(x >= -DateCache::kMaxTimeBeforeUTCInMs &&
145 x <= DateCache::kMaxTimeBeforeUTCInMs);
146 int64_t time = isolate->date_cache()->ToUTC(static_cast<int64_t>(x));
148 return *isolate->factory()->NewNumber(static_cast<double>(time));
152 RUNTIME_FUNCTION(Runtime_DateCacheVersion) {
153 HandleScope hs(isolate);
154 DCHECK(args.length() == 0);
155 if (!isolate->eternal_handles()->Exists(EternalHandles::DATE_CACHE_VERSION)) {
156 Handle<FixedArray> date_cache_version =
157 isolate->factory()->NewFixedArray(1, TENURED);
158 date_cache_version->set(0, Smi::FromInt(0));
159 isolate->eternal_handles()->CreateSingleton(
160 isolate, *date_cache_version, EternalHandles::DATE_CACHE_VERSION);
162 Handle<FixedArray> date_cache_version =
163 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton(
164 EternalHandles::DATE_CACHE_VERSION));
165 // Return result as a JS array.
166 Handle<JSObject> result =
167 isolate->factory()->NewJSObject(isolate->array_function());
168 JSArray::SetContent(Handle<JSArray>::cast(result), date_cache_version);
173 RUNTIME_FUNCTION(RuntimeReference_DateField) {
174 SealHandleScope shs(isolate);
175 DCHECK(args.length() == 2);
176 CONVERT_ARG_CHECKED(Object, obj, 0);
177 CONVERT_SMI_ARG_CHECKED(index, 1);
178 if (!obj->IsJSDate()) {
179 HandleScope scope(isolate);
180 THROW_NEW_ERROR_RETURN_FAILURE(
182 NewTypeError("not_date_object", HandleVector<Object>(NULL, 0)));
184 JSDate* date = JSDate::cast(obj);
185 if (index == 0) return date->value();
186 return JSDate::GetField(date, Smi::FromInt(index));
189 } // namespace v8::internal