6e4fc6f7a7e1f7e34f42e388c7f37e56415f3304
[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   JS_DYNAMIC = 0,
18   JS_STATIC = 1,
19 };
20
21 enum FXJSVALUETYPE {
22   VT_unknown,
23   VT_string,
24   VT_number,
25   VT_boolean,
26   VT_date,
27   VT_object,
28   VT_fxobject,
29   VT_null,
30   VT_undefined
31 };
32
33 struct FXJSErr {
34   const wchar_t* message;
35   const wchar_t* srcline;
36   unsigned linnum;
37 };
38
39 extern const wchar_t kFXJSValueNameString[];
40 extern const wchar_t kFXJSValueNameNumber[];
41 extern const wchar_t kFXJSValueNameBoolean[];
42 extern const wchar_t kFXJSValueNameDate[];
43 extern const wchar_t kFXJSValueNameObject[];
44 extern const wchar_t kFXJSValueNameFxobj[];
45 extern const wchar_t kFXJSValueNameNull[];
46 extern const wchar_t kFXJSValueNameUndefined[];
47
48 // FXJS_V8 places no interpretation on these two classes; it merely
49 // passes them on to the caller-provided LP_CONSTRUCTORs.
50 class IFXJS_Context;
51 class IFXJS_Runtime;
52
53 class JS_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 typedef void (*LP_CONSTRUCTOR)(IFXJS_Context* cc,
60                                v8::Local<v8::Object> obj,
61                                v8::Local<v8::Object> global);
62 typedef void (*LP_DESTRUCTOR)(v8::Local<v8::Object> obj);
63
64 // Call before making JS_PrepareIsolate call.
65 void JS_Initialize(unsigned int embedderDataSlot);
66 void JS_Release();
67
68 // Call before making JS_Define* calls. Resources allocated here are cleared
69 // as part of JS_ReleaseRuntime().
70 void JS_PrepareIsolate(v8::Isolate* pIsolate);
71
72 // Always returns a valid, newly-created objDefnID.
73 int JS_DefineObj(v8::Isolate* pIsolate,
74                  const wchar_t* sObjName,
75                  FXJSOBJTYPE eObjType,
76                  LP_CONSTRUCTOR pConstructor,
77                  LP_DESTRUCTOR pDestructor);
78
79 void JS_DefineObjMethod(v8::Isolate* pIsolate,
80                         int nObjDefnID,
81                         const wchar_t* sMethodName,
82                         v8::FunctionCallback pMethodCall);
83 void JS_DefineObjProperty(v8::Isolate* pIsolate,
84                           int nObjDefnID,
85                           const wchar_t* sPropName,
86                           v8::AccessorGetterCallback pPropGet,
87                           v8::AccessorSetterCallback pPropPut);
88 void JS_DefineObjAllProperties(v8::Isolate* pIsolate,
89                                int nObjDefnID,
90                                v8::NamedPropertyQueryCallback pPropQurey,
91                                v8::NamedPropertyGetterCallback pPropGet,
92                                v8::NamedPropertySetterCallback pPropPut,
93                                v8::NamedPropertyDeleterCallback pPropDel);
94 void JS_DefineObjConst(v8::Isolate* pIsolate,
95                        int nObjDefnID,
96                        const wchar_t* sConstName,
97                        v8::Local<v8::Value> pDefault);
98 void JS_DefineGlobalMethod(v8::Isolate* pIsolate,
99                            const wchar_t* sMethodName,
100                            v8::FunctionCallback pMethodCall);
101 void JS_DefineGlobalConst(v8::Isolate* pIsolate,
102                           const wchar_t* sConstName,
103                           v8::Local<v8::Value> pDefault);
104
105 // Called after JS_Define* calls made.
106 void JS_InitializeRuntime(v8::Isolate* pIsolate,
107                           IFXJS_Runtime* pFXRuntime,
108                           IFXJS_Context* context,
109                           v8::Global<v8::Context>& v8PersistentContext);
110 void JS_ReleaseRuntime(v8::Isolate* pIsolate,
111                        v8::Global<v8::Context>& v8PersistentContext);
112
113 // Called after JS_InitializeRuntime call made.
114 int JS_Execute(v8::Isolate* pIsolate,
115                IFXJS_Context* pJSContext,
116                const wchar_t* script,
117                long length,
118                FXJSErr* perror);
119
120 v8::Local<v8::Object> JS_NewFxDynamicObj(v8::Isolate* pIsolate,
121                                          IFXJS_Context* pJSContext,
122                                          int nObjDefnID);
123 v8::Local<v8::Object> JS_GetStaticObj(v8::Isolate* pIsolate, int nObjDefnID);
124 v8::Local<v8::Object> JS_GetThisObj(v8::Isolate* pIsolate);
125 int JS_GetObjDefnID(v8::Local<v8::Object> pObj);
126 v8::Isolate* JS_GetRuntime(v8::Local<v8::Object> pObj);
127 int JS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName);
128 void JS_Error(v8::Isolate* isolate, const CFX_WideString& message);
129 unsigned JS_CalcHash(const wchar_t* main, unsigned nLen);
130 unsigned JS_CalcHash(const wchar_t* main);
131 const wchar_t* JS_GetTypeof(v8::Local<v8::Value> pObj);
132 void JS_SetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj, void* p);
133 void* JS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj);
134 void JS_SetPrivate(v8::Local<v8::Object> pObj, void* p);
135 void* JS_GetPrivate(v8::Local<v8::Object> pObj);
136 void JS_FreePrivate(void* p);
137 void JS_FreePrivate(v8::Local<v8::Object> pObj);
138 v8::Local<v8::Value> JS_GetObjectValue(v8::Local<v8::Object> pObj);
139 v8::Local<v8::Value> JS_GetObjectElement(v8::Isolate* pIsolate,
140                                          v8::Local<v8::Object> pObj,
141                                          const wchar_t* PropertyName);
142 v8::Local<v8::Array> JS_GetObjectElementNames(v8::Isolate* pIsolate,
143                                               v8::Local<v8::Object> pObj);
144 void JS_PutObjectString(v8::Isolate* pIsolate,
145                         v8::Local<v8::Object> pObj,
146                         const wchar_t* PropertyName,
147                         const wchar_t* sValue);
148 void JS_PutObjectNumber(v8::Isolate* pIsolate,
149                         v8::Local<v8::Object> pObj,
150                         const wchar_t* PropertyName,
151                         int nValue);
152 void JS_PutObjectNumber(v8::Isolate* pIsolate,
153                         v8::Local<v8::Object> pObj,
154                         const wchar_t* PropertyName,
155                         float fValue);
156 void JS_PutObjectNumber(v8::Isolate* pIsolate,
157                         v8::Local<v8::Object> pObj,
158                         const wchar_t* PropertyName,
159                         double dValue);
160 void JS_PutObjectBoolean(v8::Isolate* pIsolate,
161                          v8::Local<v8::Object> pObj,
162                          const wchar_t* PropertyName,
163                          bool bValue);
164 void JS_PutObjectObject(v8::Isolate* pIsolate,
165                         v8::Local<v8::Object> pObj,
166                         const wchar_t* PropertyName,
167                         v8::Local<v8::Object> pPut);
168 void JS_PutObjectNull(v8::Isolate* pIsolate,
169                       v8::Local<v8::Object> pObj,
170                       const wchar_t* PropertyName);
171 unsigned JS_PutArrayElement(v8::Isolate* pIsolate,
172                             v8::Local<v8::Array> pArray,
173                             unsigned index,
174                             v8::Local<v8::Value> pValue,
175                             FXJSVALUETYPE eType);
176 v8::Local<v8::Value> JS_GetArrayElement(v8::Isolate* pIsolate,
177                                         v8::Local<v8::Array> pArray,
178                                         unsigned index);
179 unsigned JS_GetArrayLength(v8::Local<v8::Array> pArray);
180 v8::Local<v8::Value> JS_GetListValue(v8::Isolate* pIsolate,
181                                      v8::Local<v8::Value> pList,
182                                      int index);
183
184 v8::Local<v8::Array> JS_NewArray(v8::Isolate* pIsolate);
185 v8::Local<v8::Value> JS_NewNumber(v8::Isolate* pIsolate, int number);
186 v8::Local<v8::Value> JS_NewNumber(v8::Isolate* pIsolate, double number);
187 v8::Local<v8::Value> JS_NewNumber(v8::Isolate* pIsolate, float number);
188 v8::Local<v8::Value> JS_NewBoolean(v8::Isolate* pIsolate, bool b);
189 v8::Local<v8::Value> JS_NewObject(v8::Isolate* pIsolate,
190                                   v8::Local<v8::Object> pObj);
191 v8::Local<v8::Value> JS_NewObject2(v8::Isolate* pIsolate,
192                                    v8::Local<v8::Array> pObj);
193 v8::Local<v8::Value> JS_NewString(v8::Isolate* pIsolate, const wchar_t* string);
194 v8::Local<v8::Value> JS_NewString(v8::Isolate* pIsolate,
195                                   const wchar_t* string,
196                                   unsigned nLen);
197 v8::Local<v8::Value> JS_NewNull();
198 v8::Local<v8::Value> JS_NewDate(v8::Isolate* pIsolate, double d);
199 v8::Local<v8::Value> JS_NewValue(v8::Isolate* pIsolate);
200
201 int JS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
202 bool JS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
203 double JS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
204 v8::Local<v8::Object> JS_ToObject(v8::Isolate* pIsolate,
205                                   v8::Local<v8::Value> pValue);
206 CFX_WideString JS_ToString(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
207 v8::Local<v8::Array> JS_ToArray(v8::Isolate* pIsolate,
208                                 v8::Local<v8::Value> pValue);
209 void JS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom);
210
211 double JS_GetDateTime();
212 int JS_GetYearFromTime(double dt);
213 int JS_GetMonthFromTime(double dt);
214 int JS_GetDayFromTime(double dt);
215 int JS_GetHourFromTime(double dt);
216 int JS_GetMinFromTime(double dt);
217 int JS_GetSecFromTime(double dt);
218 double JS_DateParse(const wchar_t* string);
219 double JS_MakeDay(int nYear, int nMonth, int nDay);
220 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
221 double JS_MakeDate(double day, double time);
222 bool JS_PortIsNan(double d);
223 double JS_LocalTime(double d);
224
225 #endif  // FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_