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 "xfa/fxjse/runtime.h" | 7 #include "xfa/fxjse/runtime.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "fpdfsdk/jsapi/include/fxjs_v8.h" | 11 #include "fpdfsdk/jsapi/include/fxjs_v8.h" |
12 #include "xfa/fxjse/scope_inline.h" | 12 #include "xfa/fxjse/scope_inline.h" |
13 | 13 |
14 // Duplicates fpdfsdk's cjs_runtime.h, but keeps XFA from depending on it. | 14 // Duplicates fpdfsdk's cjs_runtime.h, but keeps XFA from depending on it. |
15 // TODO(tsepez): make a single version of this. | 15 // TODO(tsepez): make a single version of this. |
16 class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 16 class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
17 void* Allocate(size_t length) override { return calloc(1, length); } | 17 void* Allocate(size_t length) override { return calloc(1, length); } |
18 void* AllocateUninitialized(size_t length) override { return malloc(length); } | 18 void* AllocateUninitialized(size_t length) override { return malloc(length); } |
19 void Free(void* data, size_t length) override { free(data); } | 19 void Free(void* data, size_t length) override { free(data); } |
20 }; | 20 }; |
21 | 21 |
22 static void FXJSE_KillV8() { | 22 static void FXJSE_KillV8() { |
23 v8::V8::Dispose(); | 23 v8::V8::Dispose(); |
24 } | 24 } |
25 | 25 |
26 void FXJSE_Initialize() { | 26 void FXJSE_Initialize() { |
27 if (!CFXJSE_RuntimeData::g_RuntimeList) { | 27 if (!CFXJSE_IsolateTracker::g_pInstance) |
28 CFXJSE_RuntimeData::g_RuntimeList = new CFXJSE_RuntimeList; | 28 CFXJSE_IsolateTracker::g_pInstance = new CFXJSE_IsolateTracker; |
29 } | 29 |
30 static FX_BOOL bV8Initialized = FALSE; | 30 static FX_BOOL bV8Initialized = FALSE; |
31 if (bV8Initialized) { | 31 if (bV8Initialized) |
32 return; | 32 return; |
33 } | 33 |
34 bV8Initialized = TRUE; | 34 bV8Initialized = TRUE; |
35 atexit(FXJSE_KillV8); | 35 atexit(FXJSE_KillV8); |
36 } | 36 } |
37 | 37 |
38 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate) { | 38 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate, bool bOwned) { |
39 { | 39 { |
40 v8::Locker locker(pIsolate); | 40 v8::Locker locker(pIsolate); |
41 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { | 41 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { |
42 delete pData->m_pFXJSERuntimeData; | 42 delete pData->m_pFXJSERuntimeData; |
43 pData->m_pFXJSERuntimeData = nullptr; | 43 pData->m_pFXJSERuntimeData = nullptr; |
44 } | 44 } |
45 } | 45 } |
46 pIsolate->Dispose(); | 46 if (bOwned) |
47 pIsolate->Dispose(); | |
47 } | 48 } |
48 | 49 |
49 void FXJSE_Finalize() { | 50 void FXJSE_Finalize() { |
50 if (CFXJSE_RuntimeData::g_RuntimeList) { | 51 if (CFXJSE_IsolateTracker::g_pInstance) { |
dsinclair
2016/06/01 02:57:36
if (!CFXJSE_IsolateTracker::g_pInstance)
return;
Tom Sepez
2016/06/01 20:48:19
Done.
| |
51 CFXJSE_RuntimeData::g_RuntimeList->RemoveAllRuntimes( | 52 CFXJSE_IsolateTracker::g_pInstance->RemoveAll( |
52 FXJSE_Runtime_DisposeCallback); | 53 FXJSE_Runtime_DisposeCallback); |
53 delete CFXJSE_RuntimeData::g_RuntimeList; | 54 delete CFXJSE_IsolateTracker::g_pInstance; |
54 CFXJSE_RuntimeData::g_RuntimeList = NULL; | 55 CFXJSE_IsolateTracker::g_pInstance = NULL; |
dsinclair
2016/06/01 02:57:36
nit: nullptr
Tom Sepez
2016/06/01 20:48:19
Done.
| |
55 } | 56 } |
56 } | 57 } |
57 | 58 |
58 v8::Isolate* FXJSE_Runtime_Create() { | 59 v8::Isolate* FXJSE_Runtime_Create_Own() { |
59 v8::Isolate::CreateParams params; | 60 v8::Isolate::CreateParams params; |
60 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); | 61 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); |
61 v8::Isolate* pIsolate = v8::Isolate::New(params); | 62 v8::Isolate* pIsolate = v8::Isolate::New(params); |
62 ASSERT(pIsolate && CFXJSE_RuntimeData::g_RuntimeList); | 63 ASSERT(pIsolate && CFXJSE_IsolateTracker::g_pInstance); |
63 CFXJSE_RuntimeData::g_RuntimeList->AppendRuntime(pIsolate); | 64 CFXJSE_IsolateTracker::g_pInstance->Append(pIsolate); |
64 return pIsolate; | 65 return pIsolate; |
65 } | 66 } |
66 | 67 |
67 void FXJSE_Runtime_Release(v8::Isolate* pIsolate, bool bOwnedRuntime) { | 68 void FXJSE_Runtime_Release(v8::Isolate* pIsolate) { |
68 if (!pIsolate) | 69 if (!pIsolate) |
69 return; | 70 return; |
70 if (bOwnedRuntime) { | 71 CFXJSE_IsolateTracker::g_pInstance->Remove(pIsolate, |
71 ASSERT(CFXJSE_RuntimeData::g_RuntimeList); | 72 FXJSE_Runtime_DisposeCallback); |
72 CFXJSE_RuntimeData::g_RuntimeList->RemoveRuntime( | |
73 pIsolate, FXJSE_Runtime_DisposeCallback); | |
74 } else { | |
75 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { | |
76 delete pData->m_pFXJSERuntimeData; | |
77 pData->m_pFXJSERuntimeData = nullptr; | |
78 } | |
79 } | |
80 } | 73 } |
81 | 74 |
82 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { | 75 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { |
83 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); | 76 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); |
84 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | 77 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); |
85 v8::Local<v8::FunctionTemplate> hFuncTemplate = | 78 v8::Local<v8::FunctionTemplate> hFuncTemplate = |
86 v8::FunctionTemplate::New(pIsolate); | 79 v8::FunctionTemplate::New(pIsolate); |
87 v8::Local<v8::Context> hContext = | 80 v8::Local<v8::Context> hContext = |
88 v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate()); | 81 v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate()); |
89 hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate)); | 82 hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate)); |
90 pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate); | 83 pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate); |
91 pRuntimeData->m_hRootContext.Reset(pIsolate, hContext); | 84 pRuntimeData->m_hRootContext.Reset(pIsolate, hContext); |
92 return pRuntimeData; | 85 return pRuntimeData; |
93 } | 86 } |
94 | 87 |
95 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { | 88 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { |
96 FXJS_PerIsolateData::SetUp(pIsolate); | 89 FXJS_PerIsolateData::SetUp(pIsolate); |
97 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 90 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
98 if (!pData->m_pFXJSERuntimeData) | 91 if (!pData->m_pFXJSERuntimeData) |
99 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); | 92 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); |
100 return pData->m_pFXJSERuntimeData; | 93 return pData->m_pFXJSERuntimeData; |
101 } | 94 } |
102 | 95 |
103 CFXJSE_RuntimeList* CFXJSE_RuntimeData::g_RuntimeList = nullptr; | 96 CFXJSE_IsolateTracker* CFXJSE_IsolateTracker::g_pInstance = nullptr; |
104 | 97 |
105 void CFXJSE_RuntimeList::AppendRuntime(v8::Isolate* pIsolate) { | 98 void CFXJSE_IsolateTracker::Append(v8::Isolate* pIsolate) { |
106 m_RuntimeList.push_back(pIsolate); | 99 m_OwnedIsolates.push_back(pIsolate); |
107 } | 100 } |
108 | 101 |
109 void CFXJSE_RuntimeList::RemoveRuntime( | 102 void CFXJSE_IsolateTracker::Remove( |
110 v8::Isolate* pIsolate, | 103 v8::Isolate* pIsolate, |
111 CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) { | 104 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { |
112 auto it = std::find(m_RuntimeList.begin(), m_RuntimeList.end(), pIsolate); | 105 auto it = std::find(m_OwnedIsolates.begin(), m_OwnedIsolates.end(), pIsolate); |
113 if (it != m_RuntimeList.end()) | 106 bool bFound = it != m_OwnedIsolates.end(); |
114 m_RuntimeList.erase(it); | 107 if (bFound) |
115 if (lpfnDisposeCallback) | 108 m_OwnedIsolates.erase(it); |
116 lpfnDisposeCallback(pIsolate); | 109 lpfnDisposeCallback(pIsolate, bFound); |
117 } | 110 } |
118 | 111 |
119 void CFXJSE_RuntimeList::RemoveAllRuntimes( | 112 void CFXJSE_IsolateTracker::RemoveAll( |
120 CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) { | 113 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { |
121 if (lpfnDisposeCallback) { | 114 for (v8::Isolate* pIsolate : m_OwnedIsolates) |
122 for (v8::Isolate* pIsolate : m_RuntimeList) | 115 lpfnDisposeCallback(pIsolate, true); |
123 lpfnDisposeCallback(pIsolate); | 116 |
124 } | 117 m_OwnedIsolates.clear(); |
125 m_RuntimeList.clear(); | |
126 } | 118 } |
OLD | NEW |