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); |
Tom Sepez | bd45656 | 2017-08-04 14:45:14 -0700 | [diff] [blame^] | 34 | #if _FXM_PLATFORM_ == _FXM_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); |
Tom Sepez | bd45656 | 2017-08-04 14:45:14 -0700 | [diff] [blame^] | 39 | #endif // _FXM_PLATFORM_ == _FXM_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 | |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 175 | } // namespace |
| 176 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 177 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime) {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 179 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue) |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 180 | : m_pValue(pValue) {} |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 181 | |
| 182 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 183 | : m_pValue(pRuntime->NewNumber(iValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 184 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 185 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const bool& bValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 186 | : m_pValue(pRuntime->NewBoolean(bValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 187 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 188 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const float& fValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 189 | : m_pValue(pRuntime->NewNumber(fValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 190 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 191 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const double& dValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 192 | : m_pValue(pRuntime->NewNumber(dValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 193 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 194 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pObj) { |
| 195 | if (pObj) |
| 196 | m_pValue = pObj->ToV8Object(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 199 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const wchar_t* pWstr) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 200 | : m_pValue(pRuntime->NewString(pWstr)) {} |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame] | 201 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 202 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const char* pStr) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 203 | : m_pValue(pRuntime->NewString(CFX_WideString::FromLocal(pStr).c_str())) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 204 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 205 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 206 | : m_pValue(array.ToV8Array(pRuntime)) {} |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 207 | |
| 208 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Date& date) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 209 | : m_pValue(date.ToV8Date(pRuntime)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 210 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | CJS_Value::~CJS_Value() {} |
| 212 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 213 | CJS_Value::CJS_Value(const CJS_Value& other) = default; |
| 214 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 215 | void CJS_Value::Attach(v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | m_pValue = pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | void CJS_Value::Detach() { |
| 220 | m_pValue = v8::Local<v8::Value>(); |
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 { |
| 236 | return (float)ToDouble(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 237 | } |
| 238 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 239 | CJS_Object* CJS_Value::ToCJSObject(CJS_Runtime* pRuntime) const { |
| 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 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 244 | v8::Local<v8::Object> CJS_Value::ToV8Object(CJS_Runtime* pRuntime) const { |
| 245 | return pRuntime->ToObject(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 246 | } |
| 247 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 248 | CFX_WideString CJS_Value::ToCFXWideString(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 249 | return pRuntime->ToWideString(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 250 | } |
| 251 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 252 | CFX_ByteString CJS_Value::ToCFXByteString(CJS_Runtime* pRuntime) const { |
| 253 | return CFX_ByteString::FromUnicode(ToCFXWideString(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 254 | } |
| 255 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 256 | v8::Local<v8::Value> CJS_Value::ToV8Value(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | return m_pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 258 | } |
| 259 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 260 | v8::Local<v8::Array> CJS_Value::ToV8Array(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 261 | return pRuntime->ToArray(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 262 | } |
| 263 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 264 | void CJS_Value::SetNull(CJS_Runtime* pRuntime) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 265 | m_pValue = pRuntime->NewNull(); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 266 | } |
| 267 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 268 | void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) { |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 269 | bool bAllowNaN = false; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 270 | if (GetType() == VT_string) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 271 | CFX_ByteString bstr = ToCFXByteString(pRuntime); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 272 | if (bstr.GetLength() == 0) |
| 273 | return; |
| 274 | if (bstr == "NaN") |
| 275 | bAllowNaN = true; |
| 276 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 277 | v8::Isolate* pIsolate = pRuntime->GetIsolate(); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 278 | v8::TryCatch try_catch(pIsolate); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 279 | v8::MaybeLocal<v8::Number> maybeNum = |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 280 | m_pValue->ToNumber(pIsolate->GetCurrentContext()); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 281 | if (maybeNum.IsEmpty()) |
| 282 | return; |
| 283 | v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); |
| 284 | if (std::isnan(num->Value()) && !bAllowNaN) |
| 285 | return; |
| 286 | m_pValue = num; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 287 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 288 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 289 | // static |
| 290 | CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) { |
| 291 | if (value.IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | return VT_unknown; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 293 | if (value->IsString()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | return VT_string; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 295 | if (value->IsNumber()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | return VT_number; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 297 | if (value->IsBoolean()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 298 | return VT_boolean; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 299 | if (value->IsDate()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 300 | return VT_date; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 301 | if (value->IsObject()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | return VT_object; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 303 | if (value->IsNull()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | return VT_null; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 305 | if (value->IsUndefined()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | return VT_undefined; |
| 307 | return VT_unknown; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 308 | } |
| 309 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 310 | bool CJS_Value::IsArrayObject() const { |
| 311 | return !m_pValue.IsEmpty() && m_pValue->IsArray(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 312 | } |
| 313 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 314 | bool CJS_Value::IsDateObject() const { |
| 315 | return !m_pValue.IsEmpty() && m_pValue->IsDate(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 316 | } |
| 317 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 318 | bool CJS_Value::ConvertToArray(CJS_Runtime* pRuntime, CJS_Array& array) const { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 319 | if (!IsArrayObject()) |
| 320 | return false; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 321 | array.Attach(pRuntime->ToArray(m_pValue)); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 322 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 323 | } |
| 324 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 325 | bool CJS_Value::ConvertToDate(CJS_Runtime* pRuntime, CJS_Date& date) const { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 326 | if (!IsDateObject()) |
| 327 | return false; |
| 328 | v8::Local<v8::Value> mutable_value = m_pValue; |
| 329 | date.Attach(mutable_value.As<v8::Date>()); |
| 330 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 333 | CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime) |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 334 | : m_bIsSetting(0), m_Value(pRuntime), m_pJSRuntime(pRuntime) {} |
| 335 | |
| 336 | CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime, const CJS_Value& value) |
| 337 | : m_bIsSetting(0), m_Value(value), m_pJSRuntime(pRuntime) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 338 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 339 | CJS_PropValue::~CJS_PropValue() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 340 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | void CJS_PropValue::operator<<(int iValue) { |
| 342 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 343 | m_Value = CJS_Value(m_pJSRuntime.Get(), iValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 346 | void CJS_PropValue::operator>>(int& iValue) const { |
| 347 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 348 | iValue = m_Value.ToInt(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 351 | void CJS_PropValue::operator<<(bool bValue) { |
| 352 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 353 | m_Value = CJS_Value(m_pJSRuntime.Get(), bValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | void CJS_PropValue::operator>>(bool& bValue) const { |
| 357 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 358 | bValue = m_Value.ToBool(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 361 | void CJS_PropValue::operator<<(double dValue) { |
| 362 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 363 | m_Value = CJS_Value(m_pJSRuntime.Get(), dValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 366 | void CJS_PropValue::operator>>(double& dValue) const { |
| 367 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 368 | dValue = m_Value.ToDouble(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | void CJS_PropValue::operator<<(CJS_Object* pObj) { |
| 372 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 373 | m_Value = CJS_Value(m_pJSRuntime.Get(), pObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | void CJS_PropValue::operator>>(CJS_Object*& ppObj) const { |
| 377 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 378 | ppObj = m_Value.ToCJSObject(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 381 | void CJS_PropValue::operator<<(CJS_Document* pJsDoc) { |
| 382 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 383 | m_Value = CJS_Value(m_pJSRuntime.Get(), pJsDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | void CJS_PropValue::operator>>(CJS_Document*& ppJsDoc) const { |
| 387 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 388 | ppJsDoc = static_cast<CJS_Document*>(m_Value.ToCJSObject(m_pJSRuntime.Get())); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 391 | void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 392 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 393 | m_Value = CJS_Value(m_pJSRuntime.Get(), pObj); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 396 | void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 397 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 398 | ppObj = m_Value.ToV8Object(m_pJSRuntime.Get()); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 401 | void CJS_PropValue::operator<<(CFX_ByteString str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 402 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 403 | m_Value = CJS_Value(m_pJSRuntime.Get(), str.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 406 | void CJS_PropValue::operator>>(CFX_ByteString& str) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 407 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 408 | str = m_Value.ToCFXByteString(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 411 | void CJS_PropValue::operator<<(const wchar_t* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 413 | m_Value = CJS_Value(m_pJSRuntime.Get(), str); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | void CJS_PropValue::operator>>(CFX_WideString& wide_string) const { |
| 417 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 418 | wide_string = m_Value.ToCFXWideString(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | void CJS_PropValue::operator<<(CFX_WideString wide_string) { |
| 422 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 423 | 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] | 424 | } |
| 425 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 426 | void CJS_PropValue::operator>>(CJS_Array& array) const { |
| 427 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 428 | m_Value.ConvertToArray(m_pJSRuntime.Get(), array); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 431 | void CJS_PropValue::operator<<(CJS_Array& array) { |
| 432 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 433 | m_Value = CJS_Value(m_pJSRuntime.Get(), array.ToV8Array(m_pJSRuntime.Get())); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | void CJS_PropValue::operator>>(CJS_Date& date) const { |
| 437 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 438 | m_Value.ConvertToDate(m_pJSRuntime.Get(), date); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 441 | void CJS_PropValue::operator<<(CJS_Date& date) { |
| 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 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 448 | CJS_Array::CJS_Array(const CJS_Array& other) = default; |
| 449 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 450 | CJS_Array::~CJS_Array() {} |
| 451 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 452 | void CJS_Array::Attach(v8::Local<v8::Array> pArray) { |
| 453 | m_pArray = pArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 454 | } |
| 455 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 456 | void CJS_Array::GetElement(CJS_Runtime* pRuntime, |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 457 | unsigned index, |
| 458 | CJS_Value& value) const { |
| 459 | if (!m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 460 | value.Attach(pRuntime->GetArrayElement(m_pArray, index)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 461 | } |
| 462 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 463 | void CJS_Array::SetElement(CJS_Runtime* pRuntime, |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 464 | unsigned index, |
| 465 | const CJS_Value& value) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 466 | if (m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 467 | m_pArray = pRuntime->NewArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 468 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 469 | pRuntime->PutArrayElement(m_pArray, index, value.ToV8Value(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 470 | } |
| 471 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 472 | int CJS_Array::GetLength(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 473 | if (m_pArray.IsEmpty()) |
| 474 | return 0; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 475 | return pRuntime->GetArrayLength(m_pArray); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 476 | } |
| 477 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 478 | v8::Local<v8::Array> CJS_Array::ToV8Array(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 479 | if (m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 480 | m_pArray = pRuntime->NewArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 481 | |
| 482 | return m_pArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 483 | } |
| 484 | |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 485 | CJS_Date::CJS_Date() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 499 | CJS_Date::~CJS_Date() {} |
| 500 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 501 | bool CJS_Date::IsValidDate(CJS_Runtime* pRuntime) const { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 502 | return !m_pDate.IsEmpty() && !std::isnan(pRuntime->ToDouble(m_pDate)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 503 | } |
| 504 | |
tsepez | 135b998 | 2016-08-05 09:32:50 -0700 | [diff] [blame] | 505 | void CJS_Date::Attach(v8::Local<v8::Date> pDate) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 506 | m_pDate = pDate; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 507 | } |
| 508 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 509 | int CJS_Date::GetYear(CJS_Runtime* pRuntime) const { |
| 510 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 511 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 512 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 513 | return JS_GetYearFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 514 | } |
| 515 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 516 | void CJS_Date::SetYear(CJS_Runtime* pRuntime, int iYear) { |
| 517 | m_pDate = pRuntime->NewDate( |
| 518 | MakeDate(iYear, GetMonth(pRuntime), GetDay(pRuntime), GetHours(pRuntime), |
| 519 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 520 | } |
| 521 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 522 | int CJS_Date::GetMonth(CJS_Runtime* pRuntime) const { |
| 523 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 524 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 525 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 526 | return JS_GetMonthFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 527 | } |
| 528 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 529 | void CJS_Date::SetMonth(CJS_Runtime* pRuntime, int iMonth) { |
| 530 | m_pDate = pRuntime->NewDate( |
| 531 | MakeDate(GetYear(pRuntime), iMonth, GetDay(pRuntime), GetHours(pRuntime), |
| 532 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 533 | } |
| 534 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 535 | int CJS_Date::GetDay(CJS_Runtime* pRuntime) const { |
| 536 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 537 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 538 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 539 | return JS_GetDayFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 540 | } |
| 541 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 542 | void CJS_Date::SetDay(CJS_Runtime* pRuntime, int iDay) { |
| 543 | m_pDate = pRuntime->NewDate( |
| 544 | MakeDate(GetYear(pRuntime), GetMonth(pRuntime), iDay, GetHours(pRuntime), |
| 545 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
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 | int CJS_Date::GetHours(CJS_Runtime* pRuntime) const { |
| 549 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 550 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 551 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 552 | return JS_GetHourFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 553 | } |
| 554 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 555 | void CJS_Date::SetHours(CJS_Runtime* pRuntime, int iHours) { |
| 556 | m_pDate = pRuntime->NewDate( |
| 557 | MakeDate(GetYear(pRuntime), GetMonth(pRuntime), GetDay(pRuntime), iHours, |
| 558 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 559 | } |
| 560 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 561 | int CJS_Date::GetMinutes(CJS_Runtime* pRuntime) const { |
| 562 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 563 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 564 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 565 | return JS_GetMinFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 566 | } |
| 567 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 568 | void CJS_Date::SetMinutes(CJS_Runtime* pRuntime, int minutes) { |
| 569 | m_pDate = pRuntime->NewDate(MakeDate(GetYear(pRuntime), GetMonth(pRuntime), |
| 570 | GetDay(pRuntime), GetHours(pRuntime), |
| 571 | minutes, GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 572 | } |
| 573 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 574 | int CJS_Date::GetSeconds(CJS_Runtime* pRuntime) const { |
| 575 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 576 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 577 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 578 | return JS_GetSecFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 579 | } |
| 580 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 581 | void CJS_Date::SetSeconds(CJS_Runtime* pRuntime, int seconds) { |
| 582 | m_pDate = pRuntime->NewDate(MakeDate(GetYear(pRuntime), GetMonth(pRuntime), |
| 583 | GetDay(pRuntime), GetHours(pRuntime), |
| 584 | GetMinutes(pRuntime), seconds, 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 585 | } |
| 586 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 587 | double CJS_Date::ToDouble(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 588 | return !m_pDate.IsEmpty() ? pRuntime->ToDouble(m_pDate) : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 589 | } |
| 590 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 591 | CFX_WideString CJS_Date::ToString(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 592 | return !m_pDate.IsEmpty() ? pRuntime->ToWideString(m_pDate) |
| 593 | : CFX_WideString(); |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 594 | } |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 595 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 596 | v8::Local<v8::Date> CJS_Date::ToV8Date(CJS_Runtime* pRuntime) const { |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 597 | return m_pDate; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 600 | double JS_GetDateTime() { |
| 601 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 602 | return 0; |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 603 | time_t t = time(nullptr); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 604 | struct tm* pTm = localtime(&t); |
| 605 | |
| 606 | int year = pTm->tm_year + 1900; |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 607 | double t1 = TimeFromYear(year); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 608 | |
| 609 | return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + |
| 610 | pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; |
| 611 | } |
| 612 | |
| 613 | int JS_GetYearFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 614 | return YearFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | int JS_GetMonthFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 618 | return MonthFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | int JS_GetDayFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 622 | return DateFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | int JS_GetHourFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 626 | return (int)Mod(floor(dt / (60 * 60 * 1000)), 24); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | int JS_GetMinFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 630 | return (int)Mod(floor(dt / (60 * 1000)), 60); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | int JS_GetSecFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 634 | return (int)Mod(floor(dt / 1000), 60); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 635 | } |
| 636 | |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 637 | double JS_DateParse(const CFX_WideString& str) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 638 | v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); |
| 639 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 640 | v8::HandleScope scope(pIsolate); |
| 641 | |
| 642 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 643 | |
| 644 | // Use the built-in object method. |
| 645 | v8::Local<v8::Value> v = |
| 646 | context->Global() |
| 647 | ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", |
| 648 | v8::NewStringType::kNormal) |
| 649 | .ToLocalChecked()) |
| 650 | .ToLocalChecked(); |
| 651 | if (v->IsObject()) { |
| 652 | v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked(); |
| 653 | v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", |
| 654 | v8::NewStringType::kNormal) |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 655 | .ToLocalChecked()) |
| 656 | .ToLocalChecked(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 657 | if (v->IsFunction()) { |
| 658 | v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 659 | const int argc = 1; |
Tom Sepez | c6dc69f | 2017-02-23 09:53:09 -0800 | [diff] [blame] | 660 | v8::Local<v8::Value> timeStr = |
| 661 | CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString( |
| 662 | str.AsStringC()); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 663 | v8::Local<v8::Value> argv[argc] = {timeStr}; |
| 664 | v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); |
| 665 | if (v->IsNumber()) { |
| 666 | double date = v->ToNumber(context).ToLocalChecked()->Value(); |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 667 | if (!std::isfinite(date)) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 668 | return date; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 669 | return JS_LocalTime(date); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | } |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | double JS_MakeDay(int nYear, int nMonth, int nDate) { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 677 | double y = static_cast<double>(nYear); |
| 678 | double m = static_cast<double>(nMonth); |
| 679 | double dt = static_cast<double>(nDate); |
| 680 | double ym = y + floor(m / 12); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 681 | double mn = Mod(m, 12); |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 682 | double t = TimeFromYearMonth(static_cast<int>(ym), static_cast<int>(mn)); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 683 | if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1) |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 684 | return std::nan(""); |
| 685 | |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 686 | return Day(t) + dt - 1; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 690 | double h = static_cast<double>(nHour); |
| 691 | double m = static_cast<double>(nMin); |
| 692 | double s = static_cast<double>(nSec); |
| 693 | double milli = static_cast<double>(nMs); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 694 | return h * 3600000 + m * 60000 + s * 1000 + milli; |
| 695 | } |
| 696 | |
| 697 | double JS_MakeDate(double day, double time) { |
Tom Sepez | df950b8 | 2017-08-04 11:33:49 -0700 | [diff] [blame] | 698 | if (!std::isfinite(day) || !std::isfinite(time)) |
| 699 | return std::nan(""); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 700 | |
| 701 | return day * 86400000 + time; |
| 702 | } |
| 703 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 704 | double JS_LocalTime(double d) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 705 | return d + GetLocalTZA() + GetDaylightSavingTA(d); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 706 | } |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 707 | |
| 708 | std::vector<CJS_Value> JS_ExpandKeywordParams( |
| 709 | CJS_Runtime* pRuntime, |
| 710 | const std::vector<CJS_Value>& originals, |
| 711 | size_t nKeywords, |
| 712 | ...) { |
| 713 | ASSERT(nKeywords); |
| 714 | |
| 715 | std::vector<CJS_Value> result(nKeywords, CJS_Value(pRuntime)); |
| 716 | size_t size = std::min(originals.size(), nKeywords); |
| 717 | for (size_t i = 0; i < size; ++i) |
| 718 | result[i] = originals[i]; |
| 719 | |
| 720 | if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object || |
| 721 | originals[0].IsArrayObject()) { |
| 722 | return result; |
| 723 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 724 | v8::Local<v8::Object> pObj = originals[0].ToV8Object(pRuntime); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 725 | result[0] = CJS_Value(pRuntime); // Make unknown. |
| 726 | |
| 727 | va_list ap; |
| 728 | va_start(ap, nKeywords); |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 729 | for (size_t i = 0; i < nKeywords; ++i) { |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 730 | const wchar_t* property = va_arg(ap, const wchar_t*); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 731 | v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 732 | if (!v8Value->IsUndefined()) |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 733 | result[i] = CJS_Value(pRuntime, v8Value); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 734 | } |
| 735 | va_end(ap); |
| 736 | return result; |
| 737 | } |