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 "../../include/jsapi/fxjs_v8.h" | 7 #include "../../include/jsapi/fxjs_v8.h" |
8 | 8 |
9 #include "../../../core/include/fxcrt/fx_basic.h" | 9 #include "../../../core/include/fxcrt/fx_basic.h" |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 // (without actually requiring a dependency on gin itself for the standalone | 21 // (without actually requiring a dependency on gin itself for the standalone |
22 // embedders of PDFIum). The value we want to use is: | 22 // embedders of PDFIum). The value we want to use is: |
23 // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. | 23 // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. |
24 static const unsigned int kPerContextDataIndex = 3u; | 24 static const unsigned int kPerContextDataIndex = 3u; |
25 static unsigned int g_embedderDataSlot = 1u; | 25 static unsigned int g_embedderDataSlot = 1u; |
26 static v8::Isolate* g_isolate = nullptr; | 26 static v8::Isolate* g_isolate = nullptr; |
27 static size_t g_isolate_ref_count = 0; | 27 static size_t g_isolate_ref_count = 0; |
28 static FXJS_ArrayBufferAllocator* g_arrayBufferAllocator = nullptr; | 28 static FXJS_ArrayBufferAllocator* g_arrayBufferAllocator = nullptr; |
29 static v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr; | 29 static v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr; |
30 | 30 |
31 class CFXJS_PrivateData { | 31 class CFXJS_PerObjectData { |
32 public: | 32 public: |
33 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} | 33 CFXJS_PerObjectData(int nObjDefID) |
| 34 : m_ObjDefID(nObjDefID), m_pPrivate(nullptr) {} |
34 | 35 |
35 int ObjDefID; | 36 int m_ObjDefID; |
36 void* pPrivate; | 37 void* m_pPrivate; |
37 }; | 38 }; |
38 | 39 |
39 class CFXJS_ObjDefinition { | 40 class CFXJS_ObjDefinition { |
40 public: | 41 public: |
41 static int MaxID(v8::Isolate* pIsolate) { | 42 static int MaxID(v8::Isolate* pIsolate) { |
42 return static_cast<int>( | 43 return static_cast<int>( |
43 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetSize()); | 44 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetSize()); |
44 } | 45 } |
45 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { | 46 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
46 // Note: GetAt() halts if out-of-range even in release builds. | 47 // Note: GetAt() halts if out-of-range even in release builds. |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 v8::Local<v8::String> m_ObjName = | 296 v8::Local<v8::String> m_ObjName = |
296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 297 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
297 v8::NewStringType::kNormal, | 298 v8::NewStringType::kNormal, |
298 bs.GetLength()).ToLocalChecked(); | 299 bs.GetLength()).ToLocalChecked(); |
299 | 300 |
300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 301 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
301 v8Context->Global() | 302 v8Context->Global() |
302 ->GetPrototype() | 303 ->GetPrototype() |
303 ->ToObject(v8Context) | 304 ->ToObject(v8Context) |
304 .ToLocalChecked() | 305 .ToLocalChecked() |
305 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); | 306 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); |
306 | 307 |
307 if (pObjDef->m_pConstructor) | 308 if (pObjDef->m_pConstructor) |
308 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() | 309 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() |
309 ->GetPrototype() | 310 ->GetPrototype() |
310 ->ToObject(v8Context) | 311 ->ToObject(v8Context) |
311 .ToLocalChecked()); | 312 .ToLocalChecked()); |
312 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 313 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
313 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); | 314 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); |
314 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 315 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
315 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 316 pObjDef->m_StaticObj.Reset(pIsolate, obj); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 | 414 |
414 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 415 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
415 return v8::Local<v8::Object>(); | 416 return v8::Local<v8::Object>(); |
416 | 417 |
417 CFXJS_ObjDefinition* pObjDef = | 418 CFXJS_ObjDefinition* pObjDef = |
418 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 419 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
419 v8::Local<v8::Object> obj; | 420 v8::Local<v8::Object> obj; |
420 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) | 421 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
421 return v8::Local<v8::Object>(); | 422 return v8::Local<v8::Object>(); |
422 | 423 |
423 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); | 424 obj->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(nObjDefnID)); |
424 if (pObjDef->m_pConstructor) | 425 if (pObjDef->m_pConstructor) |
425 pObjDef->m_pConstructor(pIRuntime, obj); | 426 pObjDef->m_pConstructor(pIRuntime, obj); |
426 | 427 |
427 return obj; | 428 return obj; |
428 } | 429 } |
429 | 430 |
430 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { | 431 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { |
431 v8::Isolate::Scope isolate_scope(pIsolate); | 432 v8::Isolate::Scope isolate_scope(pIsolate); |
432 if (!FXJS_PerIsolateData::Get(pIsolate)) | 433 if (!FXJS_PerIsolateData::Get(pIsolate)) |
433 return v8::Local<v8::Object>(); | 434 return v8::Local<v8::Object>(); |
434 | 435 |
435 // Return the global object. | 436 // Return the global object. |
436 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 437 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
437 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); | 438 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
438 } | 439 } |
439 | 440 |
440 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) { | 441 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) { |
441 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) | 442 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
442 return -1; | 443 return -1; |
443 CFXJS_PrivateData* pPrivateData = | 444 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
444 (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); | 445 pObj->GetAlignedPointerFromInternalField(0)); |
445 if (pPrivateData) | 446 if (pPerObjectData) |
446 return pPrivateData->ObjDefID; | 447 return pPerObjectData->m_ObjDefID; |
447 return -1; | 448 return -1; |
448 } | 449 } |
449 | 450 |
450 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { | 451 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { |
451 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t | 452 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t |
452 // wide-strings isn't handled by v8, so use UTF8 as a common | 453 // wide-strings isn't handled by v8, so use UTF8 as a common |
453 // intermediate format. | 454 // intermediate format. |
454 CFX_ByteString utf8_message = message.UTF8Encode(); | 455 CFX_ByteString utf8_message = message.UTF8Encode(); |
455 pIsolate->ThrowException( | 456 pIsolate->ThrowException( |
456 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(), | 457 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(), |
(...skipping 18 matching lines...) Expand all Loading... |
475 if (pObj->IsUndefined()) | 476 if (pObj->IsUndefined()) |
476 return kFXJSValueNameUndefined; | 477 return kFXJSValueNameUndefined; |
477 return NULL; | 478 return NULL; |
478 } | 479 } |
479 | 480 |
480 void FXJS_SetPrivate(v8::Isolate* pIsolate, | 481 void FXJS_SetPrivate(v8::Isolate* pIsolate, |
481 v8::Local<v8::Object> pObj, | 482 v8::Local<v8::Object> pObj, |
482 void* p) { | 483 void* p) { |
483 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) | 484 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
484 return; | 485 return; |
485 CFXJS_PrivateData* pPrivateData = | 486 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
486 (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); | 487 pObj->GetAlignedPointerFromInternalField(0)); |
487 if (!pPrivateData) | 488 if (!pPerObjectData) |
488 return; | 489 return; |
489 pPrivateData->pPrivate = p; | 490 pPerObjectData->m_pPrivate = p; |
490 } | 491 } |
491 | 492 |
492 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) { | 493 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) { |
493 if (pObj.IsEmpty()) | 494 if (pObj.IsEmpty()) |
494 return nullptr; | 495 return nullptr; |
495 CFXJS_PrivateData* pPrivateData = nullptr; | 496 CFXJS_PerObjectData* pPerObjectData = nullptr; |
496 if (pObj->InternalFieldCount()) { | 497 if (pObj->InternalFieldCount()) { |
497 pPrivateData = | 498 pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
498 (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); | 499 pObj->GetAlignedPointerFromInternalField(0)); |
499 } else { | 500 } else { |
500 // It could be a global proxy object. | 501 // It could be a global proxy object. |
501 v8::Local<v8::Value> v = pObj->GetPrototype(); | 502 v8::Local<v8::Value> v = pObj->GetPrototype(); |
502 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 503 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
503 if (v->IsObject()) { | 504 if (v->IsObject()) { |
504 pPrivateData = (CFXJS_PrivateData*)v->ToObject(context) | 505 pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
505 .ToLocalChecked() | 506 v->ToObject(context) |
506 ->GetAlignedPointerFromInternalField(0); | 507 .ToLocalChecked() |
| 508 ->GetAlignedPointerFromInternalField(0)); |
507 } | 509 } |
508 } | 510 } |
509 return pPrivateData ? pPrivateData->pPrivate : nullptr; | 511 return pPerObjectData ? pPerObjectData->m_pPrivate : nullptr; |
510 } | 512 } |
511 | 513 |
512 void FXJS_FreePrivate(void* pPrivateData) { | 514 void FXJS_FreePrivate(void* pPerObjectData) { |
513 delete (CFXJS_PrivateData*)pPrivateData; | 515 delete static_cast<CFXJS_PerObjectData*>(pPerObjectData); |
514 } | 516 } |
515 | 517 |
516 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) { | 518 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) { |
517 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) | 519 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
518 return; | 520 return; |
519 FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0)); | 521 FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0)); |
520 pObj->SetAlignedPointerInInternalField(0, NULL); | 522 pObj->SetAlignedPointerInInternalField(0, NULL); |
521 } | 523 } |
522 | 524 |
523 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate, | 525 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate, |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 return v8::Local<v8::Array>(); | 750 return v8::Local<v8::Array>(); |
749 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 751 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
750 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 752 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
751 } | 753 } |
752 | 754 |
753 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 755 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
754 pTo = pFrom; | 756 pTo = pFrom; |
755 } | 757 } |
756 | 758 |
757 | 759 |
OLD | NEW |