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