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