OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef _JS_DEFINE_H_ | 7 #ifndef _JS_DEFINE_H_ |
8 #define _JS_DEFINE_H_ | 8 #define _JS_DEFINE_H_ |
9 | 9 |
10 typedef v8::Value JSValue; | 10 typedef v8::Value JSValue; |
(...skipping 24 matching lines...) Expand all Loading... |
35 const wchar_t* pName; | 35 const wchar_t* pName; |
36 v8::FunctionCallback pMethodCall; | 36 v8::FunctionCallback pMethodCall; |
37 unsigned nParamNum; | 37 unsigned nParamNum; |
38 }; | 38 }; |
39 | 39 |
40 typedef CFX_WideString JS_ErrorString; | 40 typedef CFX_WideString JS_ErrorString; |
41 | 41 |
42 #define JS_TRUE (unsigned)1 | 42 #define JS_TRUE (unsigned)1 |
43 #define JS_FALSE (unsigned)0 | 43 #define JS_FALSE (unsigned)0 |
44 | 44 |
45 | 45 typedef CFX_ArrayTemplate<float> CJS_PointsArray; |
46 #define CJS_PointsArray»» CFX_ArrayTemplate<float> | 46 typedef CFX_ArrayTemplate<int> CJS_IntArray; |
47 #define CJS_IntArray» » CFX_ArrayTemplate<int> | |
48 | 47 |
49 /* ====================================== PUBLIC DEFINE SPEC ===================
=========================== */ | 48 /* ====================================== PUBLIC DEFINE SPEC ===================
=========================== */ |
50 #define JS_WIDESTRING(widestring) L###widestring | 49 #define JS_WIDESTRING(widestring) L###widestring |
51 | 50 |
52 #define BEGIN_JS_STATIC_CONST(js_class_name) JSConstSpec js_class_name::JS_Class
_Consts[] = { | 51 #define BEGIN_JS_STATIC_CONST(js_class_name) JSConstSpec js_class_name::JS_Class
_Consts[] = { |
53 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) {JS_WIDESTRING(const_na
me), pValue, L"", 0}, | 52 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) {JS_WIDESTRING(const_na
me), pValue, L"", 0}, |
54 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) {JS_WIDESTRING(const_na
me), 0, JS_WIDESTRING(pValue), 1}, | 53 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) {JS_WIDESTRING(const_na
me), 0, JS_WIDESTRING(pValue), 1}, |
55 #define END_JS_STATIC_CONST() {0, 0, 0, 0}}; | 54 #define END_JS_STATIC_CONST() {0, 0, 0, 0}}; |
56 | 55 |
57 #define BEGIN_JS_STATIC_PROP(js_class_name) JSPropertySpec js_class_name::JS_Cla
ss_Properties[] = { | 56 #define BEGIN_JS_STATIC_PROP(js_class_name) JSPropertySpec js_class_name::JS_Cla
ss_Properties[] = { |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 return -1;\ | 270 return -1;\ |
272 }\ | 271 }\ |
273 void js_class_name::GetConsts(JSConstSpec*& pConsts, int& nSize)\ | 272 void js_class_name::GetConsts(JSConstSpec*& pConsts, int& nSize)\ |
274 {\ | 273 {\ |
275 pConsts = JS_Class_Consts;\ | 274 pConsts = JS_Class_Consts;\ |
276 nSize = sizeof(JS_Class_Consts)/sizeof(JSConstSpec)-1;\ | 275 nSize = sizeof(JS_Class_Consts)/sizeof(JSConstSpec)-1;\ |
277 } | 276 } |
278 | 277 |
279 /* ===================================== SPECIAL JS CLASS ======================
========================= */ | 278 /* ===================================== SPECIAL JS CLASS ======================
========================= */ |
280 | 279 |
| 280 template <class Alt> |
| 281 void JSSpecialPropQuery(const char *, v8::Local<v8::String> property,const v8::P
ropertyCallbackInfo<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.len
gth()); |
| 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.len
gth()); |
| 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.len
gth()); |
| 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.len
gth()); |
| 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 |
281 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ | 364 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ |
282 static JSBool JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObjec
t global);\ | 365 static JSBool JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObjec
t global);\ |
283 static JSBool JSDestructor(JSFXObject obj);\ | 366 static JSBool JSDestructor(JSFXObject obj);\ |
284 static void GetConsts(JSConstSpec*& pConsts, int& nSize);\ | 367 static void GetConsts(JSConstSpec*& pConsts, int& nSize);\ |
285 static void GetProperties(JSPropertySpec*& pProperties, int& nSize);\ | 368 static void GetProperties(JSPropertySpec*& pProperties, int& nSize);\ |
286 static void GetMethods(JSMethodSpec*& pMethods, int& nSize);\ | 369 static void GetMethods(JSMethodSpec*& pMethods, int& nSize);\ |
287 static JSConstSpec JS_Class_Consts[];\ | 370 static JSConstSpec JS_Class_Consts[];\ |
288 static JSPropertySpec JS_Class_Properties[];\ | 371 static JSPropertySpec JS_Class_Properties[];\ |
289 static JSMethodSpec JS_Class_Methods[];\ | 372 static JSMethodSpec JS_Class_Methods[];\ |
290 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ | 373 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ |
291 static const wchar_t* m_pClassName;\ | 374 static const wchar_t* m_pClassName;\ |
292 » static void queryprop_##js_class_name##_static(JS_PROPQUERY_ARGS);\ | 375 » static void queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info);\ |
293 » static void getprop_##js_class_name##_static(JS_NAMED_PROPGET_ARGS);\ | 376 » static void getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info);\ |
294 » static void putprop_##js_class_name##_static(JS_NAMED_PROPPUT_ARGS);\ | 377 » static void putprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info);
\ |
295 » static void delprop_##js_class_name##_static(JS_PROPDEL_ARGS) | 378 » static void delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) |
296 | 379 |
297 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ | 380 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ |
298 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ | 381 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ |
299 » void js_class_name::queryprop_##js_class_name##_static(JS_PROPQUERY_ARGS
)\ | 382 void js_class_name::queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info) { \ |
300 {\ | 383 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ |
301 » v8::Isolate* isolate = info.GetIsolate();\ | |
302 » v8::String::Utf8Value utf8_value(property);\ | |
303 » CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_val
ue.length());\ | |
304 » CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());\ | |
305 » ASSERT(pJSObj != NULL);\ | |
306 » class_alternate* pObj = (class_alternate*)pJSObj->GetEmbedObject();\ | |
307 » ASSERT(pObj != NULL);\ | |
308 » FX_BOOL bRet = FALSE;\ | |
309 » bRet = pObj->QueryProperty(propname.c_str());\ | |
310 » if (bRet)\ | |
311 » {\ | |
312 » » info.GetReturnValue().Set(0x004);\ | |
313 » » return ;\ | |
314 » }\ | |
315 » else\ | |
316 » {\ | |
317 » » info.GetReturnValue().Set(0);\ | |
318 » » return ;\ | |
319 » }\ | |
320 » return ;\ | |
321 }\ | 384 }\ |
322 » void js_class_name::getprop_##js_class_name##_static(JS_NAMED_PROPGET_AR
GS)\ | 385 void js_class_name::getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
323 {\ | 386 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ |
324 » v8::Isolate* isolate = info.GetIsolate();\ | 387 } \ |
325 » v8::Local<v8::Context> context = isolate->GetCurrentContext();\ | 388 void js_class_name::putprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info)
{\ |
326 » v8::Local<v8::Value> v = context->GetEmbedderData(1);\ | 389 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ |
327 » ASSERT(!v.IsEmpty());\ | |
328 » if(v.IsEmpty()) return;\ | |
329 » v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(v);\ | |
330 » IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();\ | |
331 » IFXJS_Context* cc = pRuntime->GetCurrentContext();\ | |
332 » v8::String::Utf8Value utf8_value(property);\ | |
333 » CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_val
ue.length());\ | |
334 » CJS_PropValue value(isolate);\ | |
335 » value.StartGetting();\ | |
336 » CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());\ | |
337 » ASSERT(pJSObj != NULL);\ | |
338 » class_alternate* pObj = (class_alternate*)pJSObj->GetEmbedObject();\ | |
339 » ASSERT(pObj != NULL);\ | |
340 » JS_ErrorString sError;\ | |
341 » FX_BOOL bRet = FALSE;\ | |
342 » bRet = pObj->DoProperty(cc, propname.c_str(), value, sError);\ | |
343 » if (bRet)\ | |
344 » {\ | |
345 » » info.GetReturnValue().Set((v8::Handle<v8::Value>)value);\ | |
346 » » return ;\ | |
347 » }\ | |
348 » else\ | |
349 » {\ | |
350 » » CFX_ByteString cbName;\ | |
351 » » cbName.Format("%s.%s", #class_name, L"GetProperty");\ | |
352 » » JS_Error(NULL,CFX_WideString::FromLocal(cbName), sError);\ | |
353 » » return ;\ | |
354 » }\ | |
355 » JS_Error(NULL,L"GetProperty", L"Embeded object not found!");\ | |
356 » return ;\ | |
357 }\ | 390 }\ |
358 » void js_class_name::putprop_##js_class_name##_static(JS_NAMED_PROPPUT_AR
GS)\ | 391 void js_class_name::delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ |
359 {\ | 392 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ |
360 » v8::Isolate* isolate = info.GetIsolate();\ | 393 } \ |
361 » v8::Local<v8::Context> context = isolate->GetCurrentContext();\ | |
362 » v8::Local<v8::Value> v = context->GetEmbedderData(1);\ | |
363 » ASSERT(!v.IsEmpty());\ | |
364 » if(v.IsEmpty()) return;\ | |
365 » v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(v);\ | |
366 » IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();\ | |
367 » IFXJS_Context* cc = pRuntime->GetCurrentContext();\ | |
368 » v8::String::Utf8Value utf8_value(property);\ | |
369 » CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_val
ue.length());\ | |
370 » CJS_PropValue PropValue(CJS_Value(isolate,value,VT_unknown));\ | |
371 » PropValue.StartSetting();\ | |
372 » CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());\ | |
373 » if(!pJSObj) return;\ | |
374 » class_alternate* pObj = (class_alternate*)pJSObj->GetEmbedObject();\ | |
375 » ASSERT(pObj != NULL);\ | |
376 » JS_ErrorString sError;\ | |
377 » FX_BOOL bRet = FALSE;\ | |
378 » bRet = pObj->DoProperty(cc, propname.c_str(), PropValue, sError);\ | |
379 » if (bRet)\ | |
380 » {\ | |
381 » » return ;\ | |
382 » }\ | |
383 » else\ | |
384 » {\ | |
385 » » CFX_ByteString cbName;\ | |
386 » » cbName.Format("%s.%s", #class_name, "PutProperty");\ | |
387 » » JS_Error(NULL,CFX_WideString::FromLocal(cbName), sError);\ | |
388 » » return ;\ | |
389 » }\ | |
390 » JS_Error(NULL,L"PutProperty", L"Embeded object not found!");\ | |
391 » return ;\ | |
392 }\ | |
393 » void js_class_name::delprop_##js_class_name##_static(JS_PROPDEL_ARGS)\ | |
394 {\ | |
395 » v8::Isolate* isolate = info.GetIsolate();\ | |
396 » v8::Local<v8::Context> context = isolate->GetCurrentContext();\ | |
397 » v8::Local<v8::Value> v = context->GetEmbedderData(1);\ | |
398 » ASSERT(!v.IsEmpty());\ | |
399 » if(v.IsEmpty()) return;\ | |
400 » v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(v);\ | |
401 » IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();\ | |
402 » IFXJS_Context* cc = pRuntime->GetCurrentContext();\ | |
403 » v8::String::Utf8Value utf8_value(property);\ | |
404 » CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_val
ue.length());\ | |
405 » CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder());\ | |
406 » ASSERT(pJSObj != NULL);\ | |
407 » class_alternate* pObj = (class_alternate*)pJSObj->GetEmbedObject();\ | |
408 » ASSERT(pObj != NULL);\ | |
409 » JS_ErrorString sError;\ | |
410 » FX_BOOL bRet = FALSE;\ | |
411 » bRet = pObj->DelProperty(cc, propname.c_str(), sError);\ | |
412 » if (bRet)\ | |
413 » {\ | |
414 » » return ;\ | |
415 » }\ | |
416 » else\ | |
417 » {\ | |
418 » » CFX_ByteString cbName;\ | |
419 » » cbName.Format("%s.%s", #class_name, "DelProperty");\ | |
420 » » return ;\ | |
421 » }\ | |
422 » return ;\ | |
423 }\ | |
424 JSBool js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObjec
t global)\ | 394 JSBool js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObjec
t global)\ |
425 {\ | 395 {\ |
426 CJS_Object* pObj = FX_NEW js_class_name(obj);\ | 396 CJS_Object* pObj = FX_NEW js_class_name(obj);\ |
427 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\ | 397 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\ |
428 JS_SetPrivate(NULL,obj, (void*)pObj); \ | 398 JS_SetPrivate(NULL,obj, (void*)pObj); \ |
429 pObj->InitInstance(cc);\ | 399 pObj->InitInstance(cc);\ |
430 return JS_TRUE;\ | 400 return JS_TRUE;\ |
431 }\ | 401 }\ |
432 \ | 402 \ |
433 JSBool js_class_name::JSDestructor(JSFXObject obj) \ | 403 JSBool js_class_name::JSDestructor(JSFXObject obj) \ |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 #define VALUE_NAME_BOOLEAN L"boolean" | 528 #define VALUE_NAME_BOOLEAN L"boolean" |
559 #define VALUE_NAME_DATE L"date" | 529 #define VALUE_NAME_DATE L"date" |
560 #define VALUE_NAME_OBJECT L"object" | 530 #define VALUE_NAME_OBJECT L"object" |
561 #define VALUE_NAME_FXOBJ L"fxobj" | 531 #define VALUE_NAME_FXOBJ L"fxobj" |
562 #define VALUE_NAME_NULL L"null" | 532 #define VALUE_NAME_NULL L"null" |
563 #define VALUE_NAME_UNDEFINED L"undefined" | 533 #define VALUE_NAME_UNDEFINED L"undefined" |
564 | 534 |
565 FXJSVALUETYPE GET_VALUE_TYPE(v8::Handle<v8::Value> p); | 535 FXJSVALUETYPE GET_VALUE_TYPE(v8::Handle<v8::Value> p); |
566 | 536 |
567 #endif //_JS_DEFINE_H_ | 537 #endif //_JS_DEFINE_H_ |
OLD | NEW |