OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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 "fxjse/runtime.h" | 7 #include "fxjs/cfxjse_runtimedata.h" |
8 | |
9 #include <algorithm> | |
10 | 8 |
11 #include "fpdfsdk/jsapi/include/fxjs_v8.h" | 9 #include "fpdfsdk/jsapi/include/fxjs_v8.h" |
12 #include "fxjse/scope_inline.h" | 10 #include "fxjs/cfxjse_isolatetracker.h" |
| 11 |
| 12 namespace { |
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 void Runtime_DisposeCallback(v8::Isolate* pIsolate, bool bOwned) { |
| 23 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { |
| 24 delete pData->m_pFXJSERuntimeData; |
| 25 pData->m_pFXJSERuntimeData = nullptr; |
| 26 } |
| 27 if (bOwned) |
| 28 pIsolate->Dispose(); |
| 29 } |
| 30 |
| 31 void KillV8() { |
23 v8::V8::Dispose(); | 32 v8::V8::Dispose(); |
24 } | 33 } |
25 | 34 |
| 35 } // namespace |
| 36 |
26 void FXJSE_Initialize() { | 37 void FXJSE_Initialize() { |
27 if (!CFXJSE_IsolateTracker::g_pInstance) | 38 if (!CFXJSE_IsolateTracker::g_pInstance) |
28 CFXJSE_IsolateTracker::g_pInstance = new CFXJSE_IsolateTracker; | 39 CFXJSE_IsolateTracker::g_pInstance = new CFXJSE_IsolateTracker; |
29 | 40 |
30 static FX_BOOL bV8Initialized = FALSE; | 41 static FX_BOOL bV8Initialized = FALSE; |
31 if (bV8Initialized) | 42 if (bV8Initialized) |
32 return; | 43 return; |
33 | 44 |
34 bV8Initialized = TRUE; | 45 bV8Initialized = TRUE; |
35 atexit(FXJSE_KillV8); | 46 atexit(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 } | 47 } |
46 | 48 |
47 void FXJSE_Finalize() { | 49 void FXJSE_Finalize() { |
48 if (!CFXJSE_IsolateTracker::g_pInstance) | 50 if (!CFXJSE_IsolateTracker::g_pInstance) |
49 return; | 51 return; |
50 | 52 |
51 CFXJSE_IsolateTracker::g_pInstance->RemoveAll(FXJSE_Runtime_DisposeCallback); | 53 CFXJSE_IsolateTracker::g_pInstance->RemoveAll(Runtime_DisposeCallback); |
52 delete CFXJSE_IsolateTracker::g_pInstance; | 54 delete CFXJSE_IsolateTracker::g_pInstance; |
53 CFXJSE_IsolateTracker::g_pInstance = nullptr; | 55 CFXJSE_IsolateTracker::g_pInstance = nullptr; |
54 } | 56 } |
55 | 57 |
56 v8::Isolate* FXJSE_Runtime_Create_Own() { | 58 v8::Isolate* FXJSE_Runtime_Create_Own() { |
57 v8::Isolate::CreateParams params; | 59 v8::Isolate::CreateParams params; |
58 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); | 60 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); |
59 v8::Isolate* pIsolate = v8::Isolate::New(params); | 61 v8::Isolate* pIsolate = v8::Isolate::New(params); |
60 ASSERT(pIsolate && CFXJSE_IsolateTracker::g_pInstance); | 62 ASSERT(pIsolate && CFXJSE_IsolateTracker::g_pInstance); |
61 CFXJSE_IsolateTracker::g_pInstance->Append(pIsolate); | 63 CFXJSE_IsolateTracker::g_pInstance->Append(pIsolate); |
62 return pIsolate; | 64 return pIsolate; |
63 } | 65 } |
64 | 66 |
65 void FXJSE_Runtime_Release(v8::Isolate* pIsolate) { | 67 void FXJSE_Runtime_Release(v8::Isolate* pIsolate) { |
66 if (!pIsolate) | 68 if (!pIsolate) |
67 return; | 69 return; |
68 CFXJSE_IsolateTracker::g_pInstance->Remove(pIsolate, | 70 CFXJSE_IsolateTracker::g_pInstance->Remove(pIsolate, Runtime_DisposeCallback); |
69 FXJSE_Runtime_DisposeCallback); | |
70 } | 71 } |
71 | 72 |
72 CFXJSE_RuntimeData::CFXJSE_RuntimeData(v8::Isolate* pIsolate) | 73 CFXJSE_RuntimeData::CFXJSE_RuntimeData(v8::Isolate* pIsolate) |
73 : m_pIsolate(pIsolate) {} | 74 : m_pIsolate(pIsolate) {} |
74 | 75 |
75 CFXJSE_RuntimeData::~CFXJSE_RuntimeData() {} | 76 CFXJSE_RuntimeData::~CFXJSE_RuntimeData() {} |
76 | 77 |
77 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { | 78 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { |
78 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); | 79 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); |
79 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | 80 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); |
(...skipping 15 matching lines...) Expand all Loading... |
95 | 96 |
96 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { | 97 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { |
97 FXJS_PerIsolateData::SetUp(pIsolate); | 98 FXJS_PerIsolateData::SetUp(pIsolate); |
98 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 99 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
99 if (!pData->m_pFXJSERuntimeData) | 100 if (!pData->m_pFXJSERuntimeData) |
100 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); | 101 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); |
101 return pData->m_pFXJSERuntimeData; | 102 return pData->m_pFXJSERuntimeData; |
102 } | 103 } |
103 | 104 |
104 CFXJSE_IsolateTracker* CFXJSE_IsolateTracker::g_pInstance = nullptr; | 105 CFXJSE_IsolateTracker* CFXJSE_IsolateTracker::g_pInstance = nullptr; |
105 | |
106 CFXJSE_IsolateTracker::CFXJSE_IsolateTracker() {} | |
107 | |
108 CFXJSE_IsolateTracker::~CFXJSE_IsolateTracker() {} | |
109 | |
110 void CFXJSE_IsolateTracker::Append(v8::Isolate* pIsolate) { | |
111 m_OwnedIsolates.push_back(pIsolate); | |
112 } | |
113 | |
114 void CFXJSE_IsolateTracker::Remove( | |
115 v8::Isolate* pIsolate, | |
116 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { | |
117 auto it = std::find(m_OwnedIsolates.begin(), m_OwnedIsolates.end(), pIsolate); | |
118 bool bFound = it != m_OwnedIsolates.end(); | |
119 if (bFound) | |
120 m_OwnedIsolates.erase(it); | |
121 lpfnDisposeCallback(pIsolate, bFound); | |
122 } | |
123 | |
124 void CFXJSE_IsolateTracker::RemoveAll( | |
125 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { | |
126 for (v8::Isolate* pIsolate : m_OwnedIsolates) | |
127 lpfnDisposeCallback(pIsolate, true); | |
128 | |
129 m_OwnedIsolates.clear(); | |
130 } | |
OLD | NEW |