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