OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 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 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/conversions-inl.h" | 8 #include "src/conversions-inl.h" |
9 #include "src/date.h" | 9 #include "src/date.h" |
10 #include "src/factory.h" | 10 #include "src/factory.h" |
11 #include "src/isolate-inl.h" | 11 #include "src/isolate-inl.h" |
12 #include "src/messages.h" | 12 #include "src/messages.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 namespace internal { | 15 namespace internal { |
16 | 16 |
17 RUNTIME_FUNCTION(Runtime_DateMakeDay) { | |
18 SealHandleScope shs(isolate); | |
19 DCHECK(args.length() == 2); | |
20 | |
21 CONVERT_SMI_ARG_CHECKED(year, 0); | |
22 CONVERT_SMI_ARG_CHECKED(month, 1); | |
23 | |
24 int days = isolate->date_cache()->DaysFromYearMonth(year, month); | |
25 RUNTIME_ASSERT(Smi::IsValid(days)); | |
26 return Smi::FromInt(days); | |
27 } | |
28 | |
29 | |
30 RUNTIME_FUNCTION(Runtime_DateSetValue) { | |
31 HandleScope scope(isolate); | |
32 DCHECK(args.length() == 3); | |
33 | |
34 CONVERT_ARG_HANDLE_CHECKED(JSDate, date, 0); | |
35 CONVERT_DOUBLE_ARG_CHECKED(time, 1); | |
36 CONVERT_SMI_ARG_CHECKED(is_utc, 2); | |
37 | |
38 DateCache* date_cache = isolate->date_cache(); | |
39 | |
40 Handle<Object> value; | |
41 bool is_value_nan = false; | |
42 if (std::isnan(time)) { | |
43 value = isolate->factory()->nan_value(); | |
44 is_value_nan = true; | |
45 } else if (!is_utc && (time < -DateCache::kMaxTimeBeforeUTCInMs || | |
46 time > DateCache::kMaxTimeBeforeUTCInMs)) { | |
47 value = isolate->factory()->nan_value(); | |
48 is_value_nan = true; | |
49 } else { | |
50 time = is_utc ? time : date_cache->ToUTC(static_cast<int64_t>(time)); | |
51 if (time < -DateCache::kMaxTimeInMs || time > DateCache::kMaxTimeInMs) { | |
52 value = isolate->factory()->nan_value(); | |
53 is_value_nan = true; | |
54 } else { | |
55 value = isolate->factory()->NewNumber(DoubleToInteger(time)); | |
56 } | |
57 } | |
58 date->SetValue(*value, is_value_nan); | |
59 return *value; | |
60 } | |
61 | |
62 | |
63 RUNTIME_FUNCTION(Runtime_IsDate) { | 17 RUNTIME_FUNCTION(Runtime_IsDate) { |
64 SealHandleScope shs(isolate); | 18 SealHandleScope shs(isolate); |
65 DCHECK_EQ(1, args.length()); | 19 DCHECK_EQ(1, args.length()); |
66 CONVERT_ARG_CHECKED(Object, obj, 0); | 20 CONVERT_ARG_CHECKED(Object, obj, 0); |
67 return isolate->heap()->ToBoolean(obj->IsJSDate()); | 21 return isolate->heap()->ToBoolean(obj->IsJSDate()); |
68 } | 22 } |
69 | 23 |
70 | 24 |
71 RUNTIME_FUNCTION(Runtime_ThrowNotDateError) { | 25 RUNTIME_FUNCTION(Runtime_ThrowNotDateError) { |
72 HandleScope scope(isolate); | 26 HandleScope scope(isolate); |
73 DCHECK(args.length() == 0); | 27 DCHECK_EQ(0, args.length()); |
74 THROW_NEW_ERROR_RETURN_FAILURE(isolate, | 28 THROW_NEW_ERROR_RETURN_FAILURE(isolate, |
75 NewTypeError(MessageTemplate::kNotDateObject)); | 29 NewTypeError(MessageTemplate::kNotDateObject)); |
76 } | 30 } |
77 | 31 |
78 | 32 |
79 RUNTIME_FUNCTION(Runtime_DateCurrentTime) { | 33 RUNTIME_FUNCTION(Runtime_DateCurrentTime) { |
80 HandleScope scope(isolate); | 34 HandleScope scope(isolate); |
81 DCHECK_EQ(0, args.length()); | 35 DCHECK_EQ(0, args.length()); |
82 return *isolate->factory()->NewNumber(JSDate::CurrentTimeValue(isolate)); | 36 return *isolate->factory()->NewNumber(JSDate::CurrentTimeValue(isolate)); |
83 } | 37 } |
84 | 38 |
85 | |
86 RUNTIME_FUNCTION(Runtime_DateLocalTimezone) { | |
87 HandleScope scope(isolate); | |
88 DCHECK(args.length() == 1); | |
89 | |
90 CONVERT_DOUBLE_ARG_CHECKED(x, 0); | |
91 RUNTIME_ASSERT(x >= -DateCache::kMaxTimeBeforeUTCInMs && | |
92 x <= DateCache::kMaxTimeBeforeUTCInMs); | |
93 const char* zone = | |
94 isolate->date_cache()->LocalTimezone(static_cast<int64_t>(x)); | |
95 Handle<String> result = | |
96 isolate->factory()->NewStringFromUtf8(CStrVector(zone)).ToHandleChecked(); | |
97 return *result; | |
98 } | |
99 | |
100 | |
101 RUNTIME_FUNCTION(Runtime_DateCacheVersion) { | |
102 HandleScope hs(isolate); | |
103 DCHECK(args.length() == 0); | |
104 if (isolate->serializer_enabled()) return isolate->heap()->undefined_value(); | |
105 if (!isolate->eternal_handles()->Exists(EternalHandles::DATE_CACHE_VERSION)) { | |
106 Handle<FixedArray> date_cache_version = | |
107 isolate->factory()->NewFixedArray(1, TENURED); | |
108 date_cache_version->set(0, Smi::FromInt(0)); | |
109 isolate->eternal_handles()->CreateSingleton( | |
110 isolate, *date_cache_version, EternalHandles::DATE_CACHE_VERSION); | |
111 } | |
112 Handle<FixedArray> date_cache_version = | |
113 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( | |
114 EternalHandles::DATE_CACHE_VERSION)); | |
115 // Return result as a JS array. | |
116 Handle<JSObject> result = | |
117 isolate->factory()->NewJSObject(isolate->array_function()); | |
118 JSArray::SetContent(Handle<JSArray>::cast(result), date_cache_version); | |
119 return *result; | |
120 } | |
121 | |
122 | |
123 RUNTIME_FUNCTION(Runtime_DateField) { | |
124 SealHandleScope shs(isolate); | |
125 DCHECK_EQ(2, args.length()); | |
126 CONVERT_ARG_CHECKED(JSDate, date, 0); | |
127 CONVERT_SMI_ARG_CHECKED(index, 1); | |
128 DCHECK_LE(0, index); | |
129 if (index == 0) return date->value(); | |
130 return JSDate::GetField(date, Smi::FromInt(index)); | |
131 } | |
132 | |
133 } // namespace internal | 39 } // namespace internal |
134 } // namespace v8 | 40 } // namespace v8 |
OLD | NEW |