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