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 typedef v8::Value JSValue;
14 typedef v8::Handle<v8::Object> JSObject;
15 typedef v8::Handle<v8::Object> JSFXObject;
17 #include "JS_Object.h"
24 const wchar_t* string;
25 FX_BYTE t; //0:double 1:str
31 v8::AccessorGetterCallback pPropGet;
32 v8::AccessorSetterCallback pPropPut;
38 v8::FunctionCallback pMethodCall;
42 /* ====================================== PUBLIC DEFINE SPEC ============================================== */
43 #define JS_WIDESTRING(widestring) L###widestring
45 #define BEGIN_JS_STATIC_CONST(js_class_name) JSConstSpec js_class_name::JS_Class_Consts[] = {
46 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) {JS_WIDESTRING(const_name), pValue, L"", 0},
47 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) {JS_WIDESTRING(const_name), 0, JS_WIDESTRING(pValue), 1},
48 #define END_JS_STATIC_CONST() {0, 0, 0, 0}};
50 #define BEGIN_JS_STATIC_PROP(js_class_name) JSPropertySpec js_class_name::JS_Class_Properties[] = {
51 #define JS_STATIC_PROP_ENTRY(prop_name) {JS_WIDESTRING(prop_name), get_##prop_name##_static, set_##prop_name##_static},
52 #define END_JS_STATIC_PROP() {0, 0, 0}};
54 #define BEGIN_JS_STATIC_METHOD(js_class_name) JSMethodSpec js_class_name::JS_Class_Methods[] = {
55 #define JS_STATIC_METHOD_ENTRY(method_name, nargs) {JS_WIDESTRING(method_name), method_name##_static, nargs},
56 #define END_JS_STATIC_METHOD() {0, 0, 0}};
58 /* ======================================== PROP CALLBACK ============================================ */
60 template <class C, FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)>
61 void JSPropGetter(const char* prop_name_string,
62 const char* class_name_string,
63 v8::Local<v8::String> property,
64 const v8::PropertyCallbackInfo<v8::Value>& info) {
65 v8::Isolate* isolate = info.GetIsolate();
66 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
67 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
68 CJS_PropValue value(isolate);
70 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());
71 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
72 CFX_WideString sError;
73 if (!(pObj->*M)(pRuntimeContext, value, sError)) {
74 JS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, sError));
77 info.GetReturnValue().Set((v8::Handle<v8::Value>)value);
80 template <class C, FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)>
81 void JSPropSetter(const char* prop_name_string,
82 const char* class_name_string,
83 v8::Local<v8::String> property,
84 v8::Local<v8::Value> value,
85 const v8::PropertyCallbackInfo<void>& info) {
86 v8::Isolate* isolate = info.GetIsolate();
87 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
88 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
89 CJS_PropValue propValue(CJS_Value(isolate, value, VT_unknown));
90 propValue.StartSetting();
91 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());
92 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
93 CFX_WideString sError;
94 if (!(pObj->*M)(pRuntimeContext, propValue, sError)) {
95 JS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, sError));
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); \
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); \
114 /* ========================================= METHOD CALLBACK =========================================== */
116 template <class C, FX_BOOL (C::*M)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)>
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 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
122 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
123 CJS_Parameters parameters;
124 for (unsigned int i = 0; i<(unsigned int)info.Length(); i++) {
125 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
127 CJS_Value valueRes(isolate);
128 CJS_Object* pJSObj = (CJS_Object *)JS_GetPrivate(isolate,info.Holder());
129 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
130 CFX_WideString sError;
131 if (!(pObj->*M)(pRuntimeContext, parameters, valueRes, sError)) {
132 JS_Error(isolate, JSFormatErrorString(class_name_string, method_name_string, sError));
135 info.GetReturnValue().Set(valueRes.ToJSValue());
138 #define JS_STATIC_METHOD(method_name, class_name) \
139 static void method_name##_static( \
140 const v8::FunctionCallbackInfo<v8::Value>& info) { \
141 JSMethod<class_name, &class_name::method_name>( \
142 #method_name, #class_name, info); \
145 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \
146 static void method_name##_static( \
147 const v8::FunctionCallbackInfo<v8::Value>& info) { \
148 JSMethod<class_alternate, &class_alternate::method_name>( \
149 #method_name, #class_name, info); \
152 /* ===================================== JS CLASS =============================================== */
154 #define DECLARE_JS_CLASS(js_class_name) \
155 static void JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject global);\
156 static void JSDestructor(JSFXObject obj);\
157 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
158 static JSConstSpec JS_Class_Consts[];\
159 static JSPropertySpec JS_Class_Properties[];\
160 static JSMethodSpec JS_Class_Methods[];\
161 static const wchar_t* m_pClassName
163 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \
164 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
165 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject global)\
167 CJS_Object* pObj = FX_NEW js_class_name(obj);\
168 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\
169 JS_SetPrivate(NULL,obj,(void*)pObj); \
170 pObj->InitInstance(cc);\
173 void js_class_name::JSDestructor(JSFXObject obj) \
175 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\
176 ASSERT(pObj != NULL);\
177 pObj->ExitInstance();\
181 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
183 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, JSConstructor, JSDestructor, 0);\
184 if (nObjDefnID >= 0)\
186 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertySpec)-1; j<szj; j++)\
188 if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_Properties[j].pName, JS_Class_Properties[j].pPropGet, JS_Class_Properties[j].pPropPut) < 0) return -1;\
190 for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-1; k<szk; k++)\
192 if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Methods[k].pName, JS_Class_Methods[k].pMethodCall, JS_Class_Methods[k].nParamNum) < 0) return -1;\
199 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name)
201 /* ======================================== CONST CLASS ============================================ */
203 #define DECLARE_JS_CLASS_CONST() \
204 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
205 static JSConstSpec JS_Class_Consts[];\
206 static const wchar_t* m_pClassName
208 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \
209 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
210 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
212 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, NULL, NULL, 0);\
215 for (int i=0, sz=sizeof(JS_Class_Consts)/sizeof(JSConstSpec)-1; i<sz; i++)\
217 if (JS_Class_Consts[i].t == 0)\
219 if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_Class_Consts[i].pName, JS_NewNumber(pRuntime,JS_Class_Consts[i].number)) < 0) return -1;\
223 if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_Class_Consts[i].pName, JS_NewString(pRuntime,JS_Class_Consts[i].string)) < 0) return -1;\
231 /* ===================================== SPECIAL JS CLASS =============================================== */
234 void JSSpecialPropQuery(const char *, v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info) {
235 v8::Isolate* isolate = info.GetIsolate();
236 v8::String::Utf8Value utf8_value(property);
237 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
238 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
239 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
240 FX_BOOL bRet = pObj->QueryProperty(propname.c_str());
241 info.GetReturnValue().Set(bRet ? 4 : 0);
245 void JSSpecialPropGet(const char* class_name,
246 v8::Local<v8::String> property,
247 const v8::PropertyCallbackInfo<v8::Value>& info) {
248 v8::Isolate* isolate = info.GetIsolate();
249 v8::Local<v8::Context> context = isolate->GetCurrentContext();
250 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
251 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
252 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
253 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
254 v8::String::Utf8Value utf8_value(property);
255 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
256 CFX_WideString sError;
257 CJS_PropValue value(isolate);
258 value.StartGetting();
259 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) {
260 JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError));
263 info.GetReturnValue().Set((v8::Handle<v8::Value>)value);
267 void JSSpecialPropPut(const char* class_name,
268 v8::Local<v8::String> property,
269 v8::Local<v8::Value> value,
270 const v8::PropertyCallbackInfo<v8::Value>& info) {
271 v8::Isolate* isolate = info.GetIsolate();
272 v8::Local<v8::Context> context = isolate->GetCurrentContext();
273 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
274 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
275 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
276 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
277 v8::String::Utf8Value utf8_value(property);
278 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
279 CFX_WideString sError;
280 CJS_PropValue PropValue(CJS_Value(isolate,value,VT_unknown));
281 PropValue.StartSetting();
282 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) {
283 JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError));
288 void JSSpecialPropDel(const char* class_name,
289 v8::Local<v8::String> property,
290 const v8::PropertyCallbackInfo<v8::Boolean>& info) {
291 v8::Isolate* isolate = info.GetIsolate();
292 v8::Local<v8::Context> context = isolate->GetCurrentContext();
293 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
294 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
295 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder()));
296 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject());
297 v8::String::Utf8Value utf8_value(property);
298 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.length());
299 CFX_WideString sError;
300 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) {
301 CFX_ByteString cbName;
302 cbName.Format("%s.%s", class_name, "DelProperty");
303 // Probably a missing call to JS_Error().
307 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \
308 static void JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject global);\
309 static void JSDestructor(JSFXObject obj);\
310 static JSConstSpec JS_Class_Consts[];\
311 static JSPropertySpec JS_Class_Properties[];\
312 static JSMethodSpec JS_Class_Methods[];\
313 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\
314 static const wchar_t* m_pClassName;\
315 static void queryprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info);\
316 static void getprop_##js_class_name##_static(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);\
317 static void putprop_##js_class_name##_static(v8::Local<v8::String> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info);\
318 static void delprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Boolean>& info)
320 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \
321 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\
322 void js_class_name::queryprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Integer>& info) { \
323 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \
325 void js_class_name::getprop_##js_class_name##_static(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) { \
326 JSSpecialPropGet<class_alternate>(#class_name, property, info); \
328 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) {\
329 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \
331 void js_class_name::delprop_##js_class_name##_static(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Boolean>& info) { \
332 JSSpecialPropDel<class_alternate>(#class_name, property, info); \
334 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject global)\
336 CJS_Object* pObj = FX_NEW js_class_name(obj);\
337 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\
338 JS_SetPrivate(NULL,obj, (void*)pObj); \
339 pObj->InitInstance(cc);\
342 void js_class_name::JSDestructor(JSFXObject obj) \
344 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\
345 ASSERT(pObj != NULL);\
346 pObj->ExitInstance();\
350 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\
353 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eObjType, JSConstructor, JSDestructor, 0);\
355 if (nObjDefnID >= 0)\
357 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertySpec)-1; j<szj; j++)\
359 if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_Properties[j].pName, JS_Class_Properties[j].pPropGet,JS_Class_Properties[j].pPropPut)<0)return -1;\
362 for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-1; k<szk; k++)\
364 if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Methods[k].pName,JS_Class_Methods[k].pMethodCall,JS_Class_Methods[k].nParamNum)<0)return -1;\
366 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;\
374 /* ======================================== GLOBAL METHODS ============================================ */
376 template <FX_BOOL (*F)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)>
377 void JSGlobalFunc(const char *func_name_string,
378 const v8::FunctionCallbackInfo<v8::Value>& info) {
379 v8::Isolate* isolate = info.GetIsolate();
380 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)isolate->GetData(2);
381 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
382 CJS_Parameters parameters;
383 for (unsigned int i = 0; i<(unsigned int)info.Length(); i++) {
384 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown));
386 CJS_Value valueRes(isolate);
387 CFX_WideString sError;
388 if (!(*F)(pRuntimeContext, parameters, valueRes, sError)) {
389 JS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError));
392 info.GetReturnValue().Set(valueRes.ToJSValue());
395 #define JS_STATIC_GLOBAL_FUN(fun_name) \
396 static void fun_name##_static(const v8::FunctionCallbackInfo<v8::Value>& info) { \
397 JSGlobalFunc<fun_name>(#fun_name, info); \
400 #define JS_STATIC_DECLARE_GLOBAL_FUN() \
401 static JSMethodSpec global_methods[]; \
402 static int Init(IJS_Runtime* pRuntime)
404 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \
405 JSMethodSpec js_class_name::global_methods[] = {
407 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name,nargs) JS_STATIC_METHOD_ENTRY(method_name,nargs)
409 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD()
411 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \
412 int js_class_name::Init(IJS_Runtime* pRuntime)\
414 for (int i=0, sz=sizeof(js_class_name::global_methods)/sizeof(JSMethodSpec)-1; i<sz; i++)\
416 if (JS_DefineGlobalMethod(pRuntime,\
417 js_class_name::global_methods[i].pName,\
418 js_class_name::global_methods[i].pMethodCall,\
419 js_class_name::global_methods[i].nParamNum\
426 /* ======================================== GLOBAL CONSTS ============================================ */
427 #define DEFINE_GLOBAL_CONST(pRuntime, const_name , const_value)\
428 if (JS_DefineGlobalConst(pRuntime,JS_WIDESTRING(const_name),JS_NewString(pRuntime,JS_WIDESTRING(const_value)))) return -1
430 /* ======================================== GLOBAL ARRAYS ============================================ */
432 #define DEFINE_GLOBAL_ARRAY(pRuntime)\
433 int size = FX_ArraySize(ArrayContent);\
435 CJS_Array array(pRuntime);\
436 for (int i=0; i<size; i++) array.SetElement(i,CJS_Value(pRuntime,ArrayContent[i]));\
438 CJS_PropValue prop(pRuntime);\
440 if (JS_DefineGlobalConst(pRuntime, (const wchar_t*)ArrayName, prop.ToJSValue()) < 0)\
443 /* ============================================================ */
445 #define VALUE_NAME_STRING L"string"
446 #define VALUE_NAME_NUMBER L"number"
447 #define VALUE_NAME_BOOLEAN L"boolean"
448 #define VALUE_NAME_DATE L"date"
449 #define VALUE_NAME_OBJECT L"object"
450 #define VALUE_NAME_FXOBJ L"fxobj"
451 #define VALUE_NAME_NULL L"null"
452 #define VALUE_NAME_UNDEFINED L"undefined"
454 FXJSVALUETYPE GET_VALUE_TYPE(v8::Handle<v8::Value> p);
456 #endif //_JS_DEFINE_H_