| 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 "../../../core/include/fxcrt/fx_basic.h" | 7 #include "../../../core/include/fxcrt/fx_basic.h" |
| 8 #include "../../include/jsapi/fxjs_v8.h" | 8 #include "../../include/jsapi/fxjs_v8.h" |
| 9 | 9 |
| 10 const wchar_t kFXJSValueNameString[] = L"string"; | 10 const wchar_t kFXJSValueNameString[] = L"string"; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { | 43 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
| 44 // Note: GetAt() halts if out-of-range even in release builds. | 44 // Note: GetAt() halts if out-of-range even in release builds. |
| 45 return static_cast<CFXJS_ObjDefinition*>( | 45 return static_cast<CFXJS_ObjDefinition*>( |
| 46 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); | 46 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); |
| 47 } | 47 } |
| 48 CFXJS_ObjDefinition(v8::Isolate* isolate, | 48 CFXJS_ObjDefinition(v8::Isolate* isolate, |
| 49 const wchar_t* sObjName, | 49 const wchar_t* sObjName, |
| 50 FXJSOBJTYPE eObjType, | 50 FXJSOBJTYPE eObjType, |
| 51 FXJS_CONSTRUCTOR pConstructor, | 51 FXJS_CONSTRUCTOR pConstructor, |
| 52 FXJS_DESTRUCTOR pDestructor) | 52 FXJS_DESTRUCTOR pDestructor) |
| 53 : objName(sObjName), | 53 : m_ObjName(sObjName), |
| 54 objType(eObjType), | 54 m_ObjType(eObjType), |
| 55 m_pConstructor(pConstructor), | 55 m_pConstructor(pConstructor), |
| 56 m_pDestructor(pDestructor), | 56 m_pDestructor(pDestructor), |
| 57 m_bSetAsGlobalObject(FALSE), | |
| 58 m_pIsolate(isolate) { | 57 m_pIsolate(isolate) { |
| 59 v8::Isolate::Scope isolate_scope(isolate); | 58 v8::Isolate::Scope isolate_scope(isolate); |
| 60 v8::HandleScope handle_scope(isolate); | 59 v8::HandleScope handle_scope(isolate); |
| 61 | 60 |
| 62 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); | 61 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); |
| 63 fun->InstanceTemplate()->SetInternalFieldCount(2); | 62 fun->InstanceTemplate()->SetInternalFieldCount(2); |
| 64 m_FunctionTemplate.Reset(isolate, fun); | 63 m_FunctionTemplate.Reset(isolate, fun); |
| 65 | 64 |
| 66 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); | 65 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); |
| 67 m_Signature.Reset(isolate, sig); | 66 m_Signature.Reset(isolate, sig); |
| 68 | |
| 69 // Document as the global object. | |
| 70 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { | |
| 71 m_bSetAsGlobalObject = TRUE; | |
| 72 } | |
| 73 } | 67 } |
| 74 | 68 |
| 75 int AssignID() { | 69 int AssignID() { |
| 76 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); | 70 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); |
| 77 pData->m_ObjectDefnArray.Add(this); | 71 pData->m_ObjectDefnArray.Add(this); |
| 78 return pData->m_ObjectDefnArray.GetSize() - 1; | 72 return pData->m_ObjectDefnArray.GetSize() - 1; |
| 79 } | 73 } |
| 80 | 74 |
| 81 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { | 75 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { |
| 82 v8::EscapableHandleScope scope(m_pIsolate); | 76 v8::EscapableHandleScope scope(m_pIsolate); |
| 83 v8::Local<v8::FunctionTemplate> function = | 77 v8::Local<v8::FunctionTemplate> function = |
| 84 m_FunctionTemplate.Get(m_pIsolate); | 78 m_FunctionTemplate.Get(m_pIsolate); |
| 85 return scope.Escape(function->InstanceTemplate()); | 79 return scope.Escape(function->InstanceTemplate()); |
| 86 } | 80 } |
| 87 | 81 |
| 88 v8::Local<v8::Signature> GetSignature() { | 82 v8::Local<v8::Signature> GetSignature() { |
| 89 v8::EscapableHandleScope scope(m_pIsolate); | 83 v8::EscapableHandleScope scope(m_pIsolate); |
| 90 return scope.Escape(m_Signature.Get(m_pIsolate)); | 84 return scope.Escape(m_Signature.Get(m_pIsolate)); |
| 91 } | 85 } |
| 92 | 86 |
| 93 const wchar_t* objName; | 87 const wchar_t* const m_ObjName; |
| 94 const FXJSOBJTYPE objType; | 88 const FXJSOBJTYPE m_ObjType; |
| 95 const FXJS_CONSTRUCTOR m_pConstructor; | 89 const FXJS_CONSTRUCTOR m_pConstructor; |
| 96 const FXJS_DESTRUCTOR m_pDestructor; | 90 const FXJS_DESTRUCTOR m_pDestructor; |
| 97 FX_BOOL m_bSetAsGlobalObject; | |
| 98 | 91 |
| 99 v8::Isolate* m_pIsolate; | 92 v8::Isolate* m_pIsolate; |
| 100 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; | 93 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; |
| 101 v8::Global<v8::Signature> m_Signature; | 94 v8::Global<v8::Signature> m_Signature; |
| 102 v8::Global<v8::Object> m_StaticObj; | 95 v8::Global<v8::Object> m_StaticObj; |
| 103 }; | 96 }; |
| 104 | 97 |
| 105 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( | 98 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
| 106 v8::Isolate* pIsolate) { | 99 v8::Isolate* pIsolate) { |
| 107 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 100 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 108 for (int i = 0; i < maxID; ++i) { | 101 for (int i = 0; i < maxID; ++i) { |
| 109 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 102 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 110 if (pObjDef->m_bSetAsGlobalObject) | 103 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) |
| 111 return pObjDef->GetInstanceTemplate(); | 104 return pObjDef->GetInstanceTemplate(); |
| 112 } | 105 } |
| 113 | |
| 114 if (!g_DefaultGlobalObjectTemplate) { | 106 if (!g_DefaultGlobalObjectTemplate) { |
| 115 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; | 107 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; |
| 116 g_DefaultGlobalObjectTemplate->Reset(pIsolate, | 108 g_DefaultGlobalObjectTemplate->Reset(pIsolate, |
| 117 v8::ObjectTemplate::New(pIsolate)); | 109 v8::ObjectTemplate::New(pIsolate)); |
| 118 } | 110 } |
| 119 return g_DefaultGlobalObjectTemplate->Get(pIsolate); | 111 return g_DefaultGlobalObjectTemplate->Get(pIsolate); |
| 120 } | 112 } |
| 121 | 113 |
| 122 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { | 114 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { |
| 123 return calloc(1, length); | 115 return calloc(1, length); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 v8::Local<v8::Context> v8Context = | 249 v8::Local<v8::Context> v8Context = |
| 258 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 250 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
| 259 v8::Context::Scope context_scope(v8Context); | 251 v8::Context::Scope context_scope(v8Context); |
| 260 | 252 |
| 261 FXJS_PerIsolateData::SetUp(pIsolate); | 253 FXJS_PerIsolateData::SetUp(pIsolate); |
| 262 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); | 254 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); |
| 263 | 255 |
| 264 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 256 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 265 for (int i = 0; i < maxID; ++i) { | 257 for (int i = 0; i < maxID; ++i) { |
| 266 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 258 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 267 CFX_WideString ws = CFX_WideString(pObjDef->objName); | 259 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
| 268 CFX_ByteString bs = ws.UTF8Encode(); | 260 v8::Local<v8::String> m_ObjName = |
| 269 v8::Local<v8::String> objName = | |
| 270 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 261 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
| 271 v8::NewStringType::kNormal, | 262 v8::NewStringType::kNormal, |
| 272 bs.GetLength()).ToLocalChecked(); | 263 bs.GetLength()).ToLocalChecked(); |
| 273 | 264 |
| 274 if (pObjDef->objType == FXJS_DYNAMIC) { | 265 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 275 // Document is set as global object, need to construct it first. | 266 v8Context->Global() |
| 276 if (ws.Equal(L"Document")) { | 267 ->GetPrototype() |
| 277 v8Context->Global() | 268 ->ToObject(v8Context) |
| 278 ->GetPrototype() | 269 .ToLocalChecked() |
| 279 ->ToObject(v8Context) | 270 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); |
| 280 .ToLocalChecked() | |
| 281 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); | |
| 282 | 271 |
| 283 if (pObjDef->m_pConstructor) | 272 if (pObjDef->m_pConstructor) |
| 284 pObjDef->m_pConstructor(context, v8Context->Global() | 273 pObjDef->m_pConstructor(context, v8Context->Global() |
| 285 ->GetPrototype() | 274 ->GetPrototype() |
| 286 ->ToObject(v8Context) | 275 ->ToObject(v8Context) |
| 287 .ToLocalChecked(), | 276 .ToLocalChecked(), |
| 288 v8Context->Global() | 277 v8Context->Global() |
| 289 ->GetPrototype() | 278 ->GetPrototype() |
| 290 ->ToObject(v8Context) | 279 ->ToObject(v8Context) |
| 291 .ToLocalChecked()); | 280 .ToLocalChecked()); |
| 292 } | 281 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
| 293 } else { | |
| 294 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); | 282 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); |
| 295 v8Context->Global()->Set(v8Context, objName, obj).FromJust(); | 283 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
| 296 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 284 pObjDef->m_StaticObj.Reset(pIsolate, obj); |
| 297 } | 285 } |
| 298 } | 286 } |
| 299 v8PersistentContext.Reset(pIsolate, v8Context); | 287 v8PersistentContext.Reset(pIsolate, v8Context); |
| 300 } | 288 } |
| 301 | 289 |
| 302 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 290 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
| 303 v8::Global<v8::Context>& v8PersistentContext) { | 291 v8::Global<v8::Context>& v8PersistentContext) { |
| 304 v8::Isolate::Scope isolate_scope(pIsolate); | 292 v8::Isolate::Scope isolate_scope(pIsolate); |
| 305 v8::Locker locker(pIsolate); | 293 v8::Locker locker(pIsolate); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 412 |
| 425 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) { | 413 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) { |
| 426 if (pObj.IsEmpty()) | 414 if (pObj.IsEmpty()) |
| 427 return NULL; | 415 return NULL; |
| 428 v8::Local<v8::Context> context = pObj->CreationContext(); | 416 v8::Local<v8::Context> context = pObj->CreationContext(); |
| 429 if (context.IsEmpty()) | 417 if (context.IsEmpty()) |
| 430 return NULL; | 418 return NULL; |
| 431 return context->GetIsolate(); | 419 return context->GetIsolate(); |
| 432 } | 420 } |
| 433 | 421 |
| 434 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName) { | |
| 435 v8::Isolate::Scope isolate_scope(pIsolate); | |
| 436 if (!FXJS_PerIsolateData::Get(pIsolate)) | |
| 437 return -1; | |
| 438 | |
| 439 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | |
| 440 for (int i = 0; i < maxID; ++i) { | |
| 441 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | |
| 442 if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0) | |
| 443 return i; | |
| 444 } | |
| 445 return -1; | |
| 446 } | |
| 447 | |
| 448 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { | 422 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { |
| 449 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t | 423 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t |
| 450 // wide-strings isn't handled by v8, so use UTF8 as a common | 424 // wide-strings isn't handled by v8, so use UTF8 as a common |
| 451 // intermediate format. | 425 // intermediate format. |
| 452 CFX_ByteString utf8_message = message.UTF8Encode(); | 426 CFX_ByteString utf8_message = message.UTF8Encode(); |
| 453 pIsolate->ThrowException( | 427 pIsolate->ThrowException( |
| 454 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(), | 428 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(), |
| 455 v8::NewStringType::kNormal).ToLocalChecked()); | 429 v8::NewStringType::kNormal).ToLocalChecked()); |
| 456 } | 430 } |
| 457 | 431 |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 return v8::Local<v8::Array>(); | 719 return v8::Local<v8::Array>(); |
| 746 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 720 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 747 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 721 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
| 748 } | 722 } |
| 749 | 723 |
| 750 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 724 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
| 751 pTo = pFrom; | 725 pTo = pFrom; |
| 752 } | 726 } |
| 753 | 727 |
| 754 | 728 |
| OLD | NEW |