OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "xfa/src/fxjse/value.h" | |
8 | |
9 #include <math.h> | |
10 | |
11 #include "xfa/src/fxjse/class.h" | |
12 #include "xfa/src/fxjse/util_inline.h" | |
13 | |
14 FX_BOOL FXJSE_Value_IsUndefined(FXJSE_HVALUE hValue) { | |
15 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
16 return lpValue && lpValue->IsUndefined(); | |
17 } | |
18 | |
19 FX_BOOL FXJSE_Value_IsNull(FXJSE_HVALUE hValue) { | |
20 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
21 return lpValue && lpValue->IsNull(); | |
22 } | |
23 | |
24 FX_BOOL FXJSE_Value_IsBoolean(FXJSE_HVALUE hValue) { | |
25 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
26 return lpValue && lpValue->IsBoolean(); | |
27 } | |
28 | |
29 FX_BOOL FXJSE_Value_IsUTF8String(FXJSE_HVALUE hValue) { | |
30 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
31 return lpValue && lpValue->IsString(); | |
32 } | |
33 | |
34 FX_BOOL FXJSE_Value_IsNumber(FXJSE_HVALUE hValue) { | |
35 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
36 return lpValue && lpValue->IsNumber(); | |
37 } | |
38 | |
39 FX_BOOL FXJSE_Value_IsInteger(FXJSE_HVALUE hValue) { | |
40 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
41 return lpValue && lpValue->IsInteger(); | |
42 } | |
43 | |
44 FX_BOOL FXJSE_Value_IsObject(FXJSE_HVALUE hValue) { | |
45 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
46 return lpValue && lpValue->IsObject(); | |
47 } | |
48 | |
49 FX_BOOL FXJSE_Value_IsArray(FXJSE_HVALUE hValue) { | |
50 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
51 return lpValue && lpValue->IsArray(); | |
52 } | |
53 | |
54 FX_BOOL FXJSE_Value_IsFunction(FXJSE_HVALUE hValue) { | |
55 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
56 return lpValue && lpValue->IsFunction(); | |
57 } | |
58 | |
59 FX_BOOL FXJSE_Value_IsDate(FXJSE_HVALUE hValue) { | |
60 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
61 return lpValue && lpValue->IsDate(); | |
62 } | |
63 | |
64 FX_BOOL FXJSE_Value_ToBoolean(FXJSE_HVALUE hValue) { | |
65 return reinterpret_cast<CFXJSE_Value*>(hValue)->ToBoolean(); | |
66 } | |
67 | |
68 FX_FLOAT FXJSE_Value_ToFloat(FXJSE_HVALUE hValue) { | |
69 return reinterpret_cast<CFXJSE_Value*>(hValue)->ToFloat(); | |
70 } | |
71 | |
72 FXJSE_DOUBLE FXJSE_Value_ToDouble(FXJSE_HVALUE hValue) { | |
73 return reinterpret_cast<CFXJSE_Value*>(hValue)->ToDouble(); | |
74 } | |
75 | |
76 void FXJSE_Value_ToUTF8String(FXJSE_HVALUE hValue, | |
77 CFX_ByteString& szStrOutput) { | |
78 return reinterpret_cast<CFXJSE_Value*>(hValue)->ToString(szStrOutput); | |
79 } | |
80 | |
81 int32_t FXJSE_Value_ToInteger(FXJSE_HVALUE hValue) { | |
82 return reinterpret_cast<CFXJSE_Value*>(hValue)->ToInteger(); | |
83 } | |
84 | |
85 void* FXJSE_Value_ToObject(FXJSE_HVALUE hValue, FXJSE_HCLASS hClass) { | |
86 CFXJSE_Class* lpClass = reinterpret_cast<CFXJSE_Class*>(hClass); | |
87 return reinterpret_cast<CFXJSE_Value*>(hValue)->ToObject(lpClass); | |
88 } | |
89 | |
90 void FXJSE_Value_SetUndefined(FXJSE_HVALUE hValue) { | |
91 reinterpret_cast<CFXJSE_Value*>(hValue)->SetUndefined(); | |
92 } | |
93 | |
94 void FXJSE_Value_SetNull(FXJSE_HVALUE hValue) { | |
95 reinterpret_cast<CFXJSE_Value*>(hValue)->SetNull(); | |
96 } | |
97 | |
98 void FXJSE_Value_SetBoolean(FXJSE_HVALUE hValue, FX_BOOL bBoolean) { | |
99 reinterpret_cast<CFXJSE_Value*>(hValue)->SetBoolean(bBoolean); | |
100 } | |
101 | |
102 void FXJSE_Value_SetUTF8String(FXJSE_HVALUE hValue, | |
103 const CFX_ByteStringC& szString) { | |
104 reinterpret_cast<CFXJSE_Value*>(hValue)->SetString(szString); | |
105 } | |
106 | |
107 void FXJSE_Value_SetInteger(FXJSE_HVALUE hValue, int32_t nInteger) { | |
108 reinterpret_cast<CFXJSE_Value*>(hValue)->SetInteger(nInteger); | |
109 } | |
110 | |
111 void FXJSE_Value_SetFloat(FXJSE_HVALUE hValue, FX_FLOAT fFloat) { | |
112 reinterpret_cast<CFXJSE_Value*>(hValue)->SetFloat(fFloat); | |
113 } | |
114 | |
115 void FXJSE_Value_SetDouble(FXJSE_HVALUE hValue, FXJSE_DOUBLE dDouble) { | |
116 reinterpret_cast<CFXJSE_Value*>(hValue)->SetDouble(dDouble); | |
117 } | |
118 | |
119 void FXJSE_Value_SetObject(FXJSE_HVALUE hValue, | |
120 void* lpObject, | |
121 FXJSE_HCLASS hClass) { | |
122 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
123 CFXJSE_Class* lpClass = reinterpret_cast<CFXJSE_Class*>(hClass); | |
124 if (!lpClass) { | |
125 ASSERT(!lpObject); | |
126 lpValue->SetJSObject(); | |
127 } else { | |
128 lpValue->SetHostObject(lpObject, lpClass); | |
129 } | |
130 } | |
131 | |
132 void FXJSE_Value_SetArray(FXJSE_HVALUE hValue, | |
133 uint32_t uValueCount, | |
134 FXJSE_HVALUE* rgValues) { | |
135 reinterpret_cast<CFXJSE_Value*>(hValue) | |
136 ->SetArray(uValueCount, reinterpret_cast<CFXJSE_Value**>(rgValues)); | |
137 } | |
138 | |
139 void FXJSE_Value_SetDate(FXJSE_HVALUE hValue, FXJSE_DOUBLE dDouble) { | |
140 reinterpret_cast<CFXJSE_Value*>(hValue)->SetDate(dDouble); | |
141 } | |
142 | |
143 void FXJSE_Value_Set(FXJSE_HVALUE hValue, FXJSE_HVALUE hOriginalValue) { | |
144 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
145 CFXJSE_Value* lpOriginalValue = | |
146 reinterpret_cast<CFXJSE_Value*>(hOriginalValue); | |
147 ASSERT(lpValue && lpOriginalValue); | |
148 lpValue->Assign(lpOriginalValue); | |
149 } | |
150 | |
151 FX_BOOL FXJSE_Value_GetObjectProp(FXJSE_HVALUE hValue, | |
152 const CFX_ByteStringC& szPropName, | |
153 FXJSE_HVALUE hPropValue) { | |
154 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
155 CFXJSE_Value* lpPropValue = reinterpret_cast<CFXJSE_Value*>(hPropValue); | |
156 ASSERT(lpValue && lpPropValue); | |
157 return lpValue->GetObjectProperty(szPropName, lpPropValue); | |
158 } | |
159 | |
160 FX_BOOL FXJSE_Value_SetObjectProp(FXJSE_HVALUE hValue, | |
161 const CFX_ByteStringC& szPropName, | |
162 FXJSE_HVALUE hPropValue) { | |
163 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
164 CFXJSE_Value* lpPropValue = reinterpret_cast<CFXJSE_Value*>(hPropValue); | |
165 ASSERT(lpValue && lpPropValue); | |
166 return lpValue->SetObjectProperty(szPropName, lpPropValue); | |
167 } | |
168 | |
169 FX_BOOL FXJSE_Value_GetObjectPropByIdx(FXJSE_HVALUE hValue, | |
170 uint32_t uPropIdx, | |
171 FXJSE_HVALUE hPropValue) { | |
172 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
173 CFXJSE_Value* lpPropValue = reinterpret_cast<CFXJSE_Value*>(hPropValue); | |
174 ASSERT(lpValue && lpPropValue); | |
175 return lpValue->GetObjectProperty(uPropIdx, lpPropValue); | |
176 } | |
177 | |
178 FX_BOOL FXJSE_Value_SetObjectPropByIdx(FXJSE_HVALUE hValue, | |
179 uint32_t uPropIdx, | |
180 FXJSE_HVALUE hPropValue) { | |
181 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
182 CFXJSE_Value* lpPropValue = reinterpret_cast<CFXJSE_Value*>(hPropValue); | |
183 ASSERT(lpValue && lpPropValue); | |
184 return lpValue->SetObjectProperty(uPropIdx, lpPropValue); | |
185 } | |
186 | |
187 FX_BOOL FXJSE_Value_DeleteObjectProp(FXJSE_HVALUE hValue, | |
188 const CFX_ByteStringC& szPropName) { | |
189 return reinterpret_cast<CFXJSE_Value*>(hValue) | |
190 ->DeleteObjectProperty(szPropName); | |
191 } | |
192 | |
193 FX_BOOL FXJSE_Value_ObjectHasOwnProp(FXJSE_HVALUE hValue, | |
194 const CFX_ByteStringC& szPropName, | |
195 FX_BOOL bUseTypeGetter) { | |
196 return reinterpret_cast<CFXJSE_Value*>(hValue) | |
197 ->HasObjectOwnProperty(szPropName, bUseTypeGetter); | |
198 } | |
199 | |
200 FX_BOOL FXJSE_Value_SetObjectOwnProp(FXJSE_HVALUE hValue, | |
201 const CFX_ByteStringC& szPropName, | |
202 FXJSE_HVALUE hPropValue) { | |
203 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
204 CFXJSE_Value* lpPropValue = reinterpret_cast<CFXJSE_Value*>(hPropValue); | |
205 ASSERT(lpValue && lpPropValue); | |
206 return lpValue->SetObjectOwnProperty(szPropName, lpPropValue); | |
207 } | |
208 | |
209 FX_BOOL FXJSE_Value_SetFunctionBind(FXJSE_HVALUE hValue, | |
210 FXJSE_HVALUE hOldFunction, | |
211 FXJSE_HVALUE hNewThis) { | |
212 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
213 CFXJSE_Value* lpOldFunction = reinterpret_cast<CFXJSE_Value*>(hOldFunction); | |
214 CFXJSE_Value* lpNewThis = reinterpret_cast<CFXJSE_Value*>(hNewThis); | |
215 ASSERT(lpValue && lpOldFunction && lpNewThis); | |
216 return lpValue->SetFunctionBind(lpOldFunction, lpNewThis); | |
217 } | |
218 | |
219 FX_BOOL FXJSE_Value_CallFunction(FXJSE_HVALUE hFunction, | |
220 FXJSE_HVALUE hThis, | |
221 FXJSE_HVALUE hRetValue, | |
222 uint32_t nArgCount, | |
223 FXJSE_HVALUE* lpArgs) { | |
224 CFXJSE_Value* lpThis = reinterpret_cast<CFXJSE_Value*>(hThis); | |
225 CFXJSE_Value* lpRetValue = reinterpret_cast<CFXJSE_Value*>(hRetValue); | |
226 return reinterpret_cast<CFXJSE_Value*>(hFunction) | |
227 ->Call(lpThis, lpRetValue, nArgCount, lpArgs); | |
228 } | |
229 | |
230 FXJSE_HVALUE FXJSE_Value_Create(FXJSE_HRUNTIME hRuntime) { | |
231 return reinterpret_cast<FXJSE_HVALUE>( | |
232 CFXJSE_Value::Create(reinterpret_cast<v8::Isolate*>(hRuntime))); | |
233 } | |
234 | |
235 void FXJSE_Value_Release(FXJSE_HVALUE hValue) { | |
236 CFXJSE_Value* lpValue = reinterpret_cast<CFXJSE_Value*>(hValue); | |
237 delete lpValue; | |
238 } | |
239 | |
240 FXJSE_HRUNTIME FXJSE_Value_GetRuntime(FXJSE_HVALUE hValue) { | |
241 return reinterpret_cast<FXJSE_HRUNTIME>( | |
242 reinterpret_cast<CFXJSE_Value*>(hValue)->GetIsolate()); | |
243 } | |
244 | |
245 void FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Name, | |
246 const CFX_ByteStringC& utf8Message) { | |
247 v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); | |
248 ASSERT(pIsolate); | |
249 | |
250 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); | |
251 v8::Local<v8::String> hMessage = v8::String::NewFromUtf8( | |
252 pIsolate, utf8Message.GetCStr(), v8::String::kNormalString, | |
253 utf8Message.GetLength()); | |
254 v8::Local<v8::Value> hError; | |
255 | |
256 if (utf8Name == "RangeError") { | |
257 hError = v8::Exception::RangeError(hMessage); | |
258 } else if (utf8Name == "ReferenceError") { | |
259 hError = v8::Exception::ReferenceError(hMessage); | |
260 } else if (utf8Name == "SyntaxError") { | |
261 hError = v8::Exception::SyntaxError(hMessage); | |
262 } else if (utf8Name == "TypeError") { | |
263 hError = v8::Exception::TypeError(hMessage); | |
264 } else { | |
265 hError = v8::Exception::Error(hMessage); | |
266 if (utf8Name != "Error" && !utf8Name.IsEmpty()) { | |
267 hError.As<v8::Object>()->Set( | |
268 v8::String::NewFromUtf8(pIsolate, "name"), | |
269 v8::String::NewFromUtf8(pIsolate, utf8Name.GetCStr(), | |
270 v8::String::kNormalString, | |
271 utf8Name.GetLength())); | |
272 } | |
273 } | |
274 pIsolate->ThrowException(hError); | |
275 } | |
276 | |
277 CFXJSE_Value* CFXJSE_Value::Create(v8::Isolate* pIsolate) { | |
278 return new CFXJSE_Value(pIsolate); | |
279 } | |
280 | |
281 void* CFXJSE_Value::ToObject(CFXJSE_Class* lpClass) const { | |
282 ASSERT(!m_hValue.IsEmpty()); | |
283 | |
284 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
285 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
286 ASSERT(!hValue.IsEmpty()); | |
287 | |
288 if (!hValue->IsObject()) | |
289 return nullptr; | |
290 | |
291 return FXJSE_RetrieveObjectBinding(hValue.As<v8::Object>(), lpClass); | |
292 } | |
293 | |
294 V8_INLINE static double FXJSE_ftod(FX_FLOAT fNumber) { | |
295 if (sizeof(FX_FLOAT) != 4) { | |
296 ASSERT(FALSE); | |
297 return fNumber; | |
298 } | |
299 | |
300 uint32_t nFloatBits = (uint32_t&)fNumber; | |
301 uint8_t nExponent = (uint8_t)(nFloatBits >> 16 >> 7); | |
302 if (nExponent == 0 || nExponent == 255) | |
303 return fNumber; | |
304 | |
305 int8_t nErrExp = nExponent - 127 - 23; | |
306 if (nErrExp >= 0) | |
307 return fNumber; | |
308 | |
309 double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2; | |
310 double dNumber = fNumber, dNumberAbs = fabs(fNumber); | |
311 double dNumberAbsMin = dNumberAbs - dwErrorHalf, | |
312 dNumberAbsMax = dNumberAbs + dwErrorHalf; | |
313 int32_t iErrPos = 0; | |
314 if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) { | |
315 dNumberAbsMin = fmod(dNumberAbsMin, 1.0); | |
316 dNumberAbsMax = fmod(dNumberAbsMax, 1.0); | |
317 int32_t iErrPosMin = 1, iErrPosMax = 38; | |
318 do { | |
319 int32_t iMid = (iErrPosMin + iErrPosMax) / 2; | |
320 double dPow = pow(10.0, iMid); | |
321 if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) { | |
322 iErrPosMin = iMid + 1; | |
323 } else { | |
324 iErrPosMax = iMid; | |
325 } | |
326 } while (iErrPosMin < iErrPosMax); | |
327 iErrPos = iErrPosMax; | |
328 } | |
329 double dPow = pow(10.0, iErrPos); | |
330 return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow | |
331 : floor(dNumber * dPow + 0.5) / dPow; | |
332 } | |
333 | |
334 void CFXJSE_Value::SetFloat(FX_FLOAT fFloat) { | |
335 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); | |
336 v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat)); | |
337 m_hValue.Reset(m_pIsolate, hValue); | |
338 } | |
339 | |
340 void CFXJSE_Value::SetHostObject(void* lpObject, CFXJSE_Class* lpClass) { | |
341 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
342 ASSERT(lpClass); | |
343 v8::Local<v8::FunctionTemplate> hClass = | |
344 v8::Local<v8::FunctionTemplate>::New(m_pIsolate, lpClass->m_hTemplate); | |
345 v8::Local<v8::Object> hObject = hClass->InstanceTemplate()->NewInstance(); | |
346 FXJSE_UpdateObjectBinding(hObject, lpObject); | |
347 m_hValue.Reset(m_pIsolate, hObject); | |
348 } | |
349 | |
350 void CFXJSE_Value::SetArray(uint32_t uValueCount, CFXJSE_Value** rgValues) { | |
351 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
352 v8::Local<v8::Array> hArrayObject = v8::Array::New(m_pIsolate, uValueCount); | |
353 if (rgValues) { | |
354 for (uint32_t i = 0; i < uValueCount; i++) { | |
355 if (rgValues[i]) { | |
356 hArrayObject->Set(i, v8::Local<v8::Value>::New( | |
357 m_pIsolate, rgValues[i]->DirectGetValue())); | |
358 } | |
359 } | |
360 } | |
361 m_hValue.Reset(m_pIsolate, hArrayObject); | |
362 } | |
363 | |
364 void CFXJSE_Value::SetDate(FXJSE_DOUBLE dDouble) { | |
365 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
366 v8::Local<v8::Value> hDate = v8::Date::New(m_pIsolate, dDouble); | |
367 m_hValue.Reset(m_pIsolate, hDate); | |
368 } | |
369 | |
370 FX_BOOL CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName, | |
371 CFXJSE_Value* lpPropValue) { | |
372 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
373 v8::Local<v8::Value> hObject = | |
374 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
375 if (!hObject->IsObject()) | |
376 return FALSE; | |
377 | |
378 v8::Local<v8::Value> hPropValue = | |
379 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue()); | |
380 return (FX_BOOL)hObject.As<v8::Object>()->Set( | |
381 v8::String::NewFromUtf8(m_pIsolate, szPropName.GetCStr(), | |
382 v8::String::kNormalString, | |
383 szPropName.GetLength()), | |
384 hPropValue); | |
385 } | |
386 | |
387 FX_BOOL CFXJSE_Value::GetObjectProperty(const CFX_ByteStringC& szPropName, | |
388 CFXJSE_Value* lpPropValue) { | |
389 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
390 v8::Local<v8::Value> hObject = | |
391 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
392 if (!hObject->IsObject()) | |
393 return FALSE; | |
394 | |
395 v8::Local<v8::Value> hPropValue = | |
396 hObject.As<v8::Object>()->Get(v8::String::NewFromUtf8( | |
397 m_pIsolate, szPropName.GetCStr(), v8::String::kNormalString, | |
398 szPropName.GetLength())); | |
399 lpPropValue->ForceSetValue(hPropValue); | |
400 return TRUE; | |
401 } | |
402 | |
403 FX_BOOL CFXJSE_Value::SetObjectProperty(uint32_t uPropIdx, | |
404 CFXJSE_Value* lpPropValue) { | |
405 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
406 v8::Local<v8::Value> hObject = | |
407 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
408 if (!hObject->IsObject()) | |
409 return FALSE; | |
410 | |
411 v8::Local<v8::Value> hPropValue = | |
412 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue()); | |
413 return (FX_BOOL)hObject.As<v8::Object>()->Set(uPropIdx, hPropValue); | |
414 } | |
415 | |
416 FX_BOOL CFXJSE_Value::GetObjectProperty(uint32_t uPropIdx, | |
417 CFXJSE_Value* lpPropValue) { | |
418 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
419 v8::Local<v8::Value> hObject = | |
420 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
421 if (!hObject->IsObject()) | |
422 return FALSE; | |
423 | |
424 v8::Local<v8::Value> hPropValue = hObject.As<v8::Object>()->Get(uPropIdx); | |
425 lpPropValue->ForceSetValue(hPropValue); | |
426 return TRUE; | |
427 } | |
428 | |
429 FX_BOOL CFXJSE_Value::DeleteObjectProperty(const CFX_ByteStringC& szPropName) { | |
430 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
431 v8::Local<v8::Value> hObject = | |
432 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
433 if (!hObject->IsObject()) | |
434 return FALSE; | |
435 | |
436 hObject.As<v8::Object>()->Delete(v8::String::NewFromUtf8( | |
437 m_pIsolate, szPropName.GetCStr(), v8::String::kNormalString, | |
438 szPropName.GetLength())); | |
439 return TRUE; | |
440 } | |
441 | |
442 FX_BOOL CFXJSE_Value::HasObjectOwnProperty(const CFX_ByteStringC& szPropName, | |
443 FX_BOOL bUseTypeGetter) { | |
444 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
445 v8::Local<v8::Value> hObject = | |
446 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
447 if (!hObject->IsObject()) | |
448 return FALSE; | |
449 | |
450 v8::Local<v8::String> hKey = v8::String::NewFromUtf8( | |
451 m_pIsolate, szPropName.GetCStr(), v8::String::kNormalString, | |
452 szPropName.GetLength()); | |
453 return hObject.As<v8::Object>()->HasRealNamedProperty(hKey) || | |
454 (bUseTypeGetter && | |
455 hObject.As<v8::Object>() | |
456 ->HasOwnProperty(m_pIsolate->GetCurrentContext(), hKey) | |
457 .FromMaybe(false)); | |
458 } | |
459 | |
460 FX_BOOL CFXJSE_Value::SetObjectOwnProperty(const CFX_ByteStringC& szPropName, | |
461 CFXJSE_Value* lpPropValue) { | |
462 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
463 v8::Local<v8::Value> hObject = | |
464 v8::Local<v8::Value>::New(m_pIsolate, m_hValue); | |
465 if (!hObject->IsObject()) | |
466 return FALSE; | |
467 | |
468 v8::Local<v8::Value> hValue = | |
469 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->m_hValue); | |
470 return hObject.As<v8::Object>() | |
471 ->DefineOwnProperty( | |
472 m_pIsolate->GetCurrentContext(), | |
473 v8::String::NewFromUtf8(m_pIsolate, szPropName.GetCStr(), | |
474 v8::String::kNormalString, | |
475 szPropName.GetLength()), | |
476 hValue) | |
477 .FromMaybe(false); | |
478 } | |
479 | |
480 FX_BOOL CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction, | |
481 CFXJSE_Value* lpNewThis) { | |
482 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
483 v8::Local<v8::Value> rgArgs[2]; | |
484 v8::Local<v8::Value> hOldFunction = | |
485 v8::Local<v8::Value>::New(m_pIsolate, lpOldFunction->DirectGetValue()); | |
486 if (hOldFunction.IsEmpty() || !hOldFunction->IsFunction()) | |
487 return FALSE; | |
488 | |
489 rgArgs[0] = hOldFunction; | |
490 v8::Local<v8::Value> hNewThis = | |
491 v8::Local<v8::Value>::New(m_pIsolate, lpNewThis->DirectGetValue()); | |
492 if (hNewThis.IsEmpty()) | |
493 return FALSE; | |
494 | |
495 rgArgs[1] = hNewThis; | |
496 v8::Local<v8::String> hBinderFuncSource = | |
497 v8::String::NewFromUtf8(m_pIsolate, | |
498 "(function (oldfunction, newthis) { return " | |
499 "oldfunction.bind(newthis); })"); | |
500 v8::Local<v8::Function> hBinderFunc = | |
501 v8::Script::Compile(hBinderFuncSource)->Run().As<v8::Function>(); | |
502 v8::Local<v8::Value> hBoundFunction = | |
503 hBinderFunc->Call(m_pIsolate->GetCurrentContext()->Global(), 2, rgArgs); | |
504 if (hBoundFunction.IsEmpty() || !hBoundFunction->IsFunction()) | |
505 return FALSE; | |
506 | |
507 m_hValue.Reset(m_pIsolate, hBoundFunction); | |
508 return TRUE; | |
509 } | |
510 | |
511 #define FXJSE_INVALID_PTR ((void*)(intptr_t)-1) | |
512 FX_BOOL CFXJSE_Value::Call(CFXJSE_Value* lpReceiver, | |
513 CFXJSE_Value* lpRetValue, | |
514 uint32_t nArgCount, | |
515 FXJSE_HVALUE* lpArgs) { | |
516 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); | |
517 v8::Local<v8::Value> hFunctionValue = | |
518 v8::Local<v8::Value>::New(m_pIsolate, DirectGetValue()); | |
519 v8::Local<v8::Object> hFunctionObject = | |
520 !hFunctionValue.IsEmpty() && hFunctionValue->IsObject() | |
521 ? hFunctionValue.As<v8::Object>() | |
522 : v8::Local<v8::Object>(); | |
523 | |
524 v8::TryCatch trycatch(m_pIsolate); | |
525 if (hFunctionObject.IsEmpty() || !hFunctionObject->IsCallable()) { | |
526 if (lpRetValue) | |
527 lpRetValue->ForceSetValue(FXJSE_CreateReturnValue(m_pIsolate, trycatch)); | |
528 return FALSE; | |
529 } | |
530 | |
531 v8::Local<v8::Value> hReturnValue; | |
532 v8::Local<v8::Value>* lpLocalArgs = NULL; | |
533 if (nArgCount) { | |
534 lpLocalArgs = FX_Alloc(v8::Local<v8::Value>, nArgCount); | |
535 for (uint32_t i = 0; i < nArgCount; i++) { | |
536 new (lpLocalArgs + i) v8::Local<v8::Value>; | |
537 CFXJSE_Value* lpArg = (CFXJSE_Value*)lpArgs[i]; | |
538 if (lpArg) { | |
539 lpLocalArgs[i] = | |
540 v8::Local<v8::Value>::New(m_pIsolate, lpArg->DirectGetValue()); | |
541 } | |
542 if (lpLocalArgs[i].IsEmpty()) { | |
543 lpLocalArgs[i] = v8::Undefined(m_pIsolate); | |
544 } | |
545 } | |
546 } | |
547 | |
548 FX_BOOL bRetValue = TRUE; | |
549 if (lpReceiver == FXJSE_INVALID_PTR) { | |
550 v8::MaybeLocal<v8::Value> maybe_retvalue = | |
551 hFunctionObject->CallAsConstructor(m_pIsolate->GetCurrentContext(), | |
552 nArgCount, lpLocalArgs); | |
553 hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>()); | |
554 } else { | |
555 v8::Local<v8::Value> hReceiver; | |
556 if (lpReceiver) { | |
557 hReceiver = | |
558 v8::Local<v8::Value>::New(m_pIsolate, lpReceiver->DirectGetValue()); | |
559 } | |
560 if (hReceiver.IsEmpty() || !hReceiver->IsObject()) | |
561 hReceiver = v8::Object::New(m_pIsolate); | |
562 | |
563 v8::MaybeLocal<v8::Value> maybe_retvalue = hFunctionObject->CallAsFunction( | |
564 m_pIsolate->GetCurrentContext(), hReceiver, nArgCount, lpLocalArgs); | |
565 hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>()); | |
566 } | |
567 | |
568 if (trycatch.HasCaught()) { | |
569 hReturnValue = FXJSE_CreateReturnValue(m_pIsolate, trycatch); | |
570 bRetValue = FALSE; | |
571 } | |
572 | |
573 if (lpRetValue) | |
574 lpRetValue->ForceSetValue(hReturnValue); | |
575 | |
576 if (lpLocalArgs) { | |
577 for (uint32_t i = 0; i < nArgCount; i++) | |
578 lpLocalArgs[i].~Local(); | |
579 FX_Free(lpLocalArgs); | |
580 } | |
581 return bRetValue; | |
582 } | |
OLD | NEW |