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_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ | 7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ | 8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
9 | 9 |
10 #include "../jsapi/fxjs_v8.h" | 10 #include "../jsapi/fxjs_v8.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 FX_BOOL (C::*M)(IFXJS_Context* cc, | 76 FX_BOOL (C::*M)(IFXJS_Context* cc, |
77 CJS_PropValue& vp, | 77 CJS_PropValue& vp, |
78 CFX_WideString& sError)> | 78 CFX_WideString& sError)> |
79 void JSPropGetter(const char* prop_name_string, | 79 void JSPropGetter(const char* prop_name_string, |
80 const char* class_name_string, | 80 const char* class_name_string, |
81 v8::Local<v8::String> property, | 81 v8::Local<v8::String> property, |
82 const v8::PropertyCallbackInfo<v8::Value>& info) { | 82 const v8::PropertyCallbackInfo<v8::Value>& info) { |
83 v8::Isolate* isolate = info.GetIsolate(); | 83 v8::Isolate* isolate = info.GetIsolate(); |
84 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 84 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
85 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 85 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
| 86 if (v.IsEmpty()) |
| 87 return; |
86 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 88 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
87 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 89 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
88 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); | 90 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); |
89 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); | 91 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); |
90 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 92 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
91 CFX_WideString sError; | 93 CFX_WideString sError; |
92 CJS_PropValue value(isolate); | 94 CJS_PropValue value(isolate); |
93 value.StartGetting(); | 95 value.StartGetting(); |
94 if (!(pObj->*M)(pContext, value, sError)) { | 96 if (!(pObj->*M)(pContext, value, sError)) { |
95 JS_Error(isolate, | 97 JS_Error(isolate, |
96 JSFormatErrorString(class_name_string, prop_name_string, sError)); | 98 JSFormatErrorString(class_name_string, prop_name_string, sError)); |
97 return; | 99 return; |
98 } | 100 } |
99 info.GetReturnValue().Set((v8::Local<v8::Value>)value); | 101 info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
100 } | 102 } |
101 | 103 |
102 template <class C, | 104 template <class C, |
103 FX_BOOL (C::*M)(IFXJS_Context* cc, | 105 FX_BOOL (C::*M)(IFXJS_Context* cc, |
104 CJS_PropValue& vp, | 106 CJS_PropValue& vp, |
105 CFX_WideString& sError)> | 107 CFX_WideString& sError)> |
106 void JSPropSetter(const char* prop_name_string, | 108 void JSPropSetter(const char* prop_name_string, |
107 const char* class_name_string, | 109 const char* class_name_string, |
108 v8::Local<v8::String> property, | 110 v8::Local<v8::String> property, |
109 v8::Local<v8::Value> value, | 111 v8::Local<v8::Value> value, |
110 const v8::PropertyCallbackInfo<void>& info) { | 112 const v8::PropertyCallbackInfo<void>& info) { |
111 v8::Isolate* isolate = info.GetIsolate(); | 113 v8::Isolate* isolate = info.GetIsolate(); |
112 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 114 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
113 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 115 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
| 116 if (v.IsEmpty()) |
| 117 return; |
114 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 118 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
115 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 119 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
116 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); | 120 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); |
117 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); | 121 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); |
118 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 122 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
119 CFX_WideString sError; | 123 CFX_WideString sError; |
120 CJS_PropValue propValue(CJS_Value(isolate, value, VT_unknown)); | 124 CJS_PropValue propValue(CJS_Value(isolate, value, VT_unknown)); |
121 propValue.StartSetting(); | 125 propValue.StartSetting(); |
122 if (!(pObj->*M)(pContext, propValue, sError)) { | 126 if (!(pObj->*M)(pContext, propValue, sError)) { |
123 JS_Error(isolate, | 127 JS_Error(isolate, |
(...skipping 22 matching lines...) Expand all Loading... |
146 FX_BOOL (C::*M)(IFXJS_Context* cc, | 150 FX_BOOL (C::*M)(IFXJS_Context* cc, |
147 const CJS_Parameters& params, | 151 const CJS_Parameters& params, |
148 CJS_Value& vRet, | 152 CJS_Value& vRet, |
149 CFX_WideString& sError)> | 153 CFX_WideString& sError)> |
150 void JSMethod(const char* method_name_string, | 154 void JSMethod(const char* method_name_string, |
151 const char* class_name_string, | 155 const char* class_name_string, |
152 const v8::FunctionCallbackInfo<v8::Value>& info) { | 156 const v8::FunctionCallbackInfo<v8::Value>& info) { |
153 v8::Isolate* isolate = info.GetIsolate(); | 157 v8::Isolate* isolate = info.GetIsolate(); |
154 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 158 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
155 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 159 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
| 160 if (v.IsEmpty()) |
| 161 return; |
156 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 162 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
157 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 163 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
158 IFXJS_Context* cc = pRuntime->GetCurrentContext(); | 164 IFXJS_Context* cc = pRuntime->GetCurrentContext(); |
159 CJS_Parameters parameters; | 165 CJS_Parameters parameters; |
160 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { | 166 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
161 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); | 167 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); |
162 } | 168 } |
163 CJS_Value valueRes(isolate); | 169 CJS_Value valueRes(isolate); |
164 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); | 170 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); |
165 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 171 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 info.GetReturnValue().Set(bRet ? 4 : 0); | 281 info.GetReturnValue().Set(bRet ? 4 : 0); |
276 } | 282 } |
277 | 283 |
278 template <class Alt> | 284 template <class Alt> |
279 void JSSpecialPropGet(const char* class_name, | 285 void JSSpecialPropGet(const char* class_name, |
280 v8::Local<v8::String> property, | 286 v8::Local<v8::String> property, |
281 const v8::PropertyCallbackInfo<v8::Value>& info) { | 287 const v8::PropertyCallbackInfo<v8::Value>& info) { |
282 v8::Isolate* isolate = info.GetIsolate(); | 288 v8::Isolate* isolate = info.GetIsolate(); |
283 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 289 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
284 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 290 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
| 291 if (v.IsEmpty()) |
| 292 return; |
285 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 293 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
286 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 294 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
287 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | 295 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
288 CJS_Object* pJSObj = | 296 CJS_Object* pJSObj = |
289 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); | 297 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); |
290 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 298 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
291 v8::String::Utf8Value utf8_value(property); | 299 v8::String::Utf8Value utf8_value(property); |
292 CFX_WideString propname = | 300 CFX_WideString propname = |
293 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 301 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
294 CFX_WideString sError; | 302 CFX_WideString sError; |
295 CJS_PropValue value(isolate); | 303 CJS_PropValue value(isolate); |
296 value.StartGetting(); | 304 value.StartGetting(); |
297 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { | 305 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { |
298 JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); | 306 JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); |
299 return; | 307 return; |
300 } | 308 } |
301 info.GetReturnValue().Set((v8::Local<v8::Value>)value); | 309 info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
302 } | 310 } |
303 | 311 |
304 template <class Alt> | 312 template <class Alt> |
305 void JSSpecialPropPut(const char* class_name, | 313 void JSSpecialPropPut(const char* class_name, |
306 v8::Local<v8::String> property, | 314 v8::Local<v8::String> property, |
307 v8::Local<v8::Value> value, | 315 v8::Local<v8::Value> value, |
308 const v8::PropertyCallbackInfo<v8::Value>& info) { | 316 const v8::PropertyCallbackInfo<v8::Value>& info) { |
309 v8::Isolate* isolate = info.GetIsolate(); | 317 v8::Isolate* isolate = info.GetIsolate(); |
310 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 318 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
311 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 319 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
| 320 if (v.IsEmpty()) |
| 321 return; |
312 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 322 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
313 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 323 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
314 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | 324 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
315 CJS_Object* pJSObj = | 325 CJS_Object* pJSObj = |
316 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); | 326 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); |
317 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 327 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
318 v8::String::Utf8Value utf8_value(property); | 328 v8::String::Utf8Value utf8_value(property); |
319 CFX_WideString propname = | 329 CFX_WideString propname = |
320 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 330 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
321 CFX_WideString sError; | 331 CFX_WideString sError; |
322 CJS_PropValue PropValue(CJS_Value(isolate, value, VT_unknown)); | 332 CJS_PropValue PropValue(CJS_Value(isolate, value, VT_unknown)); |
323 PropValue.StartSetting(); | 333 PropValue.StartSetting(); |
324 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { | 334 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { |
325 JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); | 335 JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); |
326 } | 336 } |
327 } | 337 } |
328 | 338 |
329 template <class Alt> | 339 template <class Alt> |
330 void JSSpecialPropDel(const char* class_name, | 340 void JSSpecialPropDel(const char* class_name, |
331 v8::Local<v8::String> property, | 341 v8::Local<v8::String> property, |
332 const v8::PropertyCallbackInfo<v8::Boolean>& info) { | 342 const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
333 v8::Isolate* isolate = info.GetIsolate(); | 343 v8::Isolate* isolate = info.GetIsolate(); |
334 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 344 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
335 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 345 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
| 346 if (v.IsEmpty()) |
| 347 return; |
336 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 348 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
337 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 349 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
338 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | 350 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
339 CJS_Object* pJSObj = | 351 CJS_Object* pJSObj = |
340 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); | 352 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); |
341 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 353 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
342 v8::String::Utf8Value utf8_value(property); | 354 v8::String::Utf8Value utf8_value(property); |
343 CFX_WideString propname = | 355 CFX_WideString propname = |
344 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 356 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
345 CFX_WideString sError; | 357 CFX_WideString sError; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 | 449 |
438 template <FX_BOOL (*F)(IFXJS_Context* cc, | 450 template <FX_BOOL (*F)(IFXJS_Context* cc, |
439 const CJS_Parameters& params, | 451 const CJS_Parameters& params, |
440 CJS_Value& vRet, | 452 CJS_Value& vRet, |
441 CFX_WideString& sError)> | 453 CFX_WideString& sError)> |
442 void JSGlobalFunc(const char* func_name_string, | 454 void JSGlobalFunc(const char* func_name_string, |
443 const v8::FunctionCallbackInfo<v8::Value>& info) { | 455 const v8::FunctionCallbackInfo<v8::Value>& info) { |
444 v8::Isolate* isolate = info.GetIsolate(); | 456 v8::Isolate* isolate = info.GetIsolate(); |
445 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 457 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
446 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 458 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
| 459 if (v.IsEmpty()) |
| 460 return; |
447 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 461 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
448 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 462 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
449 IFXJS_Context* cc = pRuntime->GetCurrentContext(); | 463 IFXJS_Context* cc = pRuntime->GetCurrentContext(); |
450 CJS_Parameters parameters; | 464 CJS_Parameters parameters; |
451 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { | 465 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
452 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); | 466 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); |
453 } | 467 } |
454 CJS_Value valueRes(isolate); | 468 CJS_Value valueRes(isolate); |
455 CFX_WideString sError; | 469 CFX_WideString sError; |
456 if (!(*F)(cc, parameters, valueRes, sError)) { | 470 if (!(*F)(cc, parameters, valueRes, sError)) { |
(...skipping 24 matching lines...) Expand all Loading... |
481 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate) { \ | 495 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate) { \ |
482 for (int i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ | 496 for (int i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ |
483 JS_DefineGlobalMethod(pIsolate, js_class_name::global_methods[i].pName, \ | 497 JS_DefineGlobalMethod(pIsolate, js_class_name::global_methods[i].pName, \ |
484 js_class_name::global_methods[i].pMethodCall); \ | 498 js_class_name::global_methods[i].pMethodCall); \ |
485 } \ | 499 } \ |
486 } | 500 } |
487 | 501 |
488 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p); | 502 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p); |
489 | 503 |
490 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ | 504 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
OLD | NEW |