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 "fxjse/runtime.h" | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "fpdfsdk/jsapi/include/fxjs_v8.h" | |
12 #include "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(v8::Isolate* pIsolate) | |
73 : m_pIsolate(pIsolate) {} | |
74 | |
75 CFXJSE_RuntimeData::~CFXJSE_RuntimeData() {} | |
76 | |
77 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { | |
78 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); | |
79 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | |
80 v8::Local<v8::FunctionTemplate> hFuncTemplate = | |
81 v8::FunctionTemplate::New(pIsolate); | |
82 v8::Local<v8::ObjectTemplate> hGlobalTemplate = | |
83 hFuncTemplate->InstanceTemplate(); | |
84 hGlobalTemplate->Set( | |
85 v8::Symbol::GetToStringTag(pIsolate), | |
86 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal) | |
87 .ToLocalChecked()); | |
88 v8::Local<v8::Context> hContext = | |
89 v8::Context::New(pIsolate, 0, hGlobalTemplate); | |
90 hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate)); | |
91 pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate); | |
92 pRuntimeData->m_hRootContext.Reset(pIsolate, hContext); | |
93 return pRuntimeData; | |
94 } | |
95 | |
96 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { | |
97 FXJS_PerIsolateData::SetUp(pIsolate); | |
98 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | |
99 if (!pData->m_pFXJSERuntimeData) | |
100 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); | |
101 return pData->m_pFXJSERuntimeData; | |
102 } | |
103 | |
104 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 |