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/global.h" | 7 #include "fpdfsdk/javascript/global.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "core/fxcrt/include/fx_ext.h" | 11 #include "core/fxcrt/include/fx_ext.h" |
12 #include "fpdfsdk/javascript/JS_Define.h" | 12 #include "fpdfsdk/javascript/JS_Define.h" |
13 #include "fpdfsdk/javascript/JS_EventHandler.h" | 13 #include "fpdfsdk/javascript/JS_EventHandler.h" |
14 #include "fpdfsdk/javascript/JS_GlobalData.h" | 14 #include "fpdfsdk/javascript/JS_GlobalData.h" |
15 #include "fpdfsdk/javascript/JS_Object.h" | 15 #include "fpdfsdk/javascript/JS_Object.h" |
16 #include "fpdfsdk/javascript/JS_Value.h" | 16 #include "fpdfsdk/javascript/JS_Value.h" |
17 #include "fpdfsdk/javascript/cjs_context.h" | 17 #include "fpdfsdk/javascript/cjs_context.h" |
18 #include "fpdfsdk/javascript/resource.h" | 18 #include "fpdfsdk/javascript/resource.h" |
19 | 19 |
20 // Helper class for compile-time calculation of hash values in order to | |
21 // avoid having global object initializers. | |
22 template <unsigned ACC, wchar_t... Ns> | |
23 struct CHash; | |
24 | |
25 // Only needed to hash single-character strings. | |
26 template <wchar_t N> | |
27 struct CHash<N> { | |
28 static const unsigned value = N; | |
29 }; | |
30 | |
31 template <unsigned ACC, wchar_t N> | |
32 struct CHash<ACC, N> { | |
33 static const unsigned value = (ACC * 1313LLU + N) & 0xFFFFFFFF; | |
34 }; | |
35 | |
36 template <unsigned ACC, wchar_t N, wchar_t... Ns> | |
37 struct CHash<ACC, N, Ns...> { | |
38 static const unsigned value = CHash<CHash<ACC, N>::value, Ns...>::value; | |
39 }; | |
40 | |
41 const unsigned int JSCONST_nStringHash = | |
42 CHash<'s', 't', 'r', 'i', 'n', 'g'>::value; | |
43 const unsigned int JSCONST_nNumberHash = | |
44 CHash<'n', 'u', 'm', 'b', 'e', 'r'>::value; | |
45 const unsigned int JSCONST_nBoolHash = | |
46 CHash<'b', 'o', 'o', 'l', 'e', 'a', 'n'>::value; | |
47 const unsigned int JSCONST_nDateHash = CHash<'d', 'a', 't', 'e'>::value; | |
48 const unsigned int JSCONST_nObjectHash = | |
49 CHash<'o', 'b', 'j', 'e', 'c', 't'>::value; | |
50 const unsigned int JSCONST_nFXobjHash = CHash<'f', 'x', 'o', 'b', 'j'>::value; | |
51 const unsigned int JSCONST_nNullHash = CHash<'n', 'u', 'l', 'l'>::value; | |
52 const unsigned int JSCONST_nUndefHash = | |
53 CHash<'u', 'n', 'd', 'e', 'f', 'i', 'n', 'e', 'd'>::value; | |
54 | |
55 static unsigned JS_CalcHash(const wchar_t* main) { | |
56 return (unsigned)FX_HashCode_GetW(CFX_WideStringC(main), false); | |
57 } | |
58 | |
59 #ifndef NDEBUG | |
60 class HashVerify { | |
61 public: | |
62 HashVerify(); | |
63 } g_hashVerify; | |
64 | |
65 HashVerify::HashVerify() { | |
66 ASSERT(JSCONST_nStringHash == JS_CalcHash(kFXJSValueNameString)); | |
67 ASSERT(JSCONST_nNumberHash == JS_CalcHash(kFXJSValueNameNumber)); | |
68 ASSERT(JSCONST_nBoolHash == JS_CalcHash(kFXJSValueNameBoolean)); | |
69 ASSERT(JSCONST_nDateHash == JS_CalcHash(kFXJSValueNameDate)); | |
70 ASSERT(JSCONST_nObjectHash == JS_CalcHash(kFXJSValueNameObject)); | |
71 ASSERT(JSCONST_nFXobjHash == JS_CalcHash(kFXJSValueNameFxobj)); | |
72 ASSERT(JSCONST_nNullHash == JS_CalcHash(kFXJSValueNameNull)); | |
73 ASSERT(JSCONST_nUndefHash == JS_CalcHash(kFXJSValueNameUndefined)); | |
74 } | |
75 #endif | |
76 | |
77 BEGIN_JS_STATIC_CONST(CJS_Global) | 20 BEGIN_JS_STATIC_CONST(CJS_Global) |
78 END_JS_STATIC_CONST() | 21 END_JS_STATIC_CONST() |
79 | 22 |
80 BEGIN_JS_STATIC_PROP(CJS_Global) | 23 BEGIN_JS_STATIC_PROP(CJS_Global) |
81 END_JS_STATIC_PROP() | 24 END_JS_STATIC_PROP() |
82 | 25 |
83 BEGIN_JS_STATIC_METHOD(CJS_Global) | 26 BEGIN_JS_STATIC_METHOD(CJS_Global) |
84 JS_STATIC_METHOD_ENTRY(setPersistent) | 27 JS_STATIC_METHOD_ENTRY(setPersistent) |
85 END_JS_STATIC_METHOD() | 28 END_JS_STATIC_METHOD() |
86 | 29 |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 CJS_GlobalVariableArray& array) { | 271 CJS_GlobalVariableArray& array) { |
329 v8::Isolate* isolate = pObj->GetIsolate(); | 272 v8::Isolate* isolate = pObj->GetIsolate(); |
330 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 273 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
331 | 274 |
332 v8::Local<v8::Array> pKeyList = FXJS_GetObjectElementNames(isolate, pObj); | 275 v8::Local<v8::Array> pKeyList = FXJS_GetObjectElementNames(isolate, pObj); |
333 int nObjElements = pKeyList->Length(); | 276 int nObjElements = pKeyList->Length(); |
334 for (int i = 0; i < nObjElements; i++) { | 277 for (int i = 0; i < nObjElements; i++) { |
335 CFX_WideString ws = | 278 CFX_WideString ws = |
336 FXJS_ToString(isolate, FXJS_GetArrayElement(isolate, pKeyList, i)); | 279 FXJS_ToString(isolate, FXJS_GetArrayElement(isolate, pKeyList, i)); |
337 CFX_ByteString sKey = ws.UTF8Encode(); | 280 CFX_ByteString sKey = ws.UTF8Encode(); |
338 | |
339 v8::Local<v8::Value> v = FXJS_GetObjectElement(isolate, pObj, ws); | 281 v8::Local<v8::Value> v = FXJS_GetObjectElement(isolate, pObj, ws); |
340 switch (GET_VALUE_TYPE(v)) { | 282 switch (CJS_Value::GetValueType(v)) { |
341 case CJS_Value::VT_number: { | 283 case CJS_Value::VT_number: { |
342 CJS_KeyValue* pObjElement = new CJS_KeyValue; | 284 CJS_KeyValue* pObjElement = new CJS_KeyValue; |
343 pObjElement->nType = JS_GLOBALDATA_TYPE_NUMBER; | 285 pObjElement->nType = JS_GLOBALDATA_TYPE_NUMBER; |
344 pObjElement->sKey = sKey; | 286 pObjElement->sKey = sKey; |
345 pObjElement->dData = FXJS_ToNumber(isolate, v); | 287 pObjElement->dData = FXJS_ToNumber(isolate, v); |
346 array.Add(pObjElement); | 288 array.Add(pObjElement); |
347 } break; | 289 } break; |
348 case CJS_Value::VT_boolean: { | 290 case CJS_Value::VT_boolean: { |
349 CJS_KeyValue* pObjElement = new CJS_KeyValue; | 291 CJS_KeyValue* pObjElement = new CJS_KeyValue; |
350 pObjElement->nType = JS_GLOBALDATA_TYPE_BOOLEAN; | 292 pObjElement->nType = JS_GLOBALDATA_TYPE_BOOLEAN; |
351 pObjElement->sKey = sKey; | 293 pObjElement->sKey = sKey; |
352 pObjElement->dData = FXJS_ToBoolean(isolate, v); | 294 pObjElement->dData = FXJS_ToBoolean(isolate, v); |
353 array.Add(pObjElement); | 295 array.Add(pObjElement); |
354 } break; | 296 } break; |
355 case CJS_Value::VT_string: { | 297 case CJS_Value::VT_string: { |
356 CFX_ByteString sValue = | 298 CFX_ByteString sValue = CJS_Value(pRuntime, v).ToCFXByteString(); |
357 CJS_Value(pRuntime, v, CJS_Value::VT_string).ToCFXByteString(); | |
358 CJS_KeyValue* pObjElement = new CJS_KeyValue; | 299 CJS_KeyValue* pObjElement = new CJS_KeyValue; |
359 pObjElement->nType = JS_GLOBALDATA_TYPE_STRING; | 300 pObjElement->nType = JS_GLOBALDATA_TYPE_STRING; |
360 pObjElement->sKey = sKey; | 301 pObjElement->sKey = sKey; |
361 pObjElement->sData = sValue; | 302 pObjElement->sData = sValue; |
362 array.Add(pObjElement); | 303 array.Add(pObjElement); |
363 } break; | 304 } break; |
364 case CJS_Value::VT_object: { | 305 case CJS_Value::VT_object: { |
365 CJS_KeyValue* pObjElement = new CJS_KeyValue; | 306 CJS_KeyValue* pObjElement = new CJS_KeyValue; |
366 pObjElement->nType = JS_GLOBALDATA_TYPE_OBJECT; | 307 pObjElement->nType = JS_GLOBALDATA_TYPE_OBJECT; |
367 pObjElement->sKey = sKey; | 308 pObjElement->sKey = sKey; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 pNewData->nType = JS_GLOBALDATA_TYPE_NULL; | 434 pNewData->nType = JS_GLOBALDATA_TYPE_NULL; |
494 pNewData->bPersistent = bDefaultPersistent; | 435 pNewData->bPersistent = bDefaultPersistent; |
495 } break; | 436 } break; |
496 default: | 437 default: |
497 return FALSE; | 438 return FALSE; |
498 } | 439 } |
499 | 440 |
500 m_mapGlobal[propname] = pNewData; | 441 m_mapGlobal[propname] = pNewData; |
501 return TRUE; | 442 return TRUE; |
502 } | 443 } |
503 | |
504 CJS_Value::Type GET_VALUE_TYPE(v8::Local<v8::Value> p) { | |
505 const unsigned int nHash = JS_CalcHash(FXJS_GetTypeof(p)); | |
506 | |
507 if (nHash == JSCONST_nUndefHash) | |
508 return CJS_Value::VT_undefined; | |
509 if (nHash == JSCONST_nNullHash) | |
510 return CJS_Value::VT_null; | |
511 if (nHash == JSCONST_nStringHash) | |
512 return CJS_Value::VT_string; | |
513 if (nHash == JSCONST_nNumberHash) | |
514 return CJS_Value::VT_number; | |
515 if (nHash == JSCONST_nBoolHash) | |
516 return CJS_Value::VT_boolean; | |
517 if (nHash == JSCONST_nDateHash) | |
518 return CJS_Value::VT_date; | |
519 if (nHash == JSCONST_nObjectHash) | |
520 return CJS_Value::VT_object; | |
521 if (nHash == JSCONST_nFXobjHash) | |
522 return CJS_Value::VT_fxobject; | |
523 | |
524 return CJS_Value::VT_unknown; | |
525 } | |
OLD | NEW |