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 "../../../core/include/fxcrt/fx_basic.h" | 7 #include "../../../core/include/fxcrt/fx_basic.h" |
8 #include "../../include/jsapi/fxjs_v8.h" | 8 #include "../../include/jsapi/fxjs_v8.h" |
9 | 9 |
10 const wchar_t kFXJSValueNameString[] = L"string"; | 10 const wchar_t kFXJSValueNameString[] = L"string"; |
11 const wchar_t kFXJSValueNameNumber[] = L"number"; | 11 const wchar_t kFXJSValueNameNumber[] = L"number"; |
12 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; | 12 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; |
13 const wchar_t kFXJSValueNameDate[] = L"date"; | 13 const wchar_t kFXJSValueNameDate[] = L"date"; |
14 const wchar_t kFXJSValueNameObject[] = L"object"; | 14 const wchar_t kFXJSValueNameObject[] = L"object"; |
15 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; | 15 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; |
16 const wchar_t kFXJSValueNameNull[] = L"null"; | 16 const wchar_t kFXJSValueNameNull[] = L"null"; |
17 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; | 17 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; |
18 | 18 |
19 static unsigned int g_embedderDataSlot = 1u; | |
20 | |
21 // Keep this consistent with the values defined in gin/public/context_holder.h | 19 // Keep this consistent with the values defined in gin/public/context_holder.h |
22 // (without actually requiring a dependency on gin itself for the standalone | 20 // (without actually requiring a dependency on gin itself for the standalone |
23 // embedders of PDFIum). The value we want to use is: | 21 // embedders of PDFIum). The value we want to use is: |
24 // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. | 22 // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. |
25 static const unsigned int kPerContextDataIndex = 3u; | 23 static const unsigned int kPerContextDataIndex = 3u; |
26 | 24 static unsigned int g_embedderDataSlot = 1u; |
| 25 static v8::Isolate* g_isolate = nullptr; |
| 26 static FXJS_ArrayBufferAllocator* g_arrayBufferAllocator = nullptr; |
27 static v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr; | 27 static v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr; |
28 | 28 |
29 class CFXJS_PrivateData { | 29 class CFXJS_PrivateData { |
30 public: | 30 public: |
31 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} | 31 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} |
32 | 32 |
33 int ObjDefID; | 33 int ObjDefID; |
34 void* pPrivate; | 34 void* pPrivate; |
35 }; | 35 }; |
36 | 36 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 116 } |
117 | 117 |
118 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { | 118 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
119 return malloc(length); | 119 return malloc(length); |
120 } | 120 } |
121 | 121 |
122 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { | 122 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
123 free(data); | 123 free(data); |
124 } | 124 } |
125 | 125 |
| 126 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { |
| 127 g_embedderDataSlot = embedderDataSlot; |
| 128 g_isolate = pIsolate; |
| 129 } |
| 130 |
| 131 void FXJS_Release() { |
| 132 g_DefaultGlobalObjectTemplate = nullptr; |
| 133 g_isolate = nullptr; |
| 134 |
| 135 delete g_arrayBufferAllocator; |
| 136 g_arrayBufferAllocator = nullptr; |
| 137 } |
| 138 |
| 139 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate) { |
| 140 if (g_isolate) { |
| 141 *pResultIsolate = g_isolate; |
| 142 return false; |
| 143 } |
| 144 // Provide backwards compatibility when no external isolate. |
| 145 if (!g_arrayBufferAllocator) |
| 146 g_arrayBufferAllocator = new FXJS_ArrayBufferAllocator(); |
| 147 v8::Isolate::CreateParams params; |
| 148 params.array_buffer_allocator = g_arrayBufferAllocator; |
| 149 *pResultIsolate = v8::Isolate::New(params); |
| 150 return true; |
| 151 } |
| 152 |
126 // static | 153 // static |
127 void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) { | 154 void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) { |
128 if (!pIsolate->GetData(g_embedderDataSlot)) | 155 if (!pIsolate->GetData(g_embedderDataSlot)) |
129 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData()); | 156 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData()); |
130 } | 157 } |
131 | 158 |
132 // static | 159 // static |
133 FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { | 160 FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { |
134 return static_cast<FXJS_PerIsolateData*>( | 161 return static_cast<FXJS_PerIsolateData*>( |
135 pIsolate->GetData(g_embedderDataSlot)); | 162 pIsolate->GetData(g_embedderDataSlot)); |
136 } | 163 } |
137 | 164 |
138 void FXJS_Initialize(unsigned int embedderDataSlot) { | |
139 g_embedderDataSlot = embedderDataSlot; | |
140 } | |
141 | |
142 void FXJS_Release() { | |
143 } | |
144 | |
145 int FXJS_DefineObj(v8::Isolate* pIsolate, | 165 int FXJS_DefineObj(v8::Isolate* pIsolate, |
146 const wchar_t* sObjName, | 166 const wchar_t* sObjName, |
147 FXJSOBJTYPE eObjType, | 167 FXJSOBJTYPE eObjType, |
148 FXJS_CONSTRUCTOR pConstructor, | 168 FXJS_CONSTRUCTOR pConstructor, |
149 FXJS_DESTRUCTOR pDestructor) { | 169 FXJS_DESTRUCTOR pDestructor) { |
150 v8::Isolate::Scope isolate_scope(pIsolate); | 170 v8::Isolate::Scope isolate_scope(pIsolate); |
151 v8::HandleScope handle_scope(pIsolate); | 171 v8::HandleScope handle_scope(pIsolate); |
152 | 172 |
153 FXJS_PerIsolateData::SetUp(pIsolate); | 173 FXJS_PerIsolateData::SetUp(pIsolate); |
154 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( | 174 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 return v8::Local<v8::Array>(); | 739 return v8::Local<v8::Array>(); |
720 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 740 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
721 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 741 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
722 } | 742 } |
723 | 743 |
724 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 744 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
725 pTo = pFrom; | 745 pTo = pFrom; |
726 } | 746 } |
727 | 747 |
728 | 748 |
OLD | NEW |