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