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