Pass IJS_Runtime, not IJS_Context, to native object constructors.
[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 // FXJS_V8 is a layer that makes it easier to define native objects in V8, but
8 // has no knowledge of PDF-specific native objects. It could in theory be used
9 // to implement other sets of native objects.
10
11 // PDFium code should include this file rather than including V8 headers
12 // directly.
13
14 #ifndef FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_
15 #define FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_
16
17 #include <v8.h>
18 #include "../../../core/include/fxcrt/fx_basic.h"
19
20 // FXJS_V8 places no restrictions on these two classes; it merely passes them
21 // on to caller-provided methods.
22 class IJS_Context;  // A description of the event that caused JS execution.
23 class IJS_Runtime;  // A native runtime, typically owns the v8::Context.
24
25 enum FXJSOBJTYPE {
26   FXJSOBJTYPE_DYNAMIC = 0,  // Created by native method and returned to JS.
27   FXJSOBJTYPE_STATIC,       // Created by init and hung off of global object.
28   FXJSOBJTYPE_GLOBAL,       // The global object itself (may only appear once).
29 };
30
31 struct FXJSErr {
32   const wchar_t* message;
33   const wchar_t* srcline;
34   unsigned linnum;
35 };
36
37 class FXJS_PerIsolateData {
38  public:
39   static void SetUp(v8::Isolate* pIsolate);
40   static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
41
42   CFX_PtrArray m_ObjectDefnArray;
43
44  protected:
45   FXJS_PerIsolateData() {}
46 };
47
48 extern const wchar_t kFXJSValueNameString[];
49 extern const wchar_t kFXJSValueNameNumber[];
50 extern const wchar_t kFXJSValueNameBoolean[];
51 extern const wchar_t kFXJSValueNameDate[];
52 extern const wchar_t kFXJSValueNameObject[];
53 extern const wchar_t kFXJSValueNameFxobj[];
54 extern const wchar_t kFXJSValueNameNull[];
55 extern const wchar_t kFXJSValueNameUndefined[];
56
57
58 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
59   void* Allocate(size_t length) override;
60   void* AllocateUninitialized(size_t length) override;
61   void Free(void* data, size_t length) override;
62 };
63
64 using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local<v8::Object> obj);
65 using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj);
66
67 // Call before making FXJS_PrepareIsolate call.
68 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
69 void FXJS_Release();
70
71 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each
72 // time if there is no such isolate. Returns true if a new isolate had to be
73 // created.
74 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
75
76 // Get the global isolate's ref count.
77 size_t FXJS_GlobalIsolateRefCount();
78
79 // Call before making FXJS_Define* calls. Resources allocated here are cleared
80 // as part of FXJS_ReleaseRuntime().
81 void FXJS_PrepareIsolate(v8::Isolate* pIsolate);
82
83 // Call before making JS_Define* calls. Resources allocated here are cleared
84 // as part of JS_ReleaseRuntime().
85 void JS_PrepareIsolate(v8::Isolate* pIsolate);
86
87 // Always returns a valid, newly-created objDefnID.
88 int FXJS_DefineObj(v8::Isolate* pIsolate,
89                    const wchar_t* sObjName,
90                    FXJSOBJTYPE eObjType,
91                    FXJS_CONSTRUCTOR pConstructor,
92                    FXJS_DESTRUCTOR pDestructor);
93
94 void FXJS_DefineObjMethod(v8::Isolate* pIsolate,
95                           int nObjDefnID,
96                           const wchar_t* sMethodName,
97                           v8::FunctionCallback pMethodCall);
98 void FXJS_DefineObjProperty(v8::Isolate* pIsolate,
99                             int nObjDefnID,
100                             const wchar_t* sPropName,
101                             v8::AccessorGetterCallback pPropGet,
102                             v8::AccessorSetterCallback pPropPut);
103 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate,
104                                  int nObjDefnID,
105                                  v8::NamedPropertyQueryCallback pPropQurey,
106                                  v8::NamedPropertyGetterCallback pPropGet,
107                                  v8::NamedPropertySetterCallback pPropPut,
108                                  v8::NamedPropertyDeleterCallback pPropDel);
109 void FXJS_DefineObjConst(v8::Isolate* pIsolate,
110                          int nObjDefnID,
111                          const wchar_t* sConstName,
112                          v8::Local<v8::Value> pDefault);
113 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate,
114                              const wchar_t* sMethodName,
115                              v8::FunctionCallback pMethodCall);
116 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
117                             const wchar_t* sConstName,
118                             v8::Local<v8::Value> pDefault);
119
120 // Called after FXJS_Define* calls made.
121 void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
122                             IJS_Runtime* pIRuntime,
123                             v8::Global<v8::Context>& v8PersistentContext);
124 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
125                          v8::Global<v8::Context>& v8PersistentContext);
126 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate);
127
128 // Called after FXJS_InitializeRuntime call made.
129 int FXJS_Execute(v8::Isolate* pIsolate,
130                  IJS_Context* pJSContext,
131                  const wchar_t* script,
132                  FXJSErr* perror);
133
134 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
135                                            IJS_Runtime* pJSContext,
136                                            int nObjDefnID);
137 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate);
138 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj);
139 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj);
140 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj);
141
142 void FXJS_SetPrivate(v8::Isolate* pIsolate,
143                      v8::Local<v8::Object> pObj,
144                      void* p);
145 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj);
146 void FXJS_FreePrivate(void* p);
147 void FXJS_FreePrivate(v8::Local<v8::Object> pObj);
148
149 void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message);
150 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
151                                         const wchar_t* PropertyName,
152                                         int Len = -1);
153
154 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
155                                            v8::Local<v8::Object> pObj,
156                                            const wchar_t* PropertyName);
157 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
158                                                 v8::Local<v8::Object> pObj);
159
160 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
161                                           v8::Local<v8::Array> pArray,
162                                           unsigned index);
163 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
164
165 void FXJS_PutObjectString(v8::Isolate* pIsolate,
166                           v8::Local<v8::Object> pObj,
167                           const wchar_t* PropertyName,
168                           const wchar_t* sValue);
169 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
170                           v8::Local<v8::Object> pObj,
171                           const wchar_t* PropertyName,
172                           int nValue);
173 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
174                           v8::Local<v8::Object> pObj,
175                           const wchar_t* PropertyName,
176                           float fValue);
177 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
178                           v8::Local<v8::Object> pObj,
179                           const wchar_t* PropertyName,
180                           double dValue);
181 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
182                            v8::Local<v8::Object> pObj,
183                            const wchar_t* PropertyName,
184                            bool bValue);
185 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
186                           v8::Local<v8::Object> pObj,
187                           const wchar_t* PropertyName,
188                           v8::Local<v8::Object> pPut);
189 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
190                         v8::Local<v8::Object> pObj,
191                         const wchar_t* PropertyName);
192 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
193                               v8::Local<v8::Array> pArray,
194                               unsigned index,
195                               v8::Local<v8::Value> pValue);
196
197 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate);
198 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number);
199 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number);
200 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number);
201 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b);
202 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate,
203                                     v8::Local<v8::Object> pObj);
204 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
205                                      v8::Local<v8::Array> pObj);
206 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
207                                     const wchar_t* string);
208 v8::Local<v8::Value> FXJS_NewNull();
209 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d);
210
211 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
212 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
213 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
214 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
215                                     v8::Local<v8::Value> pValue);
216 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
217                              v8::Local<v8::Value> pValue);
218 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
219                                   v8::Local<v8::Value> pValue);
220 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom);
221
222 #endif  // FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_