Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: fxjs/include/fxjs_v8.h

Issue 2245863002: Push v8::Isolate into CFXJS_Engine class (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Null isolate before letting CFXJS_Engine dtor invoked Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fxjs/fxjs_v8_embeddertest.cpp ('k') | testing/js_embedder_test.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // FXJS_V8 is a layer that makes it easier to define native objects in V8, but 7 // FXJS_V8 is a layer that makes it easier to define native objects in V8, but
8 // has no knowledge of PDF-specific native objects. It could in theory be used 8 // has no knowledge of PDF-specific native objects. It could in theory be used
9 // to implement other sets of native objects. 9 // to implement other sets of native objects.
10 10
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 protected: 118 protected:
119 FXJS_PerIsolateData(); 119 FXJS_PerIsolateData();
120 }; 120 };
121 121
122 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { 122 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
123 void* Allocate(size_t length) override; 123 void* Allocate(size_t length) override;
124 void* AllocateUninitialized(size_t length) override; 124 void* AllocateUninitialized(size_t length) override;
125 void Free(void* data, size_t length) override; 125 void Free(void* data, size_t length) override;
126 }; 126 };
127 127
128 using FXJS_CONSTRUCTOR = void (*)(CFXJS_Engine* fxjs,
129 v8::Local<v8::Object> obj);
130 using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj);
131
132 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); 128 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
133 void FXJS_Release(); 129 void FXJS_Release();
134 130
135 class CFXJS_Engine {
136 public:
137 CFXJS_Engine();
138 ~CFXJS_Engine();
139
140 protected:
141 v8::Isolate* m_isolate;
142 v8::Global<v8::Context> m_context;
143 std::vector<v8::Global<v8::Object>*> m_StaticObjects;
144 std::map<CFX_WideString, v8::Global<v8::Array>> m_ConstArrays;
145 };
146
147 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each 131 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each
148 // time if there is no such isolate. Returns true if a new isolate had to be 132 // time if there is no such isolate. Returns true if a new isolate had to be
149 // created. 133 // created.
150 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate); 134 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
151 135
152 // Get the global isolate's ref count. 136 // Get the global isolate's ref count.
153 size_t FXJS_GlobalIsolateRefCount(); 137 size_t FXJS_GlobalIsolateRefCount();
154 138
155 // Always returns a valid, newly-created objDefnID. 139 class CFXJS_Engine {
156 int FXJS_DefineObj(v8::Isolate* pIsolate, 140 public:
157 const wchar_t* sObjName, 141 CFXJS_Engine();
158 FXJSOBJTYPE eObjType, 142 ~CFXJS_Engine();
159 FXJS_CONSTRUCTOR pConstructor,
160 FXJS_DESTRUCTOR pDestructor);
161 143
162 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, 144 using Constructor = void (*)(CFXJS_Engine* pEngine,
163 int nObjDefnID, 145 v8::Local<v8::Object> obj);
164 const wchar_t* sMethodName, 146 using Destructor = void (*)(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj);
165 v8::FunctionCallback pMethodCall);
166 void FXJS_DefineObjProperty(v8::Isolate* pIsolate,
167 int nObjDefnID,
168 const wchar_t* sPropName,
169 v8::AccessorGetterCallback pPropGet,
170 v8::AccessorSetterCallback pPropPut);
171 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate,
172 int nObjDefnID,
173 v8::NamedPropertyQueryCallback pPropQurey,
174 v8::NamedPropertyGetterCallback pPropGet,
175 v8::NamedPropertySetterCallback pPropPut,
176 v8::NamedPropertyDeleterCallback pPropDel);
177 void FXJS_DefineObjConst(v8::Isolate* pIsolate,
178 int nObjDefnID,
179 const wchar_t* sConstName,
180 v8::Local<v8::Value> pDefault);
181 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate,
182 const wchar_t* sMethodName,
183 v8::FunctionCallback pMethodCall);
184 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
185 const wchar_t* sConstName,
186 v8::FunctionCallback pConstGetter);
187 147
188 // Called after FXJS_Define* calls made. 148 static CFXJS_Engine* CurrentEngineFromIsolate(v8::Isolate* pIsolate);
189 void FXJS_InitializeEngine( 149 static int GetObjDefnID(v8::Local<v8::Object> pObj);
190 v8::Isolate* pIsolate,
191 CFXJS_Engine* pEngine,
192 v8::Global<v8::Context>* pV8PersistentContext,
193 std::vector<v8::Global<v8::Object>*>* pStaticObjects);
194 void FXJS_ReleaseEngine(v8::Isolate* pIsolate,
195 v8::Global<v8::Context>* pV8PersistentContext,
196 std::vector<v8::Global<v8::Object>*>* pStaticObjects);
197 CFXJS_Engine* FXJS_GetCurrentEngineFromIsolate(v8::Isolate* pIsolate);
198 150
199 #ifdef PDF_ENABLE_XFA 151 #ifdef PDF_ENABLE_XFA
200 // Called as part of FXJS_InitializeEngine, exposed so PDF can make its 152 // Called as part of FXJS_InitializeEngine, exposed so PDF can make its
201 // own contexts compatible with XFA or vice versa. 153 // own contexts compatible with XFA or vice versa.
202 void FXJS_SetEngineForV8Context(v8::Local<v8::Context> v8Context, 154 static void SetForV8Context(v8::Local<v8::Context> v8Context,
203 CFXJS_Engine* pEngine); 155 CFXJS_Engine* pEngine);
204 #endif // PDF_ENABLE_XFA 156 #endif // PDF_ENABLE_XFA
205 157
206 // Called after FXJS_InitializeEngine call made. 158 // TODO(tsepez): to constructor.
207 int FXJS_Execute(v8::Isolate* pIsolate, 159 void SetIsolate(v8::Isolate* pIsolate) { m_isolate = pIsolate; }
208 const CFX_WideString& script, 160 v8::Isolate* GetIsolate() const { return m_isolate; }
209 FXJSErr* perror);
210 161
211 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, 162 // Always returns a valid, newly-created objDefnID.
212 CFXJS_Engine* pEngine, 163 int DefineObj(const wchar_t* sObjName,
213 int nObjDefnID, 164 FXJSOBJTYPE eObjType,
214 bool bStatic = false); 165 Constructor pConstructor,
215 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate); 166 Destructor pDestructor);
216 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj);
217 167
218 void FXJS_SetPrivate(v8::Isolate* pIsolate, 168 void DefineObjMethod(int nObjDefnID,
219 v8::Local<v8::Object> pObj, 169 const wchar_t* sMethodName,
220 void* p); 170 v8::FunctionCallback pMethodCall);
221 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj); 171 void DefineObjProperty(int nObjDefnID,
222 void FXJS_FreePrivate(void* p); 172 const wchar_t* sPropName,
223 void FXJS_FreePrivate(v8::Local<v8::Object> pObj); 173 v8::AccessorGetterCallback pPropGet,
224 void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message); 174 v8::AccessorSetterCallback pPropPut);
175 void DefineObjAllProperties(int nObjDefnID,
176 v8::NamedPropertyQueryCallback pPropQurey,
177 v8::NamedPropertyGetterCallback pPropGet,
178 v8::NamedPropertySetterCallback pPropPut,
179 v8::NamedPropertyDeleterCallback pPropDel);
180 void DefineObjConst(int nObjDefnID,
181 const wchar_t* sConstName,
182 v8::Local<v8::Value> pDefault);
183 void DefineGlobalMethod(const wchar_t* sMethodName,
184 v8::FunctionCallback pMethodCall);
185 void DefineGlobalConst(const wchar_t* sConstName,
186 v8::FunctionCallback pConstGetter);
225 187
226 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate, 188 // Called after FXJS_Define* calls made.
227 const CFX_WideString& wsPropertyName); 189 void InitializeEngine();
190 void ReleaseEngine();
228 191
229 std::vector<CFX_WideString> FXJS_GetObjectPropertyNames( 192 // Called after FXJS_InitializeEngine call made.
230 v8::Isolate* pIsolate, 193 int Execute(const CFX_WideString& script, FXJSErr* perror);
231 v8::Local<v8::Object> pObj);
232 v8::Local<v8::Value> FXJS_GetObjectProperty(v8::Isolate* pIsolate,
233 v8::Local<v8::Object> pObj,
234 const CFX_WideString& PropertyName);
235 194
236 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray); 195 v8::Local<v8::Context> NewLocalContext();
237 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate, 196 v8::Local<v8::Context> GetPersistentContext();
238 v8::Local<v8::Array> pArray,
239 unsigned index);
240 197
241 void FXJS_PutObjectString(v8::Isolate* pIsolate, 198 v8::Local<v8::Value> NewNull();
242 v8::Local<v8::Object> pObj, 199 v8::Local<v8::Array> NewArray();
243 const CFX_WideString& wsPropertyName, 200 v8::Local<v8::Value> NewNumber(int number);
244 const CFX_WideString& wsValue); 201 v8::Local<v8::Value> NewNumber(double number);
245 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, 202 v8::Local<v8::Value> NewNumber(float number);
246 v8::Local<v8::Object> pObj, 203 v8::Local<v8::Value> NewBoolean(bool b);
247 const CFX_WideString& PropertyName, 204 v8::Local<v8::Value> NewString(const wchar_t* str);
248 int nValue); 205 v8::Local<v8::Date> NewDate(double d);
249 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, 206 v8::Local<v8::Object> NewFxDynamicObj(int nObjDefnID, bool bStatic = false);
250 v8::Local<v8::Object> pObj,
251 const CFX_WideString& PropertyName,
252 float fValue);
253 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
254 v8::Local<v8::Object> pObj,
255 const CFX_WideString& PropertyName,
256 double dValue);
257 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
258 v8::Local<v8::Object> pObj,
259 const CFX_WideString& PropertyName,
260 bool bValue);
261 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
262 v8::Local<v8::Object> pObj,
263 const CFX_WideString& PropertyName,
264 v8::Local<v8::Object> pPut);
265 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
266 v8::Local<v8::Object> pObj,
267 const CFX_WideString& PropertyName);
268 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
269 v8::Local<v8::Array> pArray,
270 unsigned index,
271 v8::Local<v8::Value> pValue);
272 207
273 v8::Local<v8::Value> FXJS_NewNull(v8::Isolate* pIsolate); 208 v8::Local<v8::Object> GetThisObj();
274 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate); 209 int ToInt32(v8::Local<v8::Value> pValue);
275 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number); 210 bool ToBoolean(v8::Local<v8::Value> pValue);
276 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number); 211 double ToNumber(v8::Local<v8::Value> pValue);
277 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number); 212 CFX_WideString ToString(v8::Local<v8::Value> pValue);
278 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b); 213 v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue);
279 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate, const wchar_t* str); 214 v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue);
280 v8::Local<v8::Date> FXJS_NewDate(v8::Isolate* pIsolate, double d);
281 215
282 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); 216 unsigned GetArrayLength(v8::Local<v8::Array> pArray);
283 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); 217 v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Array> pArray,
284 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); 218 unsigned index);
285 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, 219 unsigned PutArrayElement(v8::Local<v8::Array> pArray,
286 v8::Local<v8::Value> pValue); 220 unsigned index,
287 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, 221 v8::Local<v8::Value> pValue);
288 v8::Local<v8::Value> pValue); 222
289 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, 223 std::vector<CFX_WideString> GetObjectPropertyNames(
290 v8::Local<v8::Value> pValue); 224 v8::Local<v8::Object> pObj);
225 v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj,
226 const CFX_WideString& PropertyName);
227
228 void PutObjectString(v8::Local<v8::Object> pObj,
229 const CFX_WideString& wsPropertyName,
230 const CFX_WideString& wsValue);
231 void PutObjectNumber(v8::Local<v8::Object> pObj,
232 const CFX_WideString& PropertyName,
233 int nValue);
234 void PutObjectNumber(v8::Local<v8::Object> pObj,
235 const CFX_WideString& PropertyName,
236 float fValue);
237 void PutObjectNumber(v8::Local<v8::Object> pObj,
238 const CFX_WideString& PropertyName,
239 double dValue);
240 void PutObjectBoolean(v8::Local<v8::Object> pObj,
241 const CFX_WideString& PropertyName,
242 bool bValue);
243 void PutObjectObject(v8::Local<v8::Object> pObj,
244 const CFX_WideString& PropertyName,
245 v8::Local<v8::Object> pPut);
246 void PutObjectNull(v8::Local<v8::Object> pObj,
247 const CFX_WideString& PropertyName);
248
249 // Native object binding.
250 void SetObjectPrivate(v8::Local<v8::Object> pObj, void* p);
251 void* GetObjectPrivate(v8::Local<v8::Object> pObj);
252 static void FreeObjectPrivate(void* p);
253 static void FreeObjectPrivate(v8::Local<v8::Object> pObj);
254
255 void SetConstArray(const CFX_WideString& name, v8::Local<v8::Array> array);
256 v8::Local<v8::Array> GetConstArray(const CFX_WideString& name);
257
258 v8::Local<v8::String> WSToJSString(const CFX_WideString& wsPropertyName);
259 void Error(const CFX_WideString& message);
260
261 private:
262 v8::Isolate* m_isolate;
263 v8::Global<v8::Context> m_V8PersistentContext;
264 std::vector<v8::Global<v8::Object>*> m_StaticObjects;
265 std::map<CFX_WideString, v8::Global<v8::Array>> m_ConstArrays;
266 };
267
291 #endif // FXJS_INCLUDE_FXJS_V8_H_ 268 #endif // FXJS_INCLUDE_FXJS_V8_H_
OLDNEW
« no previous file with comments | « fxjs/fxjs_v8_embeddertest.cpp ('k') | testing/js_embedder_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698