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 // 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 |
11 // PDFium code should include this file rather than including V8 headers | 11 // PDFium code should include this file rather than including V8 headers |
12 // directly. | 12 // directly. |
13 | 13 |
14 #ifndef FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_ | 14 #ifndef FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_ |
15 #define FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_ | 15 #define FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_ |
16 | 16 |
17 #include <v8.h> | 17 #include <v8.h> |
| 18 #include <v8-util.h> |
18 | 19 |
19 #include <vector> | 20 #include <vector> |
20 | 21 |
21 #include "core/include/fxcrt/fx_string.h" | 22 #include "core/include/fxcrt/fx_string.h" |
22 | 23 |
23 class CFXJS_ObjDefinition; | 24 class CFXJS_ObjDefinition; |
24 | 25 |
25 // FXJS_V8 places no restrictions on these two classes; it merely passes them | 26 // FXJS_V8 places no restrictions on these two classes; it merely passes them |
26 // on to caller-provided methods. | 27 // on to caller-provided methods. |
27 class IJS_Context; // A description of the event that caused JS execution. | 28 class IJS_Context; // A description of the event that caused JS execution. |
(...skipping 10 matching lines...) Expand all Loading... |
38 FXJSOBJTYPE_STATIC, // Created by init and hung off of global object. | 39 FXJSOBJTYPE_STATIC, // Created by init and hung off of global object. |
39 FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once). | 40 FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once). |
40 }; | 41 }; |
41 | 42 |
42 struct FXJSErr { | 43 struct FXJSErr { |
43 const wchar_t* message; | 44 const wchar_t* message; |
44 const wchar_t* srcline; | 45 const wchar_t* srcline; |
45 unsigned linnum; | 46 unsigned linnum; |
46 }; | 47 }; |
47 | 48 |
| 49 // Global weak map to save dynamic objects. |
| 50 class V8TemplateMapTraits : public v8::StdMapTraits<void*, v8::Object> { |
| 51 public: |
| 52 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType; |
| 53 typedef void WeakCallbackDataType; |
| 54 |
| 55 static WeakCallbackDataType* WeakCallbackParameter( |
| 56 MapType* map, |
| 57 void* key, |
| 58 const v8::Local<v8::Object>& value) { |
| 59 return key; |
| 60 } |
| 61 static MapType* MapFromWeakCallbackInfo( |
| 62 const v8::WeakCallbackInfo<WeakCallbackDataType>&); |
| 63 |
| 64 static void* KeyFromWeakCallbackInfo( |
| 65 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 66 return data.GetParameter(); |
| 67 } |
| 68 static const v8::PersistentContainerCallbackType kCallbackType = |
| 69 v8::kWeakWithInternalFields; |
| 70 static void DisposeWeak( |
| 71 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {} |
| 72 static void OnWeakCallback( |
| 73 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {} |
| 74 static void Dispose(v8::Isolate* isolate, |
| 75 v8::Global<v8::Object> value, |
| 76 void* key); |
| 77 static void DisposeCallbackData(WeakCallbackDataType* callbackData) {} |
| 78 }; |
| 79 |
| 80 class V8TemplateMap { |
| 81 public: |
| 82 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType; |
| 83 |
| 84 void set(void* key, v8::Local<v8::Object> handle) { |
| 85 ASSERT(!m_map.Contains(key)); |
| 86 m_map.Set(key, handle); |
| 87 } |
| 88 explicit V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {} |
| 89 friend class V8TemplateMapTraits; |
| 90 |
| 91 private: |
| 92 MapType m_map; |
| 93 }; |
| 94 |
48 class FXJS_PerIsolateData { | 95 class FXJS_PerIsolateData { |
49 public: | 96 public: |
50 static void SetUp(v8::Isolate* pIsolate); | 97 static void SetUp(v8::Isolate* pIsolate); |
51 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate); | 98 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate); |
| 99 void CreateDynamicObjsMap(v8::Isolate* pIsolate) { |
| 100 m_pDynamicObjsMap = new V8TemplateMap(pIsolate); |
| 101 } |
| 102 void ReleaseDynamicObjsMap() { |
| 103 delete m_pDynamicObjsMap; |
| 104 m_pDynamicObjsMap = nullptr; |
| 105 } |
52 | 106 |
53 std::vector<CFXJS_ObjDefinition*> m_ObjectDefnArray; | 107 std::vector<CFXJS_ObjDefinition*> m_ObjectDefnArray; |
54 #ifdef PDF_ENABLE_XFA | 108 #ifdef PDF_ENABLE_XFA |
55 CFXJSE_RuntimeData* m_pFXJSERuntimeData; | 109 CFXJSE_RuntimeData* m_pFXJSERuntimeData; |
56 #endif // PDF_ENABLE_XFA | 110 #endif // PDF_ENABLE_XFA |
| 111 V8TemplateMap* m_pDynamicObjsMap; |
57 | 112 |
58 protected: | 113 protected: |
59 #ifndef PDF_ENABLE_XFA | 114 #ifndef PDF_ENABLE_XFA |
60 FXJS_PerIsolateData() {} | 115 FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} |
61 #else // PDF_ENABLE_XFA | 116 #else // PDF_ENABLE_XFA |
62 FXJS_PerIsolateData() : m_pFXJSERuntimeData(nullptr) {} | 117 FXJS_PerIsolateData() |
| 118 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} |
63 #endif // PDF_ENABLE_XFA | 119 #endif // PDF_ENABLE_XFA |
64 }; | 120 }; |
65 | 121 |
66 extern const wchar_t kFXJSValueNameString[]; | 122 extern const wchar_t kFXJSValueNameString[]; |
67 extern const wchar_t kFXJSValueNameNumber[]; | 123 extern const wchar_t kFXJSValueNameNumber[]; |
68 extern const wchar_t kFXJSValueNameBoolean[]; | 124 extern const wchar_t kFXJSValueNameBoolean[]; |
69 extern const wchar_t kFXJSValueNameDate[]; | 125 extern const wchar_t kFXJSValueNameDate[]; |
70 extern const wchar_t kFXJSValueNameObject[]; | 126 extern const wchar_t kFXJSValueNameObject[]; |
71 extern const wchar_t kFXJSValueNameFxobj[]; | 127 extern const wchar_t kFXJSValueNameFxobj[]; |
72 extern const wchar_t kFXJSValueNameNull[]; | 128 extern const wchar_t kFXJSValueNameNull[]; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 #endif // PDF_ENABLE_XFA | 209 #endif // PDF_ENABLE_XFA |
154 | 210 |
155 // Called after FXJS_InitializeRuntime call made. | 211 // Called after FXJS_InitializeRuntime call made. |
156 int FXJS_Execute(v8::Isolate* pIsolate, | 212 int FXJS_Execute(v8::Isolate* pIsolate, |
157 IJS_Context* pJSContext, | 213 IJS_Context* pJSContext, |
158 const wchar_t* script, | 214 const wchar_t* script, |
159 FXJSErr* perror); | 215 FXJSErr* perror); |
160 | 216 |
161 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 217 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
162 IJS_Runtime* pJSContext, | 218 IJS_Runtime* pJSContext, |
163 int nObjDefnID); | 219 int nObjDefnID, |
| 220 bool bStatic = false); |
164 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate); | 221 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate); |
165 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj); | 222 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj); |
166 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj); | 223 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj); |
167 | 224 |
168 void FXJS_SetPrivate(v8::Isolate* pIsolate, | 225 void FXJS_SetPrivate(v8::Isolate* pIsolate, |
169 v8::Local<v8::Object> pObj, | 226 v8::Local<v8::Object> pObj, |
170 void* p); | 227 void* p); |
171 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj); | 228 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj); |
172 void FXJS_FreePrivate(void* p); | 229 void FXJS_FreePrivate(void* p); |
173 void FXJS_FreePrivate(v8::Local<v8::Object> pObj); | 230 void FXJS_FreePrivate(v8::Local<v8::Object> pObj); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | 296 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); |
240 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, | 297 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, |
241 v8::Local<v8::Value> pValue); | 298 v8::Local<v8::Value> pValue); |
242 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, | 299 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, |
243 v8::Local<v8::Value> pValue); | 300 v8::Local<v8::Value> pValue); |
244 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, | 301 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, |
245 v8::Local<v8::Value> pValue); | 302 v8::Local<v8::Value> pValue); |
246 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom); | 303 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom); |
247 | 304 |
248 #endif // FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_ | 305 #endif // FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_ |
OLD | NEW |