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 | |
| 22 | const uint32_t g_nan[2] = {0, 0x7FF80000}; |
| 23 | |
| 24 | double GetNan() { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 25 | return *(double*)g_nan; |
| 26 | } |
| 27 | |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 28 | double |
| 29 | MakeDate(int year, int mon, int day, int hour, int min, int sec, int ms) { |
| 30 | return JS_MakeDate(JS_MakeDay(year, mon, day), |
| 31 | JS_MakeTime(hour, min, sec, ms)); |
| 32 | } |
| 33 | |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 34 | double GetLocalTZA() { |
| 35 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 36 | return 0; |
| 37 | time_t t = 0; |
| 38 | time(&t); |
| 39 | localtime(&t); |
Tom Sepez | a2da7c5 | 2017-08-03 13:34:08 -0700 | [diff] [blame] | 40 | #if defined(_MSC_VER) |
| 41 | // In gcc 'timezone' is a global variable declared in time.h. In VC++, that |
| 42 | // variable was removed in VC++ 2015, with _get_timezone replacing it. |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 43 | long timezone = 0; |
| 44 | _get_timezone(&timezone); |
| 45 | #endif |
| 46 | return (double)(-(timezone * 1000)); |
| 47 | } |
| 48 | |
| 49 | int GetDaylightSavingTA(double d) { |
| 50 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 51 | return 0; |
| 52 | time_t t = (time_t)(d / 1000); |
| 53 | struct tm* tmp = localtime(&t); |
| 54 | if (!tmp) |
| 55 | return 0; |
| 56 | if (tmp->tm_isdst > 0) |
| 57 | // One hour. |
| 58 | return (int)60 * 60 * 1000; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | double Mod(double x, double y) { |
| 63 | double r = fmod(x, y); |
| 64 | if (r < 0) |
| 65 | r += y; |
| 66 | return r; |
| 67 | } |
| 68 | |
| 69 | int IsFinite(double v) { |
Tom Sepez | a2da7c5 | 2017-08-03 13:34:08 -0700 | [diff] [blame] | 70 | #if defined(_MSC_VER) |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 71 | return ::_finite(v); |
| 72 | #else |
| 73 | return std::fabs(v) < std::numeric_limits<double>::max(); |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | double ToInteger(double n) { |
| 78 | return (n >= 0) ? floor(n) : -floor(-n); |
| 79 | } |
| 80 | |
| 81 | bool IsLeapYear(int year) { |
| 82 | return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); |
| 83 | } |
| 84 | |
| 85 | int DayFromYear(int y) { |
| 86 | return (int)(365 * (y - 1970.0) + floor((y - 1969.0) / 4) - |
| 87 | floor((y - 1901.0) / 100) + floor((y - 1601.0) / 400)); |
| 88 | } |
| 89 | |
| 90 | double TimeFromYear(int y) { |
| 91 | return 86400000.0 * DayFromYear(y); |
| 92 | } |
| 93 | |
| 94 | static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151, |
| 95 | 181, 212, 243, 273, 304, 334}; |
| 96 | static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152, |
| 97 | 182, 213, 244, 274, 305, 335}; |
| 98 | |
| 99 | double TimeFromYearMonth(int y, int m) { |
| 100 | const uint16_t* pMonth = IsLeapYear(y) ? leapDaysMonth : daysMonth; |
| 101 | return TimeFromYear(y) + ((double)pMonth[m]) * 86400000; |
| 102 | } |
| 103 | |
| 104 | int Day(double t) { |
| 105 | return (int)floor(t / 86400000); |
| 106 | } |
| 107 | |
| 108 | int YearFromTime(double t) { |
| 109 | // estimate the time. |
| 110 | int y = 1970 + static_cast<int>(t / (365.2425 * 86400000)); |
| 111 | if (TimeFromYear(y) <= t) { |
| 112 | while (TimeFromYear(y + 1) <= t) |
| 113 | y++; |
| 114 | } else { |
| 115 | while (TimeFromYear(y) > t) |
| 116 | y--; |
| 117 | } |
| 118 | return y; |
| 119 | } |
| 120 | |
| 121 | int DayWithinYear(double t) { |
| 122 | int year = YearFromTime(t); |
| 123 | int day = Day(t); |
| 124 | return day - DayFromYear(year); |
| 125 | } |
| 126 | |
| 127 | int MonthFromTime(double t) { |
| 128 | int day = DayWithinYear(t); |
| 129 | int year = YearFromTime(t); |
| 130 | if (0 <= day && day < 31) |
| 131 | return 0; |
| 132 | if (31 <= day && day < 59 + IsLeapYear(year)) |
| 133 | return 1; |
| 134 | if ((59 + IsLeapYear(year)) <= day && day < (90 + IsLeapYear(year))) |
| 135 | return 2; |
| 136 | if ((90 + IsLeapYear(year)) <= day && day < (120 + IsLeapYear(year))) |
| 137 | return 3; |
| 138 | if ((120 + IsLeapYear(year)) <= day && day < (151 + IsLeapYear(year))) |
| 139 | return 4; |
| 140 | if ((151 + IsLeapYear(year)) <= day && day < (181 + IsLeapYear(year))) |
| 141 | return 5; |
| 142 | if ((181 + IsLeapYear(year)) <= day && day < (212 + IsLeapYear(year))) |
| 143 | return 6; |
| 144 | if ((212 + IsLeapYear(year)) <= day && day < (243 + IsLeapYear(year))) |
| 145 | return 7; |
| 146 | if ((243 + IsLeapYear(year)) <= day && day < (273 + IsLeapYear(year))) |
| 147 | return 8; |
| 148 | if ((273 + IsLeapYear(year)) <= day && day < (304 + IsLeapYear(year))) |
| 149 | return 9; |
| 150 | if ((304 + IsLeapYear(year)) <= day && day < (334 + IsLeapYear(year))) |
| 151 | return 10; |
| 152 | if ((334 + IsLeapYear(year)) <= day && day < (365 + IsLeapYear(year))) |
| 153 | return 11; |
| 154 | |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | int DateFromTime(double t) { |
| 159 | int day = DayWithinYear(t); |
| 160 | int year = YearFromTime(t); |
| 161 | int leap = IsLeapYear(year); |
| 162 | int month = MonthFromTime(t); |
| 163 | switch (month) { |
| 164 | case 0: |
| 165 | return day + 1; |
| 166 | case 1: |
| 167 | return day - 30; |
| 168 | case 2: |
| 169 | return day - 58 - leap; |
| 170 | case 3: |
| 171 | return day - 89 - leap; |
| 172 | case 4: |
| 173 | return day - 119 - leap; |
| 174 | case 5: |
| 175 | return day - 150 - leap; |
| 176 | case 6: |
| 177 | return day - 180 - leap; |
| 178 | case 7: |
| 179 | return day - 211 - leap; |
| 180 | case 8: |
| 181 | return day - 242 - leap; |
| 182 | case 9: |
| 183 | return day - 272 - leap; |
| 184 | case 10: |
| 185 | return day - 303 - leap; |
| 186 | case 11: |
| 187 | return day - 333 - leap; |
| 188 | default: |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 | |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 193 | } // namespace |
| 194 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 195 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime) {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 197 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue) |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 198 | : m_pValue(pValue) {} |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 199 | |
| 200 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 201 | : m_pValue(pRuntime->NewNumber(iValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 202 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 203 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const bool& bValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 204 | : m_pValue(pRuntime->NewBoolean(bValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 205 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 206 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const float& fValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 207 | : m_pValue(pRuntime->NewNumber(fValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 208 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 209 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const double& dValue) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 210 | : m_pValue(pRuntime->NewNumber(dValue)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 211 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 212 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pObj) { |
| 213 | if (pObj) |
| 214 | m_pValue = pObj->ToV8Object(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 217 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const wchar_t* pWstr) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 218 | : m_pValue(pRuntime->NewString(pWstr)) {} |
Tom Sepez | f79a69c | 2014-10-30 13:23:42 -0700 | [diff] [blame] | 219 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 220 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const char* pStr) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 221 | : m_pValue(pRuntime->NewString(CFX_WideString::FromLocal(pStr).c_str())) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 222 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 223 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 224 | : m_pValue(array.ToV8Array(pRuntime)) {} |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 225 | |
| 226 | CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const CJS_Date& date) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 227 | : m_pValue(date.ToV8Date(pRuntime)) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 228 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 229 | CJS_Value::~CJS_Value() {} |
| 230 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 231 | CJS_Value::CJS_Value(const CJS_Value& other) = default; |
| 232 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 233 | void CJS_Value::Attach(v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 234 | m_pValue = pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | void CJS_Value::Detach() { |
| 238 | m_pValue = v8::Local<v8::Value>(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 239 | } |
| 240 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 241 | int CJS_Value::ToInt(CJS_Runtime* pRuntime) const { |
| 242 | return pRuntime->ToInt32(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 243 | } |
| 244 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 245 | bool CJS_Value::ToBool(CJS_Runtime* pRuntime) const { |
| 246 | return pRuntime->ToBoolean(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | } |
| 248 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 249 | double CJS_Value::ToDouble(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 250 | return pRuntime->ToDouble(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 251 | } |
| 252 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 253 | float CJS_Value::ToFloat(CJS_Runtime* pRuntime) const { |
| 254 | return (float)ToDouble(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 255 | } |
| 256 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 257 | CJS_Object* CJS_Value::ToCJSObject(CJS_Runtime* pRuntime) const { |
| 258 | v8::Local<v8::Object> pObj = pRuntime->ToObject(m_pValue); |
| 259 | return static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(pObj)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 260 | } |
| 261 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 262 | v8::Local<v8::Object> CJS_Value::ToV8Object(CJS_Runtime* pRuntime) const { |
| 263 | return pRuntime->ToObject(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 264 | } |
| 265 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 266 | CFX_WideString CJS_Value::ToCFXWideString(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 267 | return pRuntime->ToWideString(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 268 | } |
| 269 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 270 | CFX_ByteString CJS_Value::ToCFXByteString(CJS_Runtime* pRuntime) const { |
| 271 | return CFX_ByteString::FromUnicode(ToCFXWideString(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 272 | } |
| 273 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 274 | v8::Local<v8::Value> CJS_Value::ToV8Value(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 275 | return m_pValue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 276 | } |
| 277 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 278 | v8::Local<v8::Array> CJS_Value::ToV8Array(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 279 | return pRuntime->ToArray(m_pValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 280 | } |
| 281 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 282 | void CJS_Value::SetNull(CJS_Runtime* pRuntime) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 283 | m_pValue = pRuntime->NewNull(); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 284 | } |
| 285 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 286 | void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) { |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 287 | bool bAllowNaN = false; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 288 | if (GetType() == VT_string) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 289 | CFX_ByteString bstr = ToCFXByteString(pRuntime); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 290 | if (bstr.GetLength() == 0) |
| 291 | return; |
| 292 | if (bstr == "NaN") |
| 293 | bAllowNaN = true; |
| 294 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 295 | v8::Isolate* pIsolate = pRuntime->GetIsolate(); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 296 | v8::TryCatch try_catch(pIsolate); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 297 | v8::MaybeLocal<v8::Number> maybeNum = |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 298 | m_pValue->ToNumber(pIsolate->GetCurrentContext()); |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 299 | if (maybeNum.IsEmpty()) |
| 300 | return; |
| 301 | v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); |
| 302 | if (std::isnan(num->Value()) && !bAllowNaN) |
| 303 | return; |
| 304 | m_pValue = num; |
Tom Sepez | 4246b00 | 2016-01-20 11:48:29 -0800 | [diff] [blame] | 305 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 307 | // static |
| 308 | CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) { |
| 309 | if (value.IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | return VT_unknown; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 311 | if (value->IsString()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | return VT_string; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 313 | if (value->IsNumber()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | return VT_number; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 315 | if (value->IsBoolean()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 316 | return VT_boolean; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 317 | if (value->IsDate()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 318 | return VT_date; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 319 | if (value->IsObject()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | return VT_object; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 321 | if (value->IsNull()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | return VT_null; |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 323 | if (value->IsUndefined()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 324 | return VT_undefined; |
| 325 | return VT_unknown; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 326 | } |
| 327 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 328 | bool CJS_Value::IsArrayObject() const { |
| 329 | return !m_pValue.IsEmpty() && m_pValue->IsArray(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 330 | } |
| 331 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 332 | bool CJS_Value::IsDateObject() const { |
| 333 | return !m_pValue.IsEmpty() && m_pValue->IsDate(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 334 | } |
| 335 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 336 | bool CJS_Value::ConvertToArray(CJS_Runtime* pRuntime, CJS_Array& array) const { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 337 | if (!IsArrayObject()) |
| 338 | return false; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 339 | array.Attach(pRuntime->ToArray(m_pValue)); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 340 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 341 | } |
| 342 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 343 | bool CJS_Value::ConvertToDate(CJS_Runtime* pRuntime, CJS_Date& date) const { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 344 | if (!IsDateObject()) |
| 345 | return false; |
| 346 | v8::Local<v8::Value> mutable_value = m_pValue; |
| 347 | date.Attach(mutable_value.As<v8::Date>()); |
| 348 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 351 | CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime) |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 352 | : m_bIsSetting(0), m_Value(pRuntime), m_pJSRuntime(pRuntime) {} |
| 353 | |
| 354 | CJS_PropValue::CJS_PropValue(CJS_Runtime* pRuntime, const CJS_Value& value) |
| 355 | : m_bIsSetting(0), m_Value(value), m_pJSRuntime(pRuntime) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 356 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 357 | CJS_PropValue::~CJS_PropValue() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 358 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 359 | void CJS_PropValue::operator<<(int iValue) { |
| 360 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 361 | m_Value = CJS_Value(m_pJSRuntime.Get(), iValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 364 | void CJS_PropValue::operator>>(int& iValue) const { |
| 365 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 366 | iValue = m_Value.ToInt(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 369 | void CJS_PropValue::operator<<(bool bValue) { |
| 370 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 371 | m_Value = CJS_Value(m_pJSRuntime.Get(), bValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 374 | void CJS_PropValue::operator>>(bool& bValue) const { |
| 375 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 376 | bValue = m_Value.ToBool(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | void CJS_PropValue::operator<<(double dValue) { |
| 380 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 381 | m_Value = CJS_Value(m_pJSRuntime.Get(), dValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | void CJS_PropValue::operator>>(double& dValue) const { |
| 385 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 386 | dValue = m_Value.ToDouble(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | void CJS_PropValue::operator<<(CJS_Object* pObj) { |
| 390 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 391 | m_Value = CJS_Value(m_pJSRuntime.Get(), pObj); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 394 | void CJS_PropValue::operator>>(CJS_Object*& ppObj) const { |
| 395 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 396 | ppObj = m_Value.ToCJSObject(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 399 | void CJS_PropValue::operator<<(CJS_Document* pJsDoc) { |
| 400 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 401 | m_Value = CJS_Value(m_pJSRuntime.Get(), pJsDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 404 | void CJS_PropValue::operator>>(CJS_Document*& ppJsDoc) const { |
| 405 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 406 | 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] | 407 | } |
| 408 | |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 409 | void CJS_PropValue::operator<<(v8::Local<v8::Object> pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 411 | m_Value = CJS_Value(m_pJSRuntime.Get(), pObj); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 414 | void CJS_PropValue::operator>>(v8::Local<v8::Object>& ppObj) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 415 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 416 | ppObj = m_Value.ToV8Object(m_pJSRuntime.Get()); |
JUN FANG | 33f6f0d | 2015-04-06 12:39:51 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 419 | void CJS_PropValue::operator<<(CFX_ByteString str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 420 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 421 | m_Value = CJS_Value(m_pJSRuntime.Get(), str.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 424 | void CJS_PropValue::operator>>(CFX_ByteString& str) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 425 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 426 | str = m_Value.ToCFXByteString(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 429 | void CJS_PropValue::operator<<(const wchar_t* str) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 431 | m_Value = CJS_Value(m_pJSRuntime.Get(), str); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 434 | void CJS_PropValue::operator>>(CFX_WideString& wide_string) const { |
| 435 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 436 | wide_string = m_Value.ToCFXWideString(m_pJSRuntime.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | void CJS_PropValue::operator<<(CFX_WideString wide_string) { |
| 440 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 441 | 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] | 442 | } |
| 443 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 444 | void CJS_PropValue::operator>>(CJS_Array& array) const { |
| 445 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 446 | m_Value.ConvertToArray(m_pJSRuntime.Get(), array); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 449 | void CJS_PropValue::operator<<(CJS_Array& array) { |
| 450 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 451 | 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] | 452 | } |
| 453 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 454 | void CJS_PropValue::operator>>(CJS_Date& date) const { |
| 455 | ASSERT(m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 456 | m_Value.ConvertToDate(m_pJSRuntime.Get(), date); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | void CJS_PropValue::operator<<(CJS_Date& date) { |
| 460 | ASSERT(!m_bIsSetting); |
Tom Sepez | 797ca5c | 2017-05-25 12:03:18 -0700 | [diff] [blame] | 461 | m_Value = CJS_Value(m_pJSRuntime.Get(), date); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 462 | } |
| 463 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 464 | CJS_Array::CJS_Array() {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 465 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 466 | CJS_Array::CJS_Array(const CJS_Array& other) = default; |
| 467 | |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 468 | CJS_Array::~CJS_Array() {} |
| 469 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 470 | void CJS_Array::Attach(v8::Local<v8::Array> pArray) { |
| 471 | m_pArray = pArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 472 | } |
| 473 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 474 | void CJS_Array::GetElement(CJS_Runtime* pRuntime, |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 475 | unsigned index, |
| 476 | CJS_Value& value) const { |
| 477 | if (!m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 478 | value.Attach(pRuntime->GetArrayElement(m_pArray, index)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 479 | } |
| 480 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 481 | void CJS_Array::SetElement(CJS_Runtime* pRuntime, |
tsepez | e5aff74 | 2016-08-08 09:49:42 -0700 | [diff] [blame] | 482 | unsigned index, |
| 483 | const CJS_Value& value) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 484 | if (m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 485 | m_pArray = pRuntime->NewArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 486 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 487 | pRuntime->PutArrayElement(m_pArray, index, value.ToV8Value(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 488 | } |
| 489 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 490 | int CJS_Array::GetLength(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 491 | if (m_pArray.IsEmpty()) |
| 492 | return 0; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 493 | return pRuntime->GetArrayLength(m_pArray); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 494 | } |
| 495 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 496 | v8::Local<v8::Array> CJS_Array::ToV8Array(CJS_Runtime* pRuntime) const { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 497 | if (m_pArray.IsEmpty()) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 498 | m_pArray = pRuntime->NewArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 499 | |
| 500 | return m_pArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 501 | } |
| 502 | |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 503 | CJS_Date::CJS_Date() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 504 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 505 | CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime) |
| 506 | : m_pDate(pRuntime->NewDate(dMsecTime)) {} |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 507 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 508 | CJS_Date::CJS_Date(CJS_Runtime* pRuntime, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 509 | int year, |
| 510 | int mon, |
| 511 | int day, |
| 512 | int hour, |
| 513 | int min, |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 514 | int sec) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 515 | : 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] | 516 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 517 | CJS_Date::~CJS_Date() {} |
| 518 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 519 | bool CJS_Date::IsValidDate(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 520 | return !m_pDate.IsEmpty() && !JS_PortIsNan(pRuntime->ToDouble(m_pDate)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 521 | } |
| 522 | |
tsepez | 135b998 | 2016-08-05 09:32:50 -0700 | [diff] [blame] | 523 | void CJS_Date::Attach(v8::Local<v8::Date> pDate) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 524 | m_pDate = 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::GetYear(CJS_Runtime* pRuntime) const { |
| 528 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 529 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 530 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 531 | return JS_GetYearFromTime(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 | void CJS_Date::SetYear(CJS_Runtime* pRuntime, int iYear) { |
| 535 | m_pDate = pRuntime->NewDate( |
| 536 | MakeDate(iYear, GetMonth(pRuntime), GetDay(pRuntime), GetHours(pRuntime), |
| 537 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 538 | } |
| 539 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 540 | int CJS_Date::GetMonth(CJS_Runtime* pRuntime) const { |
| 541 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 542 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 543 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 544 | return JS_GetMonthFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 545 | } |
| 546 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 547 | void CJS_Date::SetMonth(CJS_Runtime* pRuntime, int iMonth) { |
| 548 | m_pDate = pRuntime->NewDate( |
| 549 | MakeDate(GetYear(pRuntime), iMonth, GetDay(pRuntime), GetHours(pRuntime), |
| 550 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 551 | } |
| 552 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 553 | int CJS_Date::GetDay(CJS_Runtime* pRuntime) const { |
| 554 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 555 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 556 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 557 | return JS_GetDayFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 558 | } |
| 559 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 560 | void CJS_Date::SetDay(CJS_Runtime* pRuntime, int iDay) { |
| 561 | m_pDate = pRuntime->NewDate( |
| 562 | MakeDate(GetYear(pRuntime), GetMonth(pRuntime), iDay, GetHours(pRuntime), |
| 563 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 564 | } |
| 565 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 566 | int CJS_Date::GetHours(CJS_Runtime* pRuntime) const { |
| 567 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 568 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 569 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 570 | return JS_GetHourFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 571 | } |
| 572 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 573 | void CJS_Date::SetHours(CJS_Runtime* pRuntime, int iHours) { |
| 574 | m_pDate = pRuntime->NewDate( |
| 575 | MakeDate(GetYear(pRuntime), GetMonth(pRuntime), GetDay(pRuntime), iHours, |
| 576 | GetMinutes(pRuntime), GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 577 | } |
| 578 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 579 | int CJS_Date::GetMinutes(CJS_Runtime* pRuntime) const { |
| 580 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 581 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 582 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 583 | return JS_GetMinFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 584 | } |
| 585 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 586 | void CJS_Date::SetMinutes(CJS_Runtime* pRuntime, int minutes) { |
| 587 | m_pDate = pRuntime->NewDate(MakeDate(GetYear(pRuntime), GetMonth(pRuntime), |
| 588 | GetDay(pRuntime), GetHours(pRuntime), |
| 589 | minutes, GetSeconds(pRuntime), 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 590 | } |
| 591 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 592 | int CJS_Date::GetSeconds(CJS_Runtime* pRuntime) const { |
| 593 | if (!IsValidDate(pRuntime)) |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 594 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 595 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 596 | return JS_GetSecFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 597 | } |
| 598 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 599 | void CJS_Date::SetSeconds(CJS_Runtime* pRuntime, int seconds) { |
| 600 | m_pDate = pRuntime->NewDate(MakeDate(GetYear(pRuntime), GetMonth(pRuntime), |
| 601 | GetDay(pRuntime), GetHours(pRuntime), |
| 602 | GetMinutes(pRuntime), seconds, 0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 603 | } |
| 604 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 605 | double CJS_Date::ToDouble(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 606 | return !m_pDate.IsEmpty() ? pRuntime->ToDouble(m_pDate) : 0.0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 607 | } |
| 608 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 609 | CFX_WideString CJS_Date::ToString(CJS_Runtime* pRuntime) const { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 610 | return !m_pDate.IsEmpty() ? pRuntime->ToWideString(m_pDate) |
| 611 | : CFX_WideString(); |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 612 | } |
tsepez | fbf52c2 | 2016-07-25 11:17:07 -0700 | [diff] [blame] | 613 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 614 | v8::Local<v8::Date> CJS_Date::ToV8Date(CJS_Runtime* pRuntime) const { |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 615 | return m_pDate; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 618 | double JS_GetDateTime() { |
| 619 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 620 | return 0; |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 621 | time_t t = time(nullptr); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 622 | struct tm* pTm = localtime(&t); |
| 623 | |
| 624 | int year = pTm->tm_year + 1900; |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 625 | double t1 = TimeFromYear(year); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 626 | |
| 627 | return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + |
| 628 | pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; |
| 629 | } |
| 630 | |
| 631 | int JS_GetYearFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 632 | return YearFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | int JS_GetMonthFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 636 | return MonthFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | int JS_GetDayFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 640 | return DateFromTime(dt); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | int JS_GetHourFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 644 | return (int)Mod(floor(dt / (60 * 60 * 1000)), 24); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | int JS_GetMinFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 648 | return (int)Mod(floor(dt / (60 * 1000)), 60); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | int JS_GetSecFromTime(double dt) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 652 | return (int)Mod(floor(dt / 1000), 60); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 653 | } |
| 654 | |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 655 | double JS_DateParse(const CFX_WideString& str) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 656 | v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); |
| 657 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 658 | v8::HandleScope scope(pIsolate); |
| 659 | |
| 660 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 661 | |
| 662 | // Use the built-in object method. |
| 663 | v8::Local<v8::Value> v = |
| 664 | context->Global() |
| 665 | ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", |
| 666 | v8::NewStringType::kNormal) |
| 667 | .ToLocalChecked()) |
| 668 | .ToLocalChecked(); |
| 669 | if (v->IsObject()) { |
| 670 | v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked(); |
| 671 | v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", |
| 672 | v8::NewStringType::kNormal) |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 673 | .ToLocalChecked()) |
| 674 | .ToLocalChecked(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 675 | if (v->IsFunction()) { |
| 676 | v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 677 | const int argc = 1; |
Tom Sepez | c6dc69f | 2017-02-23 09:53:09 -0800 | [diff] [blame] | 678 | v8::Local<v8::Value> timeStr = |
| 679 | CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString( |
| 680 | str.AsStringC()); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 681 | v8::Local<v8::Value> argv[argc] = {timeStr}; |
| 682 | v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); |
| 683 | if (v->IsNumber()) { |
| 684 | double date = v->ToNumber(context).ToLocalChecked()->Value(); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 685 | if (!IsFinite(date)) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 686 | return date; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 687 | return JS_LocalTime(date); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | } |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | double JS_MakeDay(int nYear, int nMonth, int nDate) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 695 | if (!IsFinite(nYear) || !IsFinite(nMonth) || !IsFinite(nDate)) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 696 | return GetNan(); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 697 | double y = ToInteger(nYear); |
| 698 | double m = ToInteger(nMonth); |
| 699 | double dt = ToInteger(nDate); |
Dan Sinclair | 669a418 | 2017-04-03 14:51:45 -0400 | [diff] [blame] | 700 | double ym = y + floor((double)m / 12); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 701 | double mn = Mod(m, 12); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 702 | |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 703 | double t = TimeFromYearMonth((int)ym, (int)mn); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 704 | |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 705 | if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 706 | return GetNan(); |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 707 | return Day(t) + dt - 1; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 711 | if (!IsFinite(nHour) || !IsFinite(nMin) || !IsFinite(nSec) || !IsFinite(nMs)) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 712 | return GetNan(); |
| 713 | |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 714 | double h = ToInteger(nHour); |
| 715 | double m = ToInteger(nMin); |
| 716 | double s = ToInteger(nSec); |
| 717 | double milli = ToInteger(nMs); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 718 | |
| 719 | return h * 3600000 + m * 60000 + s * 1000 + milli; |
| 720 | } |
| 721 | |
| 722 | double JS_MakeDate(double day, double time) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 723 | if (!IsFinite(day) || !IsFinite(time)) |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 724 | return GetNan(); |
| 725 | |
| 726 | return day * 86400000 + time; |
| 727 | } |
| 728 | |
| 729 | bool JS_PortIsNan(double d) { |
| 730 | return d != d; |
| 731 | } |
| 732 | |
| 733 | double JS_LocalTime(double d) { |
Lei Zhang | ab5939e | 2017-06-16 02:23:18 -0700 | [diff] [blame] | 734 | return d + GetLocalTZA() + GetDaylightSavingTA(d); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 735 | } |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 736 | |
| 737 | std::vector<CJS_Value> JS_ExpandKeywordParams( |
| 738 | CJS_Runtime* pRuntime, |
| 739 | const std::vector<CJS_Value>& originals, |
| 740 | size_t nKeywords, |
| 741 | ...) { |
| 742 | ASSERT(nKeywords); |
| 743 | |
| 744 | std::vector<CJS_Value> result(nKeywords, CJS_Value(pRuntime)); |
| 745 | size_t size = std::min(originals.size(), nKeywords); |
| 746 | for (size_t i = 0; i < size; ++i) |
| 747 | result[i] = originals[i]; |
| 748 | |
| 749 | if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object || |
| 750 | originals[0].IsArrayObject()) { |
| 751 | return result; |
| 752 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 753 | v8::Local<v8::Object> pObj = originals[0].ToV8Object(pRuntime); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 754 | result[0] = CJS_Value(pRuntime); // Make unknown. |
| 755 | |
| 756 | va_list ap; |
| 757 | va_start(ap, nKeywords); |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 758 | for (size_t i = 0; i < nKeywords; ++i) { |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 759 | const wchar_t* property = va_arg(ap, const wchar_t*); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 760 | v8::Local<v8::Value> v8Value = pRuntime->GetObjectProperty(pObj, property); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 761 | if (!v8Value->IsUndefined()) |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 762 | result[i] = CJS_Value(pRuntime, v8Value); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 763 | } |
| 764 | va_end(ap); |
| 765 | return result; |
| 766 | } |