blob: fcf913a7fb79e70cca4e5a1ebf95491f69752413 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/JS_Value.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Tom Sepez39bfe122015-09-17 15:25:23 -07009#include <time.h>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010
Tom Sepezbd932572016-01-29 09:10:41 -080011#include <algorithm>
Tom Sepez39bfe122015-09-17 15:25:23 -070012#include <cmath>
13#include <limits>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050014#include <vector>
Tom Sepez39bfe122015-09-17 15:25:23 -070015
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/Document.h"
17#include "fpdfsdk/javascript/JS_Define.h"
18#include "fpdfsdk/javascript/JS_Object.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
tsepezfbf52c22016-07-25 11:17:07 -070020namespace {
21
tsepezfbf52c22016-07-25 11:17:07 -070022double
23MakeDate(int year, int mon, int day, int hour, int min, int sec, int ms) {
24 return JS_MakeDate(JS_MakeDay(year, mon, day),
25 JS_MakeTime(hour, min, sec, ms));
26}
27
Lei Zhangab5939e2017-06-16 02:23:18 -070028double GetLocalTZA() {
29 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
30 return 0;
31 time_t t = 0;
32 time(&t);
33 localtime(&t);
Dan Sinclair698aed72017-09-26 16:24:49 -040034#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Tom Sepeza2da7c52017-08-03 13:34:08 -070035 // In gcc 'timezone' is a global variable declared in time.h. In VC++, that
36 // variable was removed in VC++ 2015, with _get_timezone replacing it.
Lei Zhangab5939e2017-06-16 02:23:18 -070037 long timezone = 0;
38 _get_timezone(&timezone);
Dan Sinclair698aed72017-09-26 16:24:49 -040039#endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhangab5939e2017-06-16 02:23:18 -070040 return (double)(-(timezone * 1000));
41}
42
43int GetDaylightSavingTA(double d) {
44 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
45 return 0;
46 time_t t = (time_t)(d / 1000);
47 struct tm* tmp = localtime(&t);
48 if (!tmp)
49 return 0;
50 if (tmp->tm_isdst > 0)
51 // One hour.
52 return (int)60 * 60 * 1000;
53 return 0;
54}
55
56double Mod(double x, double y) {
57 double r = fmod(x, y);
58 if (r < 0)
59 r += y;
60 return r;
61}
62
Lei Zhangab5939e2017-06-16 02:23:18 -070063bool IsLeapYear(int year) {
64 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0));
65}
66
67int DayFromYear(int y) {
68 return (int)(365 * (y - 1970.0) + floor((y - 1969.0) / 4) -
69 floor((y - 1901.0) / 100) + floor((y - 1601.0) / 400));
70}
71
72double TimeFromYear(int y) {
73 return 86400000.0 * DayFromYear(y);
74}
75
76static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151,
77 181, 212, 243, 273, 304, 334};
78static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152,
79 182, 213, 244, 274, 305, 335};
80
81double TimeFromYearMonth(int y, int m) {
82 const uint16_t* pMonth = IsLeapYear(y) ? leapDaysMonth : daysMonth;
83 return TimeFromYear(y) + ((double)pMonth[m]) * 86400000;
84}
85
86int Day(double t) {
Tom Sepezdf950b82017-08-04 11:33:49 -070087 return static_cast<int>(floor(t / 86400000.0));
Lei Zhangab5939e2017-06-16 02:23:18 -070088}
89
90int YearFromTime(double t) {
91 // estimate the time.
Tom Sepezdf950b82017-08-04 11:33:49 -070092 int y = 1970 + static_cast<int>(t / (365.2425 * 86400000.0));
Lei Zhangab5939e2017-06-16 02:23:18 -070093 if (TimeFromYear(y) <= t) {
94 while (TimeFromYear(y + 1) <= t)
95 y++;
96 } else {
97 while (TimeFromYear(y) > t)
98 y--;
99 }
100 return y;
101}
102
103int DayWithinYear(double t) {
104 int year = YearFromTime(t);
105 int day = Day(t);
106 return day - DayFromYear(year);
107}
108
109int MonthFromTime(double t) {
110 int day = DayWithinYear(t);
111 int year = YearFromTime(t);
112 if (0 <= day && day < 31)
113 return 0;
114 if (31 <= day && day < 59 + IsLeapYear(year))
115 return 1;
116 if ((59 + IsLeapYear(year)) <= day && day < (90 + IsLeapYear(year)))
117 return 2;
118 if ((90 + IsLeapYear(year)) <= day && day < (120 + IsLeapYear(year)))
119 return 3;
120 if ((120 + IsLeapYear(year)) <= day && day < (151 + IsLeapYear(year)))
121 return 4;
122 if ((151 + IsLeapYear(year)) <= day && day < (181 + IsLeapYear(year)))
123 return 5;
124 if ((181 + IsLeapYear(year)) <= day && day < (212 + IsLeapYear(year)))
125 return 6;
126 if ((212 + IsLeapYear(year)) <= day && day < (243 + IsLeapYear(year)))
127 return 7;
128 if ((243 + IsLeapYear(year)) <= day && day < (273 + IsLeapYear(year)))
129 return 8;
130 if ((273 + IsLeapYear(year)) <= day && day < (304 + IsLeapYear(year)))
131 return 9;
132 if ((304 + IsLeapYear(year)) <= day && day < (334 + IsLeapYear(year)))
133 return 10;
134 if ((334 + IsLeapYear(year)) <= day && day < (365 + IsLeapYear(year)))
135 return 11;
136
137 return -1;
138}
139
140int DateFromTime(double t) {
141 int day = DayWithinYear(t);
142 int year = YearFromTime(t);
143 int leap = IsLeapYear(year);
144 int month = MonthFromTime(t);
145 switch (month) {
146 case 0:
147 return day + 1;
148 case 1:
149 return day - 30;
150 case 2:
151 return day - 58 - leap;
152 case 3:
153 return day - 89 - leap;
154 case 4:
155 return day - 119 - leap;
156 case 5:
157 return day - 150 - leap;
158 case 6:
159 return day - 180 - leap;
160 case 7:
161 return day - 211 - leap;
162 case 8:
163 return day - 242 - leap;
164 case 9:
165 return day - 272 - leap;
166 case 10:
167 return day - 303 - leap;
168 case 11:
169 return day - 333 - leap;
170 default:
171 return 0;
172 }
173}
174
Dan Sinclairc9708952017-10-23 09:40:59 -0400175double JS_LocalTime(double d) {
176 return d + GetLocalTZA() + GetDaylightSavingTA(d);
177}
178
tsepezfbf52c22016-07-25 11:17:07 -0700179} // namespace
180
Dan Sinclaire4974922017-10-24 09:36:16 -0400181CJS_Value::CJS_Value() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182
Dan Sinclaire4974922017-10-24 09:36:16 -0400183CJS_Value::CJS_Value(v8::Local<v8::Value> pValue) : m_pValue(pValue) {}
Tom Sepez67fd5df2015-10-08 12:24:19 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185CJS_Value::~CJS_Value() {}
186
weili625ad662016-06-15 11:21:33 -0700187CJS_Value::CJS_Value(const CJS_Value& other) = default;
188
Dan Sinclaire4974922017-10-24 09:36:16 -0400189void CJS_Value::Set(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 m_pValue = pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
tsepezb4694242016-08-15 16:44:55 -0700193int CJS_Value::ToInt(CJS_Runtime* pRuntime) const {
194 return pRuntime->ToInt32(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195}
196
tsepezb4694242016-08-15 16:44:55 -0700197bool CJS_Value::ToBool(CJS_Runtime* pRuntime) const {
198 return pRuntime->ToBoolean(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
tsepezb4694242016-08-15 16:44:55 -0700201double CJS_Value::ToDouble(CJS_Runtime* pRuntime) const {
tsepeze6cf0132017-01-18 14:38:18 -0800202 return pRuntime->ToDouble(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
tsepezb4694242016-08-15 16:44:55 -0700205float CJS_Value::ToFloat(CJS_Runtime* pRuntime) const {
Dan Sinclairc9708952017-10-23 09:40:59 -0400206 return static_cast<float>(ToDouble(pRuntime));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Dan Sinclairc9708952017-10-23 09:40:59 -0400209CJS_Object* CJS_Value::ToObject(CJS_Runtime* pRuntime) const {
tsepezb4694242016-08-15 16:44:55 -0700210 v8::Local<v8::Object> pObj = pRuntime->ToObject(m_pValue);
211 return static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(pObj));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212}
213
Dan Sinclair33d13f22017-10-23 09:44:30 -0400214CJS_Document* CJS_Value::ToDocument(CJS_Runtime* pRuntime) const {
215 return static_cast<CJS_Document*>(ToObject(pRuntime));
216}
217
Dan Sinclairc9708952017-10-23 09:40:59 -0400218CJS_Array CJS_Value::ToArray(CJS_Runtime* pRuntime) const {
219 ASSERT(IsArrayObject());
220 return CJS_Array(pRuntime->ToArray(m_pValue));
221}
222
Dan Sinclaire4974922017-10-24 09:36:16 -0400223CJS_Date CJS_Value::ToDate() const {
Dan Sinclairc9708952017-10-23 09:40:59 -0400224 ASSERT(IsDateObject());
225 v8::Local<v8::Value> mutable_value = m_pValue;
226 return CJS_Date(mutable_value.As<v8::Date>());
227}
228
tsepezb4694242016-08-15 16:44:55 -0700229v8::Local<v8::Object> CJS_Value::ToV8Object(CJS_Runtime* pRuntime) const {
230 return pRuntime->ToObject(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700231}
232
Dan Sinclairc9708952017-10-23 09:40:59 -0400233WideString CJS_Value::ToWideString(CJS_Runtime* pRuntime) const {
tsepeze6cf0132017-01-18 14:38:18 -0800234 return pRuntime->ToWideString(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
236
Dan Sinclairc9708952017-10-23 09:40:59 -0400237ByteString CJS_Value::ToByteString(CJS_Runtime* pRuntime) const {
238 return ByteString::FromUnicode(ToWideString(pRuntime));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Dan Sinclaire4974922017-10-24 09:36:16 -0400241v8::Local<v8::Value> CJS_Value::ToV8Value() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243}
244
tsepezb4694242016-08-15 16:44:55 -0700245v8::Local<v8::Array> CJS_Value::ToV8Array(CJS_Runtime* pRuntime) const {
tsepeze6cf0132017-01-18 14:38:18 -0800246 return pRuntime->ToArray(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
tsepezb4694242016-08-15 16:44:55 -0700249void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) {
Tom Sepez4246b002016-01-20 11:48:29 -0800250 bool bAllowNaN = false;
tsepez40faa792016-07-15 17:58:02 -0700251 if (GetType() == VT_string) {
Dan Sinclairc9708952017-10-23 09:40:59 -0400252 ByteString bstr = ToByteString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -0800253 if (bstr.GetLength() == 0)
254 return;
255 if (bstr == "NaN")
256 bAllowNaN = true;
257 }
tsepezb4694242016-08-15 16:44:55 -0700258 v8::Isolate* pIsolate = pRuntime->GetIsolate();
tsepezf3dc8c62016-08-10 06:29:29 -0700259 v8::TryCatch try_catch(pIsolate);
Tom Sepez4246b002016-01-20 11:48:29 -0800260 v8::MaybeLocal<v8::Number> maybeNum =
tsepezf3dc8c62016-08-10 06:29:29 -0700261 m_pValue->ToNumber(pIsolate->GetCurrentContext());
Tom Sepez4246b002016-01-20 11:48:29 -0800262 if (maybeNum.IsEmpty())
263 return;
264 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
265 if (std::isnan(num->Value()) && !bAllowNaN)
266 return;
267 m_pValue = num;
Tom Sepez4246b002016-01-20 11:48:29 -0800268}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269
tsepez40faa792016-07-15 17:58:02 -0700270// static
271CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) {
272 if (value.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 return VT_unknown;
tsepez40faa792016-07-15 17:58:02 -0700274 if (value->IsString())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 return VT_string;
tsepez40faa792016-07-15 17:58:02 -0700276 if (value->IsNumber())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 return VT_number;
tsepez40faa792016-07-15 17:58:02 -0700278 if (value->IsBoolean())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 return VT_boolean;
tsepez40faa792016-07-15 17:58:02 -0700280 if (value->IsDate())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 return VT_date;
tsepez40faa792016-07-15 17:58:02 -0700282 if (value->IsObject())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 return VT_object;
tsepez40faa792016-07-15 17:58:02 -0700284 if (value->IsNull())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 return VT_null;
tsepez40faa792016-07-15 17:58:02 -0700286 if (value->IsUndefined())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 return VT_undefined;
288 return VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289}
290
tsepezf3dc8c62016-08-10 06:29:29 -0700291bool CJS_Value::IsArrayObject() const {
292 return !m_pValue.IsEmpty() && m_pValue->IsArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293}
294
tsepezf3dc8c62016-08-10 06:29:29 -0700295bool CJS_Value::IsDateObject() const {
296 return !m_pValue.IsEmpty() && m_pValue->IsDate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700297}
298
tsepeze5aff742016-08-08 09:49:42 -0700299CJS_Array::CJS_Array() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300
Dan Sinclairc9708952017-10-23 09:40:59 -0400301CJS_Array::CJS_Array(v8::Local<v8::Array> pArray) : m_pArray(pArray) {}
302
weili625ad662016-06-15 11:21:33 -0700303CJS_Array::CJS_Array(const CJS_Array& other) = default;
304
tsepeze5aff742016-08-08 09:49:42 -0700305CJS_Array::~CJS_Array() {}
306
Dan Sinclairc9708952017-10-23 09:40:59 -0400307CJS_Value CJS_Array::GetElement(CJS_Runtime* pRuntime, unsigned index) const {
tsepeze5aff742016-08-08 09:49:42 -0700308 if (!m_pArray.IsEmpty())
Dan Sinclaire4974922017-10-24 09:36:16 -0400309 return CJS_Value(
310 v8::Local<v8::Value>(pRuntime->GetArrayElement(m_pArray, index)));
311 return {};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700312}
313
tsepezb4694242016-08-15 16:44:55 -0700314void CJS_Array::SetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700315 unsigned index,
316 const CJS_Value& value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 if (m_pArray.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700318 m_pArray = pRuntime->NewArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319
Dan Sinclaire4974922017-10-24 09:36:16 -0400320 pRuntime->PutArrayElement(m_pArray, index, value.ToV8Value());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321}
322
tsepezb4694242016-08-15 16:44:55 -0700323int CJS_Array::GetLength(CJS_Runtime* pRuntime) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 if (m_pArray.IsEmpty())
325 return 0;
tsepezb4694242016-08-15 16:44:55 -0700326 return pRuntime->GetArrayLength(m_pArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327}
328
tsepezb4694242016-08-15 16:44:55 -0700329v8::Local<v8::Array> CJS_Array::ToV8Array(CJS_Runtime* pRuntime) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 if (m_pArray.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700331 m_pArray = pRuntime->NewArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332
333 return m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334}
335
tsepezf3c88322016-08-09 07:30:38 -0700336CJS_Date::CJS_Date() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337
Dan Sinclairc9708952017-10-23 09:40:59 -0400338CJS_Date::CJS_Date(v8::Local<v8::Date> pDate) : m_pDate(pDate) {}
339
tsepezb4694242016-08-15 16:44:55 -0700340CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime)
341 : m_pDate(pRuntime->NewDate(dMsecTime)) {}
Tom Sepez67fd5df2015-10-08 12:24:19 -0700342
tsepezb4694242016-08-15 16:44:55 -0700343CJS_Date::CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 int year,
345 int mon,
346 int day,
347 int hour,
348 int min,
Tom Sepez67fd5df2015-10-08 12:24:19 -0700349 int sec)
tsepezb4694242016-08-15 16:44:55 -0700350 : m_pDate(pRuntime->NewDate(MakeDate(year, mon, day, hour, min, sec, 0))) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351
dan sinclaircbe23db2017-10-19 14:29:33 -0400352CJS_Date::CJS_Date(const CJS_Date& other) = default;
353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354CJS_Date::~CJS_Date() {}
355
tsepezb4694242016-08-15 16:44:55 -0700356bool CJS_Date::IsValidDate(CJS_Runtime* pRuntime) const {
Tom Sepezdf950b82017-08-04 11:33:49 -0700357 return !m_pDate.IsEmpty() && !std::isnan(pRuntime->ToDouble(m_pDate));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700358}
359
tsepezb4694242016-08-15 16:44:55 -0700360int CJS_Date::GetYear(CJS_Runtime* pRuntime) const {
361 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700362 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363
tsepeze6cf0132017-01-18 14:38:18 -0800364 return JS_GetYearFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365}
366
tsepezb4694242016-08-15 16:44:55 -0700367int CJS_Date::GetMonth(CJS_Runtime* pRuntime) const {
368 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700369 return 0;
tsepeze6cf0132017-01-18 14:38:18 -0800370 return JS_GetMonthFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700371}
372
tsepezb4694242016-08-15 16:44:55 -0700373int CJS_Date::GetDay(CJS_Runtime* pRuntime) const {
374 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700375 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376
tsepeze6cf0132017-01-18 14:38:18 -0800377 return JS_GetDayFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378}
379
tsepezb4694242016-08-15 16:44:55 -0700380int CJS_Date::GetHours(CJS_Runtime* pRuntime) const {
381 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700382 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383
tsepeze6cf0132017-01-18 14:38:18 -0800384 return JS_GetHourFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385}
386
tsepezb4694242016-08-15 16:44:55 -0700387int CJS_Date::GetMinutes(CJS_Runtime* pRuntime) const {
388 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700389 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390
tsepeze6cf0132017-01-18 14:38:18 -0800391 return JS_GetMinFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392}
393
tsepezb4694242016-08-15 16:44:55 -0700394int CJS_Date::GetSeconds(CJS_Runtime* pRuntime) const {
395 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700396 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397
tsepeze6cf0132017-01-18 14:38:18 -0800398 return JS_GetSecFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700399}
400
Dan Sinclaire4974922017-10-24 09:36:16 -0400401v8::Local<v8::Date> CJS_Date::ToV8Date() const {
tsepezf3c88322016-08-09 07:30:38 -0700402 return m_pDate;
Tom Sepez39bfe122015-09-17 15:25:23 -0700403}
404
Tom Sepez39bfe122015-09-17 15:25:23 -0700405double JS_GetDateTime() {
406 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
407 return 0;
thestig1cd352e2016-06-07 17:53:06 -0700408 time_t t = time(nullptr);
Tom Sepez39bfe122015-09-17 15:25:23 -0700409 struct tm* pTm = localtime(&t);
410
411 int year = pTm->tm_year + 1900;
Lei Zhangab5939e2017-06-16 02:23:18 -0700412 double t1 = TimeFromYear(year);
Tom Sepez39bfe122015-09-17 15:25:23 -0700413
414 return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 +
415 pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0;
416}
417
418int JS_GetYearFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700419 return YearFromTime(dt);
Tom Sepez39bfe122015-09-17 15:25:23 -0700420}
421
422int JS_GetMonthFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700423 return MonthFromTime(dt);
Tom Sepez39bfe122015-09-17 15:25:23 -0700424}
425
426int JS_GetDayFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700427 return DateFromTime(dt);
Tom Sepez39bfe122015-09-17 15:25:23 -0700428}
429
430int JS_GetHourFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700431 return (int)Mod(floor(dt / (60 * 60 * 1000)), 24);
Tom Sepez39bfe122015-09-17 15:25:23 -0700432}
433
434int JS_GetMinFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700435 return (int)Mod(floor(dt / (60 * 1000)), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700436}
437
438int JS_GetSecFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700439 return (int)Mod(floor(dt / 1000), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700440}
441
Ryan Harrison275e2602017-09-18 14:23:18 -0400442double JS_DateParse(const WideString& str) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700443 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
444 v8::Isolate::Scope isolate_scope(pIsolate);
445 v8::HandleScope scope(pIsolate);
446
447 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
448
449 // Use the built-in object method.
450 v8::Local<v8::Value> v =
451 context->Global()
452 ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
453 v8::NewStringType::kNormal)
454 .ToLocalChecked())
455 .ToLocalChecked();
456 if (v->IsObject()) {
457 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
458 v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
459 v8::NewStringType::kNormal)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400460 .ToLocalChecked())
461 .ToLocalChecked();
Tom Sepez39bfe122015-09-17 15:25:23 -0700462 if (v->IsFunction()) {
463 v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
Tom Sepez39bfe122015-09-17 15:25:23 -0700464 const int argc = 1;
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800465 v8::Local<v8::Value> timeStr =
466 CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString(
Ryan Harrison275e2602017-09-18 14:23:18 -0400467 str.AsStringView());
Tom Sepez39bfe122015-09-17 15:25:23 -0700468 v8::Local<v8::Value> argv[argc] = {timeStr};
469 v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
470 if (v->IsNumber()) {
471 double date = v->ToNumber(context).ToLocalChecked()->Value();
Tom Sepezdf950b82017-08-04 11:33:49 -0700472 if (!std::isfinite(date))
Tom Sepez39bfe122015-09-17 15:25:23 -0700473 return date;
tsepez86a61dc2016-03-25 10:00:11 -0700474 return JS_LocalTime(date);
Tom Sepez39bfe122015-09-17 15:25:23 -0700475 }
476 }
477 }
478 return 0;
479}
480
481double JS_MakeDay(int nYear, int nMonth, int nDate) {
Tom Sepezdf950b82017-08-04 11:33:49 -0700482 double y = static_cast<double>(nYear);
483 double m = static_cast<double>(nMonth);
484 double dt = static_cast<double>(nDate);
485 double ym = y + floor(m / 12);
Lei Zhangab5939e2017-06-16 02:23:18 -0700486 double mn = Mod(m, 12);
Tom Sepezdf950b82017-08-04 11:33:49 -0700487 double t = TimeFromYearMonth(static_cast<int>(ym), static_cast<int>(mn));
Lei Zhangab5939e2017-06-16 02:23:18 -0700488 if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1)
Tom Sepezdf950b82017-08-04 11:33:49 -0700489 return std::nan("");
490
Lei Zhangab5939e2017-06-16 02:23:18 -0700491 return Day(t) + dt - 1;
Tom Sepez39bfe122015-09-17 15:25:23 -0700492}
493
494double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) {
Tom Sepezdf950b82017-08-04 11:33:49 -0700495 double h = static_cast<double>(nHour);
496 double m = static_cast<double>(nMin);
497 double s = static_cast<double>(nSec);
498 double milli = static_cast<double>(nMs);
Tom Sepez39bfe122015-09-17 15:25:23 -0700499 return h * 3600000 + m * 60000 + s * 1000 + milli;
500}
501
502double JS_MakeDate(double day, double time) {
Tom Sepezdf950b82017-08-04 11:33:49 -0700503 if (!std::isfinite(day) || !std::isfinite(time))
504 return std::nan("");
Tom Sepez39bfe122015-09-17 15:25:23 -0700505
506 return day * 86400000 + time;
507}
508
Dan Sinclairc9708952017-10-23 09:40:59 -0400509std::vector<CJS_Value> ExpandKeywordParams(
Tom Sepezbd932572016-01-29 09:10:41 -0800510 CJS_Runtime* pRuntime,
511 const std::vector<CJS_Value>& originals,
512 size_t nKeywords,
513 ...) {
514 ASSERT(nKeywords);
515
Dan Sinclaire4974922017-10-24 09:36:16 -0400516 std::vector<CJS_Value> result(nKeywords, CJS_Value());
Tom Sepezbd932572016-01-29 09:10:41 -0800517 size_t size = std::min(originals.size(), nKeywords);
518 for (size_t i = 0; i < size; ++i)
519 result[i] = originals[i];
520
521 if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object ||
522 originals[0].IsArrayObject()) {
523 return result;
524 }
tsepezb4694242016-08-15 16:44:55 -0700525 v8::Local<v8::Object> pObj = originals[0].ToV8Object(pRuntime);
Dan Sinclaire4974922017-10-24 09:36:16 -0400526 result[0] = CJS_Value(); // Make unknown.
Tom Sepezbd932572016-01-29 09:10:41 -0800527
528 va_list ap;
529 va_start(ap, nKeywords);
Wei Li89409932016-03-28 10:33:33 -0700530 for (size_t i = 0; i < nKeywords; ++i) {
Tom Sepezbd932572016-01-29 09:10:41 -0800531 const wchar_t* property = va_arg(ap, const wchar_t*);
tsepezb4694242016-08-15 16:44:55 -0700532 v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property);
Tom Sepezbd932572016-01-29 09:10:41 -0800533 if (!v8Value->IsUndefined())
Dan Sinclaire4974922017-10-24 09:36:16 -0400534 result[i] = CJS_Value(v8Value);
Tom Sepezbd932572016-01-29 09:10:41 -0800535 }
536 va_end(ap);
537 return result;
538}