Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: fpdfsdk/javascript/JS_Value.cpp

Issue 2154503002: Remove type info from CJS_Value, interrogate v8 instead (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: nit Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/javascript/JS_Value.h ('k') | fpdfsdk/javascript/app.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 }
40 39
41 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const float& fValue) 40 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const float& fValue)
42 : m_pJSRuntime(pRuntime) { 41 : m_pJSRuntime(pRuntime) {
43 operator=(fValue); 42 operator=(fValue);
44 } 43 }
45 44
46 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const double& dValue) 45 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const double& dValue)
47 : m_pJSRuntime(pRuntime) { 46 : m_pJSRuntime(pRuntime) {
48 operator=(dValue); 47 operator=(dValue);
49 } 48 }
50 49
51 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object> pJsObj)
52 : m_pJSRuntime(pRuntime) {
53 operator=(pJsObj);
54 }
55
56 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pJsObj) 50 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pJsObj)
57 : m_pJSRuntime(pRuntime) { 51 : m_pJSRuntime(pRuntime) {
58 operator=(pJsObj); 52 operator=(pJsObj);
59 } 53 }
60 54
61 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Document* pJsDoc)
62 : m_pJSRuntime(pRuntime) {
63 m_eType = VT_object;
64 if (pJsDoc)
65 m_pValue = pJsDoc->ToV8Object();
66 }
67
68 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr) 55 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr)
69 : m_pJSRuntime(pRuntime) { 56 : m_pJSRuntime(pRuntime) {
70 operator=(pWstr); 57 operator=(pWstr);
71 } 58 }
72 59
73 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr) 60 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr)
74 : m_pJSRuntime(pRuntime) { 61 : m_pJSRuntime(pRuntime) {
75 operator=(pStr); 62 operator=(pStr);
76 } 63 }
77 64
78 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array) 65 CJS_Value::CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array)
79 : m_pJSRuntime(pRuntime) { 66 : m_pJSRuntime(pRuntime) {
80 operator=(array); 67 operator=(array);
81 } 68 }
82 69
83 CJS_Value::~CJS_Value() {} 70 CJS_Value::~CJS_Value() {}
84 71
85 CJS_Value::CJS_Value(const CJS_Value& other) = default; 72 CJS_Value::CJS_Value(const CJS_Value& other) = default;
86 73
87 void CJS_Value::Attach(v8::Local<v8::Value> pValue, Type t) { 74 void CJS_Value::Attach(v8::Local<v8::Value> pValue) {
88 m_pValue = pValue; 75 m_pValue = pValue;
89 m_eType = t;
90 } 76 }
91 77
92 void CJS_Value::Attach(CJS_Value* pValue) { 78 void CJS_Value::Attach(CJS_Value* pValue) {
93 if (pValue) 79 if (pValue)
94 Attach(pValue->ToV8Value(), pValue->GetType()); 80 Attach(pValue->ToV8Value());
95 } 81 }
96 82
97 void CJS_Value::Detach() { 83 void CJS_Value::Detach() {
98 m_pValue = v8::Local<v8::Value>(); 84 m_pValue = v8::Local<v8::Value>();
99 m_eType = VT_unknown;
100 } 85 }
101 86
102 int CJS_Value::ToInt() const { 87 int CJS_Value::ToInt() const {
103 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue); 88 return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue);
104 } 89 }
105 90
106 bool CJS_Value::ToBool() const { 91 bool CJS_Value::ToBool() const {
107 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue); 92 return FXJS_ToBoolean(m_pJSRuntime->GetIsolate(), m_pValue);
108 } 93 }
109 94
(...skipping 29 matching lines...) Expand all
139 124
140 v8::Local<v8::Array> CJS_Value::ToV8Array() const { 125 v8::Local<v8::Array> CJS_Value::ToV8Array() const {
141 if (IsArrayObject()) 126 if (IsArrayObject())
142 return v8::Local<v8::Array>::Cast( 127 return v8::Local<v8::Array>::Cast(
143 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue)); 128 FXJS_ToObject(m_pJSRuntime->GetIsolate(), m_pValue));
144 return v8::Local<v8::Array>(); 129 return v8::Local<v8::Array>();
145 } 130 }
146 131
147 void CJS_Value::MaybeCoerceToNumber() { 132 void CJS_Value::MaybeCoerceToNumber() {
148 bool bAllowNaN = false; 133 bool bAllowNaN = false;
149 if (m_eType == VT_string) { 134 if (GetType() == VT_string) {
150 CFX_ByteString bstr = ToCFXByteString(); 135 CFX_ByteString bstr = ToCFXByteString();
151 if (bstr.GetLength() == 0) 136 if (bstr.GetLength() == 0)
152 return; 137 return;
153 if (bstr == "NaN") 138 if (bstr == "NaN")
154 bAllowNaN = true; 139 bAllowNaN = true;
155 } 140 }
156 v8::TryCatch try_catch(m_pJSRuntime->GetIsolate()); 141 v8::TryCatch try_catch(m_pJSRuntime->GetIsolate());
157 v8::MaybeLocal<v8::Number> maybeNum = 142 v8::MaybeLocal<v8::Number> maybeNum =
158 m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext()); 143 m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext());
159 if (maybeNum.IsEmpty()) 144 if (maybeNum.IsEmpty())
160 return; 145 return;
161 v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); 146 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
162 if (std::isnan(num->Value()) && !bAllowNaN) 147 if (std::isnan(num->Value()) && !bAllowNaN)
163 return; 148 return;
164 m_pValue = num; 149 m_pValue = num;
165 m_eType = VT_number;
166 } 150 }
167 151
168 void CJS_Value::operator=(int iValue) { 152 void CJS_Value::operator=(int iValue) {
169 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue); 153 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue);
170 m_eType = VT_number;
171 } 154 }
172 155
173 void CJS_Value::operator=(bool bValue) { 156 void CJS_Value::operator=(bool bValue) {
174 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue); 157 m_pValue = FXJS_NewBoolean(m_pJSRuntime->GetIsolate(), bValue);
175 m_eType = VT_boolean;
176 } 158 }
177 159
178 void CJS_Value::operator=(double dValue) { 160 void CJS_Value::operator=(double dValue) {
179 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue); 161 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), dValue);
180 m_eType = VT_number;
181 } 162 }
182 163
183 void CJS_Value::operator=(float fValue) { 164 void CJS_Value::operator=(float fValue) {
184 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue); 165 m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), fValue);
185 m_eType = VT_number;
186 } 166 }
187 167
188 void CJS_Value::operator=(v8::Local<v8::Object> pObj) { 168 void CJS_Value::operator=(v8::Local<v8::Object> pObj) {
189 m_pValue = pObj; 169 m_pValue = pObj;
190 m_eType = VT_fxobject;
191 } 170 }
192 171
193 void CJS_Value::operator=(CJS_Object* pObj) { 172 void CJS_Value::operator=(CJS_Object* pObj) {
194 if (pObj) 173 if (pObj)
195 operator=(pObj->ToV8Object()); 174 operator=(pObj->ToV8Object());
196 } 175 }
197 176
198 void CJS_Value::operator=(CJS_Document* pJsDoc) {
199 m_eType = VT_object;
200 if (pJsDoc) {
201 m_pValue = pJsDoc->ToV8Object();
202 }
203 }
204
205 void CJS_Value::operator=(const FX_WCHAR* pWstr) { 177 void CJS_Value::operator=(const FX_WCHAR* pWstr) {
206 m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr); 178 m_pValue = FXJS_NewString(m_pJSRuntime->GetIsolate(), (wchar_t*)pWstr);
207 m_eType = VT_string;
208 } 179 }
209 180
210 void CJS_Value::SetNull() { 181 void CJS_Value::SetNull() {
211 m_pValue = FXJS_NewNull(m_pJSRuntime->GetIsolate()); 182 m_pValue = FXJS_NewNull(m_pJSRuntime->GetIsolate());
212 m_eType = VT_null;
213 } 183 }
214 184
215 void CJS_Value::operator=(const FX_CHAR* pStr) { 185 void CJS_Value::operator=(const FX_CHAR* pStr) {
216 operator=(CFX_WideString::FromLocal(pStr).c_str()); 186 operator=(CFX_WideString::FromLocal(pStr).c_str());
217 } 187 }
218 188
219 void CJS_Value::operator=(CJS_Array& array) { 189 void CJS_Value::operator=(CJS_Array& array) {
220 m_pValue = static_cast<v8::Local<v8::Array>>(array); 190 m_pValue = static_cast<v8::Local<v8::Array>>(array);
221 m_eType = VT_object;
222 } 191 }
223 192
224 void CJS_Value::operator=(CJS_Date& date) { 193 void CJS_Value::operator=(CJS_Date& date) {
225 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date); 194 m_pValue = FXJS_NewDate(m_pJSRuntime->GetIsolate(), (double)date);
226 m_eType = VT_date;
227 } 195 }
228 196
229 void CJS_Value::operator=(CJS_Value value) { 197 void CJS_Value::operator=(CJS_Value value) {
230 m_pValue = value.ToV8Value(); 198 m_pValue = value.ToV8Value();
231 m_eType = value.m_eType;
232 m_pJSRuntime = value.m_pJSRuntime; 199 m_pJSRuntime = value.m_pJSRuntime;
233 } 200 }
234 201
235 CJS_Value::Type CJS_Value::GetType() const { 202 // static
236 if (m_pValue.IsEmpty()) 203 CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) {
204 if (value.IsEmpty())
237 return VT_unknown; 205 return VT_unknown;
238 if (m_pValue->IsString()) 206 if (value->IsString())
239 return VT_string; 207 return VT_string;
240 if (m_pValue->IsNumber()) 208 if (value->IsNumber())
241 return VT_number; 209 return VT_number;
242 if (m_pValue->IsBoolean()) 210 if (value->IsBoolean())
243 return VT_boolean; 211 return VT_boolean;
244 if (m_pValue->IsDate()) 212 if (value->IsDate())
245 return VT_date; 213 return VT_date;
246 if (m_pValue->IsObject()) 214 if (value->IsObject())
247 return VT_object; 215 return VT_object;
248 if (m_pValue->IsNull()) 216 if (value->IsNull())
249 return VT_null; 217 return VT_null;
250 if (m_pValue->IsUndefined()) 218 if (value->IsUndefined())
251 return VT_undefined; 219 return VT_undefined;
252 return VT_unknown; 220 return VT_unknown;
253 } 221 }
254 222
255 FX_BOOL CJS_Value::IsArrayObject() const { 223 FX_BOOL CJS_Value::IsArrayObject() const {
256 if (m_pValue.IsEmpty()) 224 if (m_pValue.IsEmpty())
257 return FALSE; 225 return FALSE;
258 return m_pValue->IsArray(); 226 return m_pValue->IsArray();
259 } 227 }
260 228
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 387
420 FX_BOOL CJS_Array::IsAttached() { 388 FX_BOOL CJS_Array::IsAttached() {
421 return FALSE; 389 return FALSE;
422 } 390 }
423 391
424 void CJS_Array::GetElement(unsigned index, CJS_Value& value) { 392 void CJS_Array::GetElement(unsigned index, CJS_Value& value) {
425 if (m_pArray.IsEmpty()) 393 if (m_pArray.IsEmpty())
426 return; 394 return;
427 v8::Local<v8::Value> p = 395 v8::Local<v8::Value> p =
428 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index); 396 FXJS_GetArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index);
429 value.Attach(p, CJS_Value::VT_object); 397 value.Attach(p);
430 } 398 }
431 399
432 void CJS_Array::SetElement(unsigned index, CJS_Value value) { 400 void CJS_Array::SetElement(unsigned index, CJS_Value value) {
433 if (m_pArray.IsEmpty()) 401 if (m_pArray.IsEmpty())
434 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate()); 402 m_pArray = FXJS_NewArray(m_pJSRuntime->GetIsolate());
435 403
436 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index, 404 FXJS_PutArrayElement(m_pJSRuntime->GetIsolate(), m_pArray, index,
437 value.ToV8Value()); 405 value.ToV8Value());
438 } 406 }
439 407
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 v8::Local<v8::Object> pObj = originals[0].ToV8Object(); 859 v8::Local<v8::Object> pObj = originals[0].ToV8Object();
892 result[0] = CJS_Value(pRuntime); // Make unknown. 860 result[0] = CJS_Value(pRuntime); // Make unknown.
893 861
894 va_list ap; 862 va_list ap;
895 va_start(ap, nKeywords); 863 va_start(ap, nKeywords);
896 for (size_t i = 0; i < nKeywords; ++i) { 864 for (size_t i = 0; i < nKeywords; ++i) {
897 const wchar_t* property = va_arg(ap, const wchar_t*); 865 const wchar_t* property = va_arg(ap, const wchar_t*);
898 v8::Local<v8::Value> v8Value = 866 v8::Local<v8::Value> v8Value =
899 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property); 867 FXJS_GetObjectElement(pRuntime->GetIsolate(), pObj, property);
900 if (!v8Value->IsUndefined()) 868 if (!v8Value->IsUndefined())
901 result[i] = CJS_Value(pRuntime, v8Value, CJS_Value::VT_unknown); 869 result[i] = CJS_Value(pRuntime, v8Value);
902 } 870 }
903 va_end(ap); 871 va_end(ap);
904 return result; 872 return result;
905 } 873 }
OLDNEW
« no previous file with comments | « fpdfsdk/javascript/JS_Value.h ('k') | fpdfsdk/javascript/app.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698