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 FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ | 7 #ifndef FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ |
8 #define FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ | 8 #define FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ |
9 | 9 |
10 #include "../../include/jsapi/fxjs_v8.h" | 10 #include "../../include/jsapi/fxjs_v8.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 const wchar_t* pName; | 23 const wchar_t* pName; |
24 v8::AccessorGetterCallback pPropGet; | 24 v8::AccessorGetterCallback pPropGet; |
25 v8::AccessorSetterCallback pPropPut; | 25 v8::AccessorSetterCallback pPropPut; |
26 }; | 26 }; |
27 | 27 |
28 struct JSMethodSpec { | 28 struct JSMethodSpec { |
29 const wchar_t* pName; | 29 const wchar_t* pName; |
30 v8::FunctionCallback pMethodCall; | 30 v8::FunctionCallback pMethodCall; |
31 }; | 31 }; |
32 | 32 |
33 /* ====================================== PUBLIC DEFINE SPEC | |
34 * ============================================== */ | |
35 #define JS_WIDESTRING(widestring) L## #widestring | 33 #define JS_WIDESTRING(widestring) L## #widestring |
36 | |
37 #define BEGIN_JS_STATIC_CONST(js_class_name) \ | 34 #define BEGIN_JS_STATIC_CONST(js_class_name) \ |
38 JSConstSpec js_class_name::JS_Class_Consts[] = { | 35 JSConstSpec js_class_name::JS_Class_Consts[] = { |
39 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) \ | 36 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) \ |
40 { const_name, pValue, L"", 0 } \ | 37 { const_name, pValue, L"", 0 } \ |
41 , | 38 , |
| 39 |
42 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) \ | 40 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) \ |
43 { const_name, 0, pValue, 1 } \ | 41 { const_name, 0, pValue, 1 } \ |
44 , | 42 , |
| 43 |
45 #define END_JS_STATIC_CONST() \ | 44 #define END_JS_STATIC_CONST() \ |
46 { 0, 0, 0, 0 } \ | 45 { 0, 0, 0, 0 } \ |
47 } \ | 46 } \ |
48 ; | 47 ; |
49 | 48 |
50 #define BEGIN_JS_STATIC_PROP(js_class_name) \ | 49 #define BEGIN_JS_STATIC_PROP(js_class_name) \ |
51 JSPropertySpec js_class_name::JS_Class_Properties[] = { | 50 JSPropertySpec js_class_name::JS_Class_Properties[] = { |
52 #define JS_STATIC_PROP_ENTRY(prop_name) \ | 51 #define JS_STATIC_PROP_ENTRY(prop_name) \ |
53 { \ | 52 { \ |
54 JS_WIDESTRING(prop_name), get_##prop_name##_static, \ | 53 JS_WIDESTRING(prop_name), get_##prop_name##_static, \ |
55 set_##prop_name##_static \ | 54 set_##prop_name##_static \ |
56 } \ | 55 } \ |
57 , | 56 , |
| 57 |
58 #define END_JS_STATIC_PROP() \ | 58 #define END_JS_STATIC_PROP() \ |
59 { 0, 0, 0 } \ | 59 { 0, 0, 0 } \ |
60 } \ | 60 } \ |
61 ; | 61 ; |
62 | 62 |
63 #define BEGIN_JS_STATIC_METHOD(js_class_name) \ | 63 #define BEGIN_JS_STATIC_METHOD(js_class_name) \ |
64 JSMethodSpec js_class_name::JS_Class_Methods[] = { | 64 JSMethodSpec js_class_name::JS_Class_Methods[] = { |
65 #define JS_STATIC_METHOD_ENTRY(method_name) \ | 65 #define JS_STATIC_METHOD_ENTRY(method_name) \ |
66 { JS_WIDESTRING(method_name), method_name##_static } \ | 66 { JS_WIDESTRING(method_name), method_name##_static } \ |
67 , | 67 , |
| 68 |
68 #define END_JS_STATIC_METHOD() \ | 69 #define END_JS_STATIC_METHOD() \ |
69 { 0, 0 } \ | 70 { 0, 0 } \ |
70 } \ | 71 } \ |
71 ; | 72 ; |
72 | 73 |
73 /* ======================================== PROP CALLBACK | |
74 * ============================================ */ | |
75 | |
76 template <class C, | 74 template <class C, |
77 FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> | 75 FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> |
78 void JSPropGetter(const char* prop_name_string, | 76 void JSPropGetter(const char* prop_name_string, |
79 const char* class_name_string, | 77 const char* class_name_string, |
80 v8::Local<v8::String> property, | 78 v8::Local<v8::String> property, |
81 const v8::PropertyCallbackInfo<v8::Value>& info) { | 79 const v8::PropertyCallbackInfo<v8::Value>& info) { |
82 v8::Isolate* isolate = info.GetIsolate(); | 80 v8::Isolate* isolate = info.GetIsolate(); |
83 IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); | 81 IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
84 if (!pRuntime) | 82 if (!pRuntime) |
85 return; | 83 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ | 125 JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
128 property, info); \ | 126 property, info); \ |
129 } \ | 127 } \ |
130 static void set_##prop_name##_static( \ | 128 static void set_##prop_name##_static( \ |
131 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ | 129 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
132 const v8::PropertyCallbackInfo<void>& info) { \ | 130 const v8::PropertyCallbackInfo<void>& info) { \ |
133 JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ | 131 JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
134 property, value, info); \ | 132 property, value, info); \ |
135 } | 133 } |
136 | 134 |
137 /* ========================================= METHOD CALLBACK | |
138 * =========================================== */ | |
139 | |
140 template <class C, | 135 template <class C, |
141 FX_BOOL (C::*M)(IFXJS_Context*, | 136 FX_BOOL (C::*M)(IFXJS_Context*, |
142 const CJS_Parameters&, | 137 const CJS_Parameters&, |
143 CJS_Value&, | 138 CJS_Value&, |
144 CFX_WideString&)> | 139 CFX_WideString&)> |
145 void JSMethod(const char* method_name_string, | 140 void JSMethod(const char* method_name_string, |
146 const char* class_name_string, | 141 const char* class_name_string, |
147 const v8::FunctionCallbackInfo<v8::Value>& info) { | 142 const v8::FunctionCallbackInfo<v8::Value>& info) { |
148 v8::Isolate* isolate = info.GetIsolate(); | 143 v8::Isolate* isolate = info.GetIsolate(); |
149 IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); | 144 IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
(...skipping 23 matching lines...) Expand all Loading... |
173 info); \ | 168 info); \ |
174 } | 169 } |
175 | 170 |
176 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ | 171 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ |
177 static void method_name##_static( \ | 172 static void method_name##_static( \ |
178 const v8::FunctionCallbackInfo<v8::Value>& info) { \ | 173 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
179 JSMethod<class_alternate, &class_alternate::method_name>( \ | 174 JSMethod<class_alternate, &class_alternate::method_name>( \ |
180 #method_name, #class_name, info); \ | 175 #method_name, #class_name, info); \ |
181 } | 176 } |
182 | 177 |
183 /* ===================================== JS CLASS | 178 // All JS classes have a name, an object defintion ID, and the ability to |
184 * =============================================== */ | 179 // register themselves with FXJS_V8. We never make a BASE class on its own |
| 180 // because it can't really do anything. |
| 181 #define DECLARE_JS_CLASS_BASE_PART() \ |
| 182 static const wchar_t* g_pClassName; \ |
| 183 static int g_nObjDefnID; \ |
| 184 static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); |
185 | 185 |
186 #define DECLARE_JS_CLASS(js_class_name) \ | 186 #define IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
187 static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \ | 187 const wchar_t* js_class_name::g_pClassName = JS_WIDESTRING(class_name); \ |
188 v8::Local<v8::Object> global); \ | 188 int js_class_name::g_nObjDefnID = -1; |
189 static void JSDestructor(v8::Local<v8::Object> obj); \ | |
190 static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \ | |
191 static JSConstSpec JS_Class_Consts[]; \ | |
192 static JSPropertySpec JS_Class_Properties[]; \ | |
193 static JSMethodSpec JS_Class_Methods[]; \ | |
194 static const wchar_t* m_pClassName | |
195 | 189 |
196 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ | 190 // CONST classes provide constants, but not constructors, methods, or props. |
197 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ | 191 #define DECLARE_JS_CLASS_CONST() \ |
198 void js_class_name::JSConstructor(IFXJS_Context* cc, \ | 192 DECLARE_JS_CLASS_BASE_PART() \ |
199 v8::Local<v8::Object> obj, \ | 193 DECLARE_JS_CLASS_CONST_PART() |
200 v8::Local<v8::Object> global) { \ | 194 |
201 CJS_Object* pObj = new js_class_name(obj); \ | 195 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ |
202 pObj->SetEmbedObject(new class_alternate(pObj)); \ | 196 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
203 FXJS_SetPrivate(NULL, obj, (void*)pObj); \ | 197 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
204 pObj->InitInstance(cc); \ | 198 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
205 } \ | 199 FXJSOBJTYPE eObjType) { \ |
206 \ | 200 g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ |
207 void js_class_name::JSDestructor(v8::Local<v8::Object> obj) { \ | 201 eObjType, nullptr, nullptr); \ |
208 js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(NULL, obj); \ | 202 DefineConsts(pIsolate); \ |
209 pObj->ExitInstance(); \ | |
210 delete pObj; \ | |
211 } \ | |
212 \ | |
213 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ | |
214 FXJSOBJTYPE eObjType) { \ | |
215 int nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::m_pClassName, \ | |
216 eObjType, JSConstructor, JSDestructor); \ | |
217 for (int i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ | |
218 FXJS_DefineObjProperty( \ | |
219 pIsolate, nObjDefnID, JS_Class_Properties[i].pName, \ | |
220 JS_Class_Properties[i].pPropGet, JS_Class_Properties[i].pPropPut); \ | |
221 } \ | |
222 for (int i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ | |
223 FXJS_DefineObjMethod(pIsolate, nObjDefnID, JS_Class_Methods[i].pName, \ | |
224 JS_Class_Methods[i].pMethodCall); \ | |
225 } \ | |
226 } | 203 } |
227 | 204 |
| 205 #define DECLARE_JS_CLASS_CONST_PART() \ |
| 206 static JSConstSpec JS_Class_Consts[]; \ |
| 207 static void DefineConsts(v8::Isolate* pIsolate); |
| 208 |
| 209 #define IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
| 210 void js_class_name::DefineConsts(v8::Isolate* pIsolate) { \ |
| 211 for (size_t i = 0; i < FX_ArraySize(JS_Class_Consts) - 1; ++i) { \ |
| 212 FXJS_DefineObjConst( \ |
| 213 pIsolate, g_nObjDefnID, JS_Class_Consts[i].pName, \ |
| 214 JS_Class_Consts[i].t == 0 \ |
| 215 ? FXJS_NewNumber(pIsolate, JS_Class_Consts[i].number) \ |
| 216 : FXJS_NewString(pIsolate, JS_Class_Consts[i].string)); \ |
| 217 } \ |
| 218 } |
| 219 |
| 220 // Convenience macros for declaring classes without an alternate. |
| 221 #define DECLARE_JS_CLASS() DECLARE_JS_CLASS_RICH() |
228 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \ | 222 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \ |
229 IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name) | 223 IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name) |
230 | 224 |
231 /* ======================================== CONST CLASS | 225 // Rich JS classes provide constants, methods, properties, and the ability |
232 * ============================================ */ | 226 // to construct native object state. |
| 227 #define DECLARE_JS_CLASS_RICH() \ |
| 228 DECLARE_JS_CLASS_BASE_PART() \ |
| 229 DECLARE_JS_CLASS_CONST_PART() \ |
| 230 DECLARE_JS_CLASS_RICH_PART() |
233 | 231 |
234 #define DECLARE_JS_CLASS_CONST() \ | 232 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ |
235 static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \ | 233 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
236 static JSConstSpec JS_Class_Consts[]; \ | 234 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
237 static const wchar_t* m_pClassName | 235 IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, class_name) \ |
238 | 236 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
239 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ | 237 FXJSOBJTYPE eObjType) { \ |
240 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ | 238 g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ |
241 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ | 239 eObjType, JSConstructor, JSDestructor); \ |
242 FXJSOBJTYPE eObjType) { \ | 240 DefineConsts(pIsolate); \ |
243 int nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::m_pClassName, \ | 241 DefineProps(pIsolate); \ |
244 eObjType, NULL, NULL); \ | 242 DefineMethods(pIsolate); \ |
245 for (int i = 0; i < FX_ArraySize(JS_Class_Consts) - 1; ++i) { \ | |
246 FXJS_DefineObjConst( \ | |
247 pIsolate, nObjDefnID, JS_Class_Consts[i].pName, \ | |
248 JS_Class_Consts[i].t == 0 \ | |
249 ? FXJS_NewNumber(pIsolate, JS_Class_Consts[i].number) \ | |
250 : FXJS_NewString(pIsolate, JS_Class_Consts[i].string)); \ | |
251 } \ | |
252 } | 243 } |
253 | 244 |
254 /* ===================================== SPECIAL JS CLASS | 245 #define DECLARE_JS_CLASS_RICH_PART() \ |
255 * =============================================== */ | 246 static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \ |
| 247 v8::Local<v8::Object> global); \ |
| 248 static void JSDestructor(v8::Local<v8::Object> obj); \ |
| 249 static void DefineProps(v8::Isolate* pIsoalte); \ |
| 250 static void DefineMethods(v8::Isolate* pIsoalte); \ |
| 251 static JSPropertySpec JS_Class_Properties[]; \ |
| 252 static JSMethodSpec JS_Class_Methods[]; |
| 253 |
| 254 #define IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, \ |
| 255 class_name) \ |
| 256 void js_class_name::JSConstructor(IFXJS_Context* cc, \ |
| 257 v8::Local<v8::Object> obj, \ |
| 258 v8::Local<v8::Object> global) { \ |
| 259 CJS_Object* pObj = new js_class_name(obj); \ |
| 260 pObj->SetEmbedObject(new class_alternate(pObj)); \ |
| 261 FXJS_SetPrivate(nullptr, obj, (void*)pObj); \ |
| 262 pObj->InitInstance(cc); \ |
| 263 } \ |
| 264 void js_class_name::JSDestructor(v8::Local<v8::Object> obj) { \ |
| 265 js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(nullptr, obj); \ |
| 266 pObj->ExitInstance(); \ |
| 267 delete pObj; \ |
| 268 } \ |
| 269 void js_class_name::DefineProps(v8::Isolate* pIsolate) { \ |
| 270 for (size_t i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ |
| 271 FXJS_DefineObjProperty( \ |
| 272 pIsolate, g_nObjDefnID, JS_Class_Properties[i].pName, \ |
| 273 JS_Class_Properties[i].pPropGet, JS_Class_Properties[i].pPropPut); \ |
| 274 } \ |
| 275 } \ |
| 276 void js_class_name::DefineMethods(v8::Isolate* pIsolate) { \ |
| 277 for (size_t i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ |
| 278 FXJS_DefineObjMethod(pIsolate, g_nObjDefnID, JS_Class_Methods[i].pName, \ |
| 279 JS_Class_Methods[i].pMethodCall); \ |
| 280 } \ |
| 281 } |
| 282 |
| 283 // Special JS classes implement methods, props, and queries, but not consts. |
| 284 #define DECLARE_SPECIAL_JS_CLASS() \ |
| 285 DECLARE_JS_CLASS_BASE_PART() \ |
| 286 DECLARE_JS_CLASS_CONST_PART() \ |
| 287 DECLARE_JS_CLASS_RICH_PART() \ |
| 288 DECLARE_SPECIAL_JS_CLASS_PART() |
| 289 |
| 290 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ |
| 291 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
| 292 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
| 293 IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, class_name) \ |
| 294 IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, class_name) \ |
| 295 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
| 296 FXJSOBJTYPE eObjType) { \ |
| 297 g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ |
| 298 eObjType, JSConstructor, JSDestructor); \ |
| 299 DefineConsts(pIsolate); \ |
| 300 DefineProps(pIsolate); \ |
| 301 DefineMethods(pIsolate); \ |
| 302 DefineAllProperties(pIsolate); \ |
| 303 } |
| 304 |
| 305 #define DECLARE_SPECIAL_JS_CLASS_PART() \ |
| 306 static void queryprop_static( \ |
| 307 v8::Local<v8::String> property, \ |
| 308 const v8::PropertyCallbackInfo<v8::Integer>& info); \ |
| 309 static void getprop_static(v8::Local<v8::String> property, \ |
| 310 const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 311 static void putprop_static(v8::Local<v8::String> property, \ |
| 312 v8::Local<v8::Value> value, \ |
| 313 const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 314 static void delprop_static( \ |
| 315 v8::Local<v8::String> property, \ |
| 316 const v8::PropertyCallbackInfo<v8::Boolean>& info); \ |
| 317 static void DefineAllProperties(v8::Isolate* pIsolate); |
| 318 |
| 319 #define IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, \ |
| 320 class_name) \ |
| 321 void js_class_name::queryprop_static( \ |
| 322 v8::Local<v8::String> property, \ |
| 323 const v8::PropertyCallbackInfo<v8::Integer>& info) { \ |
| 324 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ |
| 325 } \ |
| 326 void js_class_name::getprop_static( \ |
| 327 v8::Local<v8::String> property, \ |
| 328 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 329 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ |
| 330 } \ |
| 331 void js_class_name::putprop_static( \ |
| 332 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 333 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 334 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ |
| 335 } \ |
| 336 void js_class_name::delprop_static( \ |
| 337 v8::Local<v8::String> property, \ |
| 338 const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ |
| 339 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ |
| 340 } \ |
| 341 void js_class_name::DefineAllProperties(v8::Isolate* pIsolate) { \ |
| 342 FXJS_DefineObjAllProperties( \ |
| 343 pIsolate, g_nObjDefnID, js_class_name::queryprop_static, \ |
| 344 js_class_name::getprop_static, js_class_name::putprop_static, \ |
| 345 js_class_name::delprop_static); \ |
| 346 } |
256 | 347 |
257 template <class Alt> | 348 template <class Alt> |
258 void JSSpecialPropQuery(const char*, | 349 void JSSpecialPropQuery(const char*, |
259 v8::Local<v8::String> property, | 350 v8::Local<v8::String> property, |
260 const v8::PropertyCallbackInfo<v8::Integer>& info) { | 351 const v8::PropertyCallbackInfo<v8::Integer>& info) { |
261 v8::Isolate* isolate = info.GetIsolate(); | 352 v8::Isolate* isolate = info.GetIsolate(); |
262 v8::String::Utf8Value utf8_value(property); | 353 v8::String::Utf8Value utf8_value(property); |
263 CFX_WideString propname = | 354 CFX_WideString propname = |
264 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 355 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
265 CJS_Object* pJSObj = | 356 CJS_Object* pJSObj = |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 CFX_WideString propname = | 425 CFX_WideString propname = |
335 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 426 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
336 CFX_WideString sError; | 427 CFX_WideString sError; |
337 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { | 428 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { |
338 CFX_ByteString cbName; | 429 CFX_ByteString cbName; |
339 cbName.Format("%s.%s", class_name, "DelProperty"); | 430 cbName.Format("%s.%s", class_name, "DelProperty"); |
340 // Probably a missing call to JSFX_Error(). | 431 // Probably a missing call to JSFX_Error(). |
341 } | 432 } |
342 } | 433 } |
343 | 434 |
344 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ | 435 template <FX_BOOL (*F)(IFXJS_Context* cc, |
345 static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \ | 436 const CJS_Parameters& params, |
346 v8::Local<v8::Object> global); \ | 437 CJS_Value& vRet, |
347 static void JSDestructor(v8::Local<v8::Object> obj); \ | 438 CFX_WideString& sError)> |
348 static JSConstSpec JS_Class_Consts[]; \ | |
349 static JSPropertySpec JS_Class_Properties[]; \ | |
350 static JSMethodSpec JS_Class_Methods[]; \ | |
351 static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \ | |
352 static const wchar_t* m_pClassName; \ | |
353 static void queryprop_##js_class_name##_static( \ | |
354 v8::Local<v8::String> property, \ | |
355 const v8::PropertyCallbackInfo<v8::Integer>& info); \ | |
356 static void getprop_##js_class_name##_static( \ | |
357 v8::Local<v8::String> property, \ | |
358 const v8::PropertyCallbackInfo<v8::Value>& info); \ | |
359 static void putprop_##js_class_name##_static( \ | |
360 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ | |
361 const v8::PropertyCallbackInfo<v8::Value>& info); \ | |
362 static void delprop_##js_class_name##_static( \ | |
363 v8::Local<v8::String> property, \ | |
364 const v8::PropertyCallbackInfo<v8::Boolean>& info) | |
365 | |
366 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ | |
367 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ | |
368 void js_class_name::queryprop_##js_class_name##_static( \ | |
369 v8::Local<v8::String> property, \ | |
370 const v8::PropertyCallbackInfo<v8::Integer>& info) { \ | |
371 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ | |
372 } \ | |
373 void js_class_name::getprop_##js_class_name##_static( \ | |
374 v8::Local<v8::String> property, \ | |
375 const v8::PropertyCallbackInfo<v8::Value>& info) { \ | |
376 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ | |
377 } \ | |
378 void js_class_name::putprop_##js_class_name##_static( \ | |
379 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ | |
380 const v8::PropertyCallbackInfo<v8::Value>& info) { \ | |
381 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ | |
382 } \ | |
383 void js_class_name::delprop_##js_class_name##_static( \ | |
384 v8::Local<v8::String> property, \ | |
385 const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ | |
386 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ | |
387 } \ | |
388 void js_class_name::JSConstructor(IFXJS_Context* cc, \ | |
389 v8::Local<v8::Object> obj, \ | |
390 v8::Local<v8::Object> global) { \ | |
391 CJS_Object* pObj = new js_class_name(obj); \ | |
392 pObj->SetEmbedObject(new class_alternate(pObj)); \ | |
393 FXJS_SetPrivate(NULL, obj, (void*)pObj); \ | |
394 pObj->InitInstance(cc); \ | |
395 } \ | |
396 \ | |
397 void js_class_name::JSDestructor(v8::Local<v8::Object> obj) { \ | |
398 js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(NULL, obj); \ | |
399 ASSERT(pObj != NULL); \ | |
400 pObj->ExitInstance(); \ | |
401 delete pObj; \ | |
402 } \ | |
403 \ | |
404 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ | |
405 FXJSOBJTYPE eObjType) { \ | |
406 int nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::m_pClassName, \ | |
407 eObjType, JSConstructor, JSDestructor); \ | |
408 for (int i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ | |
409 FXJS_DefineObjProperty( \ | |
410 pIsolate, nObjDefnID, JS_Class_Properties[i].pName, \ | |
411 JS_Class_Properties[i].pPropGet, JS_Class_Properties[i].pPropPut); \ | |
412 } \ | |
413 \ | |
414 for (int i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ | |
415 FXJS_DefineObjMethod(pIsolate, nObjDefnID, JS_Class_Methods[i].pName, \ | |
416 JS_Class_Methods[i].pMethodCall); \ | |
417 } \ | |
418 FXJS_DefineObjAllProperties( \ | |
419 pIsolate, nObjDefnID, \ | |
420 js_class_name::queryprop_##js_class_name##_static, \ | |
421 js_class_name::getprop_##js_class_name##_static, \ | |
422 js_class_name::putprop_##js_class_name##_static, \ | |
423 js_class_name::delprop_##js_class_name##_static); \ | |
424 } | |
425 | |
426 /* ======================================== GLOBAL METHODS | |
427 * ============================================ */ | |
428 | |
429 template <FX_BOOL ( | |
430 *F)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)> | |
431 void JSGlobalFunc(const char* func_name_string, | 439 void JSGlobalFunc(const char* func_name_string, |
432 const v8::FunctionCallbackInfo<v8::Value>& info) { | 440 const v8::FunctionCallbackInfo<v8::Value>& info) { |
433 v8::Isolate* isolate = info.GetIsolate(); | 441 v8::Isolate* isolate = info.GetIsolate(); |
434 IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); | 442 IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
435 if (!pRuntime) | 443 if (!pRuntime) |
436 return; | 444 return; |
437 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | 445 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
438 CJS_Parameters parameters; | 446 CJS_Parameters parameters; |
439 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { | 447 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
440 parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); | 448 parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); |
(...skipping 19 matching lines...) Expand all Loading... |
460 | 468 |
461 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ | 469 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
462 JSMethodSpec js_class_name::global_methods[] = { | 470 JSMethodSpec js_class_name::global_methods[] = { |
463 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \ | 471 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \ |
464 JS_STATIC_METHOD_ENTRY(method_name) | 472 JS_STATIC_METHOD_ENTRY(method_name) |
465 | 473 |
466 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() | 474 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() |
467 | 475 |
468 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ | 476 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
469 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate) { \ | 477 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate) { \ |
470 for (int i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ | 478 for (size_t i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ |
471 FXJS_DefineGlobalMethod(pIsolate, \ | 479 FXJS_DefineGlobalMethod(pIsolate, \ |
472 js_class_name::global_methods[i].pName, \ | 480 js_class_name::global_methods[i].pName, \ |
473 js_class_name::global_methods[i].pMethodCall); \ | 481 js_class_name::global_methods[i].pMethodCall); \ |
474 } \ | 482 } \ |
475 } | 483 } |
476 | 484 |
477 CJS_Value::Type GET_VALUE_TYPE(v8::Local<v8::Value> p); | 485 CJS_Value::Type GET_VALUE_TYPE(v8::Local<v8::Value> p); |
478 | 486 |
479 #endif // FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ | 487 #endif // FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ |
OLD | NEW |