0731212f6753a7836aca8e7affca897b59eda4ff
[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 DefineJSObjects(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     pObj->ExitInstance();                                                     \
216     delete pObj;                                                              \
217   }                                                                           \
218                                                                               \
219   int js_class_name::DefineJSObjects(v8::Isolate* pIsolate,                   \
220                                      FXJSOBJTYPE eObjType) {                  \
221     int nObjDefnID = JS_DefineObj(pIsolate, js_class_name::m_pClassName,      \
222                                   eObjType, JSConstructor, JSDestructor);     \
223     if (nObjDefnID < 0)                                                       \
224       return -1;                                                              \
225     for (int i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) {         \
226       if (JS_DefineObjProperty(pIsolate, nObjDefnID,                          \
227                                JS_Class_Properties[i].pName,                  \
228                                JS_Class_Properties[i].pPropGet,               \
229                                JS_Class_Properties[i].pPropPut) < 0)          \
230         return -1;                                                            \
231     }                                                                         \
232     for (int i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) {            \
233       if (JS_DefineObjMethod(pIsolate, nObjDefnID, JS_Class_Methods[i].pName, \
234                              JS_Class_Methods[i].pMethodCall) < 0)            \
235         return -1;                                                            \
236     }                                                                         \
237     return nObjDefnID;                                                        \
238   }
239
240 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \
241   IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name)
242
243 /* ======================================== CONST CLASS
244  * ============================================ */
245
246 #define DECLARE_JS_CLASS_CONST()                                           \
247   static int DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \
248   static JSConstSpec JS_Class_Consts[];                                    \
249   static const wchar_t* m_pClassName
250
251 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name)                   \
252   const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);     \
253   int js_class_name::DefineJSObjects(v8::Isolate* pIsolate,                   \
254                                      FXJSOBJTYPE eObjType) {                  \
255     int nObjDefnID = JS_DefineObj(pIsolate, js_class_name::m_pClassName,      \
256                                   eObjType, NULL, NULL);                      \
257     if (nObjDefnID >= 0) {                                                    \
258       for (int i = 0, sz = sizeof(JS_Class_Consts) / sizeof(JSConstSpec) - 1; \
259            i < sz; i++) {                                                     \
260         if (JS_Class_Consts[i].t == 0) {                                      \
261           if (JS_DefineObjConst(                                              \
262                   pIsolate, nObjDefnID, JS_Class_Consts[i].pName,             \
263                   JS_NewNumber(pIsolate, JS_Class_Consts[i].number)) < 0)     \
264             return -1;                                                        \
265         } else {                                                              \
266           if (JS_DefineObjConst(                                              \
267                   pIsolate, nObjDefnID, JS_Class_Consts[i].pName,             \
268                   JS_NewString(pIsolate, JS_Class_Consts[i].string)) < 0)     \
269             return -1;                                                        \
270         }                                                                     \
271       }                                                                       \
272       return nObjDefnID;                                                      \
273     }                                                                         \
274     return -1;                                                                \
275   }
276
277 /* ===================================== SPECIAL JS CLASS
278  * =============================================== */
279
280 template <class Alt>
281 void JSSpecialPropQuery(const char*,
282                         v8::Local<v8::String> property,
283                         const v8::PropertyCallbackInfo<v8::Integer>& info) {
284   v8::Isolate* isolate = info.GetIsolate();
285   v8::String::Utf8Value utf8_value(property);
286   CFX_WideString propname =
287       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
288   CJS_Object* pJSObj =
289       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
290   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
291   FX_BOOL bRet = pObj->QueryProperty(propname.c_str());
292   info.GetReturnValue().Set(bRet ? 4 : 0);
293 }
294
295 template <class Alt>
296 void JSSpecialPropGet(const char* class_name,
297                       v8::Local<v8::String> property,
298                       const v8::PropertyCallbackInfo<v8::Value>& info) {
299   v8::Isolate* isolate = info.GetIsolate();
300   v8::Local<v8::Context> context = isolate->GetCurrentContext();
301   v8::Local<v8::Value> v = context->GetEmbedderData(1);
302   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
303   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
304   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
305   CJS_Object* pJSObj =
306       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
307   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
308   v8::String::Utf8Value utf8_value(property);
309   CFX_WideString propname =
310       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
311   CFX_WideString sError;
312   CJS_PropValue value(isolate);
313   value.StartGetting();
314   if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) {
315     JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError));
316     return;
317   }
318   info.GetReturnValue().Set((v8::Local<v8::Value>)value);
319 }
320
321 template <class Alt>
322 void JSSpecialPropPut(const char* class_name,
323                       v8::Local<v8::String> property,
324                       v8::Local<v8::Value> value,
325                       const v8::PropertyCallbackInfo<v8::Value>& info) {
326   v8::Isolate* isolate = info.GetIsolate();
327   v8::Local<v8::Context> context = isolate->GetCurrentContext();
328   v8::Local<v8::Value> v = context->GetEmbedderData(1);
329   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
330   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
331   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
332   CJS_Object* pJSObj =
333       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
334   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
335   v8::String::Utf8Value utf8_value(property);
336   CFX_WideString propname =
337       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
338   CFX_WideString sError;
339   CJS_PropValue PropValue(CJS_Value(isolate, value, VT_unknown));
340   PropValue.StartSetting();
341   if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) {
342     JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError));
343   }
344 }
345
346 template <class Alt>
347 void JSSpecialPropDel(const char* class_name,
348                       v8::Local<v8::String> property,
349                       const v8::PropertyCallbackInfo<v8::Boolean>& info) {
350   v8::Isolate* isolate = info.GetIsolate();
351   v8::Local<v8::Context> context = isolate->GetCurrentContext();
352   v8::Local<v8::Value> v = context->GetEmbedderData(1);
353   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
354   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
355   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
356   CJS_Object* pJSObj =
357       reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
358   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
359   v8::String::Utf8Value utf8_value(property);
360   CFX_WideString propname =
361       CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
362   CFX_WideString sError;
363   if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) {
364     CFX_ByteString cbName;
365     cbName.Format("%s.%s", class_name, "DelProperty");
366     // Probably a missing call to JS_Error().
367   }
368 }
369
370 #define DECLARE_SPECIAL_JS_CLASS(js_class_name)                            \
371   static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj,  \
372                             v8::Local<v8::Object> global);                 \
373   static void JSDestructor(v8::Local<v8::Object> obj);                     \
374   static JSConstSpec JS_Class_Consts[];                                    \
375   static JSPropertySpec JS_Class_Properties[];                             \
376   static JSMethodSpec JS_Class_Methods[];                                  \
377   static int DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \
378   static const wchar_t* m_pClassName;                                      \
379   static void queryprop_##js_class_name##_static(                          \
380       v8::Local<v8::String> property,                                      \
381       const v8::PropertyCallbackInfo<v8::Integer>& info);                  \
382   static void getprop_##js_class_name##_static(                            \
383       v8::Local<v8::String> property,                                      \
384       const v8::PropertyCallbackInfo<v8::Value>& info);                    \
385   static void putprop_##js_class_name##_static(                            \
386       v8::Local<v8::String> property, v8::Local<v8::Value> value,          \
387       const v8::PropertyCallbackInfo<v8::Value>& info);                    \
388   static void delprop_##js_class_name##_static(                            \
389       v8::Local<v8::String> property,                                      \
390       const v8::PropertyCallbackInfo<v8::Boolean>& info)
391
392 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \
393   const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);      \
394   void js_class_name::queryprop_##js_class_name##_static(                      \
395       v8::Local<v8::String> property,                                          \
396       const v8::PropertyCallbackInfo<v8::Integer>& info) {                     \
397     JSSpecialPropQuery<class_alternate>(#class_name, property, info);          \
398   }                                                                            \
399   void js_class_name::getprop_##js_class_name##_static(                        \
400       v8::Local<v8::String> property,                                          \
401       const v8::PropertyCallbackInfo<v8::Value>& info) {                       \
402     JSSpecialPropGet<class_alternate>(#class_name, property, info);            \
403   }                                                                            \
404   void js_class_name::putprop_##js_class_name##_static(                        \
405       v8::Local<v8::String> property, v8::Local<v8::Value> value,              \
406       const v8::PropertyCallbackInfo<v8::Value>& info) {                       \
407     JSSpecialPropPut<class_alternate>(#class_name, property, value, info);     \
408   }                                                                            \
409   void js_class_name::delprop_##js_class_name##_static(                        \
410       v8::Local<v8::String> property,                                          \
411       const v8::PropertyCallbackInfo<v8::Boolean>& info) {                     \
412     JSSpecialPropDel<class_alternate>(#class_name, property, info);            \
413   }                                                                            \
414   void js_class_name::JSConstructor(IFXJS_Context* cc,                         \
415                                     v8::Local<v8::Object> obj,                 \
416                                     v8::Local<v8::Object> global) {            \
417     CJS_Object* pObj = new js_class_name(obj);                                 \
418     pObj->SetEmbedObject(new class_alternate(pObj));                           \
419     JS_SetPrivate(NULL, obj, (void*)pObj);                                     \
420     pObj->InitInstance(cc);                                                    \
421   }                                                                            \
422                                                                                \
423   void js_class_name::JSDestructor(v8::Local<v8::Object> obj) {                \
424     js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL, obj);            \
425     ASSERT(pObj != NULL);                                                      \
426     pObj->ExitInstance();                                                      \
427     delete pObj;                                                               \
428   }                                                                            \
429                                                                                \
430   int js_class_name::DefineJSObjects(v8::Isolate* pIsolate,                    \
431                                      FXJSOBJTYPE eObjType) {                   \
432     int nObjDefnID = JS_DefineObj(pIsolate, js_class_name::m_pClassName,       \
433                                   eObjType, JSConstructor, JSDestructor);      \
434                                                                                \
435     if (nObjDefnID < 0)                                                        \
436       return -1;                                                               \
437     for (int i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) {          \
438       if (JS_DefineObjProperty(pIsolate, nObjDefnID,                           \
439                                JS_Class_Properties[i].pName,                   \
440                                JS_Class_Properties[i].pPropGet,                \
441                                JS_Class_Properties[i].pPropPut) < 0)           \
442         return -1;                                                             \
443     }                                                                          \
444                                                                                \
445     for (int i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) {             \
446       if (JS_DefineObjMethod(pIsolate, nObjDefnID, JS_Class_Methods[i].pName,  \
447                              JS_Class_Methods[i].pMethodCall) < 0)             \
448         return -1;                                                             \
449     }                                                                          \
450     if (JS_DefineObjAllProperties(                                             \
451             pIsolate, nObjDefnID,                                              \
452             js_class_name::queryprop_##js_class_name##_static,                 \
453             js_class_name::getprop_##js_class_name##_static,                   \
454             js_class_name::putprop_##js_class_name##_static,                   \
455             js_class_name::delprop_##js_class_name##_static) < 0)              \
456       return -1;                                                               \
457     return nObjDefnID;                                                         \
458   }
459
460 /* ======================================== GLOBAL METHODS
461  * ============================================ */
462
463 template <FX_BOOL (*F)(IFXJS_Context* cc,
464                        const CJS_Parameters& params,
465                        CJS_Value& vRet,
466                        CFX_WideString& sError)>
467 void JSGlobalFunc(const char* func_name_string,
468                   const v8::FunctionCallbackInfo<v8::Value>& info) {
469   v8::Isolate* isolate = info.GetIsolate();
470   v8::Local<v8::Context> context = isolate->GetCurrentContext();
471   v8::Local<v8::Value> v = context->GetEmbedderData(1);
472   v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
473   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
474   IFXJS_Context* cc = pRuntime->GetCurrentContext();
475   CJS_Parameters parameters;
476   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
477     parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
478   }
479   CJS_Value valueRes(isolate);
480   CFX_WideString sError;
481   if (!(*F)(cc, parameters, valueRes, sError)) {
482     JS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError));
483     return;
484   }
485   info.GetReturnValue().Set(valueRes.ToV8Value());
486 }
487
488 #define JS_STATIC_GLOBAL_FUN(fun_name)                   \
489   static void fun_name##_static(                         \
490       const v8::FunctionCallbackInfo<v8::Value>& info) { \
491     JSGlobalFunc<fun_name>(#fun_name, info);             \
492   }
493
494 #define JS_STATIC_DECLARE_GLOBAL_FUN()  \
495   static JSMethodSpec global_methods[]; \
496   static int DefineJSObjects(v8::Isolate* pIsolate)
497
498 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \
499   JSMethodSpec js_class_name::global_methods[] = {
500 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \
501   JS_STATIC_METHOD_ENTRY(method_name)
502
503 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD()
504
505 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name)            \
506   int js_class_name::DefineJSObjects(v8::Isolate* pIsolate) {    \
507     for (int i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \
508       if (JS_DefineGlobalMethod(                                 \
509               pIsolate, js_class_name::global_methods[i].pName,  \
510               js_class_name::global_methods[i].pMethodCall) < 0) \
511         return -1;                                               \
512     }                                                            \
513     return 0;                                                    \
514   }
515
516 /* ============================================================ */
517
518 #define VALUE_NAME_STRING L"string"
519 #define VALUE_NAME_NUMBER L"number"
520 #define VALUE_NAME_BOOLEAN L"boolean"
521 #define VALUE_NAME_DATE L"date"
522 #define VALUE_NAME_OBJECT L"object"
523 #define VALUE_NAME_FXOBJ L"fxobj"
524 #define VALUE_NAME_NULL L"null"
525 #define VALUE_NAME_UNDEFINED L"undefined"
526
527 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p);
528
529 #endif  // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_