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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
10 #include "../jsapi/fxjs_v8.h"
13 #include "JS_Object.h"
20 const wchar_t* string;
21 FX_BYTE t; //0:double 1:str
27 v8::AccessorGetterCallback pPropGet;
28 v8::AccessorSetterCallback pPropPut;
34 v8::FunctionCallback pMethodCall;
38 /* ====================================== PUBLIC DEFINE SPEC ============================================== */
39 #define JS_WIDESTRING(widestring) L###widestring
41 #define BEGIN_JS_STATIC_CONST(js_class_name) JSConstSpec js_class_name::JS_Class_Consts[] = {
42 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) {JS_WIDESTRING(const_name), pValue, L"", 0},
43 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) {JS_WIDESTRING(const_name), 0, JS_WIDESTRING(pValue), 1},
44 #define END_JS_STATIC_CONST() {0, 0, 0, 0}};
46 #define BEGIN_JS_STATIC_PROP(js_class_name) JSPropertySpec js_class_name::JS_Class_Properties[] = {
47 #define JS_STATIC_PROP_ENTRY(prop_name) {JS_WIDESTRING(prop_name), get_##prop_name##_static, set_##prop_name##_static},
48 #define END_JS_STATIC_PROP() {0, 0, 0}};
50 #define BEGIN_JS_STATIC_METHOD(js_class_name) JSMethodSpec js_class_name::JS_Class_Methods[] = {
51 #define JS_STATIC_METHOD_ENTRY(method_name, nargs) {JS_WIDESTRING(method_name), method_name##_static, nargs},
52 #define END_JS_STATIC_METHOD() {0, 0, 0}};
54 /* ======================================== PROP CALLBACK ============================================ */
56 template <class C, FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)>
57 void JSPropGetter(const char* prop_name_string,
58 const char* class_name_string,
59 v8::Local<v8::String> property,
60 const v8::PropertyCallbackInfo<v8::Value>& info) {
61 v8::Isolate* isolate = info.GetIsolate();
62 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
63 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
64 CJS_PropValue value(isolate);
66 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());
67 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
68 CFX_WideString sError;
69 if (!(pObj->*M)(pRuntimeContext, value, sError)) {
70 JS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, sError));
73 info.GetReturnValue().Set((v8::Handle<v8::Value>)value);
76 template <class C, FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)>
77 void JSPropSetter(const char* prop_name_string,
78 const char* class_name_string,
79 v8::Local<v8::String> property,
80 v8::Local<v8::Value> value,
81 const v8::PropertyCallbackInfo<void>& info) {
82 v8::Isolate* isolate = info.GetIsolate();
83 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
84 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
85 CJS_PropValue propValue(CJS_Value(isolate, value, VT_unknown));
86 propValue.StartSetting();
87 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());
88 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
89 CFX_WideString sError;
90 if (!(pObj->*M)(pRuntimeContext, propValue, sError)) {
91 JS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, sError));
95 #define JS_STATIC_PROP(prop_name, class_name) \
96 static void get_##prop_name##_static( \
97 v8::Local<v8::String> property, \
98 const v8::PropertyCallbackInfo<v8::Value>& info) { \
99 JSPropGetter<class_name, &class_name::prop_name>( \
100 #prop_name, #class_name, property, info); \
102 static void set_##prop_name##_static( \
103 v8::Local<v8::String> property, \
104 v8::Local<v8::Value> value, \
105 const v8::PropertyCallbackInfo<void>& info) { \
106 JSPropSetter<class_name, &class_name::prop_name>( \
107 #prop_name, #class_name, property, value, info); \
110 /* ========================================= METHOD CALLBACK =========================================== */
112 template <class C, FX_BOOL (C::*M)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)>
113 void JSMethod(const char* method_name_string,
114 const char* class_name_string,
115 const v8::FunctionCallbackInfo<v8::Value>& info) {
116 v8::Isolate* isolate = info.GetIsolate();
117 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
118 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
119 CJS_Parameters parameters;
120 for (unsigned int i = 0; i<(unsigned int)info.Length(); i++) {
121 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
123 CJS_Value valueRes(isolate);
124 CJS_Object* pJSObj = (CJS_Object *)JS_GetPrivate(isolate,info.Holder());
125 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
126 CFX_WideString sError;
127 if (!(pObj->*M)(pRuntimeContext, parameters, valueRes, sError)) {
128 JS_Error(isolate, JSFormatErrorString(class_name_string, method_name_string, sError));
131 info.GetReturnValue().Set(valueRes.ToJSValue());
134 #define JS_STATIC_METHOD(method_name, class_name) \
135 static void method_name##_static( \
136 const v8::FunctionCallbackInfo<v8::Value>& info) { \
137 JSMethod<class_name, &class_name::method_name>( \
138 #method_name, #class_name, info); \
141 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \
142 static void method_name##_static( \
143 const v8::FunctionCallbackInfo<v8::Value>& info) { \
144 JSMethod<class_alternate, &class_alternate::method_name>( \
145 #method_name, #class_name, info); \
148 /* ===================================== JS CLASS =============================================== */
150 #define DECLARE_JS_CLASS(js_class_name) \
151 static void JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject global);\
152 static void JSDestructor(JSFXObject obj);\
153 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
154 static JSConstSpec JS_Class_Consts[];\
155 static JSPropertySpec JS_Class_Properties[];\
156 static JSMethodSpec JS_Class_Methods[];\
157 static const wchar_t* m_pClassName
159 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \
160 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
161 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject global)\
163 CJS_Object* pObj = FX_NEW js_class_name(obj);\
164 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\
165 JS_SetPrivate(NULL,obj,(void*)pObj); \
166 pObj->InitInstance(cc);\
169 void js_class_name::JSDestructor(JSFXObject obj) \
171 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\
172 ASSERT(pObj != NULL);\
173 pObj->ExitInstance();\
177 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
179 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, JSConstructor, JSDestructor, 0);\
180 if (nObjDefnID >= 0)\
182 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertySpec)-1; j<szj; j++)\
184 if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_Properties[j].pName, JS_Class_Properties[j].pPropGet, JS_Class_Properties[j].pPropPut) < 0) return -1;\
186 for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-1; k<szk; k++)\
188 if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Methods[k].pName, JS_Class_Methods[k].pMethodCall, JS_Class_Methods[k].nParamNum) < 0) return -1;\
195 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name)
197 /* ======================================== CONST CLASS ============================================ */
199 #define DECLARE_JS_CLASS_CONST() \
200 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
201 static JSConstSpec JS_Class_Consts[];\
202 static const wchar_t* m_pClassName
204 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \
205 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
206 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
208 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, NULL, NULL, 0);\
211 for (int i=0, sz=sizeof(JS_Class_Consts)/sizeof(JSConstSpec)-1; i<sz; i++)\
213 if (JS_Class_Consts[i].t == 0)\
215 if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_Class_Consts[i].pName, JS_NewNumber(pRuntime,JS_Class_Consts[i].number)) < 0) return -1;\
219 if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_Class_Consts[i].pName, JS_NewString(pRuntime,JS_Class_Consts[i].string)) < 0) return -1;\
227 /* ===================================== SPECIAL JS CLASS =============================================== */
230 void JSSpecialPropQuery(const char *, v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info) {
231 v8::Isolate* isolate = info.GetIsolate();
232 v8::String::Utf8Value utf8_value(property);
233 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
234 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
235 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
236 FX_BOOL bRet = pObj->QueryProperty(propname.c_str());
237 info.GetReturnValue().Set(bRet ? 4 : 0);
241 void JSSpecialPropGet(const char* class_name,
242 v8::Local<v8::String> property,
243 const v8::PropertyCallbackInfo<v8::Value>& info) {
244 v8::Isolate* isolate = info.GetIsolate();
245 v8::Local<v8::Context> context = isolate->GetCurrentContext();
246 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
247 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
248 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
249 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
250 v8::String::Utf8Value utf8_value(property);
251 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
252 CFX_WideString sError;
253 CJS_PropValue value(isolate);
254 value.StartGetting();
255 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) {
256 JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError));
259 info.GetReturnValue().Set((v8::Handle<v8::Value>)value);
263 void JSSpecialPropPut(const char* class_name,
264 v8::Local<v8::String> property,
265 v8::Local<v8::Value> value,
266 const v8::PropertyCallbackInfo<v8::Value>& info) {
267 v8::Isolate* isolate = info.GetIsolate();
268 v8::Local<v8::Context> context = isolate->GetCurrentContext();
269 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
270 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
271 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
272 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
273 v8::String::Utf8Value utf8_value(property);
274 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
275 CFX_WideString sError;
276 CJS_PropValue PropValue(CJS_Value(isolate,value,VT_unknown));
277 PropValue.StartSetting();
278 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) {
279 JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError));
284 void JSSpecialPropDel(const char* class_name,
285 v8::Local<v8::String> property,
286 const v8::PropertyCallbackInfo<v8::Boolean>& info) {
287 v8::Isolate* isolate = info.GetIsolate();
288 v8::Local<v8::Context> context = isolate->GetCurrentContext();
289 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
290 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
291 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
292 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
293 v8::String::Utf8Value utf8_value(property);
294 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
295 CFX_WideString sError;
296 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) {
297 CFX_ByteString cbName;
298 cbName.Format("%s.%s", class_name, "DelProperty");
299 // Probably a missing call to JS_Error().
303 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \
304 static void JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject global);\
305 static void JSDestructor(JSFXObject obj);\
306 static JSConstSpec JS_Class_Consts[];\
307 static JSPropertySpec JS_Class_Properties[];\
308 static JSMethodSpec JS_Class_Methods[];\
309 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
310 static const wchar_t* m_pClassName;\
311 static void queryprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info);\
312 static void getprop_##js_class_name##_static(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);\
313 static void putprop_##js_class_name##_static(v8::Local<v8::String> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info);\
314 static void delprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Boolean>& info)
316 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \
317 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
318 void js_class_name::queryprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info) { \
319 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \
321 void js_class_name::getprop_##js_class_name##_static(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) { \
322 JSSpecialPropGet<class_alternate>(#class_name, property, info); \
324 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) {\
325 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \
327 void js_class_name::delprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Boolean>& info) { \
328 JSSpecialPropDel<class_alternate>(#class_name, property, info); \
330 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject global)\
332 CJS_Object* pObj = FX_NEW js_class_name(obj);\
333 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\
334 JS_SetPrivate(NULL,obj, (void*)pObj); \
335 pObj->InitInstance(cc);\
338 void js_class_name::JSDestructor(JSFXObject obj) \
340 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\
341 ASSERT(pObj != NULL);\
342 pObj->ExitInstance();\
346 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
349 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, JSConstructor, JSDestructor, 0);\
351 if (nObjDefnID >= 0)\
353 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertySpec)-1; j<szj; j++)\
355 if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_Properties[j].pName, JS_Class_Properties[j].pPropGet,JS_Class_Properties[j].pPropPut)<0)return -1;\
358 for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-1; k<szk; k++)\
360 if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Methods[k].pName,JS_Class_Methods[k].pMethodCall,JS_Class_Methods[k].nParamNum)<0)return -1;\
362 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;\
370 /* ======================================== GLOBAL METHODS ============================================ */
372 template <FX_BOOL (*F)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)>
373 void JSGlobalFunc(const char *func_name_string,
374 const v8::FunctionCallbackInfo<v8::Value>& info) {
375 v8::Isolate* isolate = info.GetIsolate();
376 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
377 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
378 CJS_Parameters parameters;
379 for (unsigned int i = 0; i<(unsigned int)info.Length(); i++) {
380 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
382 CJS_Value valueRes(isolate);
383 CFX_WideString sError;
384 if (!(*F)(pRuntimeContext, parameters, valueRes, sError)) {
385 JS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError));
388 info.GetReturnValue().Set(valueRes.ToJSValue());
391 #define JS_STATIC_GLOBAL_FUN(fun_name) \
392 static void fun_name##_static(const v8::FunctionCallbackInfo<v8::Value>& info) { \
393 JSGlobalFunc<fun_name>(#fun_name, info); \
396 #define JS_STATIC_DECLARE_GLOBAL_FUN() \
397 static JSMethodSpec global_methods[]; \
398 static int Init(IJS_Runtime* pRuntime)
400 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \
401 JSMethodSpec js_class_name::global_methods[] = {
403 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name,nargs) JS_STATIC_METHOD_ENTRY(method_name,nargs)
405 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD()
407 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \
408 int js_class_name::Init(IJS_Runtime* pRuntime)\
410 for (int i=0, sz=sizeof(js_class_name::global_methods)/sizeof(JSMethodSpec)-1; i<sz; i++)\
412 if (JS_DefineGlobalMethod(pRuntime,\
413 js_class_name::global_methods[i].pName,\
414 js_class_name::global_methods[i].pMethodCall,\
415 js_class_name::global_methods[i].nParamNum\
422 /* ======================================== GLOBAL CONSTS ============================================ */
423 #define DEFINE_GLOBAL_CONST(pRuntime, const_name , const_value)\
424 if (JS_DefineGlobalConst(pRuntime,JS_WIDESTRING(const_name),JS_NewString(pRuntime,JS_WIDESTRING(const_value)))) return -1
426 /* ======================================== GLOBAL ARRAYS ============================================ */
428 #define DEFINE_GLOBAL_ARRAY(pRuntime)\
429 int size = FX_ArraySize(ArrayContent);\
431 CJS_Array array(pRuntime);\
432 for (int i=0; i<size; i++) array.SetElement(i,CJS_Value(pRuntime,ArrayContent[i]));\
434 CJS_PropValue prop(pRuntime);\
436 if (JS_DefineGlobalConst(pRuntime, (const wchar_t*)ArrayName, prop.ToJSValue()) < 0)\
439 /* ============================================================ */
441 #define VALUE_NAME_STRING L"string"
442 #define VALUE_NAME_NUMBER L"number"
443 #define VALUE_NAME_BOOLEAN L"boolean"
444 #define VALUE_NAME_DATE L"date"
445 #define VALUE_NAME_OBJECT L"object"
446 #define VALUE_NAME_FXOBJ L"fxobj"
447 #define VALUE_NAME_NULL L"null"
448 #define VALUE_NAME_UNDEFINED L"undefined"
450 FXJSVALUETYPE GET_VALUE_TYPE(v8::Handle<v8::Value> p);
452 #endif //_JS_DEFINE_H_