fee16e41f1bca881c204dd9c9ce5d4238aa1c2aa
[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 JSConstSpec JS_Class_Consts[];\
171         static JSPropertySpec JS_Class_Properties[];\
172         static JSMethodSpec     JS_Class_Methods[];\
173         static const wchar_t* m_pClassName
174
175 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \
176 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
177 JSBool js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject global)\
178 {\
179         CJS_Object* pObj = FX_NEW js_class_name(obj);\
180         pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\
181         JS_SetPrivate(NULL,obj,(void*)pObj); \
182         pObj->InitInstance(cc);\
183         return JS_TRUE;\
184 }\
185 \
186 JSBool js_class_name::JSDestructor(JSFXObject obj) \
187 {\
188         js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\
189         ASSERT(pObj != NULL);\
190         pObj->ExitInstance();\
191         delete pObj;\
192         return JS_TRUE;\
193 }\
194 \
195 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
196 {\
197         int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, JSConstructor, JSDestructor, 0);\
198         if (nObjDefnID >= 0)\
199         {\
200                 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertySpec)-1; j<szj; j++)\
201                 {\
202                         if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_Properties[j].pName, JS_Class_Properties[j].pPropGet, JS_Class_Properties[j].pPropPut) < 0) return -1;\
203                 }\
204                 for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-1; k<szk; k++)\
205                 {\
206                         if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Methods[k].pName, JS_Class_Methods[k].pMethodCall, JS_Class_Methods[k].nParamNum) < 0) return -1;\
207                 }\
208                 return nObjDefnID;\
209         }\
210         return -1;\
211 }
212
213 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name)
214
215 /* ======================================== CONST CLASS ============================================ */
216
217 #define DECLARE_JS_CLASS_CONST() \
218         static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
219         static JSConstSpec JS_Class_Consts[];\
220         static const wchar_t* m_pClassName
221
222 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \
223 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
224 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
225 {\
226         int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, NULL, NULL, 0);\
227         if (nObjDefnID >=0)\
228         {\
229                 for (int i=0, sz=sizeof(JS_Class_Consts)/sizeof(JSConstSpec)-1; i<sz; i++)\
230                 {\
231                         if (JS_Class_Consts[i].t == 0)\
232                         {\
233                                 if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_Class_Consts[i].pName, JS_NewNumber(pRuntime,JS_Class_Consts[i].number)) < 0) return -1;\
234                         }\
235                         else\
236                         {\
237                         if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_Class_Consts[i].pName, JS_NewString(pRuntime,JS_Class_Consts[i].string)) < 0) return -1;\
238                         }\
239                 }\
240                 return nObjDefnID;\
241         }\
242         return -1;\
243 }
244
245 /* ===================================== SPECIAL JS CLASS =============================================== */
246
247 template <class Alt>
248 void JSSpecialPropQuery(const char *, v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info) {
249   v8::Isolate* isolate = info.GetIsolate();
250   v8::String::Utf8Value utf8_value(property);
251   CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
252   CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
253   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
254   FX_BOOL bRet = pObj->QueryProperty(propname.c_str());
255   info.GetReturnValue().Set(bRet ? 4 : 0);
256 }
257
258 template <class Alt>
259 void JSSpecialPropGet(const char* class_name,
260                       v8::Local<v8::String> property,
261                       const v8::PropertyCallbackInfo<v8::Value>& info) {
262   v8::Isolate* isolate = info.GetIsolate();
263   v8::Local<v8::Context> context = isolate->GetCurrentContext();
264   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
265   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
266   CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
267   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
268   v8::String::Utf8Value utf8_value(property);
269   CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
270   JS_ErrorString sError;
271   CJS_PropValue value(isolate);
272   value.StartGetting();
273   if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) {
274     CFX_ByteString cbName;
275     cbName.Format("%s.%s", class_name, L"GetProperty");
276     JS_Error(NULL,CFX_WideString::FromLocal(cbName), sError);
277     return;
278   }
279   info.GetReturnValue().Set((v8::Handle<v8::Value>)value);
280 }
281
282 template <class Alt>
283 void JSSpecialPropPut(const char* class_name,
284                       v8::Local<v8::String> property,
285                       v8::Local<v8::Value> value,
286                       const v8::PropertyCallbackInfo<v8::Value>& info) {
287   v8::Isolate* isolate = info.GetIsolate();
288   v8::Local<v8::Context> context = isolate->GetCurrentContext();
289   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
290   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
291   CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
292   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
293   v8::String::Utf8Value utf8_value(property);
294   CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
295   JS_ErrorString sError;
296   CJS_PropValue PropValue(CJS_Value(isolate,value,VT_unknown));
297   PropValue.StartSetting();
298   if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) {
299     CFX_ByteString cbName;
300     cbName.Format("%s.%s", class_name, "PutProperty");
301     JS_Error(NULL,CFX_WideString::FromLocal(cbName), sError);
302   }
303 }
304
305 template <class Alt>
306 void JSSpecialPropDel(const char* class_name,
307                       v8::Local<v8::String> property,
308                       const v8::PropertyCallbackInfo<v8::Boolean>& info) {
309   v8::Isolate* isolate = info.GetIsolate();
310   v8::Local<v8::Context> context = isolate->GetCurrentContext();
311   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
312   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
313   CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
314   Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
315   v8::String::Utf8Value utf8_value(property);
316   CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
317   JS_ErrorString sError;
318   if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) {
319     CFX_ByteString cbName;
320     cbName.Format("%s.%s", class_name, "DelProperty");
321     // Probably a missing call to JS_Error().
322   }
323 }
324
325 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \
326         static JSBool JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject global);\
327         static JSBool JSDestructor(JSFXObject obj);\
328         static JSConstSpec JS_Class_Consts[];\
329         static JSPropertySpec JS_Class_Properties[];\
330         static JSMethodSpec     JS_Class_Methods[];\
331         static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
332         static const wchar_t* m_pClassName;\
333         static void queryprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info);\
334         static void getprop_##js_class_name##_static(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);\
335         static void putprop_##js_class_name##_static(v8::Local<v8::String> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info);\
336         static void delprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Boolean>& info)
337
338 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \
339 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
340 void js_class_name::queryprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info) { \
341   JSSpecialPropQuery<class_alternate>(#class_name, property, info);     \
342 }\
343 void js_class_name::getprop_##js_class_name##_static(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) { \
344   JSSpecialPropGet<class_alternate>(#class_name, property, info); \
345 }                                                                       \
346 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) {\
347   JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \
348 }\
349 void js_class_name::delprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Boolean>& info) { \
350   JSSpecialPropDel<class_alternate>(#class_name, property, info); \
351 } \
352 JSBool js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject  obj,JSFXObject  global)\
353 {\
354         CJS_Object* pObj = FX_NEW js_class_name(obj);\
355         pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\
356         JS_SetPrivate(NULL,obj, (void*)pObj); \
357         pObj->InitInstance(cc);\
358         return JS_TRUE;\
359 }\
360 \
361 JSBool js_class_name::JSDestructor(JSFXObject obj) \
362 {\
363         js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\
364         ASSERT(pObj != NULL);\
365         pObj->ExitInstance();\
366         delete pObj;\
367         return JS_TRUE;\
368 }\
369 \
370 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
371 {\
372 \
373         int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, JSConstructor, JSDestructor, 0);\
374 \
375         if (nObjDefnID >= 0)\
376         {\
377                 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertySpec)-1; j<szj; j++)\
378                 {\
379                         if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_Properties[j].pName, JS_Class_Properties[j].pPropGet,JS_Class_Properties[j].pPropPut)<0)return -1;\
380                 }\
381 \
382                 for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-1; k<szk; k++)\
383                 {\
384                         if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Methods[k].pName,JS_Class_Methods[k].pMethodCall,JS_Class_Methods[k].nParamNum)<0)return -1;\
385                 }\
386                 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;\
387 \
388                 return nObjDefnID;\
389         }\
390 \
391         return -1;\
392 }
393
394 /* ======================================== GLOBAL METHODS ============================================ */
395
396 template <FX_BOOL (*F)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, JS_ErrorString&)>
397 void JSGlobalFunc(const char *func_name_string,
398                   const v8::FunctionCallbackInfo<v8::Value>& info) {
399   v8::Isolate* isolate = info.GetIsolate();
400   IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
401   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
402   CJS_Parameters parameters;
403   for (unsigned int i = 0; i<(unsigned int)info.Length(); i++) {
404     parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
405   }
406   CJS_Value valueRes(isolate);
407   JS_ErrorString sError;
408   if (!(*F)(pRuntimeContext, parameters, valueRes, sError))
409   {
410     JS_Error(NULL, JS_WIDESTRING(fun_name), sError);
411     return;
412   }
413   info.GetReturnValue().Set(valueRes.ToJSValue());
414 }
415
416 #define JS_STATIC_GLOBAL_FUN(fun_name) \
417   static void fun_name##_static(const v8::FunctionCallbackInfo<v8::Value>& info) { \
418     JSGlobalFunc<fun_name>(#fun_name, info); \
419   }
420
421 #define JS_STATIC_DECLARE_GLOBAL_FUN() \
422 static JSMethodSpec     global_methods[]; \
423 static int Init(IJS_Runtime* pRuntime)
424
425 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \
426 JSMethodSpec js_class_name::global_methods[] = {
427
428 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name,nargs) JS_STATIC_METHOD_ENTRY(method_name,nargs)
429
430 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD()
431
432 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \
433 int js_class_name::Init(IJS_Runtime* pRuntime)\
434 {\
435         for (int i=0, sz=sizeof(js_class_name::global_methods)/sizeof(JSMethodSpec)-1; i<sz; i++)\
436         {\
437                 if (JS_DefineGlobalMethod(pRuntime,\
438                                 js_class_name::global_methods[i].pName,\
439                                 js_class_name::global_methods[i].pMethodCall,\
440                                 js_class_name::global_methods[i].nParamNum\
441                                 ) < 0\
442                         )return -1;\
443         }\
444         return 0;\
445 }
446
447 /* ======================================== GLOBAL CONSTS ============================================ */
448 #define DEFINE_GLOBAL_CONST(pRuntime, const_name , const_value)\
449 if (JS_DefineGlobalConst(pRuntime,JS_WIDESTRING(const_name),JS_NewString(pRuntime,JS_WIDESTRING(const_value)))) return -1
450
451 /* ======================================== GLOBAL ARRAYS ============================================ */
452
453 #define DEFINE_GLOBAL_ARRAY(pRuntime)\
454 int size = FX_ArraySize(ArrayContent);\
455 \
456 CJS_Array array(pRuntime);\
457 for (int i=0; i<size; i++) array.SetElement(i,CJS_Value(pRuntime,ArrayContent[i]));\
458 \
459 CJS_PropValue prop(pRuntime);\
460 prop << array;\
461 if (JS_DefineGlobalConst(pRuntime, (const wchar_t*)ArrayName, prop.ToJSValue()) < 0)\
462         return -1
463
464 /* ============================================================ */
465
466 #define VALUE_NAME_STRING               L"string"
467 #define VALUE_NAME_NUMBER               L"number"
468 #define VALUE_NAME_BOOLEAN              L"boolean"
469 #define VALUE_NAME_DATE                 L"date"
470 #define VALUE_NAME_OBJECT               L"object"
471 #define VALUE_NAME_FXOBJ                L"fxobj"
472 #define VALUE_NAME_NULL                 L"null"
473 #define VALUE_NAME_UNDEFINED    L"undefined"
474
475 FXJSVALUETYPE GET_VALUE_TYPE(v8::Handle<v8::Value> p);
476
477 #endif //_JS_DEFINE_H_