blob: fb38e8cbb1e635068dcc7cd28cb4c04dd79c1535 [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
tsepezf3dc8c62016-08-10 06:29:29 -0700181CJS_Value::CJS_Value(CJS_Runtime* pRuntime) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182
tsepez40faa792016-07-15 17:58:02 -0700183CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue)
tsepezf3dc8c62016-08-10 06:29:29 -0700184 : m_pValue(pValue) {}
Tom Sepez67fd5df2015-10-08 12:24:19 -0700185
Dan Sinclairc9708952017-10-23 09:40:59 -0400186CJS_Value::CJS_Value(CJS_Runtime* pRuntime, int iValue)
Dan Sinclair33d13f22017-10-23 09:44:30 -0400187 : CJS_Value(pRuntime, pRuntime->NewNumber(iValue)) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188
Dan Sinclairc9708952017-10-23 09:40:59 -0400189CJS_Value::CJS_Value(CJS_Runtime* pRuntime, bool bValue)
Dan Sinclair33d13f22017-10-23 09:44:30 -0400190 : CJS_Value(pRuntime, pRuntime->NewBoolean(bValue)) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191
Dan Sinclairc9708952017-10-23 09:40:59 -0400192CJS_Value::CJS_Value(CJS_Runtime* pRuntime, double dValue)
Dan Sinclair33d13f22017-10-23 09:44:30 -0400193 : CJS_Value(pRuntime, pRuntime->NewNumber(dValue)) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194
tsepezf3dc8c62016-08-10 06:29:29 -0700195CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pObj) {
196 if (pObj)
197 m_pValue = pObj->ToV8Object();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Dan Sinclair812e96c2017-03-13 16:43:37 -0400200CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const wchar_t* pWstr)
Dan Sinclair33d13f22017-10-23 09:44:30 -0400201 : CJS_Value(pRuntime, pRuntime->NewString(pWstr)) {}
Tom Sepezf79a69c2014-10-30 13:23:42 -0700202
Dan Sinclair812e96c2017-03-13 16:43:37 -0400203CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const char* pStr)
Dan Sinclair33d13f22017-10-23 09:44:30 -0400204 : CJS_Value(pRuntime,
205 pRuntime->NewString(WideString::FromLocal(pStr).c_str())) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206
tsepeze5aff742016-08-08 09:49:42 -0700207CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array)
Dan Sinclair33d13f22017-10-23 09:44:30 -0400208 : CJS_Value(pRuntime, array.ToV8Array(pRuntime)) {}
tsepezf3dc8c62016-08-10 06:29:29 -0700209
210CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Date& date)
Dan Sinclair33d13f22017-10-23 09:44:30 -0400211 : CJS_Value(pRuntime, date.ToV8Date(pRuntime)) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213CJS_Value::~CJS_Value() {}
214
weili625ad662016-06-15 11:21:33 -0700215CJS_Value::CJS_Value(const CJS_Value& other) = default;
216
Dan Sinclair33d13f22017-10-23 09:44:30 -0400217void CJS_Value::SetNull(CJS_Runtime* pRuntime) {
218 m_pValue = pRuntime->NewNull();
219}
220
221void CJS_Value::Set(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 m_pValue = pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Dan Sinclair33d13f22017-10-23 09:44:30 -0400225void CJS_Value::Set(CJS_Runtime* pRuntime, int val) {
226 m_pValue = pRuntime->NewNumber(val);
227}
228
229void CJS_Value::Set(CJS_Runtime* pRuntime, bool val) {
230 m_pValue = pRuntime->NewBoolean(val);
231}
232
233void CJS_Value::Set(CJS_Runtime* pRuntime, double val) {
234 m_pValue = pRuntime->NewNumber(val);
235}
236
237void CJS_Value::Set(CJS_Runtime* pRuntime, CJS_Object* pObj) {
238 m_pValue = pObj->ToV8Object();
239}
240
241void CJS_Value::Set(CJS_Runtime* pRuntime, CJS_Document* pJsDoc) {
242 m_pValue = pJsDoc->ToV8Object();
243}
244
245void CJS_Value::Set(CJS_Runtime* pRuntime, const ByteString& str) {
246 m_pValue = pRuntime->NewString(WideString::FromLocal(str.c_str()).c_str());
247}
248
249void CJS_Value::Set(CJS_Runtime* pRuntime, const WideString& str) {
250 m_pValue = pRuntime->NewString(str.c_str());
251}
252
253void CJS_Value::Set(CJS_Runtime* pRuntime, const wchar_t* c_string) {
254 m_pValue = pRuntime->NewString(c_string);
255}
256
257void CJS_Value::Set(CJS_Runtime* pRuntime, const CJS_Array& array) {
258 m_pValue = array.ToV8Array(pRuntime);
259}
260
261void CJS_Value::Set(CJS_Runtime* pRuntime, const CJS_Date& date) {
262 m_pValue = date.ToV8Date(pRuntime);
263}
264
tsepezb4694242016-08-15 16:44:55 -0700265int CJS_Value::ToInt(CJS_Runtime* pRuntime) const {
266 return pRuntime->ToInt32(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268
tsepezb4694242016-08-15 16:44:55 -0700269bool CJS_Value::ToBool(CJS_Runtime* pRuntime) const {
270 return pRuntime->ToBoolean(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
tsepezb4694242016-08-15 16:44:55 -0700273double CJS_Value::ToDouble(CJS_Runtime* pRuntime) const {
tsepeze6cf0132017-01-18 14:38:18 -0800274 return pRuntime->ToDouble(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275}
276
tsepezb4694242016-08-15 16:44:55 -0700277float CJS_Value::ToFloat(CJS_Runtime* pRuntime) const {
Dan Sinclairc9708952017-10-23 09:40:59 -0400278 return static_cast<float>(ToDouble(pRuntime));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279}
280
Dan Sinclairc9708952017-10-23 09:40:59 -0400281CJS_Object* CJS_Value::ToObject(CJS_Runtime* pRuntime) const {
tsepezb4694242016-08-15 16:44:55 -0700282 v8::Local<v8::Object> pObj = pRuntime->ToObject(m_pValue);
283 return static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(pObj));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284}
285
Dan Sinclair33d13f22017-10-23 09:44:30 -0400286CJS_Document* CJS_Value::ToDocument(CJS_Runtime* pRuntime) const {
287 return static_cast<CJS_Document*>(ToObject(pRuntime));
288}
289
Dan Sinclairc9708952017-10-23 09:40:59 -0400290CJS_Array CJS_Value::ToArray(CJS_Runtime* pRuntime) const {
291 ASSERT(IsArrayObject());
292 return CJS_Array(pRuntime->ToArray(m_pValue));
293}
294
295CJS_Date CJS_Value::ToDate(CJS_Runtime* pRuntime) const {
296 ASSERT(IsDateObject());
297 v8::Local<v8::Value> mutable_value = m_pValue;
298 return CJS_Date(mutable_value.As<v8::Date>());
299}
300
tsepezb4694242016-08-15 16:44:55 -0700301v8::Local<v8::Object> CJS_Value::ToV8Object(CJS_Runtime* pRuntime) const {
302 return pRuntime->ToObject(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303}
304
Dan Sinclairc9708952017-10-23 09:40:59 -0400305WideString CJS_Value::ToWideString(CJS_Runtime* pRuntime) const {
tsepeze6cf0132017-01-18 14:38:18 -0800306 return pRuntime->ToWideString(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307}
308
Dan Sinclairc9708952017-10-23 09:40:59 -0400309ByteString CJS_Value::ToByteString(CJS_Runtime* pRuntime) const {
310 return ByteString::FromUnicode(ToWideString(pRuntime));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311}
312
tsepezb4694242016-08-15 16:44:55 -0700313v8::Local<v8::Value> CJS_Value::ToV8Value(CJS_Runtime* pRuntime) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 return m_pValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315}
316
tsepezb4694242016-08-15 16:44:55 -0700317v8::Local<v8::Array> CJS_Value::ToV8Array(CJS_Runtime* pRuntime) const {
tsepeze6cf0132017-01-18 14:38:18 -0800318 return pRuntime->ToArray(m_pValue);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319}
320
tsepezb4694242016-08-15 16:44:55 -0700321void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) {
Tom Sepez4246b002016-01-20 11:48:29 -0800322 bool bAllowNaN = false;
tsepez40faa792016-07-15 17:58:02 -0700323 if (GetType() == VT_string) {
Dan Sinclairc9708952017-10-23 09:40:59 -0400324 ByteString bstr = ToByteString(pRuntime);
Tom Sepez4246b002016-01-20 11:48:29 -0800325 if (bstr.GetLength() == 0)
326 return;
327 if (bstr == "NaN")
328 bAllowNaN = true;
329 }
tsepezb4694242016-08-15 16:44:55 -0700330 v8::Isolate* pIsolate = pRuntime->GetIsolate();
tsepezf3dc8c62016-08-10 06:29:29 -0700331 v8::TryCatch try_catch(pIsolate);
Tom Sepez4246b002016-01-20 11:48:29 -0800332 v8::MaybeLocal<v8::Number> maybeNum =
tsepezf3dc8c62016-08-10 06:29:29 -0700333 m_pValue->ToNumber(pIsolate->GetCurrentContext());
Tom Sepez4246b002016-01-20 11:48:29 -0800334 if (maybeNum.IsEmpty())
335 return;
336 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
337 if (std::isnan(num->Value()) && !bAllowNaN)
338 return;
339 m_pValue = num;
Tom Sepez4246b002016-01-20 11:48:29 -0800340}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341
tsepez40faa792016-07-15 17:58:02 -0700342// static
343CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) {
344 if (value.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 return VT_unknown;
tsepez40faa792016-07-15 17:58:02 -0700346 if (value->IsString())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 return VT_string;
tsepez40faa792016-07-15 17:58:02 -0700348 if (value->IsNumber())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 return VT_number;
tsepez40faa792016-07-15 17:58:02 -0700350 if (value->IsBoolean())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 return VT_boolean;
tsepez40faa792016-07-15 17:58:02 -0700352 if (value->IsDate())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 return VT_date;
tsepez40faa792016-07-15 17:58:02 -0700354 if (value->IsObject())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 return VT_object;
tsepez40faa792016-07-15 17:58:02 -0700356 if (value->IsNull())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 return VT_null;
tsepez40faa792016-07-15 17:58:02 -0700358 if (value->IsUndefined())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 return VT_undefined;
360 return VT_unknown;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700361}
362
tsepezf3dc8c62016-08-10 06:29:29 -0700363bool CJS_Value::IsArrayObject() const {
364 return !m_pValue.IsEmpty() && m_pValue->IsArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700365}
366
tsepezf3dc8c62016-08-10 06:29:29 -0700367bool CJS_Value::IsDateObject() const {
368 return !m_pValue.IsEmpty() && m_pValue->IsDate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700369}
370
tsepeze5aff742016-08-08 09:49:42 -0700371CJS_Array::CJS_Array() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372
Dan Sinclairc9708952017-10-23 09:40:59 -0400373CJS_Array::CJS_Array(v8::Local<v8::Array> pArray) : m_pArray(pArray) {}
374
weili625ad662016-06-15 11:21:33 -0700375CJS_Array::CJS_Array(const CJS_Array& other) = default;
376
tsepeze5aff742016-08-08 09:49:42 -0700377CJS_Array::~CJS_Array() {}
378
Dan Sinclairc9708952017-10-23 09:40:59 -0400379CJS_Value CJS_Array::GetElement(CJS_Runtime* pRuntime, unsigned index) const {
tsepeze5aff742016-08-08 09:49:42 -0700380 if (!m_pArray.IsEmpty())
Dan Sinclairc9708952017-10-23 09:40:59 -0400381 return CJS_Value(pRuntime, pRuntime->GetArrayElement(m_pArray, index));
382
383 return CJS_Value(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384}
385
tsepezb4694242016-08-15 16:44:55 -0700386void CJS_Array::SetElement(CJS_Runtime* pRuntime,
tsepeze5aff742016-08-08 09:49:42 -0700387 unsigned index,
388 const CJS_Value& value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 if (m_pArray.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700390 m_pArray = pRuntime->NewArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391
tsepezb4694242016-08-15 16:44:55 -0700392 pRuntime->PutArrayElement(m_pArray, index, value.ToV8Value(pRuntime));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393}
394
tsepezb4694242016-08-15 16:44:55 -0700395int CJS_Array::GetLength(CJS_Runtime* pRuntime) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 if (m_pArray.IsEmpty())
397 return 0;
tsepezb4694242016-08-15 16:44:55 -0700398 return pRuntime->GetArrayLength(m_pArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700399}
400
tsepezb4694242016-08-15 16:44:55 -0700401v8::Local<v8::Array> CJS_Array::ToV8Array(CJS_Runtime* pRuntime) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 if (m_pArray.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700403 m_pArray = pRuntime->NewArray();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404
405 return m_pArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406}
407
tsepezf3c88322016-08-09 07:30:38 -0700408CJS_Date::CJS_Date() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700409
Dan Sinclairc9708952017-10-23 09:40:59 -0400410CJS_Date::CJS_Date(v8::Local<v8::Date> pDate) : m_pDate(pDate) {}
411
tsepezb4694242016-08-15 16:44:55 -0700412CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime)
413 : m_pDate(pRuntime->NewDate(dMsecTime)) {}
Tom Sepez67fd5df2015-10-08 12:24:19 -0700414
tsepezb4694242016-08-15 16:44:55 -0700415CJS_Date::CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 int year,
417 int mon,
418 int day,
419 int hour,
420 int min,
Tom Sepez67fd5df2015-10-08 12:24:19 -0700421 int sec)
tsepezb4694242016-08-15 16:44:55 -0700422 : m_pDate(pRuntime->NewDate(MakeDate(year, mon, day, hour, min, sec, 0))) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700423
dan sinclaircbe23db2017-10-19 14:29:33 -0400424CJS_Date::CJS_Date(const CJS_Date& other) = default;
425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426CJS_Date::~CJS_Date() {}
427
tsepezb4694242016-08-15 16:44:55 -0700428bool CJS_Date::IsValidDate(CJS_Runtime* pRuntime) const {
Tom Sepezdf950b82017-08-04 11:33:49 -0700429 return !m_pDate.IsEmpty() && !std::isnan(pRuntime->ToDouble(m_pDate));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430}
431
tsepezb4694242016-08-15 16:44:55 -0700432int CJS_Date::GetYear(CJS_Runtime* pRuntime) const {
433 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700434 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435
tsepeze6cf0132017-01-18 14:38:18 -0800436 return JS_GetYearFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700437}
438
tsepezb4694242016-08-15 16:44:55 -0700439int CJS_Date::GetMonth(CJS_Runtime* pRuntime) const {
440 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700441 return 0;
tsepeze6cf0132017-01-18 14:38:18 -0800442 return JS_GetMonthFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700443}
444
tsepezb4694242016-08-15 16:44:55 -0700445int CJS_Date::GetDay(CJS_Runtime* pRuntime) const {
446 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700447 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448
tsepeze6cf0132017-01-18 14:38:18 -0800449 return JS_GetDayFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700450}
451
tsepezb4694242016-08-15 16:44:55 -0700452int CJS_Date::GetHours(CJS_Runtime* pRuntime) const {
453 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700454 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455
tsepeze6cf0132017-01-18 14:38:18 -0800456 return JS_GetHourFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700457}
458
tsepezb4694242016-08-15 16:44:55 -0700459int CJS_Date::GetMinutes(CJS_Runtime* pRuntime) const {
460 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700461 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462
tsepeze6cf0132017-01-18 14:38:18 -0800463 return JS_GetMinFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464}
465
tsepezb4694242016-08-15 16:44:55 -0700466int CJS_Date::GetSeconds(CJS_Runtime* pRuntime) const {
467 if (!IsValidDate(pRuntime))
tsepezfbf52c22016-07-25 11:17:07 -0700468 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700469
tsepeze6cf0132017-01-18 14:38:18 -0800470 return JS_GetSecFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700471}
472
tsepezb4694242016-08-15 16:44:55 -0700473v8::Local<v8::Date> CJS_Date::ToV8Date(CJS_Runtime* pRuntime) const {
tsepezf3c88322016-08-09 07:30:38 -0700474 return m_pDate;
Tom Sepez39bfe122015-09-17 15:25:23 -0700475}
476
Tom Sepez39bfe122015-09-17 15:25:23 -0700477double JS_GetDateTime() {
478 if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
479 return 0;
thestig1cd352e2016-06-07 17:53:06 -0700480 time_t t = time(nullptr);
Tom Sepez39bfe122015-09-17 15:25:23 -0700481 struct tm* pTm = localtime(&t);
482
483 int year = pTm->tm_year + 1900;
Lei Zhangab5939e2017-06-16 02:23:18 -0700484 double t1 = TimeFromYear(year);
Tom Sepez39bfe122015-09-17 15:25:23 -0700485
486 return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 +
487 pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0;
488}
489
490int JS_GetYearFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700491 return YearFromTime(dt);
Tom Sepez39bfe122015-09-17 15:25:23 -0700492}
493
494int JS_GetMonthFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700495 return MonthFromTime(dt);
Tom Sepez39bfe122015-09-17 15:25:23 -0700496}
497
498int JS_GetDayFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700499 return DateFromTime(dt);
Tom Sepez39bfe122015-09-17 15:25:23 -0700500}
501
502int JS_GetHourFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700503 return (int)Mod(floor(dt / (60 * 60 * 1000)), 24);
Tom Sepez39bfe122015-09-17 15:25:23 -0700504}
505
506int JS_GetMinFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700507 return (int)Mod(floor(dt / (60 * 1000)), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700508}
509
510int JS_GetSecFromTime(double dt) {
Lei Zhangab5939e2017-06-16 02:23:18 -0700511 return (int)Mod(floor(dt / 1000), 60);
Tom Sepez39bfe122015-09-17 15:25:23 -0700512}
513
Ryan Harrison275e2602017-09-18 14:23:18 -0400514double JS_DateParse(const WideString& str) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700515 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
516 v8::Isolate::Scope isolate_scope(pIsolate);
517 v8::HandleScope scope(pIsolate);
518
519 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
520
521 // Use the built-in object method.
522 v8::Local<v8::Value> v =
523 context->Global()
524 ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date",
525 v8::NewStringType::kNormal)
526 .ToLocalChecked())
527 .ToLocalChecked();
528 if (v->IsObject()) {
529 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
530 v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse",
531 v8::NewStringType::kNormal)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400532 .ToLocalChecked())
533 .ToLocalChecked();
Tom Sepez39bfe122015-09-17 15:25:23 -0700534 if (v->IsFunction()) {
535 v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
Tom Sepez39bfe122015-09-17 15:25:23 -0700536 const int argc = 1;
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800537 v8::Local<v8::Value> timeStr =
538 CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString(
Ryan Harrison275e2602017-09-18 14:23:18 -0400539 str.AsStringView());
Tom Sepez39bfe122015-09-17 15:25:23 -0700540 v8::Local<v8::Value> argv[argc] = {timeStr};
541 v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
542 if (v->IsNumber()) {
543 double date = v->ToNumber(context).ToLocalChecked()->Value();
Tom Sepezdf950b82017-08-04 11:33:49 -0700544 if (!std::isfinite(date))
Tom Sepez39bfe122015-09-17 15:25:23 -0700545 return date;
tsepez86a61dc2016-03-25 10:00:11 -0700546 return JS_LocalTime(date);
Tom Sepez39bfe122015-09-17 15:25:23 -0700547 }
548 }
549 }
550 return 0;
551}
552
553double JS_MakeDay(int nYear, int nMonth, int nDate) {
Tom Sepezdf950b82017-08-04 11:33:49 -0700554 double y = static_cast<double>(nYear);
555 double m = static_cast<double>(nMonth);
556 double dt = static_cast<double>(nDate);
557 double ym = y + floor(m / 12);
Lei Zhangab5939e2017-06-16 02:23:18 -0700558 double mn = Mod(m, 12);
Tom Sepezdf950b82017-08-04 11:33:49 -0700559 double t = TimeFromYearMonth(static_cast<int>(ym), static_cast<int>(mn));
Lei Zhangab5939e2017-06-16 02:23:18 -0700560 if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1)
Tom Sepezdf950b82017-08-04 11:33:49 -0700561 return std::nan("");
562
Lei Zhangab5939e2017-06-16 02:23:18 -0700563 return Day(t) + dt - 1;
Tom Sepez39bfe122015-09-17 15:25:23 -0700564}
565
566double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) {
Tom Sepezdf950b82017-08-04 11:33:49 -0700567 double h = static_cast<double>(nHour);
568 double m = static_cast<double>(nMin);
569 double s = static_cast<double>(nSec);
570 double milli = static_cast<double>(nMs);
Tom Sepez39bfe122015-09-17 15:25:23 -0700571 return h * 3600000 + m * 60000 + s * 1000 + milli;
572}
573
574double JS_MakeDate(double day, double time) {
Tom Sepezdf950b82017-08-04 11:33:49 -0700575 if (!std::isfinite(day) || !std::isfinite(time))
576 return std::nan("");
Tom Sepez39bfe122015-09-17 15:25:23 -0700577
578 return day * 86400000 + time;
579}
580
Dan Sinclairc9708952017-10-23 09:40:59 -0400581std::vector<CJS_Value> ExpandKeywordParams(
Tom Sepezbd932572016-01-29 09:10:41 -0800582 CJS_Runtime* pRuntime,
583 const std::vector<CJS_Value>& originals,
584 size_t nKeywords,
585 ...) {
586 ASSERT(nKeywords);
587
588 std::vector<CJS_Value> result(nKeywords, CJS_Value(pRuntime));
589 size_t size = std::min(originals.size(), nKeywords);
590 for (size_t i = 0; i < size; ++i)
591 result[i] = originals[i];
592
593 if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object ||
594 originals[0].IsArrayObject()) {
595 return result;
596 }
tsepezb4694242016-08-15 16:44:55 -0700597 v8::Local<v8::Object> pObj = originals[0].ToV8Object(pRuntime);
Tom Sepezbd932572016-01-29 09:10:41 -0800598 result[0] = CJS_Value(pRuntime); // Make unknown.
599
600 va_list ap;
601 va_start(ap, nKeywords);
Wei Li89409932016-03-28 10:33:33 -0700602 for (size_t i = 0; i < nKeywords; ++i) {
Tom Sepezbd932572016-01-29 09:10:41 -0800603 const wchar_t* property = va_arg(ap, const wchar_t*);
tsepezb4694242016-08-15 16:44:55 -0700604 v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property);
Tom Sepezbd932572016-01-29 09:10:41 -0800605 if (!v8Value->IsUndefined())
tsepez40faa792016-07-15 17:58:02 -0700606 result[i] = CJS_Value(pRuntime, v8Value);
Tom Sepezbd932572016-01-29 09:10:41 -0800607 }
608 va_end(ap);
609 return result;
610}