Pass v8::Isolate to PDFium at init time.
[pdfium.git] / fpdfsdk / include / jsapi / fxjs_v8.h
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 // PDFium wrapper around V8 APIs. PDFium code should include this file rather
8 // than including V8 headers directly.
9
10 #ifndef FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_
11 #define FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_
12
13 #include <v8.h>
14 #include "../../../core/include/fxcrt/fx_basic.h"
15
16 // FXJS_V8 places no interpretation on these two classes; it merely
17 // passes them on to the caller-provided FXJS_CONSTRUCTORs.
18 class IFXJS_Context;
19 class IFXJS_Runtime;
20
21 enum FXJSOBJTYPE {
22   FXJS_DYNAMIC = 0,
23   FXJS_STATIC = 1,
24 };
25
26 struct FXJSErr {
27   const wchar_t* message;
28   const wchar_t* srcline;
29   unsigned linnum;
30 };
31
32 class FXJS_PerIsolateData {
33  public:
34   static void SetUp(v8::Isolate* pIsolate);
35   static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
36
37   CFX_PtrArray m_ObjectDefnArray;
38
39  protected:
40   FXJS_PerIsolateData() {}
41 };
42
43 extern const wchar_t kFXJSValueNameString[];
44 extern const wchar_t kFXJSValueNameNumber[];
45 extern const wchar_t kFXJSValueNameBoolean[];
46 extern const wchar_t kFXJSValueNameDate[];
47 extern const wchar_t kFXJSValueNameObject[];
48 extern const wchar_t kFXJSValueNameFxobj[];
49 extern const wchar_t kFXJSValueNameNull[];
50 extern const wchar_t kFXJSValueNameUndefined[];
51
52
53 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
54   void* Allocate(size_t length) override;
55   void* AllocateUninitialized(size_t length) override;
56   void Free(void* data, size_t length) override;
57 };
58
59 using FXJS_CONSTRUCTOR = void (*)(IFXJS_Context* cc,
60                                   v8::Local<v8::Object> obj,
61                                   v8::Local<v8::Object> global);
62 using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj);
63
64 // Call before making FXJS_PrepareIsolate call.
65 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
66 void FXJS_Release();
67
68 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each
69 // time if there is no such isolate. Returns true if a new isolate had to be
70 // created.
71 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
72
73 // Call before making FXJS_Define* calls. Resources allocated here are cleared
74 // as part of FXJS_ReleaseRuntime().
75 void FXJS_PrepareIsolate(v8::Isolate* pIsolate);
76
77 // Call before making JS_Define* calls. Resources allocated here are cleared
78 // as part of JS_ReleaseRuntime().
79 void JS_PrepareIsolate(v8::Isolate* pIsolate);
80
81 // Always returns a valid, newly-created objDefnID.
82 int FXJS_DefineObj(v8::Isolate* pIsolate,
83                    const wchar_t* sObjName,
84                    FXJSOBJTYPE eObjType,
85                    FXJS_CONSTRUCTOR pConstructor,
86                    FXJS_DESTRUCTOR pDestructor);
87
88 void FXJS_DefineObjMethod(v8::Isolate* pIsolate,
89                           int nObjDefnID,
90                           const wchar_t* sMethodName,
91                           v8::FunctionCallback pMethodCall);
92 void FXJS_DefineObjProperty(v8::Isolate* pIsolate,
93                             int nObjDefnID,
94                             const wchar_t* sPropName,
95                             v8::AccessorGetterCallback pPropGet,
96                             v8::AccessorSetterCallback pPropPut);
97 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate,
98                                  int nObjDefnID,
99                                  v8::NamedPropertyQueryCallback pPropQurey,
100                                  v8::NamedPropertyGetterCallback pPropGet,
101                                  v8::NamedPropertySetterCallback pPropPut,
102                                  v8::NamedPropertyDeleterCallback pPropDel);
103 void FXJS_DefineObjConst(v8::Isolate* pIsolate,
104                          int nObjDefnID,
105                          const wchar_t* sConstName,
106                          v8::Local<v8::Value> pDefault);
107 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate,
108                              const wchar_t* sMethodName,
109                              v8::FunctionCallback pMethodCall);
110 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
111                             const wchar_t* sConstName,
112                             v8::Local<v8::Value> pDefault);
113
114 // Called after FXJS_Define* calls made.
115 void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
116                             IFXJS_Runtime* pFXRuntime,
117                             IFXJS_Context* context,
118                             v8::Global<v8::Context>& v8PersistentContext);
119 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
120                          v8::Global<v8::Context>& v8PersistentContext);
121 IFXJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate);
122
123 // Called after FXJS_InitializeRuntime call made.
124 int FXJS_Execute(v8::Isolate* pIsolate,
125                  IFXJS_Context* pJSContext,
126                  const wchar_t* script,
127                  long length,
128                  FXJSErr* perror);
129
130 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
131                                            IFXJS_Context* pJSContext,
132                                            int nObjDefnID);
133 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate);
134 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj);
135 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName);
136 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj);
137 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj);
138
139 void FXJS_SetPrivate(v8::Isolate* pIsolate,
140                      v8::Local<v8::Object> pObj,
141                      void* p);
142 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj);
143 void FXJS_FreePrivate(void* p);
144 void FXJS_FreePrivate(v8::Local<v8::Object> pObj);
145
146 void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message);
147 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
148                                         const wchar_t* PropertyName,
149                                         int Len = -1);
150
151 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
152                                            v8::Local<v8::Object> pObj,
153                                            const wchar_t* PropertyName);
154 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
155                                                 v8::Local<v8::Object> pObj);
156
157 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
158                                           v8::Local<v8::Array> pArray,
159                                           unsigned index);
160 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
161
162 void FXJS_PutObjectString(v8::Isolate* pIsolate,
163                           v8::Local<v8::Object> pObj,
164                           const wchar_t* PropertyName,
165                           const wchar_t* sValue);
166 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
167                           v8::Local<v8::Object> pObj,
168                           const wchar_t* PropertyName,
169                           int nValue);
170 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
171                           v8::Local<v8::Object> pObj,
172                           const wchar_t* PropertyName,
173                           float fValue);
174 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
175                           v8::Local<v8::Object> pObj,
176                           const wchar_t* PropertyName,
177                           double dValue);
178 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
179                            v8::Local<v8::Object> pObj,
180                            const wchar_t* PropertyName,
181                            bool bValue);
182 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
183                           v8::Local<v8::Object> pObj,
184                           const wchar_t* PropertyName,
185                           v8::Local<v8::Object> pPut);
186 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
187                         v8::Local<v8::Object> pObj,
188                         const wchar_t* PropertyName);
189 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
190                               v8::Local<v8::Array> pArray,
191                               unsigned index,
192                               v8::Local<v8::Value> pValue);
193
194 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate);
195 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number);
196 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number);
197 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number);
198 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b);
199 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate,
200                                     v8::Local<v8::Object> pObj);
201 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
202                                      v8::Local<v8::Array> pObj);
203 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
204                                     const wchar_t* string);
205 v8::Local<v8::Value> FXJS_NewNull();
206 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d);
207
208 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
209 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
210 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
211 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
212                                     v8::Local<v8::Value> pValue);
213 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
214                              v8::Local<v8::Value> pValue);
215 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
216                                   v8::Local<v8::Value> pValue);
217 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom);
218
219 #endif  // FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_