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