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