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