| 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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; | |
| 99 }; | 98 }; |
| 100 | 99 |
| 101 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( | 100 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
| 102 v8::Isolate* pIsolate) { | 101 v8::Isolate* pIsolate) { |
| 103 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 102 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 104 for (int i = 0; i < maxID; ++i) { | 103 for (int i = 0; i < maxID; ++i) { |
| 105 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 104 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 106 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) | 105 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) |
| 107 return pObjDef->GetInstanceTemplate(); | 106 return pObjDef->GetInstanceTemplate(); |
| 108 } | 107 } |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 v8::Local<v8::Value> pDefault) { | 265 v8::Local<v8::Value> pDefault) { |
| 267 v8::Isolate::Scope isolate_scope(pIsolate); | 266 v8::Isolate::Scope isolate_scope(pIsolate); |
| 268 v8::HandleScope handle_scope(pIsolate); | 267 v8::HandleScope handle_scope(pIsolate); |
| 269 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); | 268 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); |
| 270 GetGlobalObjectTemplate(pIsolate)->Set( | 269 GetGlobalObjectTemplate(pIsolate)->Set( |
| 271 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), | 270 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), |
| 272 v8::NewStringType::kNormal).ToLocalChecked(), | 271 v8::NewStringType::kNormal).ToLocalChecked(), |
| 273 pDefault, v8::ReadOnly); | 272 pDefault, v8::ReadOnly); |
| 274 } | 273 } |
| 275 | 274 |
| 276 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, | 275 void FXJS_InitializeRuntime( |
| 277 IJS_Runtime* pIRuntime, | 276 v8::Isolate* pIsolate, |
| 278 v8::Global<v8::Context>& v8PersistentContext) { | 277 IJS_Runtime* pIRuntime, |
| 278 v8::Global<v8::Context>* pV8PersistentContext, |
| 279 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { |
| 279 if (pIsolate == g_isolate) | 280 if (pIsolate == g_isolate) |
| 280 ++g_isolate_ref_count; | 281 ++g_isolate_ref_count; |
| 281 | 282 |
| 282 v8::Isolate::Scope isolate_scope(pIsolate); | 283 v8::Isolate::Scope isolate_scope(pIsolate); |
| 283 v8::HandleScope handle_scope(pIsolate); | 284 v8::HandleScope handle_scope(pIsolate); |
| 284 v8::Local<v8::Context> v8Context = | 285 v8::Local<v8::Context> v8Context = |
| 285 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 286 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
| 286 v8::Context::Scope context_scope(v8Context); | 287 v8::Context::Scope context_scope(v8Context); |
| 287 | 288 |
| 288 FXJS_PerIsolateData::SetUp(pIsolate); | 289 FXJS_PerIsolateData::SetUp(pIsolate); |
| 289 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); | 290 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); |
| 290 | 291 |
| 291 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 292 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 293 pStaticObjects->resize(maxID + 1); |
| 292 for (int i = 0; i < maxID; ++i) { | 294 for (int i = 0; i < maxID; ++i) { |
| 293 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 295 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 294 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | |
| 295 v8::Local<v8::String> m_ObjName = | |
| 296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | |
| 297 v8::NewStringType::kNormal, | |
| 298 bs.GetLength()).ToLocalChecked(); | |
| 299 | |
| 300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 296 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 301 v8Context->Global() | 297 v8Context->Global() |
| 302 ->GetPrototype() | 298 ->GetPrototype() |
| 303 ->ToObject(v8Context) | 299 ->ToObject(v8Context) |
| 304 .ToLocalChecked() | 300 .ToLocalChecked() |
| 305 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); | 301 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); |
| 306 | 302 |
| 307 if (pObjDef->m_pConstructor) | 303 if (pObjDef->m_pConstructor) |
| 308 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() | 304 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() |
| 309 ->GetPrototype() | 305 ->GetPrototype() |
| 310 ->ToObject(v8Context) | 306 ->ToObject(v8Context) |
| 311 .ToLocalChecked()); | 307 .ToLocalChecked()); |
| 312 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 308 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
| 309 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
| 310 v8::Local<v8::String> m_ObjName = |
| 311 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
| 312 v8::NewStringType::kNormal, |
| 313 bs.GetLength()).ToLocalChecked(); |
| 314 |
| 313 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); | 315 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); |
| 314 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 316 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
| 315 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 317 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); |
| 316 } | 318 } |
| 317 } | 319 } |
| 318 v8PersistentContext.Reset(pIsolate, v8Context); | 320 pV8PersistentContext->Reset(pIsolate, v8Context); |
| 319 } | 321 } |
| 320 | 322 |
| 321 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 323 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
| 322 v8::Global<v8::Context>& v8PersistentContext) { | 324 v8::Global<v8::Context>* pV8PersistentContext, |
| 323 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) | 325 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { |
| 324 return; | |
| 325 | |
| 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, *pV8PersistentContext); |
| 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 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 337 for (int i = 0; i < maxID; ++i) { | 337 for (int 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 (pStaticObjects->at(i) && !pStaticObjects->at(i)->IsEmpty()) { |
| 344 pObj = v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj); | 344 pObj = v8::Local<v8::Object>::New(pIsolate, *pStaticObjects->at(i)); |
| 345 delete pStaticObjects->at(i); |
| 346 pStaticObjects->at(i) = nullptr; |
| 345 } | 347 } |
| 346 | 348 |
| 347 if (!pObj.IsEmpty()) { | 349 if (!pObj.IsEmpty()) { |
| 348 if (pObjDef->m_pDestructor) | 350 if (pObjDef->m_pDestructor) |
| 349 pObjDef->m_pDestructor(pObj); | 351 pObjDef->m_pDestructor(pObj); |
| 350 FXJS_FreePrivate(pObj); | 352 FXJS_FreePrivate(pObj); |
| 351 } | 353 } |
| 352 delete pObjDef; | 354 delete pObjDef; |
| 353 } | 355 } |
| 354 | 356 |
| 357 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) |
| 358 return; |
| 359 |
| 355 pIsolate->SetData(g_embedderDataSlot, nullptr); | 360 pIsolate->SetData(g_embedderDataSlot, nullptr); |
| 356 delete pData; | 361 delete pData; |
| 357 } | 362 } |
| 358 | 363 |
| 359 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { | 364 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { |
| 360 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 365 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 361 return static_cast<IJS_Runtime*>( | 366 return static_cast<IJS_Runtime*>( |
| 362 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); | 367 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); |
| 363 } | 368 } |
| 364 | 369 |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 return v8::Local<v8::Array>(); | 750 return v8::Local<v8::Array>(); |
| 746 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 751 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 747 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 752 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
| 748 } | 753 } |
| 749 | 754 |
| 750 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) { |
| 751 pTo = pFrom; | 756 pTo = pFrom; |
| 752 } | 757 } |
| 753 | 758 |
| 754 | 759 |
| OLD | NEW |