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.
5 #include "src/runtime/runtime-utils.h"
7 #include "src/arguments.h"
8 #include "src/conversions-inl.h"
10 #include "src/dateparser-inl.h"
11 #include "src/factory.h"
12 #include "src/isolate-inl.h"
13 #include "src/messages.h"
18 RUNTIME_FUNCTION(Runtime_DateMakeDay) {
19 SealHandleScope shs(isolate);
20 DCHECK(args.length() == 2);
22 CONVERT_SMI_ARG_CHECKED(year, 0);
23 CONVERT_SMI_ARG_CHECKED(month, 1);
25 int days = isolate->date_cache()->DaysFromYearMonth(year, month);
26 RUNTIME_ASSERT(Smi::IsValid(days));
27 return Smi::FromInt(days);
31 RUNTIME_FUNCTION(Runtime_DateSetValue) {
32 HandleScope scope(isolate);
33 DCHECK(args.length() == 3);
35 CONVERT_ARG_HANDLE_CHECKED(JSDate, date, 0);
36 CONVERT_DOUBLE_ARG_CHECKED(time, 1);
37 CONVERT_SMI_ARG_CHECKED(is_utc, 2);
39 DateCache* date_cache = isolate->date_cache();
43 bool is_value_nan = false;
44 if (std::isnan(time)) {
45 value = isolate->factory()->nan_value();
47 } else if (!is_utc && (time < -DateCache::kMaxTimeBeforeUTCInMs ||
48 time > DateCache::kMaxTimeBeforeUTCInMs)) {
49 value = isolate->factory()->nan_value();
52 time = is_utc ? time : date_cache->ToUTC(static_cast<int64_t>(time));
53 if (time < -DateCache::kMaxTimeInMs || time > DateCache::kMaxTimeInMs) {
54 value = isolate->factory()->nan_value();
57 value = isolate->factory()->NewNumber(DoubleToInteger(time));
60 date->SetValue(*value, is_value_nan);
65 RUNTIME_FUNCTION(Runtime_IsDate) {
66 SealHandleScope shs(isolate);
67 DCHECK_EQ(1, args.length());
68 CONVERT_ARG_CHECKED(Object, obj, 0);
69 return isolate->heap()->ToBoolean(obj->IsJSDate());
73 RUNTIME_FUNCTION(Runtime_ThrowNotDateError) {
74 HandleScope scope(isolate);
75 DCHECK(args.length() == 0);
76 THROW_NEW_ERROR_RETURN_FAILURE(isolate,
77 NewTypeError(MessageTemplate::kNotDateObject));
81 RUNTIME_FUNCTION(Runtime_DateCurrentTime) {
82 HandleScope scope(isolate);
83 DCHECK(args.length() == 0);
84 if (FLAG_log_timer_events || FLAG_prof_cpp) LOG(isolate, CurrentTimeEvent());
86 // According to ECMA-262, section 15.9.1, page 117, the precision of
87 // the number in a Date object representing a particular instant in
88 // time is milliseconds. Therefore, we floor the result of getting
91 if (FLAG_verify_predictable) {
92 millis = 1388534400000.0; // Jan 1 2014 00:00:00 GMT+0000
93 millis += Floor(isolate->heap()->synthetic_time());
95 millis = Floor(base::OS::TimeCurrentMillis());
97 return *isolate->factory()->NewNumber(millis);
101 RUNTIME_FUNCTION(Runtime_DateParseString) {
102 HandleScope scope(isolate);
103 DCHECK(args.length() == 2);
104 CONVERT_ARG_HANDLE_CHECKED(String, str, 0);
105 CONVERT_ARG_HANDLE_CHECKED(JSArray, output, 1);
107 RUNTIME_ASSERT(output->HasFastElements());
108 JSObject::EnsureCanContainHeapObjectElements(output);
109 RUNTIME_ASSERT(output->HasFastObjectElements());
110 Handle<FixedArray> output_array(FixedArray::cast(output->elements()));
111 RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE);
113 str = String::Flatten(str);
114 DisallowHeapAllocation no_gc;
117 String::FlatContent str_content = str->GetFlatContent();
118 if (str_content.IsOneByte()) {
119 result = DateParser::Parse(str_content.ToOneByteVector(), *output_array,
120 isolate->unicode_cache());
122 DCHECK(str_content.IsTwoByte());
123 result = DateParser::Parse(str_content.ToUC16Vector(), *output_array,
124 isolate->unicode_cache());
130 return isolate->heap()->null_value();
135 RUNTIME_FUNCTION(Runtime_DateLocalTimezone) {
136 HandleScope scope(isolate);
137 DCHECK(args.length() == 1);
139 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
140 RUNTIME_ASSERT(x >= -DateCache::kMaxTimeBeforeUTCInMs &&
141 x <= DateCache::kMaxTimeBeforeUTCInMs);
143 isolate->date_cache()->LocalTimezone(static_cast<int64_t>(x));
144 Handle<String> result =
145 isolate->factory()->NewStringFromUtf8(CStrVector(zone)).ToHandleChecked();
150 RUNTIME_FUNCTION(Runtime_DateToUTC) {
151 HandleScope scope(isolate);
152 DCHECK(args.length() == 1);
154 CONVERT_DOUBLE_ARG_CHECKED(x, 0);
155 RUNTIME_ASSERT(x >= -DateCache::kMaxTimeBeforeUTCInMs &&
156 x <= DateCache::kMaxTimeBeforeUTCInMs);
157 int64_t time = isolate->date_cache()->ToUTC(static_cast<int64_t>(x));
159 return *isolate->factory()->NewNumber(static_cast<double>(time));
163 RUNTIME_FUNCTION(Runtime_DateCacheVersion) {
164 HandleScope hs(isolate);
165 DCHECK(args.length() == 0);
166 if (isolate->serializer_enabled()) return isolate->heap()->undefined_value();
167 if (!isolate->eternal_handles()->Exists(EternalHandles::DATE_CACHE_VERSION)) {
168 Handle<FixedArray> date_cache_version =
169 isolate->factory()->NewFixedArray(1, TENURED);
170 date_cache_version->set(0, Smi::FromInt(0));
171 isolate->eternal_handles()->CreateSingleton(
172 isolate, *date_cache_version, EternalHandles::DATE_CACHE_VERSION);
174 Handle<FixedArray> date_cache_version =
175 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton(
176 EternalHandles::DATE_CACHE_VERSION));
177 // Return result as a JS array.
178 Handle<JSObject> result =
179 isolate->factory()->NewJSObject(isolate->array_function());
180 JSArray::SetContent(Handle<JSArray>::cast(result), date_cache_version);
185 RUNTIME_FUNCTION(Runtime_DateField) {
186 SealHandleScope shs(isolate);
187 DCHECK_EQ(2, args.length());
188 CONVERT_ARG_CHECKED(JSDate, date, 0);
189 CONVERT_SMI_ARG_CHECKED(index, 1);
191 if (index == 0) return date->value();
192 return JSDate::GetField(date, Smi::FromInt(index));
195 } // namespace internal