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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 pIsolate->GetData(g_embedderDataSlot)); | 215 pIsolate->GetData(g_embedderDataSlot)); |
216 } | 216 } |
217 | 217 |
218 #ifndef PDF_ENABLE_XFA | 218 #ifndef PDF_ENABLE_XFA |
219 FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} | 219 FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} |
220 #else // PDF_ENABLE_XFA | 220 #else // PDF_ENABLE_XFA |
221 FXJS_PerIsolateData::FXJS_PerIsolateData() | 221 FXJS_PerIsolateData::FXJS_PerIsolateData() |
222 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} | 222 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} |
223 #endif // PDF_ENABLE_XFA | 223 #endif // PDF_ENABLE_XFA |
224 | 224 |
| 225 CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr), m_isolateManaged(false) {} |
| 226 CFXJS_Engine::~CFXJS_Engine() {} |
| 227 |
225 int FXJS_DefineObj(v8::Isolate* pIsolate, | 228 int FXJS_DefineObj(v8::Isolate* pIsolate, |
226 const wchar_t* sObjName, | 229 const wchar_t* sObjName, |
227 FXJSOBJTYPE eObjType, | 230 FXJSOBJTYPE eObjType, |
228 FXJS_CONSTRUCTOR pConstructor, | 231 FXJS_CONSTRUCTOR pConstructor, |
229 FXJS_DESTRUCTOR pDestructor) { | 232 FXJS_DESTRUCTOR pDestructor) { |
230 v8::Isolate::Scope isolate_scope(pIsolate); | 233 v8::Isolate::Scope isolate_scope(pIsolate); |
231 v8::HandleScope handle_scope(pIsolate); | 234 v8::HandleScope handle_scope(pIsolate); |
232 | 235 |
233 FXJS_PerIsolateData::SetUp(pIsolate); | 236 FXJS_PerIsolateData::SetUp(pIsolate); |
234 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( | 237 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 v8::Local<v8::FunctionTemplate> fun = | 326 v8::Local<v8::FunctionTemplate> fun = |
324 v8::FunctionTemplate::New(pIsolate, pConstGetter); | 327 v8::FunctionTemplate::New(pIsolate, pConstGetter); |
325 fun->RemovePrototype(); | 328 fun->RemovePrototype(); |
326 GetGlobalObjectTemplate(pIsolate)->SetAccessorProperty( | 329 GetGlobalObjectTemplate(pIsolate)->SetAccessorProperty( |
327 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), | 330 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), |
328 v8::NewStringType::kNormal) | 331 v8::NewStringType::kNormal) |
329 .ToLocalChecked(), | 332 .ToLocalChecked(), |
330 fun); | 333 fun); |
331 } | 334 } |
332 | 335 |
333 void FXJS_InitializeRuntime( | 336 void FXJS_InitializeEngine( |
334 v8::Isolate* pIsolate, | 337 v8::Isolate* pIsolate, |
335 IJS_Runtime* pIRuntime, | 338 CFXJS_Engine* pEngine, |
336 v8::Global<v8::Context>* pV8PersistentContext, | 339 v8::Global<v8::Context>* pV8PersistentContext, |
337 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { | 340 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { |
338 if (pIsolate == g_isolate) | 341 if (pIsolate == g_isolate) |
339 ++g_isolate_ref_count; | 342 ++g_isolate_ref_count; |
340 | 343 |
341 v8::Isolate::Scope isolate_scope(pIsolate); | 344 v8::Isolate::Scope isolate_scope(pIsolate); |
342 v8::HandleScope handle_scope(pIsolate); | 345 v8::HandleScope handle_scope(pIsolate); |
343 | 346 |
344 // This has to happen before we call GetGlobalObjectTemplate because that | 347 // This has to happen before we call GetGlobalObjectTemplate because that |
345 // method gets the PerIsolateData from pIsolate. | 348 // method gets the PerIsolateData from pIsolate. |
346 FXJS_PerIsolateData::SetUp(pIsolate); | 349 FXJS_PerIsolateData::SetUp(pIsolate); |
347 | 350 |
348 v8::Local<v8::Context> v8Context = | 351 v8::Local<v8::Context> v8Context = |
349 v8::Context::New(pIsolate, nullptr, GetGlobalObjectTemplate(pIsolate)); | 352 v8::Context::New(pIsolate, nullptr, GetGlobalObjectTemplate(pIsolate)); |
350 v8::Context::Scope context_scope(v8Context); | 353 v8::Context::Scope context_scope(v8Context); |
351 | 354 |
352 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 355 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
353 if (!pData) | 356 if (!pData) |
354 return; | 357 return; |
355 pData->CreateDynamicObjsMap(pIsolate); | 358 pData->CreateDynamicObjsMap(pIsolate); |
356 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); | 359 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); |
357 | 360 |
358 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 361 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
359 pStaticObjects->resize(maxID + 1); | 362 pStaticObjects->resize(maxID + 1); |
360 for (int i = 0; i < maxID; ++i) { | 363 for (int i = 0; i < maxID; ++i) { |
361 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 364 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
362 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 365 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
363 v8Context->Global() | 366 v8Context->Global() |
364 ->GetPrototype() | 367 ->GetPrototype() |
365 ->ToObject(v8Context) | 368 ->ToObject(v8Context) |
366 .ToLocalChecked() | 369 .ToLocalChecked() |
367 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); | 370 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); |
368 | 371 |
369 if (pObjDef->m_pConstructor) | 372 if (pObjDef->m_pConstructor) |
370 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() | 373 pObjDef->m_pConstructor(pEngine, v8Context->Global() |
371 ->GetPrototype() | 374 ->GetPrototype() |
372 ->ToObject(v8Context) | 375 ->ToObject(v8Context) |
373 .ToLocalChecked()); | 376 .ToLocalChecked()); |
374 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 377 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
375 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | 378 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
376 v8::Local<v8::String> m_ObjName = | 379 v8::Local<v8::String> m_ObjName = |
377 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 380 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
378 v8::NewStringType::kNormal, bs.GetLength()) | 381 v8::NewStringType::kNormal, bs.GetLength()) |
379 .ToLocalChecked(); | 382 .ToLocalChecked(); |
380 | 383 |
381 v8::Local<v8::Object> obj = | 384 v8::Local<v8::Object> obj = |
382 FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i, true); | 385 FXJS_NewFxDynamicObj(pIsolate, pEngine, i, true); |
383 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 386 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
384 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); | 387 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); |
385 } | 388 } |
386 } | 389 } |
387 pV8PersistentContext->Reset(pIsolate, v8Context); | 390 pV8PersistentContext->Reset(pIsolate, v8Context); |
388 } | 391 } |
389 | 392 |
390 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 393 void FXJS_ReleaseEngine(v8::Isolate* pIsolate, |
391 v8::Global<v8::Context>* pV8PersistentContext, | 394 v8::Global<v8::Context>* pV8PersistentContext, |
392 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { | 395 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { |
393 v8::Isolate::Scope isolate_scope(pIsolate); | 396 v8::Isolate::Scope isolate_scope(pIsolate); |
394 v8::HandleScope handle_scope(pIsolate); | 397 v8::HandleScope handle_scope(pIsolate); |
395 v8::Local<v8::Context> context = | 398 v8::Local<v8::Context> context = |
396 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); | 399 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); |
397 v8::Context::Scope context_scope(context); | 400 v8::Context::Scope context_scope(context); |
398 | 401 |
399 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 402 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
400 if (!pData) | 403 if (!pData) |
401 return; | 404 return; |
402 | 405 |
(...skipping 21 matching lines...) Expand all Loading... |
424 return; | 427 return; |
425 | 428 |
426 pData->ReleaseDynamicObjsMap(); | 429 pData->ReleaseDynamicObjsMap(); |
427 for (int i = 0; i < maxID; ++i) | 430 for (int i = 0; i < maxID; ++i) |
428 delete CFXJS_ObjDefinition::ForID(pIsolate, i); | 431 delete CFXJS_ObjDefinition::ForID(pIsolate, i); |
429 | 432 |
430 pIsolate->SetData(g_embedderDataSlot, nullptr); | 433 pIsolate->SetData(g_embedderDataSlot, nullptr); |
431 delete pData; | 434 delete pData; |
432 } | 435 } |
433 | 436 |
434 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { | 437 CFXJS_Engine* FXJS_GetEngineFromIsolate(v8::Isolate* pIsolate) { |
435 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 438 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
436 return static_cast<IJS_Runtime*>( | 439 return static_cast<CFXJS_Engine*>( |
437 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); | 440 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); |
438 } | 441 } |
439 | 442 |
440 #ifdef PDF_ENABLE_XFA | 443 #ifdef PDF_ENABLE_XFA |
441 void FXJS_SetRuntimeForV8Context(v8::Local<v8::Context> v8Context, | 444 void FXJS_SetEngineForV8Context(v8::Local<v8::Context> v8Context, |
442 IJS_Runtime* pIRuntime) { | 445 CFXJS_Engine* pEngine) { |
443 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); | 446 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); |
444 } | 447 } |
445 #endif // PDF_ENABLE_XFA | 448 #endif // PDF_ENABLE_XFA |
446 | 449 |
447 int FXJS_Execute(v8::Isolate* pIsolate, | 450 int FXJS_Execute(v8::Isolate* pIsolate, |
448 const CFX_WideString& script, | 451 const CFX_WideString& script, |
449 FXJSErr* pError) { | 452 FXJSErr* pError) { |
450 v8::Isolate::Scope isolate_scope(pIsolate); | 453 v8::Isolate::Scope isolate_scope(pIsolate); |
451 v8::TryCatch try_catch(pIsolate); | 454 v8::TryCatch try_catch(pIsolate); |
452 CFX_ByteString bsScript = script.UTF8Encode(); | 455 CFX_ByteString bsScript = script.UTF8Encode(); |
453 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 456 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
(...skipping 12 matching lines...) Expand all Loading... |
466 v8::Local<v8::Value> result; | 469 v8::Local<v8::Value> result; |
467 if (!compiled_script->Run(context).ToLocal(&result)) { | 470 if (!compiled_script->Run(context).ToLocal(&result)) { |
468 v8::String::Utf8Value error(try_catch.Exception()); | 471 v8::String::Utf8Value error(try_catch.Exception()); |
469 // TODO(tsepez): return error via pError->message. | 472 // TODO(tsepez): return error via pError->message. |
470 return -1; | 473 return -1; |
471 } | 474 } |
472 return 0; | 475 return 0; |
473 } | 476 } |
474 | 477 |
475 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 478 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
476 IJS_Runtime* pIRuntime, | 479 CFXJS_Engine* pEngine, |
477 int nObjDefnID, | 480 int nObjDefnID, |
478 bool bStatic) { | 481 bool bStatic) { |
479 v8::Isolate::Scope isolate_scope(pIsolate); | 482 v8::Isolate::Scope isolate_scope(pIsolate); |
480 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 483 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
481 if (nObjDefnID == -1) { | 484 if (nObjDefnID == -1) { |
482 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 485 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); |
483 v8::Local<v8::Object> obj; | 486 v8::Local<v8::Object> obj; |
484 if (!objTempl->NewInstance(context).ToLocal(&obj)) | 487 if (!objTempl->NewInstance(context).ToLocal(&obj)) |
485 return v8::Local<v8::Object>(); | 488 return v8::Local<v8::Object>(); |
486 return obj; | 489 return obj; |
487 } | 490 } |
488 | 491 |
489 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 492 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
490 if (!pData) | 493 if (!pData) |
491 return v8::Local<v8::Object>(); | 494 return v8::Local<v8::Object>(); |
492 | 495 |
493 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 496 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
494 return v8::Local<v8::Object>(); | 497 return v8::Local<v8::Object>(); |
495 | 498 |
496 CFXJS_ObjDefinition* pObjDef = | 499 CFXJS_ObjDefinition* pObjDef = |
497 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 500 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
498 v8::Local<v8::Object> obj; | 501 v8::Local<v8::Object> obj; |
499 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) | 502 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
500 return v8::Local<v8::Object>(); | 503 return v8::Local<v8::Object>(); |
501 | 504 |
502 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); | 505 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); |
503 obj->SetAlignedPointerInInternalField(0, pPerObjData); | 506 obj->SetAlignedPointerInInternalField(0, pPerObjData); |
504 if (pObjDef->m_pConstructor) | 507 if (pObjDef->m_pConstructor) |
505 pObjDef->m_pConstructor(pIRuntime, obj); | 508 pObjDef->m_pConstructor(pEngine, obj); |
506 | 509 |
507 if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap) { | 510 if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap) { |
508 FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap->set(pPerObjData, | 511 FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap->set(pPerObjData, |
509 obj); | 512 obj); |
510 } | 513 } |
511 return obj; | 514 return obj; |
512 } | 515 } |
513 | 516 |
514 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { | 517 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { |
515 v8::Isolate::Scope isolate_scope(pIsolate); | 518 v8::Isolate::Scope isolate_scope(pIsolate); |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
811 return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length())); | 814 return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length())); |
812 } | 815 } |
813 | 816 |
814 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, | 817 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, |
815 v8::Local<v8::Value> pValue) { | 818 v8::Local<v8::Value> pValue) { |
816 if (pValue.IsEmpty()) | 819 if (pValue.IsEmpty()) |
817 return v8::Local<v8::Array>(); | 820 return v8::Local<v8::Array>(); |
818 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 821 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
819 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 822 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
820 } | 823 } |
OLD | NEW |