OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "xfa/fxjse/runtime.h" | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "fpdfsdk/jsapi/include/fxjs_v8.h" | |
12 #include "xfa/fxjse/scope_inline.h" | |
13 | |
14 // Duplicates fpdfsdk's cjs_runtime.h, but keeps XFA from depending on it. | |
15 // TODO(tsepez): make a single version of this. | |
16 class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | |
17 void* Allocate(size_t length) override { return calloc(1, length); } | |
18 void* AllocateUninitialized(size_t length) override { return malloc(length); } | |
19 void Free(void* data, size_t length) override { free(data); } | |
20 }; | |
21 | |
22 static void FXJSE_KillV8() { | |
23 v8::V8::Dispose(); | |
24 } | |
25 | |
26 void FXJSE_Initialize() { | |
27 if (!CFXJSE_IsolateTracker::g_pInstance) | |
28 CFXJSE_IsolateTracker::g_pInstance = new CFXJSE_IsolateTracker; | |
29 | |
30 static FX_BOOL bV8Initialized = FALSE; | |
31 if (bV8Initialized) | |
32 return; | |
33 | |
34 bV8Initialized = TRUE; | |
35 atexit(FXJSE_KillV8); | |
36 } | |
37 | |
38 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate, bool bOwned) { | |
39 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { | |
40 delete pData->m_pFXJSERuntimeData; | |
41 pData->m_pFXJSERuntimeData = nullptr; | |
42 } | |
43 if (bOwned) | |
44 pIsolate->Dispose(); | |
45 } | |
46 | |
47 void FXJSE_Finalize() { | |
48 if (!CFXJSE_IsolateTracker::g_pInstance) | |
49 return; | |
50 | |
51 CFXJSE_IsolateTracker::g_pInstance->RemoveAll(FXJSE_Runtime_DisposeCallback); | |
52 delete CFXJSE_IsolateTracker::g_pInstance; | |
53 CFXJSE_IsolateTracker::g_pInstance = nullptr; | |
54 } | |
55 | |
56 v8::Isolate* FXJSE_Runtime_Create_Own() { | |
57 v8::Isolate::CreateParams params; | |
58 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); | |
59 v8::Isolate* pIsolate = v8::Isolate::New(params); | |
60 ASSERT(pIsolate && CFXJSE_IsolateTracker::g_pInstance); | |
61 CFXJSE_IsolateTracker::g_pInstance->Append(pIsolate); | |
62 return pIsolate; | |
63 } | |
64 | |
65 void FXJSE_Runtime_Release(v8::Isolate* pIsolate) { | |
66 if (!pIsolate) | |
67 return; | |
68 CFXJSE_IsolateTracker::g_pInstance->Remove(pIsolate, | |
69 FXJSE_Runtime_DisposeCallback); | |
70 } | |
71 | |
72 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { | |
73 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); | |
74 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | |
75 v8::Local<v8::FunctionTemplate> hFuncTemplate = | |
76 v8::FunctionTemplate::New(pIsolate); | |
77 v8::Local<v8::Context> hContext = | |
78 v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate()); | |
79 hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate)); | |
80 pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate); | |
81 pRuntimeData->m_hRootContext.Reset(pIsolate, hContext); | |
82 return pRuntimeData; | |
83 } | |
84 | |
85 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { | |
86 FXJS_PerIsolateData::SetUp(pIsolate); | |
87 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | |
88 if (!pData->m_pFXJSERuntimeData) | |
89 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); | |
90 return pData->m_pFXJSERuntimeData; | |
91 } | |
92 | |
93 CFXJSE_IsolateTracker* CFXJSE_IsolateTracker::g_pInstance = nullptr; | |
94 | |
95 void CFXJSE_IsolateTracker::Append(v8::Isolate* pIsolate) { | |
96 m_OwnedIsolates.push_back(pIsolate); | |
97 } | |
98 | |
99 void CFXJSE_IsolateTracker::Remove( | |
100 v8::Isolate* pIsolate, | |
101 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { | |
102 auto it = std::find(m_OwnedIsolates.begin(), m_OwnedIsolates.end(), pIsolate); | |
103 bool bFound = it != m_OwnedIsolates.end(); | |
104 if (bFound) | |
105 m_OwnedIsolates.erase(it); | |
106 lpfnDisposeCallback(pIsolate, bFound); | |
107 } | |
108 | |
109 void CFXJSE_IsolateTracker::RemoveAll( | |
110 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { | |
111 for (v8::Isolate* pIsolate : m_OwnedIsolates) | |
112 lpfnDisposeCallback(pIsolate, true); | |
113 | |
114 m_OwnedIsolates.clear(); | |
115 } | |
OLD | NEW |