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 12 matching lines...) Expand all Loading... | |
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_PerObjectData { | 31 class CFXJS_PerObjectData { |
32 public: | 32 public: |
33 CFXJS_PerObjectData(int nObjDefID) | 33 explicit CFXJS_PerObjectData(int nObjDefID) |
34 : m_ObjDefID(nObjDefID), m_pPrivate(nullptr) {} | 34 : m_ObjDefID(nObjDefID), m_pPrivate(nullptr) {} |
35 | 35 |
36 int m_ObjDefID; | 36 int m_ObjDefID; |
Tom Sepez
2015/11/09 21:25:32
nit: just noticed this could be const?
Lei Zhang
2015/11/09 22:45:29
Done.
| |
37 void* m_pPrivate; | 37 void* m_pPrivate; |
38 }; | 38 }; |
39 | 39 |
40 class CFXJS_ObjDefinition { | 40 class CFXJS_ObjDefinition { |
41 public: | 41 public: |
42 static int MaxID(v8::Isolate* pIsolate) { | 42 static size_t MaxID(v8::Isolate* pIsolate) { |
43 return static_cast<int>( | 43 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.size(); |
44 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetSize()); | |
45 } | 44 } |
46 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { | 45 |
46 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, size_t id) { | |
Tom Sepez
2015/11/09 21:25:32
some places ID is an int, other places it's a size
Lei Zhang
2015/11/09 22:45:29
I went back to int, since we use -1 in some places
| |
47 // Note: GetAt() halts if out-of-range even in release builds. | 47 // Note: GetAt() halts if out-of-range even in release builds. |
48 return static_cast<CFXJS_ObjDefinition*>( | 48 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id]; |
49 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); | |
50 } | 49 } |
50 | |
51 CFXJS_ObjDefinition(v8::Isolate* isolate, | 51 CFXJS_ObjDefinition(v8::Isolate* isolate, |
52 const wchar_t* sObjName, | 52 const wchar_t* sObjName, |
53 FXJSOBJTYPE eObjType, | 53 FXJSOBJTYPE eObjType, |
54 FXJS_CONSTRUCTOR pConstructor, | 54 FXJS_CONSTRUCTOR pConstructor, |
55 FXJS_DESTRUCTOR pDestructor) | 55 FXJS_DESTRUCTOR pDestructor) |
56 : m_ObjName(sObjName), | 56 : m_ObjName(sObjName), |
57 m_ObjType(eObjType), | 57 m_ObjType(eObjType), |
58 m_pConstructor(pConstructor), | 58 m_pConstructor(pConstructor), |
59 m_pDestructor(pDestructor), | 59 m_pDestructor(pDestructor), |
60 m_pIsolate(isolate) { | 60 m_pIsolate(isolate) { |
61 v8::Isolate::Scope isolate_scope(isolate); | 61 v8::Isolate::Scope isolate_scope(isolate); |
62 v8::HandleScope handle_scope(isolate); | 62 v8::HandleScope handle_scope(isolate); |
63 | 63 |
64 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); | 64 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); |
65 fun->InstanceTemplate()->SetInternalFieldCount(2); | 65 fun->InstanceTemplate()->SetInternalFieldCount(2); |
66 m_FunctionTemplate.Reset(isolate, fun); | 66 m_FunctionTemplate.Reset(isolate, fun); |
67 | 67 |
68 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); | 68 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); |
69 m_Signature.Reset(isolate, sig); | 69 m_Signature.Reset(isolate, sig); |
70 } | 70 } |
71 | 71 |
72 int AssignID() { | 72 int AssignID() { |
73 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); | 73 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); |
74 pData->m_ObjectDefnArray.Add(this); | 74 pData->m_ObjectDefnArray.push_back(this); |
75 return pData->m_ObjectDefnArray.GetSize() - 1; | 75 return pData->m_ObjectDefnArray.size() - 1; |
76 } | 76 } |
77 | 77 |
78 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { | 78 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { |
79 v8::EscapableHandleScope scope(m_pIsolate); | 79 v8::EscapableHandleScope scope(m_pIsolate); |
80 v8::Local<v8::FunctionTemplate> function = | 80 v8::Local<v8::FunctionTemplate> function = |
81 m_FunctionTemplate.Get(m_pIsolate); | 81 m_FunctionTemplate.Get(m_pIsolate); |
82 return scope.Escape(function->InstanceTemplate()); | 82 return scope.Escape(function->InstanceTemplate()); |
83 } | 83 } |
84 | 84 |
85 v8::Local<v8::Signature> GetSignature() { | 85 v8::Local<v8::Signature> GetSignature() { |
86 v8::EscapableHandleScope scope(m_pIsolate); | 86 v8::EscapableHandleScope scope(m_pIsolate); |
87 return scope.Escape(m_Signature.Get(m_pIsolate)); | 87 return scope.Escape(m_Signature.Get(m_pIsolate)); |
88 } | 88 } |
89 | 89 |
90 const wchar_t* const m_ObjName; | 90 const wchar_t* const m_ObjName; |
91 const FXJSOBJTYPE m_ObjType; | 91 const FXJSOBJTYPE m_ObjType; |
92 const FXJS_CONSTRUCTOR m_pConstructor; | 92 const FXJS_CONSTRUCTOR m_pConstructor; |
93 const FXJS_DESTRUCTOR m_pDestructor; | 93 const FXJS_DESTRUCTOR m_pDestructor; |
94 | 94 |
95 v8::Isolate* m_pIsolate; | 95 v8::Isolate* m_pIsolate; |
96 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; | 96 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; |
97 v8::Global<v8::Signature> m_Signature; | 97 v8::Global<v8::Signature> m_Signature; |
98 v8::Global<v8::Object> m_StaticObj; | 98 v8::Global<v8::Object> m_StaticObj; |
99 }; | 99 }; |
100 | 100 |
101 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( | 101 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
102 v8::Isolate* pIsolate) { | 102 v8::Isolate* pIsolate) { |
103 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 103 size_t maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
104 for (int i = 0; i < maxID; ++i) { | 104 for (size_t i = 0; i < maxID; ++i) { |
105 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 105 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
106 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) | 106 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) |
107 return pObjDef->GetInstanceTemplate(); | 107 return pObjDef->GetInstanceTemplate(); |
108 } | 108 } |
109 if (!g_DefaultGlobalObjectTemplate) { | 109 if (!g_DefaultGlobalObjectTemplate) { |
110 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; | 110 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; |
111 g_DefaultGlobalObjectTemplate->Reset(pIsolate, | 111 g_DefaultGlobalObjectTemplate->Reset(pIsolate, |
112 v8::ObjectTemplate::New(pIsolate)); | 112 v8::ObjectTemplate::New(pIsolate)); |
113 } | 113 } |
114 return g_DefaultGlobalObjectTemplate->Get(pIsolate); | 114 return g_DefaultGlobalObjectTemplate->Get(pIsolate); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 | 281 |
282 v8::Isolate::Scope isolate_scope(pIsolate); | 282 v8::Isolate::Scope isolate_scope(pIsolate); |
283 v8::HandleScope handle_scope(pIsolate); | 283 v8::HandleScope handle_scope(pIsolate); |
284 v8::Local<v8::Context> v8Context = | 284 v8::Local<v8::Context> v8Context = |
285 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 285 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
286 v8::Context::Scope context_scope(v8Context); | 286 v8::Context::Scope context_scope(v8Context); |
287 | 287 |
288 FXJS_PerIsolateData::SetUp(pIsolate); | 288 FXJS_PerIsolateData::SetUp(pIsolate); |
289 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); | 289 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); |
290 | 290 |
291 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 291 size_t maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
292 for (int i = 0; i < maxID; ++i) { | 292 for (size_t i = 0; i < maxID; ++i) { |
293 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 293 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
294 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | 294 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
295 v8::Local<v8::String> m_ObjName = | 295 v8::Local<v8::String> m_ObjName = |
296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
297 v8::NewStringType::kNormal, | 297 v8::NewStringType::kNormal, |
298 bs.GetLength()).ToLocalChecked(); | 298 bs.GetLength()).ToLocalChecked(); |
299 | 299 |
300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
301 v8Context->Global() | 301 v8Context->Global() |
302 ->GetPrototype() | 302 ->GetPrototype() |
(...skipping 23 matching lines...) Expand all Loading... | |
326 v8::Isolate::Scope isolate_scope(pIsolate); | 326 v8::Isolate::Scope isolate_scope(pIsolate); |
327 v8::HandleScope handle_scope(pIsolate); | 327 v8::HandleScope handle_scope(pIsolate); |
328 v8::Local<v8::Context> context = | 328 v8::Local<v8::Context> context = |
329 v8::Local<v8::Context>::New(pIsolate, v8PersistentContext); | 329 v8::Local<v8::Context>::New(pIsolate, v8PersistentContext); |
330 v8::Context::Scope context_scope(context); | 330 v8::Context::Scope context_scope(context); |
331 | 331 |
332 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 332 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
333 if (!pData) | 333 if (!pData) |
334 return; | 334 return; |
335 | 335 |
336 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 336 size_t maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
337 for (int i = 0; i < maxID; ++i) { | 337 for (size_t i = 0; i < maxID; ++i) { |
338 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 338 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
339 v8::Local<v8::Object> pObj; | 339 v8::Local<v8::Object> pObj; |
340 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 340 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
341 pObj = | 341 pObj = |
342 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); | 342 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
343 } else if (!pObjDef->m_StaticObj.IsEmpty()) { | 343 } else if (!pObjDef->m_StaticObj.IsEmpty()) { |
344 pObj = v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj); | 344 pObj = v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj); |
345 } | 345 } |
346 | 346 |
347 if (!pObj.IsEmpty()) { | 347 if (!pObj.IsEmpty()) { |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
546 return v8::Local<v8::Array>(); | 546 return v8::Local<v8::Array>(); |
547 v8::Local<v8::Array> val; | 547 v8::Local<v8::Array> val; |
548 if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val)) | 548 if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val)) |
549 return v8::Local<v8::Array>(); | 549 return v8::Local<v8::Array>(); |
550 return val; | 550 return val; |
551 } | 551 } |
552 | 552 |
553 void FXJS_PutObjectString(v8::Isolate* pIsolate, | 553 void FXJS_PutObjectString(v8::Isolate* pIsolate, |
554 v8::Local<v8::Object> pObj, | 554 v8::Local<v8::Object> pObj, |
555 const wchar_t* PropertyName, | 555 const wchar_t* PropertyName, |
556 const wchar_t* sValue) // VT_string | 556 const wchar_t* sValue) { |
557 { | |
558 if (pObj.IsEmpty()) | 557 if (pObj.IsEmpty()) |
559 return; | 558 return; |
560 pObj->Set(pIsolate->GetCurrentContext(), | 559 pObj->Set(pIsolate->GetCurrentContext(), |
561 FXJS_WSToJSString(pIsolate, PropertyName), | 560 FXJS_WSToJSString(pIsolate, PropertyName), |
562 FXJS_WSToJSString(pIsolate, sValue)).FromJust(); | 561 FXJS_WSToJSString(pIsolate, sValue)).FromJust(); |
563 } | 562 } |
564 | 563 |
565 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | 564 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, |
566 v8::Local<v8::Object> pObj, | 565 v8::Local<v8::Object> pObj, |
567 const wchar_t* PropertyName, | 566 const wchar_t* PropertyName, |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
745 return v8::Local<v8::Array>(); | 744 return v8::Local<v8::Array>(); |
746 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 745 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
747 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 746 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
748 } | 747 } |
749 | 748 |
750 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 749 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
751 pTo = pFrom; | 750 pTo = pFrom; |
752 } | 751 } |
753 | 752 |
754 | 753 |
OLD | NEW |