| 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 "fxjs/include/fxjs_v8.h" | 7 #include "fxjs/include/fxjs_v8.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 class CFXJS_ObjDefinition { | 33 class CFXJS_ObjDefinition { |
| 34 public: | 34 public: |
| 35 static int MaxID(v8::Isolate* pIsolate) { | 35 static int MaxID(v8::Isolate* pIsolate) { |
| 36 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.size(); | 36 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.size(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { | 39 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
| 40 // Note: GetAt() halts if out-of-range even in release builds. | 40 // Note: GetAt() halts if out-of-range even in release builds. |
| 41 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id]; | 41 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id].get(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 CFXJS_ObjDefinition(v8::Isolate* isolate, | 44 CFXJS_ObjDefinition(v8::Isolate* isolate, |
| 45 const wchar_t* sObjName, | 45 const wchar_t* sObjName, |
| 46 FXJSOBJTYPE eObjType, | 46 FXJSOBJTYPE eObjType, |
| 47 CFXJS_Engine::Constructor pConstructor, | 47 CFXJS_Engine::Constructor pConstructor, |
| 48 CFXJS_Engine::Destructor pDestructor) | 48 CFXJS_Engine::Destructor pDestructor) |
| 49 : m_ObjName(sObjName), | 49 : m_ObjName(sObjName), |
| 50 m_ObjType(eObjType), | 50 m_ObjType(eObjType), |
| 51 m_pConstructor(pConstructor), | 51 m_pConstructor(pConstructor), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 63 .ToLocalChecked()); | 63 .ToLocalChecked()); |
| 64 } | 64 } |
| 65 m_FunctionTemplate.Reset(isolate, fun); | 65 m_FunctionTemplate.Reset(isolate, fun); |
| 66 | 66 |
| 67 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); | 67 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); |
| 68 m_Signature.Reset(isolate, sig); | 68 m_Signature.Reset(isolate, sig); |
| 69 } | 69 } |
| 70 | 70 |
| 71 int AssignID() { | 71 int AssignID() { |
| 72 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); | 72 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); |
| 73 pData->m_ObjectDefnArray.push_back(this); | 73 pData->m_ObjectDefnArray.emplace_back(this); |
| 74 return pData->m_ObjectDefnArray.size() - 1; | 74 return pData->m_ObjectDefnArray.size() - 1; |
| 75 } | 75 } |
| 76 | 76 |
| 77 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { | 77 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { |
| 78 v8::EscapableHandleScope scope(m_pIsolate); | 78 v8::EscapableHandleScope scope(m_pIsolate); |
| 79 v8::Local<v8::FunctionTemplate> function = | 79 v8::Local<v8::FunctionTemplate> function = |
| 80 m_FunctionTemplate.Get(m_pIsolate); | 80 m_FunctionTemplate.Get(m_pIsolate); |
| 81 return scope.Escape(function->InstanceTemplate()); | 81 return scope.Escape(function->InstanceTemplate()); |
| 82 } | 82 } |
| 83 | 83 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 if (!pObjDef) | 143 if (!pObjDef) |
| 144 return; | 144 return; |
| 145 if (pObjDef->m_pDestructor) | 145 if (pObjDef->m_pDestructor) |
| 146 pObjDef->m_pDestructor(pEngine, obj); | 146 pObjDef->m_pDestructor(pEngine, obj); |
| 147 CFXJS_Engine::FreeObjectPrivate(obj); | 147 CFXJS_Engine::FreeObjectPrivate(obj); |
| 148 } | 148 } |
| 149 | 149 |
| 150 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( | 150 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( |
| 151 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { | 151 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 152 V8TemplateMap* pMap = | 152 V8TemplateMap* pMap = |
| 153 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap; | 153 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap.get(); |
| 154 return pMap ? &pMap->m_map : nullptr; | 154 return pMap ? &pMap->m_map : nullptr; |
| 155 } | 155 } |
| 156 | 156 |
| 157 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { | 157 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { |
| 158 if (g_isolate) { | 158 if (g_isolate) { |
| 159 ASSERT(g_embedderDataSlot == embedderDataSlot); | 159 ASSERT(g_embedderDataSlot == embedderDataSlot); |
| 160 ASSERT(g_isolate == pIsolate); | 160 ASSERT(g_isolate == pIsolate); |
| 161 return; | 161 return; |
| 162 } | 162 } |
| 163 g_embedderDataSlot = embedderDataSlot; | 163 g_embedderDataSlot = embedderDataSlot; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 void V8TemplateMap::set(void* key, v8::Local<v8::Object> handle) { | 199 void V8TemplateMap::set(void* key, v8::Local<v8::Object> handle) { |
| 200 ASSERT(!m_map.Contains(key)); | 200 ASSERT(!m_map.Contains(key)); |
| 201 m_map.Set(key, handle); | 201 m_map.Set(key, handle); |
| 202 } | 202 } |
| 203 | 203 |
| 204 FXJS_PerIsolateData::~FXJS_PerIsolateData() {} | 204 FXJS_PerIsolateData::~FXJS_PerIsolateData() {} |
| 205 | 205 |
| 206 // static | 206 // static |
| 207 void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) { | 207 void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) { |
| 208 if (!pIsolate->GetData(g_embedderDataSlot)) | 208 if (!pIsolate->GetData(g_embedderDataSlot)) |
| 209 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData()); | 209 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData(pIsolate)); |
| 210 } | 210 } |
| 211 | 211 |
| 212 // static | 212 // static |
| 213 FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { | 213 FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { |
| 214 return static_cast<FXJS_PerIsolateData*>( | 214 return static_cast<FXJS_PerIsolateData*>( |
| 215 pIsolate->GetData(g_embedderDataSlot)); | 215 pIsolate->GetData(g_embedderDataSlot)); |
| 216 } | 216 } |
| 217 | 217 |
| 218 FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} | 218 FXJS_PerIsolateData::FXJS_PerIsolateData(v8::Isolate* pIsolate) |
| 219 : m_pDynamicObjsMap(new V8TemplateMap(pIsolate)) {} |
| 219 | 220 |
| 220 CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {} | 221 CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {} |
| 221 | 222 |
| 222 CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : m_isolate(pIsolate) {} | 223 CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : m_isolate(pIsolate) {} |
| 223 | 224 |
| 224 CFXJS_Engine::~CFXJS_Engine() { | 225 CFXJS_Engine::~CFXJS_Engine() { |
| 225 m_V8PersistentContext.Reset(); | 226 m_V8PersistentContext.Reset(); |
| 226 } | 227 } |
| 227 | 228 |
| 228 // static | 229 // static |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 v8::HandleScope handle_scope(m_isolate); | 374 v8::HandleScope handle_scope(m_isolate); |
| 374 | 375 |
| 375 // This has to happen before we call GetGlobalObjectTemplate because that | 376 // This has to happen before we call GetGlobalObjectTemplate because that |
| 376 // method gets the PerIsolateData from m_isolate. | 377 // method gets the PerIsolateData from m_isolate. |
| 377 FXJS_PerIsolateData::SetUp(m_isolate); | 378 FXJS_PerIsolateData::SetUp(m_isolate); |
| 378 | 379 |
| 379 v8::Local<v8::Context> v8Context = | 380 v8::Local<v8::Context> v8Context = |
| 380 v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate)); | 381 v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate)); |
| 381 v8::Context::Scope context_scope(v8Context); | 382 v8::Context::Scope context_scope(v8Context); |
| 382 | 383 |
| 383 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate); | |
| 384 if (!pData) | |
| 385 return; | |
| 386 pData->CreateDynamicObjsMap(m_isolate); | |
| 387 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this); | 384 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this); |
| 388 | 385 |
| 389 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate); | 386 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate); |
| 390 m_StaticObjects.resize(maxID + 1); | 387 m_StaticObjects.resize(maxID + 1); |
| 391 for (int i = 0; i < maxID; ++i) { | 388 for (int i = 0; i < maxID; ++i) { |
| 392 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i); | 389 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i); |
| 393 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 390 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 394 v8Context->Global() | 391 v8Context->Global() |
| 395 ->GetPrototype() | 392 ->GetPrototype() |
| 396 ->ToObject(v8Context) | 393 ->ToObject(v8Context) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 pObjDef->m_pDestructor(this, pObj); | 445 pObjDef->m_pDestructor(this, pObj); |
| 449 FreeObjectPrivate(pObj); | 446 FreeObjectPrivate(pObj); |
| 450 } | 447 } |
| 451 } | 448 } |
| 452 | 449 |
| 453 m_V8PersistentContext.Reset(); | 450 m_V8PersistentContext.Reset(); |
| 454 | 451 |
| 455 if (m_isolate == g_isolate && --g_isolate_ref_count > 0) | 452 if (m_isolate == g_isolate && --g_isolate_ref_count > 0) |
| 456 return; | 453 return; |
| 457 | 454 |
| 458 pData->ReleaseDynamicObjsMap(); | 455 delete pData; |
| 459 for (int i = 0; i < maxID; ++i) | |
| 460 delete CFXJS_ObjDefinition::ForID(m_isolate, i); | |
| 461 | |
| 462 m_isolate->SetData(g_embedderDataSlot, nullptr); | 456 m_isolate->SetData(g_embedderDataSlot, nullptr); |
| 463 delete pData; | |
| 464 } | 457 } |
| 465 | 458 |
| 466 int CFXJS_Engine::Execute(const CFX_WideString& script, FXJSErr* pError) { | 459 int CFXJS_Engine::Execute(const CFX_WideString& script, FXJSErr* pError) { |
| 467 v8::Isolate::Scope isolate_scope(m_isolate); | 460 v8::Isolate::Scope isolate_scope(m_isolate); |
| 468 v8::TryCatch try_catch(m_isolate); | 461 v8::TryCatch try_catch(m_isolate); |
| 469 CFX_ByteString bsScript = script.UTF8Encode(); | 462 CFX_ByteString bsScript = script.UTF8Encode(); |
| 470 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); | 463 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 471 v8::Local<v8::Script> compiled_script; | 464 v8::Local<v8::Script> compiled_script; |
| 472 if (!v8::Script::Compile(context, | 465 if (!v8::Script::Compile(context, |
| 473 v8::String::NewFromUtf8(m_isolate, bsScript.c_str(), | 466 v8::String::NewFromUtf8(m_isolate, bsScript.c_str(), |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 } | 790 } |
| 798 | 791 |
| 799 void CFXJS_Engine::SetConstArray(const CFX_WideString& name, | 792 void CFXJS_Engine::SetConstArray(const CFX_WideString& name, |
| 800 v8::Local<v8::Array> array) { | 793 v8::Local<v8::Array> array) { |
| 801 m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array); | 794 m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array); |
| 802 } | 795 } |
| 803 | 796 |
| 804 v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const CFX_WideString& name) { | 797 v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const CFX_WideString& name) { |
| 805 return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]); | 798 return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]); |
| 806 } | 799 } |
| OLD | NEW |