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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 /* ======================================== PROP CALLBACK | 72 /* ======================================== PROP CALLBACK |
73 * ============================================ */ | 73 * ============================================ */ |
74 | 74 |
75 template <class C, | 75 template <class C, |
76 FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> | 76 FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> |
77 void JSPropGetter(const char* prop_name_string, | 77 void JSPropGetter(const char* prop_name_string, |
78 const char* class_name_string, | 78 const char* class_name_string, |
79 v8::Local<v8::String> property, | 79 v8::Local<v8::String> property, |
80 const v8::PropertyCallbackInfo<v8::Value>& info) { | 80 const v8::PropertyCallbackInfo<v8::Value>& info) { |
81 v8::Isolate* isolate = info.GetIsolate(); | 81 v8::Isolate* isolate = info.GetIsolate(); |
82 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); | 82 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
83 IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); | 83 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
84 CJS_PropValue value(isolate); | 84 if (v.IsEmpty()) |
85 value.StartGetting(); | 85 return; |
86 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | |
87 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | |
88 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | |
86 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); | 89 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); |
87 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 90 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
88 CFX_WideString sError; | 91 CFX_WideString sError; |
92 CJS_PropValue value(isolate); | |
93 value.StartGetting(); | |
89 if (!(pObj->*M)(pRuntimeContext, value, sError)) { | 94 if (!(pObj->*M)(pRuntimeContext, value, sError)) { |
90 FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, | 95 FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, |
91 sError)); | 96 sError)); |
92 return; | 97 return; |
93 } | 98 } |
94 info.GetReturnValue().Set((v8::Local<v8::Value>)value); | 99 info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
95 } | 100 } |
96 | 101 |
97 template <class C, | 102 template <class C, |
98 FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> | 103 FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> |
99 void JSPropSetter(const char* prop_name_string, | 104 void JSPropSetter(const char* prop_name_string, |
100 const char* class_name_string, | 105 const char* class_name_string, |
101 v8::Local<v8::String> property, | 106 v8::Local<v8::String> property, |
102 v8::Local<v8::Value> value, | 107 v8::Local<v8::Value> value, |
103 const v8::PropertyCallbackInfo<void>& info) { | 108 const v8::PropertyCallbackInfo<void>& info) { |
104 v8::Isolate* isolate = info.GetIsolate(); | 109 v8::Isolate* isolate = info.GetIsolate(); |
105 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); | 110 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
106 IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); | 111 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
107 CJS_PropValue propValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); | 112 if (v.IsEmpty()) |
108 propValue.StartSetting(); | 113 return; |
114 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | |
115 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | |
116 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | |
109 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); | 117 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); |
110 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 118 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
111 CFX_WideString sError; | 119 CFX_WideString sError; |
120 CJS_PropValue propValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); | |
121 propValue.StartSetting(); | |
112 if (!(pObj->*M)(pRuntimeContext, propValue, sError)) { | 122 if (!(pObj->*M)(pRuntimeContext, propValue, sError)) { |
113 FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, | 123 FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, |
114 sError)); | 124 sError)); |
115 } | 125 } |
116 } | 126 } |
117 | 127 |
118 #define JS_STATIC_PROP(prop_name, class_name) \ | 128 #define JS_STATIC_PROP(prop_name, class_name) \ |
119 static void get_##prop_name##_static( \ | 129 static void get_##prop_name##_static( \ |
120 v8::Local<v8::String> property, \ | 130 v8::Local<v8::String> property, \ |
121 const v8::PropertyCallbackInfo<v8::Value>& info) { \ | 131 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
(...skipping 12 matching lines...) Expand all Loading... | |
134 | 144 |
135 template <class C, | 145 template <class C, |
136 FX_BOOL (C::*M)(IFXJS_Context*, | 146 FX_BOOL (C::*M)(IFXJS_Context*, |
137 const CJS_Parameters&, | 147 const CJS_Parameters&, |
138 CJS_Value&, | 148 CJS_Value&, |
139 CFX_WideString&)> | 149 CFX_WideString&)> |
140 void JSMethod(const char* method_name_string, | 150 void JSMethod(const char* method_name_string, |
141 const char* class_name_string, | 151 const char* class_name_string, |
142 const v8::FunctionCallbackInfo<v8::Value>& info) { | 152 const v8::FunctionCallbackInfo<v8::Value>& info) { |
143 v8::Isolate* isolate = info.GetIsolate(); | 153 v8::Isolate* isolate = info.GetIsolate(); |
144 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); | 154 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
145 IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); | 155 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
156 if (v.IsEmpty()) | |
157 return; | |
158 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | |
159 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | |
160 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | |
146 CJS_Parameters parameters; | 161 CJS_Parameters parameters; |
147 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { | 162 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
148 parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); | 163 parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); |
149 } | 164 } |
150 CJS_Value valueRes(isolate); | 165 CJS_Value valueRes(isolate); |
151 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); | 166 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); |
152 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 167 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
153 CFX_WideString sError; | 168 CFX_WideString sError; |
154 if (!(pObj->*M)(pRuntimeContext, parameters, valueRes, sError)) { | 169 if (!(pObj->*M)(pRuntimeContext, parameters, valueRes, sError)) { |
155 FXJS_Error(isolate, JSFormatErrorString(class_name_string, | 170 FXJS_Error(isolate, JSFormatErrorString(class_name_string, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); | 276 FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); |
262 info.GetReturnValue().Set(bRet ? 4 : 0); | 277 info.GetReturnValue().Set(bRet ? 4 : 0); |
263 } | 278 } |
264 | 279 |
265 template <class Alt> | 280 template <class Alt> |
266 void JSSpecialPropGet(const char* class_name, | 281 void JSSpecialPropGet(const char* class_name, |
267 v8::Local<v8::String> property, | 282 v8::Local<v8::String> property, |
268 const v8::PropertyCallbackInfo<v8::Value>& info) { | 283 const v8::PropertyCallbackInfo<v8::Value>& info) { |
269 v8::Isolate* isolate = info.GetIsolate(); | 284 v8::Isolate* isolate = info.GetIsolate(); |
270 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 285 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
271 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); | 286 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
272 IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); | 287 if (v.IsEmpty()) |
288 return; | |
289 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | |
290 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | |
291 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | |
273 CJS_Object* pJSObj = | 292 CJS_Object* pJSObj = |
274 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); | 293 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
275 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 294 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
276 v8::String::Utf8Value utf8_value(property); | 295 v8::String::Utf8Value utf8_value(property); |
277 CFX_WideString propname = | 296 CFX_WideString propname = |
278 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 297 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
279 CFX_WideString sError; | 298 CFX_WideString sError; |
280 CJS_PropValue value(isolate); | 299 CJS_PropValue value(isolate); |
281 value.StartGetting(); | 300 value.StartGetting(); |
282 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { | 301 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { |
283 FXJS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); | 302 FXJS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); |
284 return; | 303 return; |
285 } | 304 } |
286 info.GetReturnValue().Set((v8::Local<v8::Value>)value); | 305 info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
287 } | 306 } |
288 | 307 |
289 template <class Alt> | 308 template <class Alt> |
290 void JSSpecialPropPut(const char* class_name, | 309 void JSSpecialPropPut(const char* class_name, |
291 v8::Local<v8::String> property, | 310 v8::Local<v8::String> property, |
292 v8::Local<v8::Value> value, | 311 v8::Local<v8::Value> value, |
293 const v8::PropertyCallbackInfo<v8::Value>& info) { | 312 const v8::PropertyCallbackInfo<v8::Value>& info) { |
294 v8::Isolate* isolate = info.GetIsolate(); | 313 v8::Isolate* isolate = info.GetIsolate(); |
295 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 314 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
296 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); | 315 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
297 IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); | 316 if (v.IsEmpty()) |
317 return; | |
318 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | |
319 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | |
320 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | |
298 CJS_Object* pJSObj = | 321 CJS_Object* pJSObj = |
299 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); | 322 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
300 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 323 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
301 v8::String::Utf8Value utf8_value(property); | 324 v8::String::Utf8Value utf8_value(property); |
302 CFX_WideString propname = | 325 CFX_WideString propname = |
303 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 326 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
304 CFX_WideString sError; | 327 CFX_WideString sError; |
305 CJS_PropValue PropValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); | 328 CJS_PropValue PropValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); |
306 PropValue.StartSetting(); | 329 PropValue.StartSetting(); |
307 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { | 330 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { |
308 FXJS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); | 331 FXJS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); |
309 } | 332 } |
310 } | 333 } |
311 | 334 |
312 template <class Alt> | 335 template <class Alt> |
313 void JSSpecialPropDel(const char* class_name, | 336 void JSSpecialPropDel(const char* class_name, |
314 v8::Local<v8::String> property, | 337 v8::Local<v8::String> property, |
315 const v8::PropertyCallbackInfo<v8::Boolean>& info) { | 338 const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
316 v8::Isolate* isolate = info.GetIsolate(); | 339 v8::Isolate* isolate = info.GetIsolate(); |
317 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 340 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
318 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); | 341 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
319 IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); | 342 if (v.IsEmpty()) |
343 return; | |
344 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | |
345 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | |
346 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | |
320 CJS_Object* pJSObj = | 347 CJS_Object* pJSObj = |
321 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); | 348 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
322 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 349 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
323 v8::String::Utf8Value utf8_value(property); | 350 v8::String::Utf8Value utf8_value(property); |
324 CFX_WideString propname = | 351 CFX_WideString propname = |
325 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); | 352 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
326 CFX_WideString sError; | 353 CFX_WideString sError; |
327 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { | 354 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { |
328 CFX_ByteString cbName; | 355 CFX_ByteString cbName; |
329 cbName.Format("%s.%s", class_name, "DelProperty"); | 356 cbName.Format("%s.%s", class_name, "DelProperty"); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 } | 441 } |
415 | 442 |
416 /* ======================================== GLOBAL METHODS | 443 /* ======================================== GLOBAL METHODS |
417 * ============================================ */ | 444 * ============================================ */ |
418 | 445 |
419 template <FX_BOOL ( | 446 template <FX_BOOL ( |
420 *F)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)> | 447 *F)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)> |
421 void JSGlobalFunc(const char* func_name_string, | 448 void JSGlobalFunc(const char* func_name_string, |
422 const v8::FunctionCallbackInfo<v8::Value>& info) { | 449 const v8::FunctionCallbackInfo<v8::Value>& info) { |
423 v8::Isolate* isolate = info.GetIsolate(); | 450 v8::Isolate* isolate = info.GetIsolate(); |
424 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); | 451 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
425 IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); | 452 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
jochen (gone - plz use gerrit)
2015/09/25 11:17:10
if you use GetAlignedPointerFromInternalField / Se
Tom Sepez
2015/09/25 16:40:11
I'll do this in a follow-up patch on master, then
| |
453 if (v.IsEmpty()) | |
454 return; | |
455 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | |
456 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | |
457 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | |
426 CJS_Parameters parameters; | 458 CJS_Parameters parameters; |
427 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { | 459 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
428 parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); | 460 parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); |
429 } | 461 } |
430 CJS_Value valueRes(isolate); | 462 CJS_Value valueRes(isolate); |
431 CFX_WideString sError; | 463 CFX_WideString sError; |
432 if (!(*F)(pRuntimeContext, parameters, valueRes, sError)) { | 464 if (!(*F)(pRuntimeContext, parameters, valueRes, sError)) { |
433 FXJS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError)); | 465 FXJS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError)); |
434 return; | 466 return; |
435 } | 467 } |
(...skipping 22 matching lines...) Expand all Loading... | |
458 for (int i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ | 490 for (int i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ |
459 FXJS_DefineGlobalMethod(pIsolate, \ | 491 FXJS_DefineGlobalMethod(pIsolate, \ |
460 js_class_name::global_methods[i].pName, \ | 492 js_class_name::global_methods[i].pName, \ |
461 js_class_name::global_methods[i].pMethodCall); \ | 493 js_class_name::global_methods[i].pMethodCall); \ |
462 } \ | 494 } \ |
463 } | 495 } |
464 | 496 |
465 CJS_Value::Type GET_VALUE_TYPE(v8::Local<v8::Value> p); | 497 CJS_Value::Type GET_VALUE_TYPE(v8::Local<v8::Value> p); |
466 | 498 |
467 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ | 499 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
OLD | NEW |