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