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