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