Introduce kPerIsolateDataIndex and tidy JS_Define.h
[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);
66 void FXJS_Release();
67
68 // Call before making FXJS_Define* calls. Resources allocated here are cleared
69 // as part of FXJS_ReleaseRuntime().
70 void FXJS_PrepareIsolate(v8::Isolate* pIsolate);
71
72 // Call before making JS_Define* calls. Resources allocated here are cleared
73 // as part of JS_ReleaseRuntime().
74 void JS_PrepareIsolate(v8::Isolate* pIsolate);
75
76 // Always returns a valid, newly-created objDefnID.
77 int FXJS_DefineObj(v8::Isolate* pIsolate,
78                    const wchar_t* sObjName,
79                    FXJSOBJTYPE eObjType,
80                    FXJS_CONSTRUCTOR pConstructor,
81                    FXJS_DESTRUCTOR pDestructor);
82
83 void FXJS_DefineObjMethod(v8::Isolate* pIsolate,
84                           int nObjDefnID,
85                           const wchar_t* sMethodName,
86                           v8::FunctionCallback pMethodCall);
87 void FXJS_DefineObjProperty(v8::Isolate* pIsolate,
88                             int nObjDefnID,
89                             const wchar_t* sPropName,
90                             v8::AccessorGetterCallback pPropGet,
91                             v8::AccessorSetterCallback pPropPut);
92 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate,
93                                  int nObjDefnID,
94                                  v8::NamedPropertyQueryCallback pPropQurey,
95                                  v8::NamedPropertyGetterCallback pPropGet,
96                                  v8::NamedPropertySetterCallback pPropPut,
97                                  v8::NamedPropertyDeleterCallback pPropDel);
98 void FXJS_DefineObjConst(v8::Isolate* pIsolate,
99                          int nObjDefnID,
100                          const wchar_t* sConstName,
101                          v8::Local<v8::Value> pDefault);
102 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate,
103                              const wchar_t* sMethodName,
104                              v8::FunctionCallback pMethodCall);
105 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
106                             const wchar_t* sConstName,
107                             v8::Local<v8::Value> pDefault);
108
109 // Called after FXJS_Define* calls made.
110 void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
111                             IFXJS_Runtime* pFXRuntime,
112                             IFXJS_Context* context,
113                             v8::Global<v8::Context>& v8PersistentContext);
114 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
115                          v8::Global<v8::Context>& v8PersistentContext);
116 IFXJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate);
117
118 // Called after FXJS_InitializeRuntime call made.
119 int FXJS_Execute(v8::Isolate* pIsolate,
120                  IFXJS_Context* pJSContext,
121                  const wchar_t* script,
122                  long length,
123                  FXJSErr* perror);
124
125 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
126                                            IFXJS_Context* pJSContext,
127                                            int nObjDefnID);
128 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate);
129 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj);
130 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName);
131 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj);
132 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj);
133
134 void FXJS_SetPrivate(v8::Isolate* pIsolate,
135                      v8::Local<v8::Object> pObj,
136                      void* p);
137 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj);
138 void FXJS_FreePrivate(void* p);
139 void FXJS_FreePrivate(v8::Local<v8::Object> pObj);
140
141 void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message);
142 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
143                                         const wchar_t* PropertyName,
144                                         int Len = -1);
145
146 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
147                                            v8::Local<v8::Object> pObj,
148                                            const wchar_t* PropertyName);
149 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
150                                                 v8::Local<v8::Object> pObj);
151
152 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
153                                           v8::Local<v8::Array> pArray,
154                                           unsigned index);
155 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
156
157 void FXJS_PutObjectString(v8::Isolate* pIsolate,
158                           v8::Local<v8::Object> pObj,
159                           const wchar_t* PropertyName,
160                           const wchar_t* sValue);
161 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
162                           v8::Local<v8::Object> pObj,
163                           const wchar_t* PropertyName,
164                           int nValue);
165 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
166                           v8::Local<v8::Object> pObj,
167                           const wchar_t* PropertyName,
168                           float fValue);
169 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
170                           v8::Local<v8::Object> pObj,
171                           const wchar_t* PropertyName,
172                           double dValue);
173 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
174                            v8::Local<v8::Object> pObj,
175                            const wchar_t* PropertyName,
176                            bool bValue);
177 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
178                           v8::Local<v8::Object> pObj,
179                           const wchar_t* PropertyName,
180                           v8::Local<v8::Object> pPut);
181 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
182                         v8::Local<v8::Object> pObj,
183                         const wchar_t* PropertyName);
184 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
185                               v8::Local<v8::Array> pArray,
186                               unsigned index,
187                               v8::Local<v8::Value> pValue);
188
189 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate);
190 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number);
191 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number);
192 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number);
193 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b);
194 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate,
195                                     v8::Local<v8::Object> pObj);
196 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
197                                      v8::Local<v8::Array> pObj);
198 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
199                                     const wchar_t* string);
200 v8::Local<v8::Value> FXJS_NewNull();
201 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d);
202
203 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
204 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
205 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
206 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
207                                     v8::Local<v8::Value> pValue);
208 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
209                              v8::Local<v8::Value> pValue);
210 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
211                                   v8::Local<v8::Value> pValue);
212 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom);
213
214 #endif  // FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_