Remove some abstractions in fxjs_v8.h.
[pdfium.git] / fpdfsdk / include / javascript / JS_Define.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 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_
8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_
9
10 #include "../jsapi/fxjs_v8.h"
11 #include "resource.h"
12 #include "JS_Object.h"
13 #include "JS_Value.h"
14
15 struct JSConstSpec {
16   const wchar_t* pName;
17   double number;
18   const wchar_t* string;
19   uint8_t t;  // 0:double 1:str
20 };
21
22 struct JSPropertySpec {
23   const wchar_t* pName;
24   v8::AccessorGetterCallback pPropGet;
25   v8::AccessorSetterCallback pPropPut;
26 };
27
28 struct JSMethodSpec {
29   const wchar_t* pName;
30   v8::FunctionCallback pMethodCall;
31 };
32
33 /* ====================================== PUBLIC DEFINE SPEC
34  * ============================================== */
35 #define JS_WIDESTRING(widestring) L## #widestring
36
37 #define BEGIN_JS_STATIC_CONST(js_class_name) \
38   JSConstSpec js_class_name::JS_Class_Consts[] = {
39 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) \
40   { const_name, pValue, L"", 0 }                         \
41   ,
42 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) \
43   { const_name, 0, pValue, 1 }                           \
44   ,
45 #define END_JS_STATIC_CONST() \
46   { 0, 0, 0, 0 }              \
47   }                           \
48   ;
49
50 #define BEGIN_JS_STATIC_PROP(js_class_name) \
51   JSPropertySpec js_class_name::JS_Class_Properties[] = {
52 #define JS_STATIC_PROP_ENTRY(prop_name)                                          \
53   {                                                                              \
54     JS_WIDESTRING(prop_name), get_##prop_name##_static, set_##prop_name##_static \
55   }                                                                              \
56   ,
57 #define END_JS_STATIC_PROP() \
58   { 0, 0, 0 }                \
59   }                          \
60   ;
61
62 #define BEGIN_JS_STATIC_METHOD(js_class_name) \
63   JSMethodSpec js_class_name::JS_Class_Methods[] = {
64 #define JS_STATIC_METHOD_ENTRY(method_name)            \
65   { JS_WIDESTRING(method_name), method_name##_static } \
66   ,
67 #define END_JS_STATIC_METHOD() \
68   { 0, 0 }                     \
69   }                            \
70   ;
71
72 /* ======================================== PROP CALLBACK
73  * ============================================ */
74
75 template <class C,
76           FX_BOOL (C::*M)(IFXJS_Context* cc,
77                           CJS_PropValue& vp,
78                           CFX_WideString& sError)>
79 void JSPropGetter(const char* prop_name_string,
80                   const char* class_name_string,
81                   v8::Local<v8::String> property,
82                   const v8::PropertyCallbackInfo<v8::Value>& info) {
83   v8::Isolate* isolate = info.GetIsolate();
84   v8::Local<v8::Context> context = isolate->GetCurrentContext();
85   v8::Local<v8::Value> v = context->GetEmbedderData(1);
86   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
87   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
88   IFXJS_Context* pContext = pRuntime->GetCurrentContext();
89   CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder());
90   C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
91   CFX_WideString sError;
92   CJS_PropValue value(isolate);
93   value.StartGetting();
94   if (!(pObj->*M)(pContext, value, sError)) {
95     JS_Error(isolate,
96              JSFormatErrorString(class_name_string, prop_name_string, sError));
97     return;
98   }
99   info.GetReturnValue().Set((v8::Local<v8::Value>)value);
100 }
101
102 template <class C,
103           FX_BOOL (C::*M)(IFXJS_Context* cc,
104                           CJS_PropValue& vp,
105                           CFX_WideString& sError)>
106 void JSPropSetter(const char* prop_name_string,
107                   const char* class_name_string,
108                   v8::Local<v8::String> property,
109                   v8::Local<v8::Value> value,
110                   const v8::PropertyCallbackInfo<void>& info) {
111   v8::Isolate* isolate = info.GetIsolate();
112   v8::Local<v8::Context> context = isolate->GetCurrentContext();
113   v8::Local<v8::Value> v = context->GetEmbedderData(1);
114   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
115   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
116   IFXJS_Context* pContext = pRuntime->GetCurrentContext();
117   CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder());
118   C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
119   CFX_WideString sError;
120   CJS_PropValue propValue(CJS_Value(isolate, value, VT_unknown));
121   propValue.StartSetting();
122   if (!(pObj->*M)(pContext, propValue, sError)) {
123     JS_Error(isolate,
124              JSFormatErrorString(class_name_string, prop_name_string, sError));
125   }
126 }
127
128 #define JS_STATIC_PROP(prop_name, class_name)                                 \
129   static void get_##prop_name##_static(                                       \
130       v8::Local<v8::String> property,                                         \
131       const v8::PropertyCallbackInfo<v8::Value>& info) {                      \
132     JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \
133                                                      property, info);         \
134   }                                                                           \
135   static void set_##prop_name##_static(                                       \
136       v8::Local<v8::String> property, v8::Local<v8::Value> value,             \
137       const v8::PropertyCallbackInfo<void>& info) {                           \
138     JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \
139                                                      property, value, info);  \
140   }
141
142 /* ========================================= METHOD CALLBACK
143  * =========================================== */
144
145 template <class C,
146           FX_BOOL (C::*M)(IFXJS_Context* cc,
147                           const CJS_Parameters& params,
148                           CJS_Value& vRet,
149                           CFX_WideString& sError)>
150 void JSMethod(const char* method_name_string,
151               const char* class_name_string,
152               const v8::FunctionCallbackInfo<v8::Value>& info) {
153   v8::Isolate* isolate = info.GetIsolate();
154   v8::Local<v8::Context> context = isolate->GetCurrentContext();
155   v8::Local<v8::Value> v = context->GetEmbedderData(1);
156   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
157   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
158   IFXJS_Context* cc = pRuntime->GetCurrentContext();
159   CJS_Parameters parameters;
160   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
161     parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
162   }
163   CJS_Value valueRes(isolate);
164   CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder());
165   C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
166   CFX_WideString sError;
167   if (!(pObj->*M)(cc, parameters, valueRes, sError)) {
168     JS_Error(isolate, JSFormatErrorString(class_name_string, method_name_string,
169                                           sError));
170     return;
171   }
172   info.GetReturnValue().Set(valueRes.ToV8Value());
173 }
174
175 #define JS_STATIC_METHOD(method_name, class_name)                             \
176   static void method_name##_static(                                           \
177       const v8::FunctionCallbackInfo<v8::Value>& info) {                      \
178     JSMethod<class_name, &class_name::method_name>(#method_name, #class_name, \
179                                                    info);                     \
180   }
181
182 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \
183   static void method_name##_static(                                        \
184       const v8::FunctionCallbackInfo<v8::Value>& info) {                   \
185     JSMethod<class_alternate, &class_alternate::method_name>(              \
186         #method_name, #class_name, info);                                  \
187   }
188
189 /* ===================================== JS CLASS
190  * =============================================== */
191
192 #define DECLARE_JS_CLASS(js_class_name)                                   \
193   static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \
194                             v8::Local<v8::Object> global);                \
195   static void JSDestructor(v8::Local<v8::Object> obj);                    \
196   static int Init(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType);           \
197   static JSConstSpec JS_Class_Consts[];                                   \
198   static JSPropertySpec JS_Class_Properties[];                            \
199   static JSMethodSpec JS_Class_Methods[];                                 \
200   static const wchar_t* m_pClassName
201
202 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name)    \
203   const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);      \
204   void js_class_name::JSConstructor(IFXJS_Context* cc,                         \
205                                     v8::Local<v8::Object> obj,                 \
206                                     v8::Local<v8::Object> global) {            \
207     CJS_Object* pObj = new js_class_name(obj);                                 \
208     pObj->SetEmbedObject(new class_alternate(pObj));                           \
209     JS_SetPrivate(NULL, obj, (void*)pObj);                                     \
210     pObj->InitInstance(cc);                                                    \
211   }                                                                            \
212                                                                                \
213   void js_class_name::JSDestructor(v8::Local<v8::Object> obj) {                \
214     js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL, obj);            \
215     ASSERT(pObj != NULL);                                                      \
216     pObj->ExitInstance();                                                      \
217     delete pObj;                                                               \
218   }                                                                            \
219                                                                                \
220   int js_class_name::Init(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType) {       \
221     int nObjDefnID = JS_DefineObj(pIsolate, js_class_name::m_pClassName,       \
222                                   eObjType, JSConstructor, JSDestructor);      \
223     if (nObjDefnID >= 0) {                                                     \
224       for (int j = 0,                                                          \
225                szj = sizeof(JS_Class_Properties) / sizeof(JSPropertySpec) - 1; \
226            j < szj; j++) {                                                     \
227         if (JS_DefineObjProperty(pIsolate, nObjDefnID,                         \
228                                  JS_Class_Properties[j].pName,                 \
229                                  JS_Class_Properties[j].pPropGet,              \
230                                  JS_Class_Properties[j].pPropPut) < 0)         \
231           return -1;                                                           \
232       }                                                                        \
233       for (int k = 0,                                                          \
234                szk = sizeof(JS_Class_Methods) / sizeof(JSMethodSpec) - 1;      \
235            k < szk; k++) {                                                     \
236         if (JS_DefineObjMethod(pIsolate, nObjDefnID,                           \
237                                JS_Class_Methods[k].pName,                      \
238                                JS_Class_Methods[k].pMethodCall) < 0)           \
239           return -1;                                                           \
240       }                                                                        \
241       return nObjDefnID;                                                       \
242     }                                                                          \
243     return -1;                                                                 \
244   }
245
246 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \
247   IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name)
248
249 /* ======================================== CONST CLASS
250  * ============================================ */
251
252 #define DECLARE_JS_CLASS_CONST()                                \
253   static int Init(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \
254   static JSConstSpec JS_Class_Consts[];                         \
255   static const wchar_t* m_pClassName
256
257 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name)                   \
258   const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);     \
259   int js_class_name::Init(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType) {      \
260     int nObjDefnID = JS_DefineObj(pIsolate, js_class_name::m_pClassName,      \
261                                   eObjType, NULL, NULL);                      \
262     if (nObjDefnID >= 0) {                                                    \
263       for (int i = 0, sz = sizeof(JS_Class_Consts) / sizeof(JSConstSpec) - 1; \
264            i < sz; i++) {                                                     \
265         if (JS_Class_Consts[i].t == 0) {                                      \
266           if (JS_DefineObjConst(                                              \
267                   pIsolate, nObjDefnID, JS_Class_Consts[i].pName,             \
268                   JS_NewNumber(pIsolate, JS_Class_Consts[i].number)) < 0)     \
269             return -1;                                                        \
270         } else {                                                              \
271           if (JS_DefineObjConst(                                              \
272                   pIsolate, nObjDefnID, JS_Class_Consts[i].pName,             \
273                   JS_NewString(pIsolate, JS_Class_Consts[i].string)) < 0)     \
274             return -1;                                                        \
275         }                                                                     \
276       }                                                                       \
277       return nObjDefnID;                                                      \
278     }                                                                         \
279     return -1;                                                                \
280   }
281
282 /* ===================================== SPECIAL JS CLASS
283  * =============================================== */
284
285 template <class Alt>
286 void JSSpecialPropQuery(const char*,
287                         v8::Local<v8::String> property,
288                         const v8::PropertyCallbackInfo<v8::Integer>& info) {
289   v8::Isolate* isolate = info.GetIsolate();
290   v8::String::Utf8Value utf8_value(property);
291   CFX_WideString propname =
292       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
293   CJS_Object* pJSObj =
294       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
295   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
296   FX_BOOL bRet = pObj->QueryProperty(propname.c_str());
297   info.GetReturnValue().Set(bRet ? 4 : 0);
298 }
299
300 template <class Alt>
301 void JSSpecialPropGet(const char* class_name,
302                       v8::Local<v8::String> property,
303                       const v8::PropertyCallbackInfo<v8::Value>& info) {
304   v8::Isolate* isolate = info.GetIsolate();
305   v8::Local<v8::Context> context = isolate->GetCurrentContext();
306   v8::Local<v8::Value> v = context->GetEmbedderData(1);
307   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
308   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
309   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
310   CJS_Object* pJSObj =
311       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
312   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
313   v8::String::Utf8Value utf8_value(property);
314   CFX_WideString propname =
315       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
316   CFX_WideString sError;
317   CJS_PropValue value(isolate);
318   value.StartGetting();
319   if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) {
320     JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError));
321     return;
322   }
323   info.GetReturnValue().Set((v8::Local<v8::Value>)value);
324 }
325
326 template <class Alt>
327 void JSSpecialPropPut(const char* class_name,
328                       v8::Local<v8::String> property,
329                       v8::Local<v8::Value> value,
330                       const v8::PropertyCallbackInfo<v8::Value>& info) {
331   v8::Isolate* isolate = info.GetIsolate();
332   v8::Local<v8::Context> context = isolate->GetCurrentContext();
333   v8::Local<v8::Value> v = context->GetEmbedderData(1);
334   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
335   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
336   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
337   CJS_Object* pJSObj =
338       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
339   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
340   v8::String::Utf8Value utf8_value(property);
341   CFX_WideString propname =
342       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
343   CFX_WideString sError;
344   CJS_PropValue PropValue(CJS_Value(isolate, value, VT_unknown));
345   PropValue.StartSetting();
346   if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) {
347     JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError));
348   }
349 }
350
351 template <class Alt>
352 void JSSpecialPropDel(const char* class_name,
353                       v8::Local<v8::String> property,
354                       const v8::PropertyCallbackInfo<v8::Boolean>& info) {
355   v8::Isolate* isolate = info.GetIsolate();
356   v8::Local<v8::Context> context = isolate->GetCurrentContext();
357   v8::Local<v8::Value> v = context->GetEmbedderData(1);
358   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
359   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
360   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
361   CJS_Object* pJSObj =
362       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
363   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
364   v8::String::Utf8Value utf8_value(property);
365   CFX_WideString propname =
366       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
367   CFX_WideString sError;
368   if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) {
369     CFX_ByteString cbName;
370     cbName.Format("%s.%s", class_name, "DelProperty");
371     // Probably a missing call to JS_Error().
372   }
373 }
374
375 #define DECLARE_SPECIAL_JS_CLASS(js_class_name)                           \
376   static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \
377                             v8::Local<v8::Object> global);                \
378   static void JSDestructor(v8::Local<v8::Object> obj);                    \
379   static JSConstSpec JS_Class_Consts[];                                   \
380   static JSPropertySpec JS_Class_Properties[];                            \
381   static JSMethodSpec JS_Class_Methods[];                                 \
382   static int Init(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType);           \
383   static const wchar_t* m_pClassName;                                     \
384   static void queryprop_##js_class_name##_static(                         \
385       v8::Local<v8::String> property,                                     \
386       const v8::PropertyCallbackInfo<v8::Integer>& info);                 \
387   static void getprop_##js_class_name##_static(                           \
388       v8::Local<v8::String> property,                                     \
389       const v8::PropertyCallbackInfo<v8::Value>& info);                   \
390   static void putprop_##js_class_name##_static(                           \
391       v8::Local<v8::String> property, v8::Local<v8::Value> value,         \
392       const v8::PropertyCallbackInfo<v8::Value>& info);                   \
393   static void delprop_##js_class_name##_static(                           \
394       v8::Local<v8::String> property,                                     \
395       const v8::PropertyCallbackInfo<v8::Boolean>& info)
396
397 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \
398   const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);      \
399   void js_class_name::queryprop_##js_class_name##_static(                      \
400       v8::Local<v8::String> property,                                          \
401       const v8::PropertyCallbackInfo<v8::Integer>& info) {                     \
402     JSSpecialPropQuery<class_alternate>(#class_name, property, info);          \
403   }                                                                            \
404   void js_class_name::getprop_##js_class_name##_static(                        \
405       v8::Local<v8::String> property,                                          \
406       const v8::PropertyCallbackInfo<v8::Value>& info) {                       \
407     JSSpecialPropGet<class_alternate>(#class_name, property, info);            \
408   }                                                                            \
409   void js_class_name::putprop_##js_class_name##_static(                        \
410       v8::Local<v8::String> property, v8::Local<v8::Value> value,              \
411       const v8::PropertyCallbackInfo<v8::Value>& info) {                       \
412     JSSpecialPropPut<class_alternate>(#class_name, property, value, info);     \
413   }                                                                            \
414   void js_class_name::delprop_##js_class_name##_static(                        \
415       v8::Local<v8::String> property,                                          \
416       const v8::PropertyCallbackInfo<v8::Boolean>& info) {                     \
417     JSSpecialPropDel<class_alternate>(#class_name, property, info);            \
418   }                                                                            \
419   void js_class_name::JSConstructor(IFXJS_Context* cc,                         \
420                                     v8::Local<v8::Object> obj,                 \
421                                     v8::Local<v8::Object> global) {            \
422     CJS_Object* pObj = new js_class_name(obj);                                 \
423     pObj->SetEmbedObject(new class_alternate(pObj));                           \
424     JS_SetPrivate(NULL, obj, (void*)pObj);                                     \
425     pObj->InitInstance(cc);                                                    \
426   }                                                                            \
427                                                                                \
428   void js_class_name::JSDestructor(v8::Local<v8::Object> obj) {                \
429     js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL, obj);            \
430     ASSERT(pObj != NULL);                                                      \
431     pObj->ExitInstance();                                                      \
432     delete pObj;                                                               \
433   }                                                                            \
434                                                                                \
435   int js_class_name::Init(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType) {       \
436     int nObjDefnID = JS_DefineObj(pIsolate, js_class_name::m_pClassName,       \
437                                   eObjType, JSConstructor, JSDestructor);      \
438                                                                                \
439     if (nObjDefnID >= 0) {                                                     \
440       for (int j = 0,                                                          \
441                szj = sizeof(JS_Class_Properties) / sizeof(JSPropertySpec) - 1; \
442            j < szj; j++) {                                                     \
443         if (JS_DefineObjProperty(pIsolate, nObjDefnID,                         \
444                                  JS_Class_Properties[j].pName,                 \
445                                  JS_Class_Properties[j].pPropGet,              \
446                                  JS_Class_Properties[j].pPropPut) < 0)         \
447           return -1;                                                           \
448       }                                                                        \
449                                                                                \
450       for (int k = 0,                                                          \
451                szk = sizeof(JS_Class_Methods) / sizeof(JSMethodSpec) - 1;      \
452            k < szk; k++) {                                                     \
453         if (JS_DefineObjMethod(pIsolate, nObjDefnID,                           \
454                                JS_Class_Methods[k].pName,                      \
455                                JS_Class_Methods[k].pMethodCall) < 0)           \
456           return -1;                                                           \
457       }                                                                        \
458       if (JS_DefineObjAllProperties(                                           \
459               pIsolate, nObjDefnID,                                            \
460               js_class_name::queryprop_##js_class_name##_static,               \
461               js_class_name::getprop_##js_class_name##_static,                 \
462               js_class_name::putprop_##js_class_name##_static,                 \
463               js_class_name::delprop_##js_class_name##_static) < 0)            \
464         return -1;                                                             \
465                                                                                \
466       return nObjDefnID;                                                       \
467     }                                                                          \
468                                                                                \
469     return -1;                                                                 \
470   }
471
472 /* ======================================== GLOBAL METHODS
473  * ============================================ */
474
475 template <FX_BOOL (*F)(IFXJS_Context* cc,
476                        const CJS_Parameters& params,
477                        CJS_Value& vRet,
478                        CFX_WideString& sError)>
479 void JSGlobalFunc(const char* func_name_string,
480                   const v8::FunctionCallbackInfo<v8::Value>& info) {
481   v8::Isolate* isolate = info.GetIsolate();
482   v8::Local<v8::Context> context = isolate->GetCurrentContext();
483   v8::Local<v8::Value> v = context->GetEmbedderData(1);
484   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
485   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
486   IFXJS_Context* cc = pRuntime->GetCurrentContext();
487   CJS_Parameters parameters;
488   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
489     parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
490   }
491   CJS_Value valueRes(isolate);
492   CFX_WideString sError;
493   if (!(*F)(cc, parameters, valueRes, sError)) {
494     JS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError));
495     return;
496   }
497   info.GetReturnValue().Set(valueRes.ToV8Value());
498 }
499
500 #define JS_STATIC_GLOBAL_FUN(fun_name)                   \
501   static void fun_name##_static(                         \
502       const v8::FunctionCallbackInfo<v8::Value>& info) { \
503     JSGlobalFunc<fun_name>(#fun_name, info);             \
504   }
505
506 #define JS_STATIC_DECLARE_GLOBAL_FUN()  \
507   static JSMethodSpec global_methods[]; \
508   static int Init(v8::Isolate* pIsolate)
509
510 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \
511   JSMethodSpec js_class_name::global_methods[] = {
512 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \
513   JS_STATIC_METHOD_ENTRY(method_name)
514
515 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD()
516
517 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name)            \
518   int js_class_name::Init(v8::Isolate* pIsolate) {               \
519     for (int i = 0, sz = sizeof(js_class_name::global_methods) / \
520                              sizeof(JSMethodSpec) -              \
521                          1;                                      \
522          i < sz; i++) {                                          \
523       if (JS_DefineGlobalMethod(                                 \
524               pIsolate, js_class_name::global_methods[i].pName,  \
525               js_class_name::global_methods[i].pMethodCall) < 0) \
526         return -1;                                               \
527     }                                                            \
528     return 0;                                                    \
529   }
530
531 /* ======================================== GLOBAL CONSTS
532  * ============================================ */
533 #define DEFINE_GLOBAL_CONST(pIsolate, const_name, const_value) \
534   if (JS_DefineGlobalConst(                                    \
535           pIsolate, JS_WIDESTRING(const_name),                 \
536           JS_NewString(pIsolate, JS_WIDESTRING(const_value)))) \
537   return -1
538
539 /* ======================================== GLOBAL ARRAYS
540  * ============================================ */
541
542 #define DEFINE_GLOBAL_ARRAY(pIsolate)                           \
543   int size = FX_ArraySize(ArrayContent);                        \
544                                                                 \
545   CJS_Array array(pIsolate);                                    \
546   for (int i = 0; i < size; i++)                                \
547     array.SetElement(i, CJS_Value(pIsolate, ArrayContent[i]));  \
548                                                                 \
549   CJS_PropValue prop(pIsolate);                                 \
550   prop << array;                                                \
551   if (JS_DefineGlobalConst(pIsolate, (const wchar_t*)ArrayName, \
552                            prop.ToV8Value()) < 0)               \
553   return -1
554
555 /* ============================================================ */
556
557 #define VALUE_NAME_STRING L"string"
558 #define VALUE_NAME_NUMBER L"number"
559 #define VALUE_NAME_BOOLEAN L"boolean"
560 #define VALUE_NAME_DATE L"date"
561 #define VALUE_NAME_OBJECT L"object"
562 #define VALUE_NAME_FXOBJ L"fxobj"
563 #define VALUE_NAME_NULL L"null"
564 #define VALUE_NAME_UNDEFINED L"undefined"
565
566 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p);
567
568 #endif  // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_