blob: 52afc491b233ce2a1280174cbbeb5a23a4a3a35d [file] [log] [blame]
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001// 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.
Tom Sepez2311b782015-02-23 10:22:51 -08004
John Abd-El-Malek5110c472014-05-17 22:33:34 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepezba038bc2015-10-08 12:03:00 -07007// 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.
Tom Sepez9a3f8122015-04-07 15:35:48 -070013
dsinclairb3f24672016-07-12 10:42:14 -070014#ifndef FXJS_INCLUDE_FXJS_V8_H_
15#define FXJS_INCLUDE_FXJS_V8_H_
John Abd-El-Malek5110c472014-05-17 22:33:34 -070016
jinming_wang61dc96f2016-01-28 14:39:56 +080017#include <v8-util.h>
Dan Sinclair61046b92016-02-18 14:48:48 -050018#include <v8.h>
Lei Zhanga688a042015-11-09 13:57:49 -080019
tsepeza4941912016-08-15 11:40:12 -070020#include <map>
Lei Zhangd88a3642015-11-10 09:38:57 -080021#include <vector>
22
Dan Sinclaira8a28e02016-03-23 15:41:39 -040023#include "core/fxcrt/include/fx_string.h"
Lei Zhangd88a3642015-11-10 09:38:57 -080024
tsepeza4941912016-08-15 11:40:12 -070025class CFXJS_Engine;
Lei Zhangd88a3642015-11-10 09:38:57 -080026class CFXJS_ObjDefinition;
Tom Sepezed7b2b52015-09-22 08:36:17 -070027
tsepeza4941912016-08-15 11:40:12 -070028// FXJS_V8 places no restrictions on this class; it merely passes it
Tom Sepez33420902015-10-13 15:00:10 -070029// on to caller-provided methods.
30class IJS_Context; // A description of the event that caused JS execution.
Tom Sepezed7b2b52015-09-22 08:36:17 -070031
Tom Sepez51da0932015-11-25 16:05:49 -080032#ifdef PDF_ENABLE_XFA
Tom Sepezed7b2b52015-09-22 08:36:17 -070033// FXJS_V8 places no interpreation on this calass; it merely passes it
34// along to XFA.
35class CFXJSE_RuntimeData;
Tom Sepez40e9ff32015-11-30 12:39:54 -080036#endif // PDF_ENABLE_XFA
John Abd-El-Malek5110c472014-05-17 22:33:34 -070037
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038enum FXJSOBJTYPE {
Tom Sepezcd56a7d2015-10-06 11:45:28 -070039 FXJSOBJTYPE_DYNAMIC = 0, // Created by native method and returned to JS.
40 FXJSOBJTYPE_STATIC, // Created by init and hung off of global object.
41 FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once).
John Abd-El-Malek5110c472014-05-17 22:33:34 -070042};
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044struct FXJSErr {
45 const wchar_t* message;
46 const wchar_t* srcline;
47 unsigned linnum;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070048};
49
jinming_wang61dc96f2016-01-28 14:39:56 +080050// Global weak map to save dynamic objects.
51class V8TemplateMapTraits : public v8::StdMapTraits<void*, v8::Object> {
52 public:
53 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
54 typedef void WeakCallbackDataType;
55
56 static WeakCallbackDataType* WeakCallbackParameter(
57 MapType* map,
58 void* key,
59 const v8::Local<v8::Object>& value) {
60 return key;
61 }
62 static MapType* MapFromWeakCallbackInfo(
63 const v8::WeakCallbackInfo<WeakCallbackDataType>&);
64
65 static void* KeyFromWeakCallbackInfo(
66 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
67 return data.GetParameter();
68 }
69 static const v8::PersistentContainerCallbackType kCallbackType =
70 v8::kWeakWithInternalFields;
71 static void DisposeWeak(
72 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
73 static void OnWeakCallback(
74 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
75 static void Dispose(v8::Isolate* isolate,
76 v8::Global<v8::Object> value,
77 void* key);
78 static void DisposeCallbackData(WeakCallbackDataType* callbackData) {}
79};
80
81class V8TemplateMap {
82 public:
83 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
84
weili625ad662016-06-15 11:21:33 -070085 explicit V8TemplateMap(v8::Isolate* isolate);
86 ~V8TemplateMap();
87
88 void set(void* key, v8::Local<v8::Object> handle);
89
jinming_wang61dc96f2016-01-28 14:39:56 +080090 friend class V8TemplateMapTraits;
91
92 private:
93 MapType m_map;
94};
95
Tom Sepezed7b2b52015-09-22 08:36:17 -070096class FXJS_PerIsolateData {
97 public:
weili625ad662016-06-15 11:21:33 -070098 ~FXJS_PerIsolateData();
99
Tom Sepezed7b2b52015-09-22 08:36:17 -0700100 static void SetUp(v8::Isolate* pIsolate);
101 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
weili625ad662016-06-15 11:21:33 -0700102
jinming_wang61dc96f2016-01-28 14:39:56 +0800103 void CreateDynamicObjsMap(v8::Isolate* pIsolate) {
Tom Sepez9967cc52016-03-24 11:45:37 -0700104 if (!m_pDynamicObjsMap)
105 m_pDynamicObjsMap = new V8TemplateMap(pIsolate);
jinming_wang61dc96f2016-01-28 14:39:56 +0800106 }
107 void ReleaseDynamicObjsMap() {
108 delete m_pDynamicObjsMap;
109 m_pDynamicObjsMap = nullptr;
110 }
Tom Sepezed7b2b52015-09-22 08:36:17 -0700111
Lei Zhangd88a3642015-11-10 09:38:57 -0800112 std::vector<CFXJS_ObjDefinition*> m_ObjectDefnArray;
Tom Sepez51da0932015-11-25 16:05:49 -0800113#ifdef PDF_ENABLE_XFA
Tom Sepezed7b2b52015-09-22 08:36:17 -0700114 CFXJSE_RuntimeData* m_pFXJSERuntimeData;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800115#endif // PDF_ENABLE_XFA
jinming_wang61dc96f2016-01-28 14:39:56 +0800116 V8TemplateMap* m_pDynamicObjsMap;
Tom Sepezed7b2b52015-09-22 08:36:17 -0700117
118 protected:
weili625ad662016-06-15 11:21:33 -0700119 FXJS_PerIsolateData();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700120};
121
Tom Sepez39bfe122015-09-17 15:25:23 -0700122class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700123 void* Allocate(size_t length) override;
124 void* AllocateUninitialized(size_t length) override;
125 void Free(void* data, size_t length) override;
126};
127
tsepeza4941912016-08-15 11:40:12 -0700128using FXJS_CONSTRUCTOR = void (*)(CFXJS_Engine* fxjs,
129 v8::Local<v8::Object> obj);
Tom Sepez39bfe122015-09-17 15:25:23 -0700130using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj);
131
Tom Sepeza72e8e22015-10-07 10:17:53 -0700132void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
Tom Sepez39bfe122015-09-17 15:25:23 -0700133void FXJS_Release();
134
tsepeza4941912016-08-15 11:40:12 -0700135class CFXJS_Engine {
136 public:
137 CFXJS_Engine();
138 ~CFXJS_Engine();
139
140 protected:
141 v8::Isolate* m_isolate;
142 v8::Global<v8::Context> m_context;
143 std::vector<v8::Global<v8::Object>*> m_StaticObjects;
144 std::map<CFX_WideString, v8::Global<v8::Array>> m_ConstArrays;
145};
146
Tom Sepeza72e8e22015-10-07 10:17:53 -0700147// Gets the global isolate set by FXJS_Initialize(), or makes a new one each
148// time if there is no such isolate. Returns true if a new isolate had to be
149// created.
150bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
151
Lei Zhang3fa115b2015-10-08 12:04:47 -0700152// Get the global isolate's ref count.
153size_t FXJS_GlobalIsolateRefCount();
154
Tom Sepez142165e2015-09-11 13:21:50 -0700155// Always returns a valid, newly-created objDefnID.
Tom Sepez39bfe122015-09-17 15:25:23 -0700156int FXJS_DefineObj(v8::Isolate* pIsolate,
157 const wchar_t* sObjName,
158 FXJSOBJTYPE eObjType,
159 FXJS_CONSTRUCTOR pConstructor,
160 FXJS_DESTRUCTOR pDestructor);
Tom Sepez142165e2015-09-11 13:21:50 -0700161
Tom Sepez39bfe122015-09-17 15:25:23 -0700162void FXJS_DefineObjMethod(v8::Isolate* pIsolate,
Tom Sepez142165e2015-09-11 13:21:50 -0700163 int nObjDefnID,
Tom Sepez39bfe122015-09-17 15:25:23 -0700164 const wchar_t* sMethodName,
165 v8::FunctionCallback pMethodCall);
166void FXJS_DefineObjProperty(v8::Isolate* pIsolate,
167 int nObjDefnID,
168 const wchar_t* sPropName,
169 v8::AccessorGetterCallback pPropGet,
170 v8::AccessorSetterCallback pPropPut);
171void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate,
172 int nObjDefnID,
173 v8::NamedPropertyQueryCallback pPropQurey,
174 v8::NamedPropertyGetterCallback pPropGet,
175 v8::NamedPropertySetterCallback pPropPut,
176 v8::NamedPropertyDeleterCallback pPropDel);
177void FXJS_DefineObjConst(v8::Isolate* pIsolate,
178 int nObjDefnID,
179 const wchar_t* sConstName,
180 v8::Local<v8::Value> pDefault);
181void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate,
182 const wchar_t* sMethodName,
183 v8::FunctionCallback pMethodCall);
184void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
185 const wchar_t* sConstName,
Tom Sepez297b5152016-03-04 13:43:46 -0800186 v8::FunctionCallback pConstGetter);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700187
Tom Sepez39bfe122015-09-17 15:25:23 -0700188// Called after FXJS_Define* calls made.
tsepeza4941912016-08-15 11:40:12 -0700189void FXJS_InitializeEngine(
Tom Sepez4237aed2015-11-10 15:19:17 -0800190 v8::Isolate* pIsolate,
tsepeza4941912016-08-15 11:40:12 -0700191 CFXJS_Engine* pEngine,
Tom Sepez4237aed2015-11-10 15:19:17 -0800192 v8::Global<v8::Context>* pV8PersistentContext,
193 std::vector<v8::Global<v8::Object>*>* pStaticObjects);
tsepeza4941912016-08-15 11:40:12 -0700194void FXJS_ReleaseEngine(v8::Isolate* pIsolate,
195 v8::Global<v8::Context>* pV8PersistentContext,
196 std::vector<v8::Global<v8::Object>*>* pStaticObjects);
197CFXJS_Engine* FXJS_GetCurrentEngineFromIsolate(v8::Isolate* pIsolate);
Tom Sepezb8ec0a32015-11-20 14:23:02 -0800198
Tom Sepez51da0932015-11-25 16:05:49 -0800199#ifdef PDF_ENABLE_XFA
tsepeza4941912016-08-15 11:40:12 -0700200// Called as part of FXJS_InitializeEngine, exposed so PDF can make its
Tom Sepez4f4603c2015-11-10 15:03:12 -0800201// own contexts compatible with XFA or vice versa.
tsepeza4941912016-08-15 11:40:12 -0700202void FXJS_SetEngineForV8Context(v8::Local<v8::Context> v8Context,
203 CFXJS_Engine* pEngine);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800204#endif // PDF_ENABLE_XFA
Tom Sepez4f4603c2015-11-10 15:03:12 -0800205
tsepeza4941912016-08-15 11:40:12 -0700206// Called after FXJS_InitializeEngine call made.
Tom Sepez39bfe122015-09-17 15:25:23 -0700207int FXJS_Execute(v8::Isolate* pIsolate,
dsinclair884b4f92016-06-06 09:49:32 -0700208 const CFX_WideString& script,
Tom Sepez39bfe122015-09-17 15:25:23 -0700209 FXJSErr* perror);
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700210
Tom Sepez39bfe122015-09-17 15:25:23 -0700211v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
tsepeza4941912016-08-15 11:40:12 -0700212 CFXJS_Engine* pEngine,
jinming_wang61dc96f2016-01-28 14:39:56 +0800213 int nObjDefnID,
214 bool bStatic = false);
Tom Sepez39bfe122015-09-17 15:25:23 -0700215v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate);
216int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700217
Tom Sepez39bfe122015-09-17 15:25:23 -0700218void FXJS_SetPrivate(v8::Isolate* pIsolate,
219 v8::Local<v8::Object> pObj,
220 void* p);
Tom Sepez39bfe122015-09-17 15:25:23 -0700221void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj);
Tom Sepez39bfe122015-09-17 15:25:23 -0700222void FXJS_FreePrivate(void* p);
223void FXJS_FreePrivate(v8::Local<v8::Object> pObj);
Tom Sepez39bfe122015-09-17 15:25:23 -0700224void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message);
Tom Sepez39bfe122015-09-17 15:25:23 -0700225
tsepez018935c2016-04-15 13:15:12 -0700226v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
227 const CFX_WideString& wsPropertyName);
tsepezd0b6ed12016-08-11 19:50:57 -0700228
229std::vector<CFX_WideString> FXJS_GetObjectPropertyNames(
230 v8::Isolate* pIsolate,
231 v8::Local<v8::Object> pObj);
232v8::Local<v8::Value> FXJS_GetObjectProperty(v8::Isolate* pIsolate,
233 v8::Local<v8::Object> pObj,
234 const CFX_WideString& PropertyName);
235
236unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
Tom Sepez39bfe122015-09-17 15:25:23 -0700237v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
238 v8::Local<v8::Array> pArray,
239 unsigned index);
Tom Sepez39bfe122015-09-17 15:25:23 -0700240
241void FXJS_PutObjectString(v8::Isolate* pIsolate,
242 v8::Local<v8::Object> pObj,
tsepez018935c2016-04-15 13:15:12 -0700243 const CFX_WideString& wsPropertyName,
244 const CFX_WideString& wsValue);
Tom Sepez39bfe122015-09-17 15:25:23 -0700245void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
246 v8::Local<v8::Object> pObj,
tsepez018935c2016-04-15 13:15:12 -0700247 const CFX_WideString& PropertyName,
Tom Sepez39bfe122015-09-17 15:25:23 -0700248 int nValue);
249void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
250 v8::Local<v8::Object> pObj,
tsepez018935c2016-04-15 13:15:12 -0700251 const CFX_WideString& PropertyName,
Tom Sepez39bfe122015-09-17 15:25:23 -0700252 float fValue);
253void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
254 v8::Local<v8::Object> pObj,
tsepez018935c2016-04-15 13:15:12 -0700255 const CFX_WideString& PropertyName,
Tom Sepez39bfe122015-09-17 15:25:23 -0700256 double dValue);
257void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
258 v8::Local<v8::Object> pObj,
tsepez018935c2016-04-15 13:15:12 -0700259 const CFX_WideString& PropertyName,
Tom Sepez39bfe122015-09-17 15:25:23 -0700260 bool bValue);
261void FXJS_PutObjectObject(v8::Isolate* pIsolate,
262 v8::Local<v8::Object> pObj,
tsepez018935c2016-04-15 13:15:12 -0700263 const CFX_WideString& PropertyName,
Tom Sepez39bfe122015-09-17 15:25:23 -0700264 v8::Local<v8::Object> pPut);
265void FXJS_PutObjectNull(v8::Isolate* pIsolate,
266 v8::Local<v8::Object> pObj,
tsepez018935c2016-04-15 13:15:12 -0700267 const CFX_WideString& PropertyName);
Tom Sepez39bfe122015-09-17 15:25:23 -0700268unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
269 v8::Local<v8::Array> pArray,
270 unsigned index,
271 v8::Local<v8::Value> pValue);
272
tsepez88b66862016-07-14 12:07:48 -0700273v8::Local<v8::Value> FXJS_NewNull(v8::Isolate* pIsolate);
Tom Sepez39bfe122015-09-17 15:25:23 -0700274v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate);
275v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number);
276v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number);
277v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number);
278v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b);
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500279v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate, const wchar_t* str);
tsepez135b9982016-08-05 09:32:50 -0700280v8::Local<v8::Date> FXJS_NewDate(v8::Isolate* pIsolate, double d);
Tom Sepez39bfe122015-09-17 15:25:23 -0700281
282int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
283bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
284double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
285v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
286 v8::Local<v8::Value> pValue);
287CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
288 v8::Local<v8::Value> pValue);
289v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 v8::Local<v8::Value> pValue);
dsinclairb3f24672016-07-12 10:42:14 -0700291#endif // FXJS_INCLUDE_FXJS_V8_H_