Chromium Code Reviews| 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 "../../include/jsapi/fxjs_v8.h" | 7 #include "../../include/jsapi/fxjs_v8.h" |
| 8 | 8 |
| 9 #include "../../../core/include/fxcrt/fx_basic.h" | 9 #include "../../../core/include/fxcrt/fx_basic.h" |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { | 45 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
| 46 // Note: GetAt() halts if out-of-range even in release builds. | 46 // Note: GetAt() halts if out-of-range even in release builds. |
| 47 return static_cast<CFXJS_ObjDefinition*>( | 47 return static_cast<CFXJS_ObjDefinition*>( |
| 48 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); | 48 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); |
| 49 } | 49 } |
| 50 CFXJS_ObjDefinition(v8::Isolate* isolate, | 50 CFXJS_ObjDefinition(v8::Isolate* isolate, |
| 51 const wchar_t* sObjName, | 51 const wchar_t* sObjName, |
| 52 FXJSOBJTYPE eObjType, | 52 FXJSOBJTYPE eObjType, |
| 53 FXJS_CONSTRUCTOR pConstructor, | 53 FXJS_CONSTRUCTOR pConstructor, |
| 54 FXJS_DESTRUCTOR pDestructor) | 54 FXJS_DESTRUCTOR pDestructor) |
| 55 : objName(sObjName), | 55 : m_ObjName(sObjName), |
| 56 objType(eObjType), | 56 m_ObjType(eObjType), |
| 57 m_pConstructor(pConstructor), | 57 m_pConstructor(pConstructor), |
| 58 m_pDestructor(pDestructor), | 58 m_pDestructor(pDestructor), |
| 59 m_bSetAsGlobalObject(FALSE), | |
| 60 m_pIsolate(isolate) { | 59 m_pIsolate(isolate) { |
| 61 v8::Isolate::Scope isolate_scope(isolate); | 60 v8::Isolate::Scope isolate_scope(isolate); |
| 62 v8::HandleScope handle_scope(isolate); | 61 v8::HandleScope handle_scope(isolate); |
| 63 | 62 |
| 64 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); | 63 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); |
| 65 fun->InstanceTemplate()->SetInternalFieldCount(2); | 64 fun->InstanceTemplate()->SetInternalFieldCount(2); |
| 66 m_FunctionTemplate.Reset(isolate, fun); | 65 m_FunctionTemplate.Reset(isolate, fun); |
| 67 | 66 |
| 68 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); | 67 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); |
| 69 m_Signature.Reset(isolate, sig); | 68 m_Signature.Reset(isolate, sig); |
| 70 | |
| 71 // Document as the global object. | |
| 72 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { | |
| 73 m_bSetAsGlobalObject = TRUE; | |
| 74 } | |
| 75 } | 69 } |
| 76 | 70 |
| 77 int AssignID() { | 71 int AssignID() { |
| 78 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); | 72 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); |
| 79 pData->m_ObjectDefnArray.Add(this); | 73 pData->m_ObjectDefnArray.Add(this); |
| 80 return pData->m_ObjectDefnArray.GetSize() - 1; | 74 return pData->m_ObjectDefnArray.GetSize() - 1; |
| 81 } | 75 } |
| 82 | 76 |
| 83 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { | 77 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { |
| 84 v8::EscapableHandleScope scope(m_pIsolate); | 78 v8::EscapableHandleScope scope(m_pIsolate); |
| 85 v8::Local<v8::FunctionTemplate> function = | 79 v8::Local<v8::FunctionTemplate> function = |
| 86 m_FunctionTemplate.Get(m_pIsolate); | 80 m_FunctionTemplate.Get(m_pIsolate); |
| 87 return scope.Escape(function->InstanceTemplate()); | 81 return scope.Escape(function->InstanceTemplate()); |
| 88 } | 82 } |
| 89 | 83 |
| 90 v8::Local<v8::Signature> GetSignature() { | 84 v8::Local<v8::Signature> GetSignature() { |
| 91 v8::EscapableHandleScope scope(m_pIsolate); | 85 v8::EscapableHandleScope scope(m_pIsolate); |
| 92 return scope.Escape(m_Signature.Get(m_pIsolate)); | 86 return scope.Escape(m_Signature.Get(m_pIsolate)); |
| 93 } | 87 } |
| 94 | 88 |
| 95 const wchar_t* objName; | 89 const wchar_t* m_ObjName; |
|
Lei Zhang
2015/10/06 01:15:21
const wchar_t* const?
Tom Sepez
2015/10/06 15:33:20
Why not? Done.
Lei Zhang
2015/10/06 18:16:17
So the pointer can't accidentally change either. T
| |
| 96 const FXJSOBJTYPE objType; | 90 const FXJSOBJTYPE m_ObjType; |
| 97 const FXJS_CONSTRUCTOR m_pConstructor; | 91 const FXJS_CONSTRUCTOR m_pConstructor; |
| 98 const FXJS_DESTRUCTOR m_pDestructor; | 92 const FXJS_DESTRUCTOR m_pDestructor; |
| 99 FX_BOOL m_bSetAsGlobalObject; | |
| 100 | 93 |
| 101 v8::Isolate* m_pIsolate; | 94 v8::Isolate* m_pIsolate; |
| 102 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; | 95 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; |
| 103 v8::Global<v8::Signature> m_Signature; | 96 v8::Global<v8::Signature> m_Signature; |
| 104 v8::Global<v8::Object> m_StaticObj; | 97 v8::Global<v8::Object> m_StaticObj; |
| 105 }; | 98 }; |
| 106 | 99 |
| 107 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( | 100 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
| 108 v8::Isolate* pIsolate) { | 101 v8::Isolate* pIsolate) { |
| 109 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 102 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 110 for (int i = 0; i < maxID; ++i) { | 103 for (int i = 0; i < maxID; ++i) { |
| 111 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 104 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 112 if (pObjDef->m_bSetAsGlobalObject) | 105 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) |
| 113 return pObjDef->GetInstanceTemplate(); | 106 return pObjDef->GetInstanceTemplate(); |
| 114 } | 107 } |
| 115 | |
| 116 if (!g_DefaultGlobalObjectTemplate) { | 108 if (!g_DefaultGlobalObjectTemplate) { |
| 117 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; | 109 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; |
| 118 g_DefaultGlobalObjectTemplate->Reset(pIsolate, | 110 g_DefaultGlobalObjectTemplate->Reset(pIsolate, |
| 119 v8::ObjectTemplate::New(pIsolate)); | 111 v8::ObjectTemplate::New(pIsolate)); |
| 120 } | 112 } |
| 121 return g_DefaultGlobalObjectTemplate->Get(pIsolate); | 113 return g_DefaultGlobalObjectTemplate->Get(pIsolate); |
| 122 } | 114 } |
| 123 | 115 |
| 124 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { | 116 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { |
| 125 return calloc(1, length); | 117 return calloc(1, length); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 v8::Local<v8::Context> v8Context = | 284 v8::Local<v8::Context> v8Context = |
| 293 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); | 285 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
| 294 v8::Context::Scope context_scope(v8Context); | 286 v8::Context::Scope context_scope(v8Context); |
| 295 | 287 |
| 296 FXJS_PerIsolateData::SetUp(pIsolate); | 288 FXJS_PerIsolateData::SetUp(pIsolate); |
| 297 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); | 289 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); |
| 298 | 290 |
| 299 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 291 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 300 for (int i = 0; i < maxID; ++i) { | 292 for (int i = 0; i < maxID; ++i) { |
| 301 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 293 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 302 CFX_WideString ws = CFX_WideString(pObjDef->objName); | 294 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); |
| 303 CFX_ByteString bs = ws.UTF8Encode(); | 295 v8::Local<v8::String> m_ObjName = |
| 304 v8::Local<v8::String> objName = | |
| 305 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
| 306 v8::NewStringType::kNormal, | 297 v8::NewStringType::kNormal, |
| 307 bs.GetLength()).ToLocalChecked(); | 298 bs.GetLength()).ToLocalChecked(); |
| 308 | 299 |
| 309 if (pObjDef->objType == FXJS_DYNAMIC) { | 300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 310 // Document is set as global object, need to construct it first. | 301 v8Context->Global() |
| 311 if (ws.Equal(L"Document")) { | 302 ->GetPrototype() |
| 312 v8Context->Global() | 303 ->ToObject(v8Context) |
| 313 ->GetPrototype() | 304 .ToLocalChecked() |
| 314 ->ToObject(v8Context) | 305 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); |
| 315 .ToLocalChecked() | |
| 316 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); | |
| 317 | 306 |
| 318 if (pObjDef->m_pConstructor) { | 307 if (pObjDef->m_pConstructor) |
| 319 pObjDef->m_pConstructor(context, v8Context->Global() | 308 pObjDef->m_pConstructor(context, v8Context->Global() |
| 320 ->GetPrototype() | 309 ->GetPrototype() |
| 321 ->ToObject(v8Context) | 310 ->ToObject(v8Context) |
| 322 .ToLocalChecked(), | 311 .ToLocalChecked(), |
| 323 v8Context->Global() | 312 v8Context->Global() |
| 324 ->GetPrototype() | 313 ->GetPrototype() |
| 325 ->ToObject(v8Context) | 314 ->ToObject(v8Context) |
| 326 .ToLocalChecked()); | 315 .ToLocalChecked()); |
| 327 } | 316 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
| 328 } | |
| 329 } else { | |
| 330 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); | 317 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); |
| 331 v8Context->Global()->Set(v8Context, objName, obj).FromJust(); | 318 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); |
| 332 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 319 pObjDef->m_StaticObj.Reset(pIsolate, obj); |
| 333 } | 320 } |
| 334 } | 321 } |
| 335 v8PersistentContext.Reset(pIsolate, v8Context); | 322 v8PersistentContext.Reset(pIsolate, v8Context); |
| 336 } | 323 } |
| 337 | 324 |
| 338 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 325 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
| 339 v8::Global<v8::Context>& v8PersistentContext) { | 326 v8::Global<v8::Context>& v8PersistentContext) { |
| 340 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) | 327 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) |
| 341 return; | 328 return; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 | 447 |
| 461 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) { | 448 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) { |
| 462 if (pObj.IsEmpty()) | 449 if (pObj.IsEmpty()) |
| 463 return NULL; | 450 return NULL; |
| 464 v8::Local<v8::Context> context = pObj->CreationContext(); | 451 v8::Local<v8::Context> context = pObj->CreationContext(); |
| 465 if (context.IsEmpty()) | 452 if (context.IsEmpty()) |
| 466 return NULL; | 453 return NULL; |
| 467 return context->GetIsolate(); | 454 return context->GetIsolate(); |
| 468 } | 455 } |
| 469 | 456 |
| 470 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName) { | |
| 471 v8::Isolate::Scope isolate_scope(pIsolate); | |
| 472 if (!FXJS_PerIsolateData::Get(pIsolate)) | |
| 473 return -1; | |
| 474 | |
| 475 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | |
| 476 for (int i = 0; i < maxID; ++i) { | |
| 477 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | |
| 478 if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0) | |
| 479 return i; | |
| 480 } | |
| 481 return -1; | |
| 482 } | |
| 483 | |
| 484 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { | 457 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { |
| 485 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t | 458 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t |
| 486 // wide-strings isn't handled by v8, so use UTF8 as a common | 459 // wide-strings isn't handled by v8, so use UTF8 as a common |
| 487 // intermediate format. | 460 // intermediate format. |
| 488 CFX_ByteString utf8_message = message.UTF8Encode(); | 461 CFX_ByteString utf8_message = message.UTF8Encode(); |
| 489 pIsolate->ThrowException( | 462 pIsolate->ThrowException( |
| 490 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(), | 463 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(), |
| 491 v8::NewStringType::kNormal).ToLocalChecked()); | 464 v8::NewStringType::kNormal).ToLocalChecked()); |
| 492 } | 465 } |
| 493 | 466 |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 782 return v8::Local<v8::Array>(); | 755 return v8::Local<v8::Array>(); |
| 783 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 756 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 784 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 757 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
| 785 } | 758 } |
| 786 | 759 |
| 787 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 760 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
| 788 pTo = pFrom; | 761 pTo = pFrom; |
| 789 } | 762 } |
| 790 | 763 |
| 791 | 764 |
| OLD | NEW |