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_JSAPI_INCLUDE_FXJS_V8_H_ | 14 #ifndef FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ |
15 #define FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ | 15 #define FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ |
16 | 16 |
17 #include <v8-util.h> | 17 #include <v8-util.h> |
18 #include <v8.h> | 18 #include <v8.h> |
19 | 19 |
20 #include <vector> | 20 #include <vector> |
21 | 21 |
22 #include "core/fxcrt/include/fx_string.h" | 22 #include "core/fxcrt/include/fx_string.h" |
23 | 23 #include "fxjse/include/fxjs_perisolatedata.h" |
24 class CFXJS_ObjDefinition; | |
25 | 24 |
26 // FXJS_V8 places no restrictions on these two classes; it merely passes them | 25 // FXJS_V8 places no restrictions on these two classes; it merely passes them |
27 // on to caller-provided methods. | 26 // on to caller-provided methods. |
28 class IJS_Context; // A description of the event that caused JS execution. | 27 class IJS_Context; // A description of the event that caused JS execution. |
29 class IJS_Runtime; // A native runtime, typically owns the v8::Context. | 28 class IJS_Runtime; // A native runtime, typically owns the v8::Context. |
30 | 29 |
31 #ifdef PDF_ENABLE_XFA | 30 #ifdef PDF_ENABLE_XFA |
32 // FXJS_V8 places no interpreation on this calass; it merely passes it | 31 // FXJS_V8 places no interpreation on this calass; it merely passes it |
33 // along to XFA. | 32 // along to XFA. |
34 class CFXJSE_RuntimeData; | 33 class CFXJSE_RuntimeData; |
35 #endif // PDF_ENABLE_XFA | 34 #endif // PDF_ENABLE_XFA |
36 | 35 |
37 enum FXJSOBJTYPE { | |
38 FXJSOBJTYPE_DYNAMIC = 0, // Created by native method and returned to JS. | |
39 FXJSOBJTYPE_STATIC, // Created by init and hung off of global object. | |
40 FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once). | |
41 }; | |
42 | |
43 struct FXJSErr { | 36 struct FXJSErr { |
44 const wchar_t* message; | 37 const wchar_t* message; |
45 const wchar_t* srcline; | 38 const wchar_t* srcline; |
46 unsigned linnum; | 39 unsigned linnum; |
47 }; | 40 }; |
48 | 41 |
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 | |
95 class FXJS_PerIsolateData { | |
96 public: | |
97 static void SetUp(v8::Isolate* pIsolate); | |
98 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate); | |
99 void CreateDynamicObjsMap(v8::Isolate* pIsolate) { | |
100 if (!m_pDynamicObjsMap) | |
101 m_pDynamicObjsMap = new V8TemplateMap(pIsolate); | |
102 } | |
103 void ReleaseDynamicObjsMap() { | |
104 delete m_pDynamicObjsMap; | |
105 m_pDynamicObjsMap = nullptr; | |
106 } | |
107 | |
108 std::vector<CFXJS_ObjDefinition*> m_ObjectDefnArray; | |
109 #ifdef PDF_ENABLE_XFA | |
110 CFXJSE_RuntimeData* m_pFXJSERuntimeData; | |
111 #endif // PDF_ENABLE_XFA | |
112 V8TemplateMap* m_pDynamicObjsMap; | |
113 | |
114 protected: | |
115 #ifndef PDF_ENABLE_XFA | |
116 FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} | |
117 #else // PDF_ENABLE_XFA | |
118 FXJS_PerIsolateData() | |
119 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} | |
120 #endif // PDF_ENABLE_XFA | |
121 }; | |
122 | |
123 extern const wchar_t kFXJSValueNameString[]; | 42 extern const wchar_t kFXJSValueNameString[]; |
124 extern const wchar_t kFXJSValueNameNumber[]; | 43 extern const wchar_t kFXJSValueNameNumber[]; |
125 extern const wchar_t kFXJSValueNameBoolean[]; | 44 extern const wchar_t kFXJSValueNameBoolean[]; |
126 extern const wchar_t kFXJSValueNameDate[]; | 45 extern const wchar_t kFXJSValueNameDate[]; |
127 extern const wchar_t kFXJSValueNameObject[]; | 46 extern const wchar_t kFXJSValueNameObject[]; |
128 extern const wchar_t kFXJSValueNameFxobj[]; | 47 extern const wchar_t kFXJSValueNameFxobj[]; |
129 extern const wchar_t kFXJSValueNameNull[]; | 48 extern const wchar_t kFXJSValueNameNull[]; |
130 extern const wchar_t kFXJSValueNameUndefined[]; | 49 extern const wchar_t kFXJSValueNameUndefined[]; |
131 | 50 |
132 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 51 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
133 void* Allocate(size_t length) override; | 52 void* Allocate(size_t length) override; |
134 void* AllocateUninitialized(size_t length) override; | 53 void* AllocateUninitialized(size_t length) override; |
135 void Free(void* data, size_t length) override; | 54 void Free(void* data, size_t length) override; |
136 }; | 55 }; |
137 | 56 |
138 using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local<v8::Object> obj); | |
139 using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj); | |
140 | |
141 // Call before making FXJS_PrepareIsolate call. | 57 // Call before making FXJS_PrepareIsolate call. |
142 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); | 58 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); |
143 void FXJS_Release(); | 59 void FXJS_Release(); |
144 | 60 |
145 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each | 61 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each |
146 // time if there is no such isolate. Returns true if a new isolate had to be | 62 // time if there is no such isolate. Returns true if a new isolate had to be |
147 // created. | 63 // created. |
148 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate); | 64 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate); |
149 | 65 |
150 // Get the global isolate's ref count. | 66 // Get the global isolate's ref count. |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | 208 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); |
293 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, | 209 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, |
294 v8::Local<v8::Value> pValue); | 210 v8::Local<v8::Value> pValue); |
295 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, | 211 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, |
296 v8::Local<v8::Value> pValue); | 212 v8::Local<v8::Value> pValue); |
297 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, | 213 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, |
298 v8::Local<v8::Value> pValue); | 214 v8::Local<v8::Value> pValue); |
299 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom); | 215 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom); |
300 | 216 |
301 #endif // FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ | 217 #endif // FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ |
OLD | NEW |