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