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