| 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 #include "fxjs/include/fxjs_v8.h" | 7 #include "fxjs/include/fxjs_v8.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { | 39 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
| 40 // Note: GetAt() halts if out-of-range even in release builds. | 40 // Note: GetAt() halts if out-of-range even in release builds. |
| 41 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id]; | 41 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id]; |
| 42 } | 42 } |
| 43 | 43 |
| 44 CFXJS_ObjDefinition(v8::Isolate* isolate, | 44 CFXJS_ObjDefinition(v8::Isolate* isolate, |
| 45 const wchar_t* sObjName, | 45 const wchar_t* sObjName, |
| 46 FXJSOBJTYPE eObjType, | 46 FXJSOBJTYPE eObjType, |
| 47 FXJS_CONSTRUCTOR pConstructor, | 47 CFXJS_Engine::Constructor pConstructor, |
| 48 FXJS_DESTRUCTOR pDestructor) | 48 CFXJS_Engine::Destructor pDestructor) |
| 49 : m_ObjName(sObjName), | 49 : m_ObjName(sObjName), |
| 50 m_ObjType(eObjType), | 50 m_ObjType(eObjType), |
| 51 m_pConstructor(pConstructor), | 51 m_pConstructor(pConstructor), |
| 52 m_pDestructor(pDestructor), | 52 m_pDestructor(pDestructor), |
| 53 m_pIsolate(isolate) { | 53 m_pIsolate(isolate) { |
| 54 v8::Isolate::Scope isolate_scope(isolate); | 54 v8::Isolate::Scope isolate_scope(isolate); |
| 55 v8::HandleScope handle_scope(isolate); | 55 v8::HandleScope handle_scope(isolate); |
| 56 | 56 |
| 57 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); | 57 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); |
| 58 fun->InstanceTemplate()->SetInternalFieldCount(2); | 58 fun->InstanceTemplate()->SetInternalFieldCount(2); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 81 return scope.Escape(function->InstanceTemplate()); | 81 return scope.Escape(function->InstanceTemplate()); |
| 82 } | 82 } |
| 83 | 83 |
| 84 v8::Local<v8::Signature> GetSignature() { | 84 v8::Local<v8::Signature> GetSignature() { |
| 85 v8::EscapableHandleScope scope(m_pIsolate); | 85 v8::EscapableHandleScope scope(m_pIsolate); |
| 86 return scope.Escape(m_Signature.Get(m_pIsolate)); | 86 return scope.Escape(m_Signature.Get(m_pIsolate)); |
| 87 } | 87 } |
| 88 | 88 |
| 89 const wchar_t* const m_ObjName; | 89 const wchar_t* const m_ObjName; |
| 90 const FXJSOBJTYPE m_ObjType; | 90 const FXJSOBJTYPE m_ObjType; |
| 91 const FXJS_CONSTRUCTOR m_pConstructor; | 91 const CFXJS_Engine::Constructor m_pConstructor; |
| 92 const FXJS_DESTRUCTOR m_pDestructor; | 92 const CFXJS_Engine::Destructor m_pDestructor; |
| 93 | 93 |
| 94 v8::Isolate* m_pIsolate; | 94 v8::Isolate* m_pIsolate; |
| 95 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; | 95 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; |
| 96 v8::Global<v8::Signature> m_Signature; | 96 v8::Global<v8::Signature> m_Signature; |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( | 99 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
| 100 v8::Isolate* pIsolate) { | 100 v8::Isolate* pIsolate) { |
| 101 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 101 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 102 for (int i = 0; i < maxID; ++i) { | 102 for (int i = 0; i < maxID; ++i) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 128 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { | 128 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
| 129 free(data); | 129 free(data); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, | 132 void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, |
| 133 v8::Global<v8::Object> value, | 133 v8::Global<v8::Object> value, |
| 134 void* key) { | 134 void* key) { |
| 135 v8::Local<v8::Object> obj = value.Get(isolate); | 135 v8::Local<v8::Object> obj = value.Get(isolate); |
| 136 if (obj.IsEmpty()) | 136 if (obj.IsEmpty()) |
| 137 return; | 137 return; |
| 138 int id = FXJS_GetObjDefnID(obj); | 138 CFXJS_Engine* pEngine = CFXJS_Engine::CurrentEngineFromIsolate(isolate); |
| 139 int id = pEngine->GetObjDefnID(obj); |
| 139 if (id == -1) | 140 if (id == -1) |
| 140 return; | 141 return; |
| 141 | |
| 142 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id); | 142 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id); |
| 143 if (!pObjDef) | 143 if (!pObjDef) |
| 144 return; | 144 return; |
| 145 if (pObjDef->m_pDestructor) | 145 if (pObjDef->m_pDestructor) |
| 146 pObjDef->m_pDestructor(obj); | 146 pObjDef->m_pDestructor(pEngine, obj); |
| 147 FXJS_FreePrivate(obj); | 147 CFXJS_Engine::FreeObjectPrivate(obj); |
| 148 } | 148 } |
| 149 | 149 |
| 150 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( | 150 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( |
| 151 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { | 151 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 152 V8TemplateMap* pMap = | 152 V8TemplateMap* pMap = |
| 153 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap; | 153 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap; |
| 154 return pMap ? &pMap->m_map : nullptr; | 154 return pMap ? &pMap->m_map : nullptr; |
| 155 } | 155 } |
| 156 | 156 |
| 157 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { | 157 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 } | 216 } |
| 217 | 217 |
| 218 #ifndef PDF_ENABLE_XFA | 218 #ifndef PDF_ENABLE_XFA |
| 219 FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} | 219 FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} |
| 220 #else // PDF_ENABLE_XFA | 220 #else // PDF_ENABLE_XFA |
| 221 FXJS_PerIsolateData::FXJS_PerIsolateData() | 221 FXJS_PerIsolateData::FXJS_PerIsolateData() |
| 222 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} | 222 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} |
| 223 #endif // PDF_ENABLE_XFA | 223 #endif // PDF_ENABLE_XFA |
| 224 | 224 |
| 225 CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {} | 225 CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {} |
| 226 CFXJS_Engine::~CFXJS_Engine() {} | |
| 227 | 226 |
| 228 int FXJS_DefineObj(v8::Isolate* pIsolate, | 227 CFXJS_Engine::~CFXJS_Engine() { |
| 229 const wchar_t* sObjName, | 228 m_V8PersistentContext.Reset(); |
| 230 FXJSOBJTYPE eObjType, | 229 } |
| 231 FXJS_CONSTRUCTOR pConstructor, | |
| 232 FXJS_DESTRUCTOR pDestructor) { | |
| 233 v8::Isolate::Scope isolate_scope(pIsolate); | |
| 234 v8::HandleScope handle_scope(pIsolate); | |
| 235 | 230 |
| 236 FXJS_PerIsolateData::SetUp(pIsolate); | 231 // static |
| 232 CFXJS_Engine* CFXJS_Engine::CurrentEngineFromIsolate(v8::Isolate* pIsolate) { |
| 233 return static_cast<CFXJS_Engine*>( |
| 234 pIsolate->GetCurrentContext()->GetAlignedPointerFromEmbedderData( |
| 235 kPerContextDataIndex)); |
| 236 } |
| 237 |
| 238 #ifdef PDF_ENABLE_XFA |
| 239 // static |
| 240 void CFXJS_Engine::SetForV8Context(v8::Local<v8::Context> v8Context, |
| 241 CFXJS_Engine* pEngine) { |
| 242 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); |
| 243 } |
| 244 #endif // PDF_ENABLE_XFA |
| 245 |
| 246 // static |
| 247 int CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) { |
| 248 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
| 249 return -1; |
| 250 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
| 251 pObj->GetAlignedPointerFromInternalField(0)); |
| 252 if (!pPerObjectData) |
| 253 return -1; |
| 254 return pPerObjectData->m_ObjDefID; |
| 255 } |
| 256 |
| 257 // static |
| 258 void CFXJS_Engine::FreeObjectPrivate(void* pPerObjectData) { |
| 259 delete static_cast<CFXJS_PerObjectData*>(pPerObjectData); |
| 260 } |
| 261 |
| 262 // static |
| 263 void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) { |
| 264 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
| 265 return; |
| 266 FreeObjectPrivate(pObj->GetAlignedPointerFromInternalField(0)); |
| 267 pObj->SetAlignedPointerInInternalField(0, nullptr); |
| 268 } |
| 269 |
| 270 int CFXJS_Engine::DefineObj(const wchar_t* sObjName, |
| 271 FXJSOBJTYPE eObjType, |
| 272 CFXJS_Engine::Constructor pConstructor, |
| 273 CFXJS_Engine::Destructor pDestructor) { |
| 274 v8::Isolate::Scope isolate_scope(m_isolate); |
| 275 v8::HandleScope handle_scope(m_isolate); |
| 276 FXJS_PerIsolateData::SetUp(m_isolate); |
| 237 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( | 277 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( |
| 238 pIsolate, sObjName, eObjType, pConstructor, pDestructor); | 278 m_isolate, sObjName, eObjType, pConstructor, pDestructor); |
| 239 return pObjDef->AssignID(); | 279 return pObjDef->AssignID(); |
| 240 } | 280 } |
| 241 | 281 |
| 242 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, | 282 void CFXJS_Engine::DefineObjMethod(int nObjDefnID, |
| 243 int nObjDefnID, | 283 const wchar_t* sMethodName, |
| 244 const wchar_t* sMethodName, | 284 v8::FunctionCallback pMethodCall) { |
| 245 v8::FunctionCallback pMethodCall) { | 285 v8::Isolate::Scope isolate_scope(m_isolate); |
| 246 v8::Isolate::Scope isolate_scope(pIsolate); | 286 v8::HandleScope handle_scope(m_isolate); |
| 247 v8::HandleScope handle_scope(pIsolate); | |
| 248 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); | 287 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
| 249 CFXJS_ObjDefinition* pObjDef = | 288 CFXJS_ObjDefinition* pObjDef = |
| 250 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 289 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
| 251 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( | 290 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( |
| 252 pIsolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature()); | 291 m_isolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature()); |
| 253 fun->RemovePrototype(); | 292 fun->RemovePrototype(); |
| 254 pObjDef->GetInstanceTemplate()->Set( | 293 pObjDef->GetInstanceTemplate()->Set( |
| 255 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), | 294 v8::String::NewFromUtf8(m_isolate, bsMethodName.c_str(), |
| 256 v8::NewStringType::kNormal) | 295 v8::NewStringType::kNormal) |
| 257 .ToLocalChecked(), | 296 .ToLocalChecked(), |
| 258 fun, v8::ReadOnly); | 297 fun, v8::ReadOnly); |
| 259 } | 298 } |
| 260 | 299 |
| 261 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, | 300 void CFXJS_Engine::DefineObjProperty(int nObjDefnID, |
| 262 int nObjDefnID, | 301 const wchar_t* sPropName, |
| 263 const wchar_t* sPropName, | 302 v8::AccessorGetterCallback pPropGet, |
| 264 v8::AccessorGetterCallback pPropGet, | 303 v8::AccessorSetterCallback pPropPut) { |
| 265 v8::AccessorSetterCallback pPropPut) { | 304 v8::Isolate::Scope isolate_scope(m_isolate); |
| 266 v8::Isolate::Scope isolate_scope(pIsolate); | 305 v8::HandleScope handle_scope(m_isolate); |
| 267 v8::HandleScope handle_scope(pIsolate); | |
| 268 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); | 306 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); |
| 269 CFXJS_ObjDefinition* pObjDef = | 307 CFXJS_ObjDefinition* pObjDef = |
| 270 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 308 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
| 271 pObjDef->GetInstanceTemplate()->SetAccessor( | 309 pObjDef->GetInstanceTemplate()->SetAccessor( |
| 272 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), | 310 v8::String::NewFromUtf8(m_isolate, bsPropertyName.c_str(), |
| 273 v8::NewStringType::kNormal) | 311 v8::NewStringType::kNormal) |
| 274 .ToLocalChecked(), | 312 .ToLocalChecked(), |
| 275 pPropGet, pPropPut); | 313 pPropGet, pPropPut); |
| 276 } | 314 } |
| 277 | 315 |
| 278 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, | 316 void CFXJS_Engine::DefineObjAllProperties( |
| 279 int nObjDefnID, | 317 int nObjDefnID, |
| 280 v8::NamedPropertyQueryCallback pPropQurey, | 318 v8::NamedPropertyQueryCallback pPropQurey, |
| 281 v8::NamedPropertyGetterCallback pPropGet, | 319 v8::NamedPropertyGetterCallback pPropGet, |
| 282 v8::NamedPropertySetterCallback pPropPut, | 320 v8::NamedPropertySetterCallback pPropPut, |
| 283 v8::NamedPropertyDeleterCallback pPropDel) { | 321 v8::NamedPropertyDeleterCallback pPropDel) { |
| 284 v8::Isolate::Scope isolate_scope(pIsolate); | 322 v8::Isolate::Scope isolate_scope(m_isolate); |
| 285 v8::HandleScope handle_scope(pIsolate); | 323 v8::HandleScope handle_scope(m_isolate); |
| 286 CFXJS_ObjDefinition* pObjDef = | 324 CFXJS_ObjDefinition* pObjDef = |
| 287 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 325 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
| 288 pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut, | 326 pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut, |
| 289 pPropQurey, pPropDel); | 327 pPropQurey, pPropDel); |
| 290 } | 328 } |
| 291 | 329 |
| 292 void FXJS_DefineObjConst(v8::Isolate* pIsolate, | 330 void CFXJS_Engine::DefineObjConst(int nObjDefnID, |
| 293 int nObjDefnID, | 331 const wchar_t* sConstName, |
| 294 const wchar_t* sConstName, | 332 v8::Local<v8::Value> pDefault) { |
| 295 v8::Local<v8::Value> pDefault) { | 333 v8::Isolate::Scope isolate_scope(m_isolate); |
| 296 v8::Isolate::Scope isolate_scope(pIsolate); | 334 v8::HandleScope handle_scope(m_isolate); |
| 297 v8::HandleScope handle_scope(pIsolate); | |
| 298 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); | 335 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); |
| 299 CFXJS_ObjDefinition* pObjDef = | 336 CFXJS_ObjDefinition* pObjDef = |
| 300 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 337 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
| 301 pObjDef->GetInstanceTemplate()->Set(pIsolate, bsConstName.c_str(), pDefault); | 338 pObjDef->GetInstanceTemplate()->Set(m_isolate, bsConstName.c_str(), pDefault); |
| 302 } | 339 } |
| 303 | 340 |
| 304 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, | 341 void CFXJS_Engine::DefineGlobalMethod(const wchar_t* sMethodName, |
| 305 const wchar_t* sMethodName, | 342 v8::FunctionCallback pMethodCall) { |
| 306 v8::FunctionCallback pMethodCall) { | 343 v8::Isolate::Scope isolate_scope(m_isolate); |
| 307 v8::Isolate::Scope isolate_scope(pIsolate); | 344 v8::HandleScope handle_scope(m_isolate); |
| 308 v8::HandleScope handle_scope(pIsolate); | |
| 309 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); | 345 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
| 310 v8::Local<v8::FunctionTemplate> fun = | 346 v8::Local<v8::FunctionTemplate> fun = |
| 311 v8::FunctionTemplate::New(pIsolate, pMethodCall); | 347 v8::FunctionTemplate::New(m_isolate, pMethodCall); |
| 312 fun->RemovePrototype(); | 348 fun->RemovePrototype(); |
| 313 GetGlobalObjectTemplate(pIsolate)->Set( | 349 GetGlobalObjectTemplate(m_isolate)->Set( |
| 314 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), | 350 v8::String::NewFromUtf8(m_isolate, bsMethodName.c_str(), |
| 315 v8::NewStringType::kNormal) | 351 v8::NewStringType::kNormal) |
| 316 .ToLocalChecked(), | 352 .ToLocalChecked(), |
| 317 fun, v8::ReadOnly); | 353 fun, v8::ReadOnly); |
| 318 } | 354 } |
| 319 | 355 |
| 320 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, | 356 void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName, |
| 321 const wchar_t* sConstName, | 357 v8::FunctionCallback pConstGetter) { |
| 322 v8::FunctionCallback pConstGetter) { | 358 v8::Isolate::Scope isolate_scope(m_isolate); |
| 323 v8::Isolate::Scope isolate_scope(pIsolate); | 359 v8::HandleScope handle_scope(m_isolate); |
| 324 v8::HandleScope handle_scope(pIsolate); | |
| 325 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); | 360 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); |
| 326 v8::Local<v8::FunctionTemplate> fun = | 361 v8::Local<v8::FunctionTemplate> fun = |
| 327 v8::FunctionTemplate::New(pIsolate, pConstGetter); | 362 v8::FunctionTemplate::New(m_isolate, pConstGetter); |
| 328 fun->RemovePrototype(); | 363 fun->RemovePrototype(); |
| 329 GetGlobalObjectTemplate(pIsolate)->SetAccessorProperty( | 364 GetGlobalObjectTemplate(m_isolate)->SetAccessorProperty( |
| 330 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), | 365 v8::String::NewFromUtf8(m_isolate, bsConst.c_str(), |
| 331 v8::NewStringType::kNormal) | 366 v8::NewStringType::kNormal) |
| 332 .ToLocalChecked(), | 367 .ToLocalChecked(), |
| 333 fun); | 368 fun); |
| 334 } | 369 } |
| 335 | 370 |
| 336 void FXJS_InitializeEngine( | 371 void CFXJS_Engine::InitializeEngine() { |
| 337 v8::Isolate* pIsolate, | 372 if (m_isolate == g_isolate) |
| 338 CFXJS_Engine* pEngine, | |
| 339 v8::Global<v8::Context>* pV8PersistentContext, | |
| 340 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { | |
| 341 if (pIsolate == g_isolate) | |
| 342 ++g_isolate_ref_count; | 373 ++g_isolate_ref_count; |
| 343 | 374 |
| 344 v8::Isolate::Scope isolate_scope(pIsolate); | 375 v8::Isolate::Scope isolate_scope(m_isolate); |
| 345 v8::HandleScope handle_scope(pIsolate); | 376 v8::HandleScope handle_scope(m_isolate); |
| 346 | 377 |
| 347 // This has to happen before we call GetGlobalObjectTemplate because that | 378 // This has to happen before we call GetGlobalObjectTemplate because that |
| 348 // method gets the PerIsolateData from pIsolate. | 379 // method gets the PerIsolateData from m_isolate. |
| 349 FXJS_PerIsolateData::SetUp(pIsolate); | 380 FXJS_PerIsolateData::SetUp(m_isolate); |
| 350 | 381 |
| 351 v8::Local<v8::Context> v8Context = | 382 v8::Local<v8::Context> v8Context = |
| 352 v8::Context::New(pIsolate, nullptr, GetGlobalObjectTemplate(pIsolate)); | 383 v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate)); |
| 353 v8::Context::Scope context_scope(v8Context); | 384 v8::Context::Scope context_scope(v8Context); |
| 354 | 385 |
| 355 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 386 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate); |
| 356 if (!pData) | 387 if (!pData) |
| 357 return; | 388 return; |
| 358 pData->CreateDynamicObjsMap(pIsolate); | 389 pData->CreateDynamicObjsMap(m_isolate); |
| 359 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); | 390 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this); |
| 360 | 391 |
| 361 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 392 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate); |
| 362 pStaticObjects->resize(maxID + 1); | 393 m_StaticObjects.resize(maxID + 1); |
| 363 for (int i = 0; i < maxID; ++i) { | 394 for (int i = 0; i < maxID; ++i) { |
| 364 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 395 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i); |
| 365 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 396 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 366 v8Context->Global() | 397 v8Context->Global() |
| 367 ->GetPrototype() | 398 ->GetPrototype() |
| 368 ->ToObject(v8Context) | 399 ->ToObject(v8Context) |
| 369 .ToLocalChecked() | 400 .ToLocalChecked() |
| 370 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); | 401 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); |
| 371 | 402 |
| 372 if (pObjDef->m_pConstructor) | 403 if (pObjDef->m_pConstructor) |
| 373 pObjDef->m_pConstructor(pEngine, v8Context->Global() | 404 pObjDef->m_pConstructor(this, v8Context->Global() |
| 374 ->GetPrototype() | 405 ->GetPrototype() |
| 375 ->ToObject(v8Context) | 406 ->ToObject(v8Context) |
| 376 .ToLocalChecked()); | 407 .ToLocalChecked()); |
| 377 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { | 408 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
| 378 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); | 409 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
| 379 v8::Local<v8::String> m_ObjName = | 410 v8::Local<v8::String> m_ObjName = |
| 380 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 411 v8::String::NewFromUtf8(m_isolate, bs.c_str(), |
| 381 v8::NewStringType::kNormal, bs.GetLength()) | 412 v8::NewStringType::kNormal, bs.GetLength()) |
| 382 .ToLocalChecked(); | 413 .ToLocalChecked(); |
| 383 | 414 |
| 384 v8::Local<v8::Object> obj = | 415 v8::Local<v8::Object> obj = NewFxDynamicObj(i, true); |
| 385 FXJS_NewFxDynamicObj(pIsolate, pEngine, i, true); | |
| 386 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); | 416 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
| 387 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); | 417 m_StaticObjects[i] = new v8::Global<v8::Object>(m_isolate, obj); |
| 388 } | 418 } |
| 389 } | 419 } |
| 390 pV8PersistentContext->Reset(pIsolate, v8Context); | 420 m_V8PersistentContext.Reset(m_isolate, v8Context); |
| 391 } | 421 } |
| 392 | 422 |
| 393 void FXJS_ReleaseEngine(v8::Isolate* pIsolate, | 423 void CFXJS_Engine::ReleaseEngine() { |
| 394 v8::Global<v8::Context>* pV8PersistentContext, | 424 v8::Isolate::Scope isolate_scope(m_isolate); |
| 395 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { | 425 v8::HandleScope handle_scope(m_isolate); |
| 396 v8::Isolate::Scope isolate_scope(pIsolate); | |
| 397 v8::HandleScope handle_scope(pIsolate); | |
| 398 v8::Local<v8::Context> context = | 426 v8::Local<v8::Context> context = |
| 399 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); | 427 v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext); |
| 400 v8::Context::Scope context_scope(context); | 428 v8::Context::Scope context_scope(context); |
| 401 | 429 |
| 402 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 430 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate); |
| 403 if (!pData) | 431 if (!pData) |
| 404 return; | 432 return; |
| 405 | 433 |
| 406 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 434 m_ConstArrays.clear(); |
| 435 |
| 436 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate); |
| 407 for (int i = 0; i < maxID; ++i) { | 437 for (int i = 0; i < maxID; ++i) { |
| 408 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 438 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i); |
| 409 v8::Local<v8::Object> pObj; | 439 v8::Local<v8::Object> pObj; |
| 410 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { | 440 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 411 pObj = | 441 pObj = |
| 412 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); | 442 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
| 413 } else if (pStaticObjects->at(i) && !pStaticObjects->at(i)->IsEmpty()) { | 443 } else if (m_StaticObjects[i] && !m_StaticObjects[i]->IsEmpty()) { |
| 414 pObj = v8::Local<v8::Object>::New(pIsolate, *pStaticObjects->at(i)); | 444 pObj = v8::Local<v8::Object>::New(m_isolate, *m_StaticObjects[i]); |
| 415 delete pStaticObjects->at(i); | 445 delete m_StaticObjects[i]; |
| 416 pStaticObjects->at(i) = nullptr; | 446 m_StaticObjects[i] = nullptr; |
| 417 } | 447 } |
| 418 | 448 |
| 419 if (!pObj.IsEmpty()) { | 449 if (!pObj.IsEmpty()) { |
| 420 if (pObjDef->m_pDestructor) | 450 if (pObjDef->m_pDestructor) |
| 421 pObjDef->m_pDestructor(pObj); | 451 pObjDef->m_pDestructor(this, pObj); |
| 422 FXJS_FreePrivate(pObj); | 452 FreeObjectPrivate(pObj); |
| 423 } | 453 } |
| 424 } | 454 } |
| 425 | 455 |
| 426 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) | 456 m_V8PersistentContext.Reset(); |
| 457 |
| 458 if (m_isolate == g_isolate && --g_isolate_ref_count > 0) |
| 427 return; | 459 return; |
| 428 | 460 |
| 429 pData->ReleaseDynamicObjsMap(); | 461 pData->ReleaseDynamicObjsMap(); |
| 430 for (int i = 0; i < maxID; ++i) | 462 for (int i = 0; i < maxID; ++i) |
| 431 delete CFXJS_ObjDefinition::ForID(pIsolate, i); | 463 delete CFXJS_ObjDefinition::ForID(m_isolate, i); |
| 432 | 464 |
| 433 pIsolate->SetData(g_embedderDataSlot, nullptr); | 465 m_isolate->SetData(g_embedderDataSlot, nullptr); |
| 434 delete pData; | 466 delete pData; |
| 435 } | 467 } |
| 436 | 468 |
| 437 CFXJS_Engine* FXJS_GetCurrentEngineFromIsolate(v8::Isolate* pIsolate) { | 469 int CFXJS_Engine::Execute(const CFX_WideString& script, FXJSErr* pError) { |
| 438 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 470 v8::Isolate::Scope isolate_scope(m_isolate); |
| 439 return static_cast<CFXJS_Engine*>( | 471 v8::TryCatch try_catch(m_isolate); |
| 440 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); | |
| 441 } | |
| 442 | |
| 443 #ifdef PDF_ENABLE_XFA | |
| 444 void FXJS_SetEngineForV8Context(v8::Local<v8::Context> v8Context, | |
| 445 CFXJS_Engine* pEngine) { | |
| 446 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); | |
| 447 } | |
| 448 #endif // PDF_ENABLE_XFA | |
| 449 | |
| 450 int FXJS_Execute(v8::Isolate* pIsolate, | |
| 451 const CFX_WideString& script, | |
| 452 FXJSErr* pError) { | |
| 453 v8::Isolate::Scope isolate_scope(pIsolate); | |
| 454 v8::TryCatch try_catch(pIsolate); | |
| 455 CFX_ByteString bsScript = script.UTF8Encode(); | 472 CFX_ByteString bsScript = script.UTF8Encode(); |
| 456 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 473 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 457 v8::Local<v8::Script> compiled_script; | 474 v8::Local<v8::Script> compiled_script; |
| 458 if (!v8::Script::Compile(context, | 475 if (!v8::Script::Compile(context, |
| 459 v8::String::NewFromUtf8(pIsolate, bsScript.c_str(), | 476 v8::String::NewFromUtf8(m_isolate, bsScript.c_str(), |
| 460 v8::NewStringType::kNormal, | 477 v8::NewStringType::kNormal, |
| 461 bsScript.GetLength()) | 478 bsScript.GetLength()) |
| 462 .ToLocalChecked()) | 479 .ToLocalChecked()) |
| 463 .ToLocal(&compiled_script)) { | 480 .ToLocal(&compiled_script)) { |
| 464 v8::String::Utf8Value error(try_catch.Exception()); | 481 v8::String::Utf8Value error(try_catch.Exception()); |
| 465 // TODO(tsepez): return error via pError->message. | 482 // TODO(tsepez): return error via pError->message. |
| 466 return -1; | 483 return -1; |
| 467 } | 484 } |
| 468 | 485 |
| 469 v8::Local<v8::Value> result; | 486 v8::Local<v8::Value> result; |
| 470 if (!compiled_script->Run(context).ToLocal(&result)) { | 487 if (!compiled_script->Run(context).ToLocal(&result)) { |
| 471 v8::String::Utf8Value error(try_catch.Exception()); | 488 v8::String::Utf8Value error(try_catch.Exception()); |
| 472 // TODO(tsepez): return error via pError->message. | 489 // TODO(tsepez): return error via pError->message. |
| 473 return -1; | 490 return -1; |
| 474 } | 491 } |
| 475 return 0; | 492 return 0; |
| 476 } | 493 } |
| 477 | 494 |
| 478 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 495 v8::Local<v8::Object> CFXJS_Engine::NewFxDynamicObj(int nObjDefnID, |
| 479 CFXJS_Engine* pEngine, | 496 bool bStatic) { |
| 480 int nObjDefnID, | 497 v8::Isolate::Scope isolate_scope(m_isolate); |
| 481 bool bStatic) { | 498 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 482 v8::Isolate::Scope isolate_scope(pIsolate); | |
| 483 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | |
| 484 if (nObjDefnID == -1) { | 499 if (nObjDefnID == -1) { |
| 485 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 500 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(m_isolate); |
| 486 v8::Local<v8::Object> obj; | 501 v8::Local<v8::Object> obj; |
| 487 if (!objTempl->NewInstance(context).ToLocal(&obj)) | 502 if (!objTempl->NewInstance(context).ToLocal(&obj)) |
| 488 return v8::Local<v8::Object>(); | 503 return v8::Local<v8::Object>(); |
| 489 return obj; | 504 return obj; |
| 490 } | 505 } |
| 491 | 506 |
| 492 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 507 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate); |
| 493 if (!pData) | 508 if (!pData) |
| 494 return v8::Local<v8::Object>(); | 509 return v8::Local<v8::Object>(); |
| 495 | 510 |
| 496 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 511 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(m_isolate)) |
| 497 return v8::Local<v8::Object>(); | 512 return v8::Local<v8::Object>(); |
| 498 | 513 |
| 499 CFXJS_ObjDefinition* pObjDef = | 514 CFXJS_ObjDefinition* pObjDef = |
| 500 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 515 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
| 501 v8::Local<v8::Object> obj; | 516 v8::Local<v8::Object> obj; |
| 502 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) | 517 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
| 503 return v8::Local<v8::Object>(); | 518 return v8::Local<v8::Object>(); |
| 504 | 519 |
| 505 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); | 520 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); |
| 506 obj->SetAlignedPointerInInternalField(0, pPerObjData); | 521 obj->SetAlignedPointerInInternalField(0, pPerObjData); |
| 507 if (pObjDef->m_pConstructor) | 522 if (pObjDef->m_pConstructor) |
| 508 pObjDef->m_pConstructor(pEngine, obj); | 523 pObjDef->m_pConstructor(this, obj); |
| 509 | 524 |
| 510 if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap) { | 525 if (!bStatic && FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap) { |
| 511 FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap->set(pPerObjData, | 526 FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap->set(pPerObjData, |
| 512 obj); | 527 obj); |
| 513 } | 528 } |
| 514 return obj; | 529 return obj; |
| 515 } | 530 } |
| 516 | 531 |
| 517 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { | 532 v8::Local<v8::Object> CFXJS_Engine::GetThisObj() { |
| 518 v8::Isolate::Scope isolate_scope(pIsolate); | 533 v8::Isolate::Scope isolate_scope(m_isolate); |
| 519 if (!FXJS_PerIsolateData::Get(pIsolate)) | 534 if (!FXJS_PerIsolateData::Get(m_isolate)) |
| 520 return v8::Local<v8::Object>(); | 535 return v8::Local<v8::Object>(); |
| 521 | 536 |
| 522 // Return the global object. | 537 // Return the global object. |
| 523 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 538 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 524 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); | 539 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
| 525 } | 540 } |
| 526 | 541 |
| 527 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) { | 542 void CFXJS_Engine::Error(const CFX_WideString& message) { |
| 528 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) | |
| 529 return -1; | |
| 530 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( | |
| 531 pObj->GetAlignedPointerFromInternalField(0)); | |
| 532 if (pPerObjectData) | |
| 533 return pPerObjectData->m_ObjDefID; | |
| 534 return -1; | |
| 535 } | |
| 536 | |
| 537 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { | |
| 538 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t | 543 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t |
| 539 // wide-strings isn't handled by v8, so use UTF8 as a common | 544 // wide-strings isn't handled by v8, so use UTF8 as a common |
| 540 // intermediate format. | 545 // intermediate format. |
| 541 CFX_ByteString utf8_message = message.UTF8Encode(); | 546 CFX_ByteString utf8_message = message.UTF8Encode(); |
| 542 pIsolate->ThrowException(v8::String::NewFromUtf8(pIsolate, | 547 m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate, |
| 543 utf8_message.c_str(), | 548 utf8_message.c_str(), |
| 544 v8::NewStringType::kNormal) | 549 v8::NewStringType::kNormal) |
| 545 .ToLocalChecked()); | 550 .ToLocalChecked()); |
| 546 } | 551 } |
| 547 | 552 |
| 548 void FXJS_SetPrivate(v8::Isolate* pIsolate, | 553 void CFXJS_Engine::SetObjectPrivate(v8::Local<v8::Object> pObj, void* p) { |
| 549 v8::Local<v8::Object> pObj, | |
| 550 void* p) { | |
| 551 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) | 554 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
| 552 return; | 555 return; |
| 553 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( | 556 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
| 554 pObj->GetAlignedPointerFromInternalField(0)); | 557 pObj->GetAlignedPointerFromInternalField(0)); |
| 555 if (!pPerObjectData) | 558 if (!pPerObjectData) |
| 556 return; | 559 return; |
| 557 pPerObjectData->m_pPrivate = p; | 560 pPerObjectData->m_pPrivate = p; |
| 558 } | 561 } |
| 559 | 562 |
| 560 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) { | 563 void* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) { |
| 561 if (pObj.IsEmpty()) | 564 if (pObj.IsEmpty()) |
| 562 return nullptr; | 565 return nullptr; |
| 563 CFXJS_PerObjectData* pPerObjectData = nullptr; | 566 CFXJS_PerObjectData* pPerObjectData = nullptr; |
| 564 if (pObj->InternalFieldCount()) { | 567 if (pObj->InternalFieldCount()) { |
| 565 pPerObjectData = static_cast<CFXJS_PerObjectData*>( | 568 pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
| 566 pObj->GetAlignedPointerFromInternalField(0)); | 569 pObj->GetAlignedPointerFromInternalField(0)); |
| 567 } else { | 570 } else { |
| 568 // It could be a global proxy object. | 571 // It could be a global proxy object. |
| 569 v8::Local<v8::Value> v = pObj->GetPrototype(); | 572 v8::Local<v8::Value> v = pObj->GetPrototype(); |
| 570 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 573 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 571 if (v->IsObject()) { | 574 if (v->IsObject()) { |
| 572 pPerObjectData = static_cast<CFXJS_PerObjectData*>( | 575 pPerObjectData = static_cast<CFXJS_PerObjectData*>( |
| 573 v->ToObject(context) | 576 v->ToObject(context) |
| 574 .ToLocalChecked() | 577 .ToLocalChecked() |
| 575 ->GetAlignedPointerFromInternalField(0)); | 578 ->GetAlignedPointerFromInternalField(0)); |
| 576 } | 579 } |
| 577 } | 580 } |
| 578 return pPerObjectData ? pPerObjectData->m_pPrivate : nullptr; | 581 return pPerObjectData ? pPerObjectData->m_pPrivate : nullptr; |
| 579 } | 582 } |
| 580 | 583 |
| 581 void FXJS_FreePrivate(void* pPerObjectData) { | 584 v8::Local<v8::String> CFXJS_Engine::WSToJSString( |
| 582 delete static_cast<CFXJS_PerObjectData*>(pPerObjectData); | 585 const CFX_WideString& wsPropertyName) { |
| 583 } | 586 v8::Isolate* pIsolate = m_isolate ? m_isolate : v8::Isolate::GetCurrent(); |
| 584 | |
| 585 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) { | |
| 586 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) | |
| 587 return; | |
| 588 FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0)); | |
| 589 pObj->SetAlignedPointerInInternalField(0, nullptr); | |
| 590 } | |
| 591 | |
| 592 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate, | |
| 593 const CFX_WideString& wsPropertyName) { | |
| 594 CFX_ByteString bs = wsPropertyName.UTF8Encode(); | 587 CFX_ByteString bs = wsPropertyName.UTF8Encode(); |
| 595 if (!pIsolate) | |
| 596 pIsolate = v8::Isolate::GetCurrent(); | |
| 597 return v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 588 return v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
| 598 v8::NewStringType::kNormal, bs.GetLength()) | 589 v8::NewStringType::kNormal, bs.GetLength()) |
| 599 .ToLocalChecked(); | 590 .ToLocalChecked(); |
| 600 } | 591 } |
| 601 | 592 |
| 602 v8::Local<v8::Value> FXJS_GetObjectProperty( | 593 v8::Local<v8::Value> CFXJS_Engine::GetObjectProperty( |
| 603 v8::Isolate* pIsolate, | |
| 604 v8::Local<v8::Object> pObj, | 594 v8::Local<v8::Object> pObj, |
| 605 const CFX_WideString& wsPropertyName) { | 595 const CFX_WideString& wsPropertyName) { |
| 606 if (pObj.IsEmpty()) | 596 if (pObj.IsEmpty()) |
| 607 return v8::Local<v8::Value>(); | 597 return v8::Local<v8::Value>(); |
| 608 v8::Local<v8::Value> val; | 598 v8::Local<v8::Value> val; |
| 609 if (!pObj->Get(pIsolate->GetCurrentContext(), | 599 if (!pObj->Get(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName)) |
| 610 FXJS_WSToJSString(pIsolate, wsPropertyName)) | |
| 611 .ToLocal(&val)) | 600 .ToLocal(&val)) |
| 612 return v8::Local<v8::Value>(); | 601 return v8::Local<v8::Value>(); |
| 613 return val; | 602 return val; |
| 614 } | 603 } |
| 615 | 604 |
| 616 std::vector<CFX_WideString> FXJS_GetObjectPropertyNames( | 605 std::vector<CFX_WideString> CFXJS_Engine::GetObjectPropertyNames( |
| 617 v8::Isolate* pIsolate, | |
| 618 v8::Local<v8::Object> pObj) { | 606 v8::Local<v8::Object> pObj) { |
| 619 if (pObj.IsEmpty()) | 607 if (pObj.IsEmpty()) |
| 620 return std::vector<CFX_WideString>(); | 608 return std::vector<CFX_WideString>(); |
| 621 | 609 |
| 622 v8::Local<v8::Array> val; | 610 v8::Local<v8::Array> val; |
| 623 if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val)) | 611 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 612 if (!pObj->GetPropertyNames(context).ToLocal(&val)) |
| 624 return std::vector<CFX_WideString>(); | 613 return std::vector<CFX_WideString>(); |
| 625 | 614 |
| 626 std::vector<CFX_WideString> result; | 615 std::vector<CFX_WideString> result; |
| 627 for (uint32_t i = 0; i < val->Length(); ++i) { | 616 for (uint32_t i = 0; i < val->Length(); ++i) { |
| 628 result.push_back(FXJS_ToString( | 617 result.push_back(ToString(val->Get(context, i).ToLocalChecked())); |
| 629 pIsolate, val->Get(pIsolate->GetCurrentContext(), i).ToLocalChecked())); | |
| 630 } | 618 } |
| 631 | 619 |
| 632 return result; | 620 return result; |
| 633 } | 621 } |
| 634 | 622 |
| 635 void FXJS_PutObjectString(v8::Isolate* pIsolate, | 623 void CFXJS_Engine::PutObjectString(v8::Local<v8::Object> pObj, |
| 636 v8::Local<v8::Object> pObj, | 624 const CFX_WideString& wsPropertyName, |
| 637 const CFX_WideString& wsPropertyName, | 625 const CFX_WideString& wsValue) { |
| 638 const CFX_WideString& wsValue) { | 626 if (pObj.IsEmpty()) |
| 639 if (pObj.IsEmpty()) | 627 return; |
| 640 return; | 628 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), |
| 641 pObj->Set(pIsolate->GetCurrentContext(), | 629 WSToJSString(wsValue)) |
| 642 FXJS_WSToJSString(pIsolate, wsPropertyName), | 630 .FromJust(); |
| 643 FXJS_WSToJSString(pIsolate, wsValue)) | 631 } |
| 644 .FromJust(); | 632 |
| 645 } | 633 void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj, |
| 646 | 634 const CFX_WideString& wsPropertyName, |
| 647 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | 635 int nValue) { |
| 648 v8::Local<v8::Object> pObj, | 636 if (pObj.IsEmpty()) |
| 649 const CFX_WideString& wsPropertyName, | 637 return; |
| 650 int nValue) { | 638 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), |
| 651 if (pObj.IsEmpty()) | 639 v8::Int32::New(m_isolate, nValue)) |
| 652 return; | 640 .FromJust(); |
| 653 pObj->Set(pIsolate->GetCurrentContext(), | 641 } |
| 654 FXJS_WSToJSString(pIsolate, wsPropertyName), | 642 |
| 655 v8::Int32::New(pIsolate, nValue)) | 643 void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj, |
| 656 .FromJust(); | 644 const CFX_WideString& wsPropertyName, |
| 657 } | 645 float fValue) { |
| 658 | 646 if (pObj.IsEmpty()) |
| 659 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | 647 return; |
| 660 v8::Local<v8::Object> pObj, | 648 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), |
| 661 const CFX_WideString& wsPropertyName, | 649 v8::Number::New(m_isolate, (double)fValue)) |
| 662 float fValue) { | 650 .FromJust(); |
| 663 if (pObj.IsEmpty()) | 651 } |
| 664 return; | 652 |
| 665 pObj->Set(pIsolate->GetCurrentContext(), | 653 void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj, |
| 666 FXJS_WSToJSString(pIsolate, wsPropertyName), | 654 const CFX_WideString& wsPropertyName, |
| 667 v8::Number::New(pIsolate, (double)fValue)) | 655 double dValue) { |
| 668 .FromJust(); | 656 if (pObj.IsEmpty()) |
| 669 } | 657 return; |
| 670 | 658 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), |
| 671 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | 659 v8::Number::New(m_isolate, (double)dValue)) |
| 672 v8::Local<v8::Object> pObj, | 660 .FromJust(); |
| 673 const CFX_WideString& wsPropertyName, | 661 } |
| 674 double dValue) { | 662 |
| 675 if (pObj.IsEmpty()) | 663 void CFXJS_Engine::PutObjectBoolean(v8::Local<v8::Object> pObj, |
| 676 return; | 664 const CFX_WideString& wsPropertyName, |
| 677 pObj->Set(pIsolate->GetCurrentContext(), | 665 bool bValue) { |
| 678 FXJS_WSToJSString(pIsolate, wsPropertyName), | 666 if (pObj.IsEmpty()) |
| 679 v8::Number::New(pIsolate, (double)dValue)) | 667 return; |
| 680 .FromJust(); | 668 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), |
| 681 } | 669 v8::Boolean::New(m_isolate, bValue)) |
| 682 | 670 .FromJust(); |
| 683 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate, | 671 } |
| 684 v8::Local<v8::Object> pObj, | 672 |
| 685 const CFX_WideString& wsPropertyName, | 673 void CFXJS_Engine::PutObjectObject(v8::Local<v8::Object> pObj, |
| 686 bool bValue) { | 674 const CFX_WideString& wsPropertyName, |
| 687 if (pObj.IsEmpty()) | 675 v8::Local<v8::Object> pPut) { |
| 688 return; | 676 if (pObj.IsEmpty()) |
| 689 pObj->Set(pIsolate->GetCurrentContext(), | 677 return; |
| 690 FXJS_WSToJSString(pIsolate, wsPropertyName), | 678 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), pPut) |
| 691 v8::Boolean::New(pIsolate, bValue)) | 679 .FromJust(); |
| 692 .FromJust(); | 680 } |
| 693 } | 681 |
| 694 | 682 void CFXJS_Engine::PutObjectNull(v8::Local<v8::Object> pObj, |
| 695 void FXJS_PutObjectObject(v8::Isolate* pIsolate, | 683 const CFX_WideString& wsPropertyName) { |
| 696 v8::Local<v8::Object> pObj, | 684 if (pObj.IsEmpty()) |
| 697 const CFX_WideString& wsPropertyName, | 685 return; |
| 698 v8::Local<v8::Object> pPut) { | 686 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), |
| 699 if (pObj.IsEmpty()) | |
| 700 return; | |
| 701 pObj->Set(pIsolate->GetCurrentContext(), | |
| 702 FXJS_WSToJSString(pIsolate, wsPropertyName), pPut) | |
| 703 .FromJust(); | |
| 704 } | |
| 705 | |
| 706 void FXJS_PutObjectNull(v8::Isolate* pIsolate, | |
| 707 v8::Local<v8::Object> pObj, | |
| 708 const CFX_WideString& wsPropertyName) { | |
| 709 if (pObj.IsEmpty()) | |
| 710 return; | |
| 711 pObj->Set(pIsolate->GetCurrentContext(), | |
| 712 FXJS_WSToJSString(pIsolate, wsPropertyName), | |
| 713 v8::Local<v8::Object>()) | 687 v8::Local<v8::Object>()) |
| 714 .FromJust(); | 688 .FromJust(); |
| 715 } | 689 } |
| 716 | 690 |
| 717 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate) { | 691 v8::Local<v8::Array> CFXJS_Engine::NewArray() { |
| 718 return v8::Array::New(pIsolate); | 692 return v8::Array::New(m_isolate); |
| 719 } | 693 } |
| 720 | 694 |
| 721 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate, | 695 unsigned CFXJS_Engine::PutArrayElement(v8::Local<v8::Array> pArray, |
| 722 v8::Local<v8::Array> pArray, | 696 unsigned index, |
| 723 unsigned index, | 697 v8::Local<v8::Value> pValue) { |
| 724 v8::Local<v8::Value> pValue) { | |
| 725 if (pArray.IsEmpty()) | 698 if (pArray.IsEmpty()) |
| 726 return 0; | 699 return 0; |
| 727 if (pArray->Set(pIsolate->GetCurrentContext(), index, pValue).IsNothing()) | 700 if (pArray->Set(m_isolate->GetCurrentContext(), index, pValue).IsNothing()) |
| 728 return 0; | 701 return 0; |
| 729 return 1; | 702 return 1; |
| 730 } | 703 } |
| 731 | 704 |
| 732 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate, | 705 v8::Local<v8::Value> CFXJS_Engine::GetArrayElement(v8::Local<v8::Array> pArray, |
| 733 v8::Local<v8::Array> pArray, | 706 unsigned index) { |
| 734 unsigned index) { | |
| 735 if (pArray.IsEmpty()) | 707 if (pArray.IsEmpty()) |
| 736 return v8::Local<v8::Value>(); | 708 return v8::Local<v8::Value>(); |
| 737 v8::Local<v8::Value> val; | 709 v8::Local<v8::Value> val; |
| 738 if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val)) | 710 if (!pArray->Get(m_isolate->GetCurrentContext(), index).ToLocal(&val)) |
| 739 return v8::Local<v8::Value>(); | 711 return v8::Local<v8::Value>(); |
| 740 return val; | 712 return val; |
| 741 } | 713 } |
| 742 | 714 |
| 743 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray) { | 715 unsigned CFXJS_Engine::GetArrayLength(v8::Local<v8::Array> pArray) { |
| 744 if (pArray.IsEmpty()) | 716 if (pArray.IsEmpty()) |
| 745 return 0; | 717 return 0; |
| 746 return pArray->Length(); | 718 return pArray->Length(); |
| 747 } | 719 } |
| 748 | 720 |
| 749 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number) { | 721 v8::Local<v8::Context> CFXJS_Engine::NewLocalContext() { |
| 750 return v8::Int32::New(pIsolate, number); | 722 return v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext); |
| 751 } | 723 } |
| 752 | 724 |
| 753 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number) { | 725 v8::Local<v8::Context> CFXJS_Engine::GetPersistentContext() { |
| 754 return v8::Number::New(pIsolate, number); | 726 return m_V8PersistentContext.Get(m_isolate); |
| 755 } | 727 } |
| 756 | 728 |
| 757 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number) { | 729 v8::Local<v8::Value> CFXJS_Engine::NewNumber(int number) { |
| 758 return v8::Number::New(pIsolate, (float)number); | 730 return v8::Int32::New(m_isolate, number); |
| 759 } | 731 } |
| 760 | 732 |
| 761 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b) { | 733 v8::Local<v8::Value> CFXJS_Engine::NewNumber(double number) { |
| 762 return v8::Boolean::New(pIsolate, b); | 734 return v8::Number::New(m_isolate, number); |
| 763 } | 735 } |
| 764 | 736 |
| 765 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate, const wchar_t* str) { | 737 v8::Local<v8::Value> CFXJS_Engine::NewNumber(float number) { |
| 766 return FXJS_WSToJSString(pIsolate, str); | 738 return v8::Number::New(m_isolate, (float)number); |
| 767 } | 739 } |
| 768 | 740 |
| 769 v8::Local<v8::Value> FXJS_NewNull(v8::Isolate* pIsolate) { | 741 v8::Local<v8::Value> CFXJS_Engine::NewBoolean(bool b) { |
| 742 return v8::Boolean::New(m_isolate, b); |
| 743 } |
| 744 |
| 745 v8::Local<v8::Value> CFXJS_Engine::NewString(const wchar_t* str) { |
| 746 return WSToJSString(str); |
| 747 } |
| 748 |
| 749 v8::Local<v8::Value> CFXJS_Engine::NewNull() { |
| 770 return v8::Local<v8::Value>(); | 750 return v8::Local<v8::Value>(); |
| 771 } | 751 } |
| 772 | 752 |
| 773 v8::Local<v8::Date> FXJS_NewDate(v8::Isolate* pIsolate, double d) { | 753 v8::Local<v8::Date> CFXJS_Engine::NewDate(double d) { |
| 774 return v8::Date::New(pIsolate->GetCurrentContext(), d) | 754 return v8::Date::New(m_isolate->GetCurrentContext(), d) |
| 775 .ToLocalChecked() | 755 .ToLocalChecked() |
| 776 .As<v8::Date>(); | 756 .As<v8::Date>(); |
| 777 } | 757 } |
| 778 | 758 |
| 779 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) { | 759 int CFXJS_Engine::ToInt32(v8::Local<v8::Value> pValue) { |
| 780 if (pValue.IsEmpty()) | 760 if (pValue.IsEmpty()) |
| 781 return 0; | 761 return 0; |
| 782 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 762 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 783 return pValue->ToInt32(context).ToLocalChecked()->Value(); | 763 return pValue->ToInt32(context).ToLocalChecked()->Value(); |
| 784 } | 764 } |
| 785 | 765 |
| 786 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) { | 766 bool CFXJS_Engine::ToBoolean(v8::Local<v8::Value> pValue) { |
| 787 if (pValue.IsEmpty()) | 767 if (pValue.IsEmpty()) |
| 788 return false; | 768 return false; |
| 789 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 769 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 790 return pValue->ToBoolean(context).ToLocalChecked()->Value(); | 770 return pValue->ToBoolean(context).ToLocalChecked()->Value(); |
| 791 } | 771 } |
| 792 | 772 |
| 793 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) { | 773 double CFXJS_Engine::ToNumber(v8::Local<v8::Value> pValue) { |
| 794 if (pValue.IsEmpty()) | 774 if (pValue.IsEmpty()) |
| 795 return 0.0; | 775 return 0.0; |
| 796 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 776 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 797 return pValue->ToNumber(context).ToLocalChecked()->Value(); | 777 return pValue->ToNumber(context).ToLocalChecked()->Value(); |
| 798 } | 778 } |
| 799 | 779 |
| 800 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, | 780 v8::Local<v8::Object> CFXJS_Engine::ToObject(v8::Local<v8::Value> pValue) { |
| 801 v8::Local<v8::Value> pValue) { | |
| 802 if (pValue.IsEmpty()) | 781 if (pValue.IsEmpty()) |
| 803 return v8::Local<v8::Object>(); | 782 return v8::Local<v8::Object>(); |
| 804 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 783 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 805 return pValue->ToObject(context).ToLocalChecked(); | 784 return pValue->ToObject(context).ToLocalChecked(); |
| 806 } | 785 } |
| 807 | 786 |
| 808 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, | 787 CFX_WideString CFXJS_Engine::ToString(v8::Local<v8::Value> pValue) { |
| 809 v8::Local<v8::Value> pValue) { | |
| 810 if (pValue.IsEmpty()) | 788 if (pValue.IsEmpty()) |
| 811 return L""; | 789 return L""; |
| 812 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 790 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 813 v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked()); | 791 v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked()); |
| 814 return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length())); | 792 return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length())); |
| 815 } | 793 } |
| 816 | 794 |
| 817 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, | 795 v8::Local<v8::Array> CFXJS_Engine::ToArray(v8::Local<v8::Value> pValue) { |
| 818 v8::Local<v8::Value> pValue) { | |
| 819 if (pValue.IsEmpty()) | 796 if (pValue.IsEmpty()) |
| 820 return v8::Local<v8::Array>(); | 797 return v8::Local<v8::Array>(); |
| 821 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 798 v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 822 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 799 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
| 823 } | 800 } |
| 801 |
| 802 void CFXJS_Engine::SetConstArray(const CFX_WideString& name, |
| 803 v8::Local<v8::Array> array) { |
| 804 m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array); |
| 805 } |
| 806 |
| 807 v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const CFX_WideString& name) { |
| 808 return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]); |
| 809 } |
| OLD | NEW |