| 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::Locker locker(pIsolate); | 284 v8::Locker locker(pIsolate); |
| 284 v8::HandleScope handle_scope(pIsolate); | 285 v8::HandleScope handle_scope(pIsolate); |
| 285 v8::Local<v8::Context> v8Context = | 286 v8::Local<v8::Context> v8Context = |
| 286 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 287 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
| 287 v8::Context::Scope context_scope(v8Context); | 288 v8::Context::Scope context_scope(v8Context); |
| 288 | 289 |
| 289 FXJS_PerIsolateData::SetUp(pIsolate); | 290 FXJS_PerIsolateData::SetUp(pIsolate); |
| 290 FXJS_SetRuntimeForV8Context(v8Context, pIRuntime); | 291 FXJS_SetRuntimeForV8Context(v8Context, pIRuntime); |
| 291 | 292 |
| 292 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 293 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 294 pStaticObjects->resize(maxID + 1); |
| 293 for (int i = 0; i < maxID; ++i) { | 295 for (int i = 0; i < maxID; ++i) { |
| 294 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 296 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 295 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | |
| 296 v8::Local<v8::String> m_ObjName = | |
| 297 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | |
| 298 v8::NewStringType::kNormal, | |
| 299 bs.GetLength()).ToLocalChecked(); | |
| 300 | |
| 301 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 297 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 302 v8Context->Global() | 298 v8Context->Global() |
| 303 ->GetPrototype() | 299 ->GetPrototype() |
| 304 ->ToObject(v8Context) | 300 ->ToObject(v8Context) |
| 305 .ToLocalChecked() | 301 .ToLocalChecked() |
| 306 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); | 302 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); |
| 307 | 303 |
| 308 if (pObjDef->m_pConstructor) | 304 if (pObjDef->m_pConstructor) |
| 309 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() | 305 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() |
| 310 ->GetPrototype() | 306 ->GetPrototype() |
| 311 ->ToObject(v8Context) | 307 ->ToObject(v8Context) |
| 312 .ToLocalChecked()); | 308 .ToLocalChecked()); |
| 313 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 309 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
| 310 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
| 311 v8::Local<v8::String> m_ObjName = |
| 312 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
| 313 v8::NewStringType::kNormal, |
| 314 bs.GetLength()).ToLocalChecked(); |
| 315 |
| 314 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); | 316 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); |
| 315 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 317 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
| 316 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 318 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); |
| 317 } | 319 } |
| 318 } | 320 } |
| 319 v8PersistentContext.Reset(pIsolate, v8Context); | 321 pV8PersistentContext->Reset(pIsolate, v8Context); |
| 320 } | 322 } |
| 321 | 323 |
| 322 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 324 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
| 323 v8::Global<v8::Context>& v8PersistentContext) { | 325 v8::Global<v8::Context>* pV8PersistentContext, |
| 324 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) | 326 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { |
| 325 return; | |
| 326 | |
| 327 v8::Isolate::Scope isolate_scope(pIsolate); | 327 v8::Isolate::Scope isolate_scope(pIsolate); |
| 328 v8::Locker locker(pIsolate); | 328 v8::Locker locker(pIsolate); |
| 329 v8::HandleScope handle_scope(pIsolate); | 329 v8::HandleScope handle_scope(pIsolate); |
| 330 v8::Local<v8::Context> context = | 330 v8::Local<v8::Context> context = |
| 331 v8::Local<v8::Context>::New(pIsolate, v8PersistentContext); | 331 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); |
| 332 v8::Context::Scope context_scope(context); | 332 v8::Context::Scope context_scope(context); |
| 333 | 333 |
| 334 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 334 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
| 335 if (!pData) | 335 if (!pData) |
| 336 return; | 336 return; |
| 337 | 337 |
| 338 // XFA, if present, should have already cleaned itself up. | 338 // XFA, if present, should have already cleaned itself up. |
| 339 FXSYS_assert(!pData->m_pFXJSERuntimeData); | 339 FXSYS_assert(!pData->m_pFXJSERuntimeData); |
| 340 | 340 |
| 341 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 341 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 342 for (int i = 0; i < maxID; ++i) { | 342 for (int i = 0; i < maxID; ++i) { |
| 343 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 343 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 344 v8::Local<v8::Object> pObj; | 344 v8::Local<v8::Object> pObj; |
| 345 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 345 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 346 pObj = | 346 pObj = |
| 347 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); | 347 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
| 348 } else if (!pObjDef->m_StaticObj.IsEmpty()) { | 348 } else if (pStaticObjects->at(i) && !pStaticObjects->at(i)->IsEmpty()) { |
| 349 pObj = v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj); | 349 pObj = v8::Local<v8::Object>::New(pIsolate, *pStaticObjects->at(i)); |
| 350 delete pStaticObjects->at(i); |
| 351 pStaticObjects->at(i) = nullptr; |
| 350 } | 352 } |
| 351 | 353 |
| 352 if (!pObj.IsEmpty()) { | 354 if (!pObj.IsEmpty()) { |
| 353 if (pObjDef->m_pDestructor) | 355 if (pObjDef->m_pDestructor) |
| 354 pObjDef->m_pDestructor(pObj); | 356 pObjDef->m_pDestructor(pObj); |
| 355 FXJS_FreePrivate(pObj); | 357 FXJS_FreePrivate(pObj); |
| 356 } | 358 } |
| 357 delete pObjDef; | 359 delete pObjDef; |
| 358 } | 360 } |
| 359 | 361 |
| 362 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) |
| 363 return; |
| 364 |
| 360 pIsolate->SetData(g_embedderDataSlot, nullptr); | 365 pIsolate->SetData(g_embedderDataSlot, nullptr); |
| 361 delete pData; | 366 delete pData; |
| 362 } | 367 } |
| 363 | 368 |
| 364 void FXJS_SetRuntimeForV8Context(v8::Local<v8::Context> v8Context, | 369 void FXJS_SetRuntimeForV8Context(v8::Local<v8::Context> v8Context, |
| 365 IJS_Runtime* pIRuntime) { | 370 IJS_Runtime* pIRuntime) { |
| 366 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); | 371 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); |
| 367 } | 372 } |
| 368 | 373 |
| 369 IJS_Runtime* FXJS_GetRuntimeFromV8Context(v8::Local<v8::Context> v8Context) { | 374 IJS_Runtime* FXJS_GetRuntimeFromV8Context(v8::Local<v8::Context> v8Context) { |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 return v8::Local<v8::Array>(); | 758 return v8::Local<v8::Array>(); |
| 754 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 759 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 755 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 760 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
| 756 } | 761 } |
| 757 | 762 |
| 758 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 763 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
| 759 pTo = pFrom; | 764 pTo = pFrom; |
| 760 } | 765 } |
| 761 | 766 |
| 762 | 767 |
| OLD | NEW |