OLD | NEW |
---|---|
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "fpdfsdk/javascript/JS_Value.h" | 7 #include "fpdfsdk/javascript/JS_Value.h" |
8 | 8 |
9 #include <time.h> | 9 #include <time.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <cmath> | 12 #include <cmath> |
13 #include <limits> | 13 #include <limits> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "fpdfsdk/javascript/Document.h" | 16 #include "fpdfsdk/javascript/Document.h" |
17 #include "fpdfsdk/javascript/JS_Define.h" | 17 #include "fpdfsdk/javascript/JS_Define.h" |
18 #include "fpdfsdk/javascript/JS_Object.h" | 18 #include "fpdfsdk/javascript/JS_Object.h" |
19 | 19 |
20 static const uint32_t g_nan[2] = {0, 0x7FF80000}; | 20 static const uint32_t g_nan[2] = {0, 0x7FF80000}; |
21 static double GetNan() { | 21 static double GetNan() { |
22 return *(double*)g_nan; | 22 return *(double*)g_nan; |
23 } | 23 } |
24 | 24 |
25 CJS_Value::CJS_Value(CJS_Runtime* pRuntime) | 25 CJS_Value::CJS_Value(CJS_Runtime* pRuntime) : m_pJSRuntime(pRuntime) {} |
26 : m_eType(VT_unknown), m_pJSRuntime(pRuntime) {} | |
27 | 26 |
28 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t) | 27 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue) |
29 : m_eType(t), m_pValue(pValue), m_pJSRuntime(pRuntime) {} | 28 : m_pValue(pValue), m_pJSRuntime(pRuntime) {} |
30 | 29 |
31 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) | 30 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const int& iValue) |
32 : m_pJSRuntime(pRuntime) { | 31 : m_pJSRuntime(pRuntime) { |
33 operator=(iValue); | 32 operator=(iValue); |
34 } | 33 } |
35 | 34 |
36 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const bool& bValue) | 35 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const bool& bValue) |
37 : m_pJSRuntime(pRuntime) { | 36 : m_pJSRuntime(pRuntime) { |
38 operator=(bValue); | 37 operator=(bValue); |
39 } | 38 } |
(...skipping 13 matching lines...) Expand all Loading... | |
53 operator=(pJsObj); | 52 operator=(pJsObj); |
54 } | 53 } |
55 | 54 |
56 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pJsObj) | 55 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pJsObj) |
57 : m_pJSRuntime(pRuntime) { | 56 : m_pJSRuntime(pRuntime) { |
58 operator=(pJsObj); | 57 operator=(pJsObj); |
59 } | 58 } |
60 | 59 |
61 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Document* pJsDoc) | 60 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Document* pJsDoc) |
62 : m_pJSRuntime(pRuntime) { | 61 : m_pJSRuntime(pRuntime) { |
63 m_eType = VT_object; | |
64 if (pJsDoc) | 62 if (pJsDoc) |
65 m_pValue = pJsDoc->ToV8Object(); | 63 m_pValue = pJsDoc->ToV8Object(); |
66 } | 64 } |
67 | 65 |
68 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr) | 66 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr) |
69 : m_pJSRuntime(pRuntime) { | 67 : m_pJSRuntime(pRuntime) { |
70 operator=(pWstr); | 68 operator=(pWstr); |
71 } | 69 } |
72 | 70 |
73 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr) | 71 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr) |
74 : m_pJSRuntime(pRuntime) { | 72 : m_pJSRuntime(pRuntime) { |
75 operator=(pStr); | 73 operator=(pStr); |
76 } | 74 } |
77 | 75 |
78 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array) | 76 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array) |
79 : m_pJSRuntime(pRuntime) { | 77 : m_pJSRuntime(pRuntime) { |
80 operator=(array); | 78 operator=(array); |
81 } | 79 } |
82 | 80 |
83 CJS_Value::~CJS_Value() {} | 81 CJS_Value::~CJS_Value() {} |
84 | 82 |
85 CJS_Value::CJS_Value(const CJS_Value& other) = default; | 83 CJS_Value::CJS_Value(const CJS_Value& other) = default; |
86 | 84 |
87 void CJS_Value::Attach(v8::Local<v8::Value> pValue, Type t) { | 85 void CJS_Value::Attach(v8::Local<v8::Value> pValue) { |
88 m_pValue = pValue; | 86 m_pValue = pValue; |
89 m_eType = t; | |
90 } | 87 } |
91 | 88 |
92 void CJS_Value::Attach(CJS_Value* pValue) { | 89 void CJS_Value::Attach(CJS_Value* pValue) { |
93 if (pValue) | 90 if (pValue) |
94 Attach(pValue->ToV8Value(), pValue->GetType()); | 91 Attach(pValue->ToV8Value()); |
95 } | 92 } |
96 | 93 |
97 void CJS_Value::Detach() { | 94 void CJS_Value::Detach() { |
98 m_pValue = v8::Local<v8::Value>(); | 95 m_pValue = v8::Local<v8::Value>(); |
99 m_eType = VT_unknown; | |
100 } | 96 } |
101 | 97 |
102 int CJS_Value::ToInt() const { | 98 int CJS_Value::ToInt() const { |
103 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue); | 99 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue); |
104 } | 100 } |
105 | 101 |
106 bool CJS_Value::ToBool() const { | 102 bool CJS_Value::ToBool() const { |
107 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue); | 103 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue); |
108 } | 104 } |
109 | 105 |
(...skipping 29 matching lines...) Expand all Loading... | |
139 | 135 |
140 v8::Local<v8::Array> CJS_Value::ToV8Array() const { | 136 v8::Local<v8::Array> CJS_Value::ToV8Array() const { |
141 if (IsArrayObject()) | 137 if (IsArrayObject()) |
142 return v8::Local<v8::Array>::Cast( | 138 return v8::Local<v8::Array>::Cast( |
143 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue)); | 139 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue)); |
144 return v8::Local<v8::Array>(); | 140 return v8::Local<v8::Array>(); |
145 } | 141 } |
146 | 142 |
147 void CJS_Value::MaybeCoerceToNumber() { | 143 void CJS_Value::MaybeCoerceToNumber() { |
148 bool bAllowNaN = false; | 144 bool bAllowNaN = false; |
149 if (m_eType == VT_string) { | 145 if (GetType() == VT_string) { |
150 CFX_ByteString bstr = ToCFXByteString(); | 146 CFX_ByteString bstr = ToCFXByteString(); |
151 if (bstr.GetLength() == 0) | 147 if (bstr.GetLength() == 0) |
152 return; | 148 return; |
153 if (bstr == "NaN") | 149 if (bstr == "NaN") |
154 bAllowNaN = true; | 150 bAllowNaN = true; |
155 } | 151 } |
156 v8::TryCatch try_catch(m_pJSRuntime->GetIsolate()); | 152 v8::TryCatch try_catch(m_pJSRuntime->GetIsolate()); |
157 v8::MaybeLocal<v8::Number> maybeNum = | 153 v8::MaybeLocal<v8::Number> maybeNum = |
158 m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext()); | 154 m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext()); |
159 if (maybeNum.IsEmpty()) | 155 if (maybeNum.IsEmpty()) |
160 return; | 156 return; |
161 v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); | 157 v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); |
162 if (std::isnan(num->Value()) && !bAllowNaN) | 158 if (std::isnan(num->Value()) && !bAllowNaN) |
163 return; | 159 return; |
164 m_pValue = num; | 160 m_pValue = num; |
165 m_eType = VT_number; | |
166 } | 161 } |
167 | 162 |
168 void CJS_Value::operator=(int iValue) { | 163 void CJS_Value::operator=(int iValue) { |
169 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue); | 164 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue); |
170 m_eType = VT_number; | |
171 } | 165 } |
172 | 166 |
173 void CJS_Value::operator=(bool bValue) { | 167 void CJS_Value::operator=(bool bValue) { |
174 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue); | 168 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue); |
175 m_eType = VT_boolean; | |
176 } | 169 } |
177 | 170 |
178 void CJS_Value::operator=(double dValue) { | 171 void CJS_Value::operator=(double dValue) { |
179 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue); | 172 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue); |
180 m_eType = VT_number; | |
181 } | 173 } |
182 | 174 |
183 void CJS_Value::operator=(float fValue) { | 175 void CJS_Value::operator=(float fValue) { |
184 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue); | 176 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue); |
185 m_eType = VT_number; | |
186 } | 177 } |
187 | 178 |
188 void CJS_Value::operator=(v8::Local<v8::Object> pObj) { | 179 void CJS_Value::operator=(v8::Local<v8::Object> pObj) { |
189 m_pValue = pObj; | 180 m_pValue = pObj; |
190 m_eType = VT_fxobject; | |
191 } | 181 } |
192 | 182 |
193 void CJS_Value::operator=(CJS_Object* pObj) { | 183 void CJS_Value::operator=(CJS_Object* pObj) { |
194 if (pObj) | 184 if (pObj) |
195 operator=(pObj->ToV8Object()); | 185 operator=(pObj->ToV8Object()); |
196 } | 186 } |
197 | 187 |
198 void CJS_Value::operator=(CJS_Document* pJsDoc) { | 188 void CJS_Value::operator=(CJS_Document* pJsDoc) { |
199 m_eType = VT_object; | 189 if (pJsDoc) |
Tom Sepez
2016/07/15 22:27:43
e.g., instant type confusion if nullptr ever passe
| |
200 if (pJsDoc) { | |
201 m_pValue = pJsDoc->ToV8Object(); | 190 m_pValue = pJsDoc->ToV8Object(); |
202 } | |
203 } | 191 } |
204 | 192 |
205 void CJS_Value::operator=(const FX_WCHAR* pWstr) { | 193 void CJS_Value::operator=(const FX_WCHAR* pWstr) { |
206 m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr); | 194 m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr); |
207 m_eType = VT_string; | |
208 } | 195 } |
209 | 196 |
210 void CJS_Value::SetNull() { | 197 void CJS_Value::SetNull() { |
211 m_pValue = FXJS_NewNull(m_pJSRuntime->GetIsolate()); | 198 m_pValue = FXJS_NewNull(m_pJSRuntime->GetIsolate()); |
212 m_eType = VT_null; | |
213 } | 199 } |
214 | 200 |
215 void CJS_Value::operator=(const FX_CHAR* pStr) { | 201 void CJS_Value::operator=(const FX_CHAR* pStr) { |
216 operator=(CFX_WideString::FromLocal(pStr).c_str()); | 202 operator=(CFX_WideString::FromLocal(pStr).c_str()); |
217 } | 203 } |
218 | 204 |
219 void CJS_Value::operator=(CJS_Array& array) { | 205 void CJS_Value::operator=(CJS_Array& array) { |
220 m_pValue = static_cast<v8::Local<v8::Array>>(array); | 206 m_pValue = static_cast<v8::Local<v8::Array>>(array); |
221 m_eType = VT_object; | |
222 } | 207 } |
223 | 208 |
224 void CJS_Value::operator=(CJS_Date& date) { | 209 void CJS_Value::operator=(CJS_Date& date) { |
225 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); | 210 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); |
226 m_eType = VT_date; | |
227 } | 211 } |
228 | 212 |
229 void CJS_Value::operator=(CJS_Value value) { | 213 void CJS_Value::operator=(CJS_Value value) { |
230 m_pValue = value.ToV8Value(); | 214 m_pValue = value.ToV8Value(); |
231 m_eType = value.m_eType; | |
232 m_pJSRuntime = value.m_pJSRuntime; | 215 m_pJSRuntime = value.m_pJSRuntime; |
233 } | 216 } |
234 | 217 |
235 CJS_Value::Type CJS_Value::GetType() const { | 218 // static |
236 if (m_pValue.IsEmpty()) | 219 CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) { |
220 if (value.IsEmpty()) | |
237 return VT_unknown; | 221 return VT_unknown; |
238 if (m_pValue->IsString()) | 222 if (value->IsString()) |
239 return VT_string; | 223 return VT_string; |
240 if (m_pValue->IsNumber()) | 224 if (value->IsNumber()) |
241 return VT_number; | 225 return VT_number; |
242 if (m_pValue->IsBoolean()) | 226 if (value->IsBoolean()) |
243 return VT_boolean; | 227 return VT_boolean; |
244 if (m_pValue->IsDate()) | 228 if (value->IsDate()) |
245 return VT_date; | 229 return VT_date; |
246 if (m_pValue->IsObject()) | 230 if (value->IsObject()) |
247 return VT_object; | 231 return VT_object; |
248 if (m_pValue->IsNull()) | 232 if (value->IsNull()) |
249 return VT_null; | 233 return VT_null; |
250 if (m_pValue->IsUndefined()) | 234 if (value->IsUndefined()) |
251 return VT_undefined; | 235 return VT_undefined; |
252 return VT_unknown; | 236 return VT_unknown; |
253 } | 237 } |
254 | 238 |
255 FX_BOOL CJS_Value::IsArrayObject() const { | 239 FX_BOOL CJS_Value::IsArrayObject() const { |
256 if (m_pValue.IsEmpty()) | 240 if (m_pValue.IsEmpty()) |
257 return FALSE; | 241 return FALSE; |
258 return m_pValue->IsArray(); | 242 return m_pValue->IsArray(); |
259 } | 243 } |
260 | 244 |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 | 403 |
420 FX_BOOL CJS_Array::IsAttached() { | 404 FX_BOOL CJS_Array::IsAttached() { |
421 return FALSE; | 405 return FALSE; |
422 } | 406 } |
423 | 407 |
424 void CJS_Array::GetElement(unsigned index, CJS_Value& value) { | 408 void CJS_Array::GetElement(unsigned index, CJS_Value& value) { |
425 if (m_pArray.IsEmpty()) | 409 if (m_pArray.IsEmpty()) |
426 return; | 410 return; |
427 v8::Local<v8::Value> p = | 411 v8::Local<v8::Value> p = |
428 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index); | 412 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index); |
429 value.Attach(p, CJS_Value::VT_object); | 413 value.Attach(p); |
430 } | 414 } |
431 | 415 |
432 void CJS_Array::SetElement(unsigned index, CJS_Value value) { | 416 void CJS_Array::SetElement(unsigned index, CJS_Value value) { |
433 if (m_pArray.IsEmpty()) | 417 if (m_pArray.IsEmpty()) |
434 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); | 418 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); |
435 | 419 |
436 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index, | 420 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index, |
437 value.ToV8Value()); | 421 value.ToV8Value()); |
438 } | 422 } |
439 | 423 |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
891 v8::Local<v8::Object> pObj = originals[0].ToV8Object(); | 875 v8::Local<v8::Object> pObj = originals[0].ToV8Object(); |
892 result[0] = CJS_Value(pRuntime); // Make unknown. | 876 result[0] = CJS_Value(pRuntime); // Make unknown. |
893 | 877 |
894 va_list ap; | 878 va_list ap; |
895 va_start(ap, nKeywords); | 879 va_start(ap, nKeywords); |
896 for (size_t i = 0; i < nKeywords; ++i) { | 880 for (size_t i = 0; i < nKeywords; ++i) { |
897 const wchar_t* property = va_arg(ap, const wchar_t*); | 881 const wchar_t* property = va_arg(ap, const wchar_t*); |
898 v8::Local<v8::Value> v8Value = | 882 v8::Local<v8::Value> v8Value = |
899 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property); | 883 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property); |
900 if (!v8Value->IsUndefined()) | 884 if (!v8Value->IsUndefined()) |
901 result[i] = CJS_Value(pRuntime, v8Value, CJS_Value::VT_unknown); | 885 result[i] = CJS_Value(pRuntime, v8Value); |
902 } | 886 } |
903 va_end(ap); | 887 va_end(ap); |
904 return result; | 888 return result; |
905 } | 889 } |
OLD | NEW |