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