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 #include "../jsapi/fxjs_v8.h" | 10 #include "../jsapi/fxjs_v8.h" |
11 #include "resource.h" | 11 #include "resource.h" |
12 | 12 |
13 typedef v8::Value JSValue; | 13 typedef v8::Value JSValue; |
14 typedef v8::Handle<v8::Object> JSObject; | 14 typedef v8::Handle<v8::Object> JSObject; |
15 typedef v8::Handle<v8::Object> JSFXObject; | 15 typedef v8::Handle<v8::Object> JSFXObject; |
16 typedef unsigned JSBool; | |
17 | 16 |
18 #include "JS_Object.h" | 17 #include "JS_Object.h" |
19 #include "JS_Value.h" | 18 #include "JS_Value.h" |
20 | 19 |
21 struct JSConstSpec | 20 struct JSConstSpec |
22 { | 21 { |
23 const wchar_t* pName; | 22 const wchar_t* pName; |
24 double number; | 23 double number; |
25 const wchar_t* string; | 24 const wchar_t* string; |
26 FX_BYTE t; //0:double 1:str | 25 FX_BYTE t; //0:double 1:str |
27 }; | 26 }; |
28 | 27 |
29 struct JSPropertySpec | 28 struct JSPropertySpec |
30 { | 29 { |
31 const wchar_t* pName; | 30 const wchar_t* pName; |
32 v8::AccessorGetterCallback pPropGet; | 31 v8::AccessorGetterCallback pPropGet; |
33 v8::AccessorSetterCallback pPropPut; | 32 v8::AccessorSetterCallback pPropPut; |
34 }; | 33 }; |
35 | 34 |
36 struct JSMethodSpec | 35 struct JSMethodSpec |
37 { | 36 { |
38 const wchar_t* pName; | 37 const wchar_t* pName; |
39 v8::FunctionCallback pMethodCall; | 38 v8::FunctionCallback pMethodCall; |
40 unsigned nParamNum; | 39 unsigned nParamNum; |
41 }; | 40 }; |
42 | 41 |
43 #define JS_TRUE (unsigned)1 | |
44 #define JS_FALSE (unsigned)0 | |
45 | |
46 typedef CFX_WideString JS_ErrorString; | |
47 typedef CFX_ArrayTemplate<float> CJS_PointsArray; | |
48 typedef CFX_ArrayTemplate<int> CJS_IntArray; | |
49 | |
50 /* ====================================== PUBLIC DEFINE SPEC ===================
=========================== */ | 42 /* ====================================== PUBLIC DEFINE SPEC ===================
=========================== */ |
51 #define JS_WIDESTRING(widestring) L###widestring | 43 #define JS_WIDESTRING(widestring) L###widestring |
52 | 44 |
53 #define BEGIN_JS_STATIC_CONST(js_class_name) JSConstSpec js_class_name::JS_Class
_Consts[] = { | 45 #define BEGIN_JS_STATIC_CONST(js_class_name) JSConstSpec js_class_name::JS_Class
_Consts[] = { |
54 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) {JS_WIDESTRING(const_na
me), pValue, L"", 0}, | 46 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) {JS_WIDESTRING(const_na
me), pValue, L"", 0}, |
55 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) {JS_WIDESTRING(const_na
me), 0, JS_WIDESTRING(pValue), 1}, | 47 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) {JS_WIDESTRING(const_na
me), 0, JS_WIDESTRING(pValue), 1}, |
56 #define END_JS_STATIC_CONST() {0, 0, 0, 0}}; | 48 #define END_JS_STATIC_CONST() {0, 0, 0, 0}}; |
57 | 49 |
58 #define BEGIN_JS_STATIC_PROP(js_class_name) JSPropertySpec js_class_name::JS_Cla
ss_Properties[] = { | 50 #define BEGIN_JS_STATIC_PROP(js_class_name) JSPropertySpec js_class_name::JS_Cla
ss_Properties[] = { |
59 #define JS_STATIC_PROP_ENTRY(prop_name) {JS_WIDESTRING(prop_name), get_##prop_na
me##_static, set_##prop_name##_static}, | 51 #define JS_STATIC_PROP_ENTRY(prop_name) {JS_WIDESTRING(prop_name), get_##prop_na
me##_static, set_##prop_name##_static}, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ | 154 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ |
163 static void method_name##_static( \ | 155 static void method_name##_static( \ |
164 const v8::FunctionCallbackInfo<v8::Value>& info) { \ | 156 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
165 JSMethod<class_alternate, &class_alternate::method_name>( \ | 157 JSMethod<class_alternate, &class_alternate::method_name>( \ |
166 #method_name, #class_name, info); \ | 158 #method_name, #class_name, info); \ |
167 } | 159 } |
168 | 160 |
169 /* ===================================== JS CLASS ==============================
================= */ | 161 /* ===================================== JS CLASS ==============================
================= */ |
170 | 162 |
171 #define DECLARE_JS_CLASS(js_class_name) \ | 163 #define DECLARE_JS_CLASS(js_class_name) \ |
172 » static JSBool JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject
global);\ | 164 » static void JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject g
lobal);\ |
173 » static JSBool JSDestructor(JSFXObject obj);\ | 165 » static void JSDestructor(JSFXObject obj);\ |
174 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ | 166 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ |
175 static JSConstSpec JS_Class_Consts[];\ | 167 static JSConstSpec JS_Class_Consts[];\ |
176 static JSPropertySpec JS_Class_Properties[];\ | 168 static JSPropertySpec JS_Class_Properties[];\ |
177 static JSMethodSpec JS_Class_Methods[];\ | 169 static JSMethodSpec JS_Class_Methods[];\ |
178 static const wchar_t* m_pClassName | 170 static const wchar_t* m_pClassName |
179 | 171 |
180 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ | 172 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ |
181 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ | 173 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ |
182 JSBool js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObjec
t global)\ | 174 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject
global)\ |
183 {\ | 175 {\ |
184 CJS_Object* pObj = FX_NEW js_class_name(obj);\ | 176 CJS_Object* pObj = FX_NEW js_class_name(obj);\ |
185 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\ | 177 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\ |
186 JS_SetPrivate(NULL,obj,(void*)pObj); \ | 178 JS_SetPrivate(NULL,obj,(void*)pObj); \ |
187 pObj->InitInstance(cc);\ | 179 pObj->InitInstance(cc);\ |
188 return JS_TRUE;\ | |
189 }\ | 180 }\ |
190 \ | 181 \ |
191 JSBool js_class_name::JSDestructor(JSFXObject obj) \ | 182 void js_class_name::JSDestructor(JSFXObject obj) \ |
192 {\ | 183 {\ |
193 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\ | 184 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\ |
194 ASSERT(pObj != NULL);\ | 185 ASSERT(pObj != NULL);\ |
195 pObj->ExitInstance();\ | 186 pObj->ExitInstance();\ |
196 delete pObj;\ | 187 delete pObj;\ |
197 return JS_TRUE;\ | |
198 }\ | 188 }\ |
199 \ | 189 \ |
200 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\ | 190 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\ |
201 {\ | 191 {\ |
202 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eOb
jType, JSConstructor, JSDestructor, 0);\ | 192 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eOb
jType, JSConstructor, JSDestructor, 0);\ |
203 if (nObjDefnID >= 0)\ | 193 if (nObjDefnID >= 0)\ |
204 {\ | 194 {\ |
205 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertyS
pec)-1; j<szj; j++)\ | 195 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertyS
pec)-1; j<szj; j++)\ |
206 {\ | 196 {\ |
207 if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_
Properties[j].pName, JS_Class_Properties[j].pPropGet, JS_Class_Properties[j].pPr
opPut) < 0) return -1;\ | 197 if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_
Properties[j].pName, JS_Class_Properties[j].pPropGet, JS_Class_Properties[j].pPr
opPut) < 0) return -1;\ |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.len
gth()); | 313 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.len
gth()); |
324 CFX_WideString sError; | 314 CFX_WideString sError; |
325 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { | 315 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { |
326 CFX_ByteString cbName; | 316 CFX_ByteString cbName; |
327 cbName.Format("%s.%s", class_name, "DelProperty"); | 317 cbName.Format("%s.%s", class_name, "DelProperty"); |
328 // Probably a missing call to JS_Error(). | 318 // Probably a missing call to JS_Error(). |
329 } | 319 } |
330 } | 320 } |
331 | 321 |
332 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ | 322 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ |
333 » static JSBool JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObjec
t global);\ | 323 » static void JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject
global);\ |
334 » static JSBool JSDestructor(JSFXObject obj);\ | 324 » static void JSDestructor(JSFXObject obj);\ |
335 static JSConstSpec JS_Class_Consts[];\ | 325 static JSConstSpec JS_Class_Consts[];\ |
336 static JSPropertySpec JS_Class_Properties[];\ | 326 static JSPropertySpec JS_Class_Properties[];\ |
337 static JSMethodSpec JS_Class_Methods[];\ | 327 static JSMethodSpec JS_Class_Methods[];\ |
338 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ | 328 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ |
339 static const wchar_t* m_pClassName;\ | 329 static const wchar_t* m_pClassName;\ |
340 static void queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info);\ | 330 static void queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info);\ |
341 static void getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info);\ | 331 static void getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info);\ |
342 static void putprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info);
\ | 332 static void putprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info);
\ |
343 static void delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) | 333 static void delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) |
344 | 334 |
345 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ | 335 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ |
346 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ | 336 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ |
347 void js_class_name::queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info) { \ | 337 void js_class_name::queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info) { \ |
348 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ | 338 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ |
349 }\ | 339 }\ |
350 void js_class_name::getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info) { \ | 340 void js_class_name::getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
351 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ | 341 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ |
352 } \ | 342 } \ |
353 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)
{\ | 343 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)
{\ |
354 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ | 344 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ |
355 }\ | 345 }\ |
356 void js_class_name::delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ | 346 void js_class_name::delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ |
357 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ | 347 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ |
358 } \ | 348 } \ |
359 JSBool js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObjec
t global)\ | 349 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject
global)\ |
360 {\ | 350 {\ |
361 CJS_Object* pObj = FX_NEW js_class_name(obj);\ | 351 CJS_Object* pObj = FX_NEW js_class_name(obj);\ |
362 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\ | 352 pObj->SetEmbedObject(FX_NEW class_alternate(pObj));\ |
363 JS_SetPrivate(NULL,obj, (void*)pObj); \ | 353 JS_SetPrivate(NULL,obj, (void*)pObj); \ |
364 pObj->InitInstance(cc);\ | 354 pObj->InitInstance(cc);\ |
365 return JS_TRUE;\ | |
366 }\ | 355 }\ |
367 \ | 356 \ |
368 JSBool js_class_name::JSDestructor(JSFXObject obj) \ | 357 void js_class_name::JSDestructor(JSFXObject obj) \ |
369 {\ | 358 {\ |
370 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\ | 359 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\ |
371 ASSERT(pObj != NULL);\ | 360 ASSERT(pObj != NULL);\ |
372 pObj->ExitInstance();\ | 361 pObj->ExitInstance();\ |
373 delete pObj;\ | 362 delete pObj;\ |
374 return JS_TRUE;\ | |
375 }\ | 363 }\ |
376 \ | 364 \ |
377 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\ | 365 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\ |
378 {\ | 366 {\ |
379 \ | 367 \ |
380 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eOb
jType, JSConstructor, JSDestructor, 0);\ | 368 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eOb
jType, JSConstructor, JSDestructor, 0);\ |
381 \ | 369 \ |
382 if (nObjDefnID >= 0)\ | 370 if (nObjDefnID >= 0)\ |
383 {\ | 371 {\ |
384 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertyS
pec)-1; j<szj; j++)\ | 372 for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertyS
pec)-1; j<szj; j++)\ |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 #define VALUE_NAME_BOOLEAN L"boolean" | 466 #define VALUE_NAME_BOOLEAN L"boolean" |
479 #define VALUE_NAME_DATE L"date" | 467 #define VALUE_NAME_DATE L"date" |
480 #define VALUE_NAME_OBJECT L"object" | 468 #define VALUE_NAME_OBJECT L"object" |
481 #define VALUE_NAME_FXOBJ L"fxobj" | 469 #define VALUE_NAME_FXOBJ L"fxobj" |
482 #define VALUE_NAME_NULL L"null" | 470 #define VALUE_NAME_NULL L"null" |
483 #define VALUE_NAME_UNDEFINED L"undefined" | 471 #define VALUE_NAME_UNDEFINED L"undefined" |
484 | 472 |
485 FXJSVALUETYPE GET_VALUE_TYPE(v8::Handle<v8::Value> p); | 473 FXJSVALUETYPE GET_VALUE_TYPE(v8::Handle<v8::Value> p); |
486 | 474 |
487 #endif //_JS_DEFINE_H_ | 475 #endif //_JS_DEFINE_H_ |
OLD | NEW |