John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/JS_Value.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 9 | #include <time.h> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 10 | |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 11 | #include <algorithm> |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 12 | #include <cmath> |
| 13 | #include <limits> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 14 | #include <vector> |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 15 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 16 | #include "fpdfsdk/javascript/Document.h" |
| 17 | #include "fpdfsdk/javascript/JS_Define.h" |
| 18 | #include "fpdfsdk/javascript/JS_Object.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 20 | namespace { |
| 21 | |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 22 | double |
| 23 | MakeDate(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 Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 28 | double GetLocalTZA() { |
| 29 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 30 | return 0; |
| 31 | time_t t = 0; |
| 32 | time(&t); |
| 33 | localtime(&t); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 34 | #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Tom Sepez | a2da7c5 | 2017-08-03 13:34:08 -0700 | [diff] [blame] | 35 | // 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 Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 37 | long timezone = 0; |
| 38 | _get_timezone(&timezone); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 39 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 40 | return (double)(-(timezone * 1000)); |
| 41 | } |
| 42 | |
| 43 | int 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 | |
| 56 | double Mod(double x, double y) { |
| 57 | double r = fmod(x, y); |
| 58 | if (r < 0) |
| 59 | r += y; |
| 60 | return r; |
| 61 | } |
| 62 | |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 63 | bool IsLeapYear(int year) { |
| 64 | return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); |
| 65 | } |
| 66 | |
| 67 | int 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 | |
| 72 | double TimeFromYear(int y) { |
| 73 | return 86400000.0 * DayFromYear(y); |
| 74 | } |
| 75 | |
| 76 | static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151, |
| 77 | 181, 212, 243, 273, 304, 334}; |
| 78 | static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152, |
| 79 | 182, 213, 244, 274, 305, 335}; |
| 80 | |
| 81 | double TimeFromYearMonth(int y, int m) { |
| 82 | const uint16_t* pMonth = IsLeapYear(y) ? leapDaysMonth : daysMonth; |
| 83 | return TimeFromYear(y) + ((double)pMonth[m]) * 86400000; |
| 84 | } |
| 85 | |
| 86 | int Day(double t) { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 87 | return static_cast<int>(floor(t / 86400000.0)); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int YearFromTime(double t) { |
| 91 | // estimate the time. |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 92 | int y = 1970 + static_cast<int>(t / (365.2425 * 86400000.0)); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 93 | 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 | |
| 103 | int DayWithinYear(double t) { |
| 104 | int year = YearFromTime(t); |
| 105 | int day = Day(t); |
| 106 | return day - DayFromYear(year); |
| 107 | } |
| 108 | |
| 109 | int 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 | |
| 140 | int 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 Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 175 | double JS_LocalTime(double d) { |
| 176 | return d + GetLocalTZA() + GetDaylightSavingTA(d); |
| 177 | } |
| 178 | |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 179 | } // namespace |
| 180 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 181 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime) {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 183 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue) |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 184 | : m_pValue(pValue) {} |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 185 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 186 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, int iValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 187 | : m_pValue(pRuntime->NewNumber(iValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 188 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 189 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, bool bValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 190 | : m_pValue(pRuntime->NewBoolean(bValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 191 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 192 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, float fValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 193 | : m_pValue(pRuntime->NewNumber(fValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 194 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 195 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, double dValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 196 | : m_pValue(pRuntime->NewNumber(dValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 197 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 198 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pObj) { |
| 199 | if (pObj) |
| 200 | m_pValue = pObj->ToV8Object(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 203 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const wchar_t* pWstr) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 204 | : m_pValue(pRuntime->NewString(pWstr)) {} |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame] | 205 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 206 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const char* pStr) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 207 | : m_pValue(pRuntime->NewString(WideString::FromLocal(pStr).c_str())) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 208 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 209 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 210 | : m_pValue(array.ToV8Array(pRuntime)) {} |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 211 | |
| 212 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Date& date) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 213 | : m_pValue(date.ToV8Date(pRuntime)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 214 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | CJS_Value::~CJS_Value() {} |
| 216 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 217 | CJS_Value::CJS_Value(const CJS_Value& other) = default; |
| 218 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 219 | void CJS_Value::Set(v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | m_pValue = pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 221 | } |
| 222 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 223 | int CJS_Value::ToInt(CJS_Runtime* pRuntime) const { |
| 224 | return pRuntime->ToInt32(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 225 | } |
| 226 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 227 | bool CJS_Value::ToBool(CJS_Runtime* pRuntime) const { |
| 228 | return pRuntime->ToBoolean(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 229 | } |
| 230 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 231 | double CJS_Value::ToDouble(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 232 | return pRuntime->ToDouble(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 233 | } |
| 234 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 235 | float CJS_Value::ToFloat(CJS_Runtime* pRuntime) const { |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 236 | return static_cast<float>(ToDouble(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 239 | CJS_Object* CJS_Value::ToObject(CJS_Runtime* pRuntime) const { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 240 | v8::Local<v8::Object> pObj = pRuntime->ToObject(m_pValue); |
| 241 | return static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(pObj)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 244 | CJS_Array CJS_Value::ToArray(CJS_Runtime* pRuntime) const { |
| 245 | ASSERT(IsArrayObject()); |
| 246 | return CJS_Array(pRuntime->ToArray(m_pValue)); |
| 247 | } |
| 248 | |
| 249 | CJS_Date CJS_Value::ToDate(CJS_Runtime* pRuntime) const { |
| 250 | ASSERT(IsDateObject()); |
| 251 | v8::Local<v8::Value> mutable_value = m_pValue; |
| 252 | return CJS_Date(mutable_value.As<v8::Date>()); |
| 253 | } |
| 254 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 255 | v8::Local<v8::Object> CJS_Value::ToV8Object(CJS_Runtime* pRuntime) const { |
| 256 | return pRuntime->ToObject(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 259 | WideString CJS_Value::ToWideString(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 260 | return pRuntime->ToWideString(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 263 | ByteString CJS_Value::ToByteString(CJS_Runtime* pRuntime) const { |
| 264 | return ByteString::FromUnicode(ToWideString(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 265 | } |
| 266 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 267 | v8::Local<v8::Value> CJS_Value::ToV8Value(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | return m_pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 269 | } |
| 270 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 271 | v8::Local<v8::Array> CJS_Value::ToV8Array(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 272 | return pRuntime->ToArray(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 273 | } |
| 274 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 275 | void CJS_Value::SetNull(CJS_Runtime* pRuntime) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 276 | m_pValue = pRuntime->NewNull(); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 277 | } |
| 278 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 279 | void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) { |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 280 | bool bAllowNaN = false; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 281 | if (GetType() == VT_string) { |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 282 | ByteString bstr = ToByteString(pRuntime); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 283 | if (bstr.GetLength() == 0) |
| 284 | return; |
| 285 | if (bstr == "NaN") |
| 286 | bAllowNaN = true; |
| 287 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 288 | v8::Isolate* pIsolate = pRuntime->GetIsolate(); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 289 | v8::TryCatch try_catch(pIsolate); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 290 | v8::MaybeLocal<v8::Number> maybeNum = |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 291 | m_pValue->ToNumber(pIsolate->GetCurrentContext()); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 292 | if (maybeNum.IsEmpty()) |
| 293 | return; |
| 294 | v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); |
| 295 | if (std::isnan(num->Value()) && !bAllowNaN) |
| 296 | return; |
| 297 | m_pValue = num; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 298 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 299 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 300 | // static |
| 301 | CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) { |
| 302 | if (value.IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 303 | return VT_unknown; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 304 | if (value->IsString()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 305 | return VT_string; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 306 | if (value->IsNumber()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 307 | return VT_number; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 308 | if (value->IsBoolean()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 309 | return VT_boolean; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 310 | if (value->IsDate()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | return VT_date; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 312 | if (value->IsObject()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 313 | return VT_object; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 314 | if (value->IsNull()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | return VT_null; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 316 | if (value->IsUndefined()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | return VT_undefined; |
| 318 | return VT_unknown; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 319 | } |
| 320 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 321 | bool CJS_Value::IsArrayObject() const { |
| 322 | return !m_pValue.IsEmpty() && m_pValue->IsArray(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 323 | } |
| 324 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 325 | bool CJS_Value::IsDateObject() const { |
| 326 | return !m_pValue.IsEmpty() && m_pValue->IsDate(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 329 | CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime) |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 330 | : m_bIsSetting(0), m_Value(pRuntime), m_pJSRuntime(pRuntime) {} |
| 331 | |
| 332 | CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime, const CJS_Value& value) |
| 333 | : m_bIsSetting(0), m_Value(value), m_pJSRuntime(pRuntime) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 334 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 335 | CJS_PropValue::~CJS_PropValue() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 336 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 337 | void CJS_PropValue::Set(int iValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 339 | m_Value = CJS_Value(m_pJSRuntime.Get(), iValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 340 | } |
| 341 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 342 | int CJS_PropValue::ToInt() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 343 | ASSERT(m_bIsSetting); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 344 | return m_Value.ToInt(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 345 | } |
| 346 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 347 | void CJS_PropValue::Set(bool bValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 348 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 349 | m_Value = CJS_Value(m_pJSRuntime.Get(), bValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 350 | } |
| 351 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 352 | bool CJS_PropValue::ToBool() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 353 | ASSERT(m_bIsSetting); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 354 | return m_Value.ToBool(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 355 | } |
| 356 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 357 | void CJS_PropValue::Set(double dValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 358 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 359 | m_Value = CJS_Value(m_pJSRuntime.Get(), dValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 360 | } |
| 361 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 362 | double CJS_PropValue::ToDouble() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 363 | ASSERT(m_bIsSetting); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 364 | return m_Value.ToDouble(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 365 | } |
| 366 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 367 | void CJS_PropValue::Set(CJS_Object* pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 368 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 369 | m_Value = CJS_Value(m_pJSRuntime.Get(), pObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 370 | } |
| 371 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 372 | CJS_Object* CJS_PropValue::ToObject() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | ASSERT(m_bIsSetting); |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 374 | return m_Value.ToObject(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 375 | } |
| 376 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 377 | void CJS_PropValue::Set(CJS_Document* pJsDoc) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 378 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 379 | m_Value = CJS_Value(m_pJSRuntime.Get(), pJsDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 380 | } |
| 381 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 382 | CJS_Document* CJS_PropValue::ToDocument() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 383 | ASSERT(m_bIsSetting); |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 384 | return static_cast<CJS_Document*>(m_Value.ToObject(m_pJSRuntime.Get())); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 385 | } |
| 386 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 387 | void CJS_PropValue::Set(v8::Local<v8::Object> pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 388 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 389 | m_Value = CJS_Value(m_pJSRuntime.Get(), pObj); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 390 | } |
| 391 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 392 | v8::Local<v8::Object> CJS_PropValue::ToV8Object() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 393 | ASSERT(m_bIsSetting); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 394 | return m_Value.ToV8Object(m_pJSRuntime.Get()); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 395 | } |
| 396 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 397 | void CJS_PropValue::Set(const ByteString& str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 398 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 399 | m_Value = CJS_Value(m_pJSRuntime.Get(), str.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 400 | } |
| 401 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 402 | ByteString CJS_PropValue::ToByteString() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 403 | ASSERT(m_bIsSetting); |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 404 | return m_Value.ToByteString(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 405 | } |
| 406 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 407 | void CJS_PropValue::Set(const wchar_t* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 408 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 409 | m_Value = CJS_Value(m_pJSRuntime.Get(), str); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 410 | } |
| 411 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 412 | WideString CJS_PropValue::ToWideString() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 413 | ASSERT(m_bIsSetting); |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 414 | return m_Value.ToWideString(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 415 | } |
| 416 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 417 | void CJS_PropValue::Set(const WideString& wide_string) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 418 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 419 | m_Value = CJS_Value(m_pJSRuntime.Get(), wide_string.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 420 | } |
| 421 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 422 | CJS_Array CJS_PropValue::ToArray() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 423 | ASSERT(m_bIsSetting); |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 424 | if (!m_Value.IsArrayObject()) |
| 425 | return CJS_Array(); |
| 426 | return m_Value.ToArray(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 427 | } |
| 428 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 429 | void CJS_PropValue::Set(const CJS_Array& array) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | ASSERT(!m_bIsSetting); |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 431 | m_Value = CJS_Value(m_pJSRuntime.Get(), array); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 432 | } |
| 433 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 434 | CJS_Date CJS_PropValue::ToDate() const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 435 | ASSERT(m_bIsSetting); |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 436 | if (!m_Value.IsDateObject()) |
| 437 | return CJS_Date(); |
| 438 | return m_Value.ToDate(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 439 | } |
| 440 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 441 | void CJS_PropValue::Set(const CJS_Date& date) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 443 | m_Value = CJS_Value(m_pJSRuntime.Get(), date); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 444 | } |
| 445 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 446 | CJS_Array::CJS_Array() {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 447 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 448 | CJS_Array::CJS_Array(v8::Local<v8::Array> pArray) : m_pArray(pArray) {} |
| 449 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 450 | CJS_Array::CJS_Array(const CJS_Array& other) = default; |
| 451 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 452 | CJS_Array::~CJS_Array() {} |
| 453 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 454 | CJS_Value CJS_Array::GetElement(CJS_Runtime* pRuntime, unsigned index) const { |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 455 | if (!m_pArray.IsEmpty()) |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 456 | return CJS_Value(pRuntime, pRuntime->GetArrayElement(m_pArray, index)); |
| 457 | |
| 458 | return CJS_Value(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 459 | } |
| 460 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 461 | void CJS_Array::SetElement(CJS_Runtime* pRuntime, |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 462 | unsigned index, |
| 463 | const CJS_Value& value) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 464 | if (m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 465 | m_pArray = pRuntime->NewArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 466 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 467 | pRuntime->PutArrayElement(m_pArray, index, value.ToV8Value(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 468 | } |
| 469 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 470 | int CJS_Array::GetLength(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 471 | if (m_pArray.IsEmpty()) |
| 472 | return 0; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 473 | return pRuntime->GetArrayLength(m_pArray); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 474 | } |
| 475 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 476 | v8::Local<v8::Array> CJS_Array::ToV8Array(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 477 | if (m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 478 | m_pArray = pRuntime->NewArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 479 | |
| 480 | return m_pArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 481 | } |
| 482 | |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 483 | CJS_Date::CJS_Date() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 484 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 485 | CJS_Date::CJS_Date(v8::Local<v8::Date> pDate) : m_pDate(pDate) {} |
| 486 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 487 | CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime) |
| 488 | : m_pDate(pRuntime->NewDate(dMsecTime)) {} |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 489 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 490 | CJS_Date::CJS_Date(CJS_Runtime* pRuntime, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 491 | int year, |
| 492 | int mon, |
| 493 | int day, |
| 494 | int hour, |
| 495 | int min, |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 496 | int sec) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 497 | : m_pDate(pRuntime->NewDate(MakeDate(year, mon, day, hour, min, sec, 0))) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 498 | |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 499 | CJS_Date::CJS_Date(const CJS_Date& other) = default; |
| 500 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 501 | CJS_Date::~CJS_Date() {} |
| 502 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 503 | bool CJS_Date::IsValidDate(CJS_Runtime* pRuntime) const { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 504 | return !m_pDate.IsEmpty() && !std::isnan(pRuntime->ToDouble(m_pDate)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 505 | } |
| 506 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 507 | int CJS_Date::GetYear(CJS_Runtime* pRuntime) const { |
| 508 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 509 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 510 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 511 | return JS_GetYearFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 512 | } |
| 513 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 514 | int CJS_Date::GetMonth(CJS_Runtime* pRuntime) const { |
| 515 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 516 | return 0; |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 517 | return JS_GetMonthFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 518 | } |
| 519 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 520 | int CJS_Date::GetDay(CJS_Runtime* pRuntime) const { |
| 521 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 522 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 523 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 524 | return JS_GetDayFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 525 | } |
| 526 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 527 | int CJS_Date::GetHours(CJS_Runtime* pRuntime) const { |
| 528 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 529 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 530 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 531 | return JS_GetHourFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 532 | } |
| 533 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 534 | int CJS_Date::GetMinutes(CJS_Runtime* pRuntime) const { |
| 535 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 536 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 537 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 538 | return JS_GetMinFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 539 | } |
| 540 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 541 | int CJS_Date::GetSeconds(CJS_Runtime* pRuntime) const { |
| 542 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 543 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 544 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 545 | return JS_GetSecFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 546 | } |
| 547 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 548 | v8::Local<v8::Date> CJS_Date::ToV8Date(CJS_Runtime* pRuntime) const { |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 549 | return m_pDate; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 552 | double JS_GetDateTime() { |
| 553 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 554 | return 0; |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 555 | time_t t = time(nullptr); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 556 | struct tm* pTm = localtime(&t); |
| 557 | |
| 558 | int year = pTm->tm_year + 1900; |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 559 | double t1 = TimeFromYear(year); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 560 | |
| 561 | return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + |
| 562 | pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; |
| 563 | } |
| 564 | |
| 565 | int JS_GetYearFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 566 | return YearFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | int JS_GetMonthFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 570 | return MonthFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | int JS_GetDayFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 574 | return DateFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | int JS_GetHourFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 578 | return (int)Mod(floor(dt / (60 * 60 * 1000)), 24); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | int JS_GetMinFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 582 | return (int)Mod(floor(dt / (60 * 1000)), 60); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | int JS_GetSecFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 586 | return (int)Mod(floor(dt / 1000), 60); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 589 | double JS_DateParse(const WideString& str) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 590 | v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); |
| 591 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 592 | v8::HandleScope scope(pIsolate); |
| 593 | |
| 594 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 595 | |
| 596 | // Use the built-in object method. |
| 597 | v8::Local<v8::Value> v = |
| 598 | context->Global() |
| 599 | ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", |
| 600 | v8::NewStringType::kNormal) |
| 601 | .ToLocalChecked()) |
| 602 | .ToLocalChecked(); |
| 603 | if (v->IsObject()) { |
| 604 | v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked(); |
| 605 | v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", |
| 606 | v8::NewStringType::kNormal) |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 607 | .ToLocalChecked()) |
| 608 | .ToLocalChecked(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 609 | if (v->IsFunction()) { |
| 610 | v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 611 | const int argc = 1; |
Tom Sepez | c6dc69f | 2017-02-23 09:53:09 -0800 | [diff] [blame] | 612 | v8::Local<v8::Value> timeStr = |
| 613 | CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString( |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 614 | str.AsStringView()); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 615 | v8::Local<v8::Value> argv[argc] = {timeStr}; |
| 616 | v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); |
| 617 | if (v->IsNumber()) { |
| 618 | double date = v->ToNumber(context).ToLocalChecked()->Value(); |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 619 | if (!std::isfinite(date)) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 620 | return date; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 621 | return JS_LocalTime(date); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | double JS_MakeDay(int nYear, int nMonth, int nDate) { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 629 | double y = static_cast<double>(nYear); |
| 630 | double m = static_cast<double>(nMonth); |
| 631 | double dt = static_cast<double>(nDate); |
| 632 | double ym = y + floor(m / 12); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 633 | double mn = Mod(m, 12); |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 634 | double t = TimeFromYearMonth(static_cast<int>(ym), static_cast<int>(mn)); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 635 | if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1) |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 636 | return std::nan(""); |
| 637 | |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 638 | return Day(t) + dt - 1; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 642 | double h = static_cast<double>(nHour); |
| 643 | double m = static_cast<double>(nMin); |
| 644 | double s = static_cast<double>(nSec); |
| 645 | double milli = static_cast<double>(nMs); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 646 | return h * 3600000 + m * 60000 + s * 1000 + milli; |
| 647 | } |
| 648 | |
| 649 | double JS_MakeDate(double day, double time) { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 650 | if (!std::isfinite(day) || !std::isfinite(time)) |
| 651 | return std::nan(""); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 652 | |
| 653 | return day * 86400000 + time; |
| 654 | } |
| 655 | |
Dan Sinclair | c970895 | 2017-10-23 09:40:59 -0400 | [diff] [blame^] | 656 | std::vector<CJS_Value> ExpandKeywordParams( |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 657 | CJS_Runtime* pRuntime, |
| 658 | const std::vector<CJS_Value>& originals, |
| 659 | size_t nKeywords, |
| 660 | ...) { |
| 661 | ASSERT(nKeywords); |
| 662 | |
| 663 | std::vector<CJS_Value> result(nKeywords, CJS_Value(pRuntime)); |
| 664 | size_t size = std::min(originals.size(), nKeywords); |
| 665 | for (size_t i = 0; i < size; ++i) |
| 666 | result[i] = originals[i]; |
| 667 | |
| 668 | if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object || |
| 669 | originals[0].IsArrayObject()) { |
| 670 | return result; |
| 671 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 672 | v8::Local<v8::Object> pObj = originals[0].ToV8Object(pRuntime); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 673 | result[0] = CJS_Value(pRuntime); // Make unknown. |
| 674 | |
| 675 | va_list ap; |
| 676 | va_start(ap, nKeywords); |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 677 | for (size_t i = 0; i < nKeywords; ++i) { |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 678 | const wchar_t* property = va_arg(ap, const wchar_t*); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 679 | v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 680 | if (!v8Value->IsUndefined()) |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 681 | result[i] = CJS_Value(pRuntime, v8Value); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 682 | } |
| 683 | va_end(ap); |
| 684 | return result; |
| 685 | } |