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 "fpdfsdk/include/jsapi/fxjs_v8.h" | 7 #include "fpdfsdk/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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 118 } |
119 | 119 |
120 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { | 120 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
121 return malloc(length); | 121 return malloc(length); |
122 } | 122 } |
123 | 123 |
124 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { | 124 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
125 free(data); | 125 free(data); |
126 } | 126 } |
127 | 127 |
| 128 void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, |
| 129 v8::Global<v8::Object> value, |
| 130 void* key) { |
| 131 v8::Local<v8::Object> obj = value.Get(isolate); |
| 132 if (obj.IsEmpty()) |
| 133 return; |
| 134 int id = FXJS_GetObjDefnID(obj); |
| 135 if (id == -1) |
| 136 return; |
| 137 |
| 138 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id); |
| 139 if (!pObjDef) |
| 140 return; |
| 141 if (pObjDef->m_pDestructor) |
| 142 pObjDef->m_pDestructor(obj); |
| 143 FXJS_FreePrivate(obj); |
| 144 } |
| 145 |
| 146 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( |
| 147 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 148 V8TemplateMap* pMap = |
| 149 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap; |
| 150 return pMap ? &pMap->m_map : nullptr; |
| 151 } |
| 152 |
128 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { | 153 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { |
129 if (g_isolate) { | 154 if (g_isolate) { |
130 ASSERT(g_embedderDataSlot == embedderDataSlot); | 155 ASSERT(g_embedderDataSlot == embedderDataSlot); |
131 ASSERT(g_isolate == pIsolate); | 156 ASSERT(g_isolate == pIsolate); |
132 return; | 157 return; |
133 } | 158 } |
134 g_embedderDataSlot = embedderDataSlot; | 159 g_embedderDataSlot = embedderDataSlot; |
135 g_isolate = pIsolate; | 160 g_isolate = pIsolate; |
136 } | 161 } |
137 | 162 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 v8::Isolate::Scope isolate_scope(pIsolate); | 308 v8::Isolate::Scope isolate_scope(pIsolate); |
284 #ifdef PDF_ENABLE_XFA | 309 #ifdef PDF_ENABLE_XFA |
285 v8::Locker locker(pIsolate); | 310 v8::Locker locker(pIsolate); |
286 #endif // PDF_ENABLE_XFA | 311 #endif // PDF_ENABLE_XFA |
287 v8::HandleScope handle_scope(pIsolate); | 312 v8::HandleScope handle_scope(pIsolate); |
288 v8::Local<v8::Context> v8Context = | 313 v8::Local<v8::Context> v8Context = |
289 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 314 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
290 v8::Context::Scope context_scope(v8Context); | 315 v8::Context::Scope context_scope(v8Context); |
291 | 316 |
292 FXJS_PerIsolateData::SetUp(pIsolate); | 317 FXJS_PerIsolateData::SetUp(pIsolate); |
| 318 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
| 319 if (!pData) |
| 320 return; |
| 321 pData->CreateDynamicObjsMap(pIsolate); |
293 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); | 322 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); |
294 | 323 |
295 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 324 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
296 pStaticObjects->resize(maxID + 1); | 325 pStaticObjects->resize(maxID + 1); |
297 for (int i = 0; i < maxID; ++i) { | 326 for (int i = 0; i < maxID; ++i) { |
298 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 327 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
299 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 328 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
300 v8Context->Global() | 329 v8Context->Global() |
301 ->GetPrototype() | 330 ->GetPrototype() |
302 ->ToObject(v8Context) | 331 ->ToObject(v8Context) |
303 .ToLocalChecked() | 332 .ToLocalChecked() |
304 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); | 333 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); |
305 | 334 |
306 if (pObjDef->m_pConstructor) | 335 if (pObjDef->m_pConstructor) |
307 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() | 336 pObjDef->m_pConstructor(pIRuntime, v8Context->Global() |
308 ->GetPrototype() | 337 ->GetPrototype() |
309 ->ToObject(v8Context) | 338 ->ToObject(v8Context) |
310 .ToLocalChecked()); | 339 .ToLocalChecked()); |
311 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 340 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
312 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | 341 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
313 v8::Local<v8::String> m_ObjName = | 342 v8::Local<v8::String> m_ObjName = |
314 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 343 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
315 v8::NewStringType::kNormal, | 344 v8::NewStringType::kNormal, |
316 bs.GetLength()).ToLocalChecked(); | 345 bs.GetLength()).ToLocalChecked(); |
317 | 346 |
318 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); | 347 v8::Local<v8::Object> obj = |
| 348 FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i, true); |
319 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 349 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
320 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); | 350 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); |
321 } | 351 } |
322 } | 352 } |
323 pV8PersistentContext->Reset(pIsolate, v8Context); | 353 pV8PersistentContext->Reset(pIsolate, v8Context); |
324 } | 354 } |
325 | 355 |
326 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 356 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
327 v8::Global<v8::Context>* pV8PersistentContext, | 357 v8::Global<v8::Context>* pV8PersistentContext, |
328 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { | 358 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { |
329 v8::Isolate::Scope isolate_scope(pIsolate); | 359 v8::Isolate::Scope isolate_scope(pIsolate); |
330 #ifdef PDF_ENABLE_XFA | 360 #ifdef PDF_ENABLE_XFA |
331 v8::Locker locker(pIsolate); | 361 v8::Locker locker(pIsolate); |
332 #endif // PDF_ENABLE_XFA | 362 #endif // PDF_ENABLE_XFA |
333 v8::HandleScope handle_scope(pIsolate); | 363 v8::HandleScope handle_scope(pIsolate); |
334 v8::Local<v8::Context> context = | 364 v8::Local<v8::Context> context = |
335 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); | 365 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); |
336 v8::Context::Scope context_scope(context); | 366 v8::Context::Scope context_scope(context); |
337 | 367 |
338 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 368 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
339 if (!pData) | 369 if (!pData) |
340 return; | 370 return; |
| 371 pData->ReleaseDynamicObjsMap(); |
341 | 372 |
342 #ifdef PDF_ENABLE_XFA | 373 #ifdef PDF_ENABLE_XFA |
343 // XFA, if present, should have already cleaned itself up. | 374 // XFA, if present, should have already cleaned itself up. |
344 FXSYS_assert(!pData->m_pFXJSERuntimeData); | 375 FXSYS_assert(!pData->m_pFXJSERuntimeData); |
345 #endif // PDF_ENABLE_XFA | 376 #endif // PDF_ENABLE_XFA |
346 | 377 |
347 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 378 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
348 for (int i = 0; i < maxID; ++i) { | 379 for (int i = 0; i < maxID; ++i) { |
349 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 380 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
350 v8::Local<v8::Object> pObj; | 381 v8::Local<v8::Object> pObj; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 if (!compiled_script->Run(context).ToLocal(&result)) { | 441 if (!compiled_script->Run(context).ToLocal(&result)) { |
411 v8::String::Utf8Value error(try_catch.Exception()); | 442 v8::String::Utf8Value error(try_catch.Exception()); |
412 // TODO(tsepez): return error via pError->message. | 443 // TODO(tsepez): return error via pError->message. |
413 return -1; | 444 return -1; |
414 } | 445 } |
415 return 0; | 446 return 0; |
416 } | 447 } |
417 | 448 |
418 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 449 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
419 IJS_Runtime* pIRuntime, | 450 IJS_Runtime* pIRuntime, |
420 int nObjDefnID) { | 451 int nObjDefnID, |
| 452 bool bStatic) { |
421 v8::Isolate::Scope isolate_scope(pIsolate); | 453 v8::Isolate::Scope isolate_scope(pIsolate); |
422 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 454 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
423 if (nObjDefnID == -1) { | 455 if (nObjDefnID == -1) { |
424 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 456 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); |
425 v8::Local<v8::Object> obj; | 457 v8::Local<v8::Object> obj; |
426 if (!objTempl->NewInstance(context).ToLocal(&obj)) | 458 if (!objTempl->NewInstance(context).ToLocal(&obj)) |
427 return v8::Local<v8::Object>(); | 459 return v8::Local<v8::Object>(); |
428 return obj; | 460 return obj; |
429 } | 461 } |
430 | 462 |
431 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 463 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
432 if (!pData) | 464 if (!pData) |
433 return v8::Local<v8::Object>(); | 465 return v8::Local<v8::Object>(); |
434 | 466 |
435 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 467 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
436 return v8::Local<v8::Object>(); | 468 return v8::Local<v8::Object>(); |
437 | 469 |
438 CFXJS_ObjDefinition* pObjDef = | 470 CFXJS_ObjDefinition* pObjDef = |
439 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 471 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
440 v8::Local<v8::Object> obj; | 472 v8::Local<v8::Object> obj; |
441 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) | 473 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
442 return v8::Local<v8::Object>(); | 474 return v8::Local<v8::Object>(); |
443 | 475 |
444 obj->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(nObjDefnID)); | 476 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); |
| 477 obj->SetAlignedPointerInInternalField(0, pPerObjData); |
445 if (pObjDef->m_pConstructor) | 478 if (pObjDef->m_pConstructor) |
446 pObjDef->m_pConstructor(pIRuntime, obj); | 479 pObjDef->m_pConstructor(pIRuntime, obj); |
447 | 480 |
| 481 if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap) { |
| 482 FXJS_PerIsolateData::Get(pIsolate) |
| 483 ->m_pDynamicObjsMap->set(pPerObjData, obj); |
| 484 } |
448 return obj; | 485 return obj; |
449 } | 486 } |
450 | 487 |
451 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { | 488 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { |
452 v8::Isolate::Scope isolate_scope(pIsolate); | 489 v8::Isolate::Scope isolate_scope(pIsolate); |
453 if (!FXJS_PerIsolateData::Get(pIsolate)) | 490 if (!FXJS_PerIsolateData::Get(pIsolate)) |
454 return v8::Local<v8::Object>(); | 491 return v8::Local<v8::Object>(); |
455 | 492 |
456 // Return the global object. | 493 // Return the global object. |
457 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 494 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 return v8::Local<v8::Array>(); | 806 return v8::Local<v8::Array>(); |
770 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 807 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
771 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 808 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
772 } | 809 } |
773 | 810 |
774 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 811 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
775 pTo = pFrom; | 812 pTo = pFrom; |
776 } | 813 } |
777 | 814 |
778 | 815 |
OLD | NEW |