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