From ddaa40fe873070d3aae9a21b9a93848fc7e809f1 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 6 Jun 2018 18:30:15 +0000 Subject: Stop using some v8::Context slot to find runtime. Instead, use the object binding's pointer. Puts the cart back behind the horse. Change-Id: I4c06ae991b871c6e90b0e6c70b69886addca2354 Reviewed-on: https://pdfium-review.googlesource.com/33630 Commit-Queue: dsinclair Reviewed-by: dsinclair --- fxjs/cfxjs_engine.cpp | 5 +++-- fxjs/cfxjs_engine.h | 5 +---- fxjs/cjs_app.cpp | 22 ++++++++++--------- fxjs/cjs_document.cpp | 33 ++++++++++++++--------------- fxjs/cjs_eventhandler.cpp | 18 +++++++++------- fxjs/cjs_field.cpp | 10 ++++----- fxjs/cjs_global.cpp | 54 +++++++++++++++++++++-------------------------- fxjs/cjs_runtime.cpp | 12 +++++------ fxjs/js_define.h | 27 +++++++++++------------- 9 files changed, 87 insertions(+), 99 deletions(-) diff --git a/fxjs/cfxjs_engine.cpp b/fxjs/cfxjs_engine.cpp index 107ed3abae..5d0f3b0ec5 100644 --- a/fxjs/cfxjs_engine.cpp +++ b/fxjs/cfxjs_engine.cpp @@ -591,15 +591,16 @@ void CFXJS_Engine::Error(const WideString& message) { GetIsolate()->ThrowException(NewString(message.AsStringView())); } +// static CJS_Object* CFXJS_Engine::GetObjectPrivate(v8::Local pObj) { CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj); if (!pData && !pObj.IsEmpty()) { // It could be a global proxy object. v8::Local v = pObj->GetPrototype(); - v8::Local context = GetIsolate()->GetCurrentContext(); if (v->IsObject()) { pData = CFXJS_PerObjectData::GetFromObject( - v->ToObject(context).ToLocalChecked()); + v->ToObject(v8::Isolate::GetCurrent()->GetCurrentContext()) + .ToLocalChecked()); } } return pData ? pData->m_pPrivate.get() : nullptr; diff --git a/fxjs/cfxjs_engine.h b/fxjs/cfxjs_engine.h index d1fb70cc28..0c9d70d83d 100644 --- a/fxjs/cfxjs_engine.h +++ b/fxjs/cfxjs_engine.h @@ -85,7 +85,7 @@ class CFXJS_Engine : public CFX_V8 { static CFXJS_Engine* EngineFromContext(v8::Local pContext); static int GetObjDefnID(v8::Local pObj); - + static CJS_Object* GetObjectPrivate(v8::Local pObj); static void SetObjectPrivate(v8::Local pObj, std::unique_ptr p); static void FreeObjectPrivate(v8::Local pObj); @@ -128,9 +128,6 @@ class CFXJS_Engine : public CFX_V8 { v8::Local GetThisObj(); v8::Local NewFXJSBoundObject(int nObjDefnID, bool bStatic = false); - // Retrieve native object binding. - CJS_Object* GetObjectPrivate(v8::Local pObj); - void Error(const WideString& message); v8::Local GetV8Context() { diff --git a/fxjs/cjs_app.cpp b/fxjs/cjs_app.cpp index 3f9244cf66..1997e9afe9 100644 --- a/fxjs/cjs_app.cpp +++ b/fxjs/cjs_app.cpp @@ -99,9 +99,10 @@ CJS_App::~CJS_App() = default; CJS_Return CJS_App::get_active_docs(CJS_Runtime* pRuntime) { CJS_Document* pJSDocument = nullptr; v8::Local pObj = pRuntime->GetThisObj(); - if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_Document::GetObjDefnID()) - pJSDocument = static_cast(pRuntime->GetObjectPrivate(pObj)); - + if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_Document::GetObjDefnID()) { + pJSDocument = + static_cast(CFXJS_Engine::GetObjectPrivate(pObj)); + } v8::Local aDocs = pRuntime->NewArray(); pRuntime->PutArrayElement( aDocs, 0, @@ -326,8 +327,9 @@ CJS_Return CJS_App::setInterval( if (pRetObj.IsEmpty()) return CJS_Return(false); - CJS_TimerObj* pJS_TimerObj = - static_cast(pRuntime->GetObjectPrivate(pRetObj)); + auto* pJS_TimerObj = + static_cast(CFXJS_Engine::GetObjectPrivate(pRetObj)); + pJS_TimerObj->SetTimer(pTimerRef); return CJS_Return(pRetObj); } @@ -354,8 +356,9 @@ CJS_Return CJS_App::setTimeOut( if (pRetObj.IsEmpty()) return CJS_Return(false); - CJS_TimerObj* pJS_TimerObj = - static_cast(pRuntime->GetObjectPrivate(pRetObj)); + auto* pJS_TimerObj = + static_cast(CFXJS_Engine::GetObjectPrivate(pRetObj)); + pJS_TimerObj->SetTimer(pTimerRef); return CJS_Return(pRetObj); } @@ -389,12 +392,11 @@ void CJS_App::ClearTimerCommon(CJS_Runtime* pRuntime, if (CFXJS_Engine::GetObjDefnID(pObj) != CJS_TimerObj::GetObjDefnID()) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(pObj); + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(pObj); if (!pJSObj) return; - CJS_TimerObj* pJS_TimerObj = static_cast(pJSObj); - GlobalTimer::Cancel(pJS_TimerObj->GetTimerID()); + GlobalTimer::Cancel(static_cast(pJSObj)->GetTimerID()); } CJS_Return CJS_App::execMenuItem( diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp index 3022519293..d6dc4d6020 100644 --- a/fxjs/cjs_document.cpp +++ b/fxjs/cjs_document.cpp @@ -254,12 +254,12 @@ CJS_Return CJS_Document::getField( if (pFieldObj.IsEmpty()) return CJS_Return(false); - CJS_Field* pJSField = - static_cast(pRuntime->GetObjectPrivate(pFieldObj)); - pJSField->AttachField(this, wideName); + auto* pJSField = + static_cast(CFXJS_Engine::GetObjectPrivate(pFieldObj)); if (!pJSField) return CJS_Return(false); + pJSField->AttachField(this, wideName); return CJS_Return(pJSField->ToV8Object()); } @@ -362,7 +362,7 @@ CJS_Return CJS_Document::print( if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_PrintParamsObj::GetObjDefnID()) { v8::Local pObj = pRuntime->ToObject(params[8]); - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(pObj); + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(pObj); if (pJSObj) { CJS_PrintParamsObj* printObj = static_cast(pJSObj); @@ -1013,8 +1013,7 @@ CJS_Return CJS_Document::getAnnot( CPDFSDK_AnnotIteration annotIteration(pPageView, false); CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr; for (const auto& pSDKAnnotCur : annotIteration) { - CPDFSDK_BAAnnot* pBAAnnot = - static_cast(pSDKAnnotCur.Get()); + auto* pBAAnnot = static_cast(pSDKAnnotCur.Get()); if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) { pSDKBAAnnot = pBAAnnot; break; @@ -1028,8 +1027,8 @@ CJS_Return CJS_Document::getAnnot( if (pObj.IsEmpty()) return CJS_Return(false); - CJS_Annot* pJS_Annot = - static_cast(pRuntime->GetObjectPrivate(pObj)); + auto* pJS_Annot = + static_cast(CFXJS_Engine::GetObjectPrivate(pObj)); if (!pJS_Annot) return CJS_Return(false); @@ -1063,8 +1062,8 @@ CJS_Return CJS_Document::getAnnots( if (pObj.IsEmpty()) return CJS_Return(false); - CJS_Annot* pJS_Annot = - static_cast(pRuntime->GetObjectPrivate(pObj)); + auto* pJS_Annot = + static_cast(CFXJS_Engine::GetObjectPrivate(pObj)); pJS_Annot->SetSDKAnnot(static_cast(pSDKAnnotCur.Get())); pRuntime->PutArrayElement( annots, i, @@ -1119,7 +1118,7 @@ CJS_Return CJS_Document::addIcon( return CJS_Return(JSGetStringFromID(JSMessage::kTypeError)); v8::Local pObj = pRuntime->ToObject(params[1]); - CJS_Object* obj = pRuntime->GetObjectPrivate(pObj); + CJS_Object* obj = CFXJS_Engine::GetObjectPrivate(pObj); if (!obj) return CJS_Return(JSGetStringFromID(JSMessage::kTypeError)); @@ -1140,8 +1139,8 @@ CJS_Return CJS_Document::get_icons(CJS_Runtime* pRuntime) { if (pObj.IsEmpty()) return CJS_Return(false); - CJS_Icon* pJS_Icon = - static_cast(pRuntime->GetObjectPrivate(pObj)); + auto* pJS_Icon = + static_cast(CFXJS_Engine::GetObjectPrivate(pObj)); pJS_Icon->SetIconName(name); pRuntime->PutArrayElement(Icons, i++, pJS_Icon @@ -1172,12 +1171,12 @@ CJS_Return CJS_Document::getIcon( if (pObj.IsEmpty()) return CJS_Return(false); - CJS_Icon* pJS_Icon = static_cast(pRuntime->GetObjectPrivate(pObj)); - if (!pJS_Icon) + auto* pJSIcon = static_cast(CFXJS_Engine::GetObjectPrivate(pObj)); + if (!pJSIcon) return CJS_Return(false); - pJS_Icon->SetIconName(*it); - return CJS_Return(pJS_Icon->ToV8Object()); + pJSIcon->SetIconName(*it); + return CJS_Return(pJSIcon->ToV8Object()); } CJS_Return CJS_Document::removeIcon( diff --git a/fxjs/cjs_eventhandler.cpp b/fxjs/cjs_eventhandler.cpp index 2f42d4157f..0fd330fd13 100644 --- a/fxjs/cjs_eventhandler.cpp +++ b/fxjs/cjs_eventhandler.cpp @@ -580,10 +580,11 @@ CJS_Field* CJS_EventHandler::Source() { if (pFieldObj.IsEmpty()) return nullptr; - CJS_Document* pJSDocument = - static_cast(pRuntime->GetObjectPrivate(pDocObj)); - CJS_Field* pJSField = - static_cast(pRuntime->GetObjectPrivate(pFieldObj)); + auto* pJSDocument = + static_cast(CFXJS_Engine::GetObjectPrivate(pDocObj)); + + auto* pJSField = + static_cast(CFXJS_Engine::GetObjectPrivate(pFieldObj)); pJSDocument->SetFormFillEnv(m_pTargetFormFillEnv ? m_pTargetFormFillEnv.Get() @@ -605,10 +606,11 @@ CJS_Field* CJS_EventHandler::Target_Field() { if (pFieldObj.IsEmpty()) return nullptr; - CJS_Document* pJSDocument = - static_cast(pRuntime->GetObjectPrivate(pDocObj)); - CJS_Field* pJSField = - static_cast(pRuntime->GetObjectPrivate(pFieldObj)); + auto* pJSDocument = + static_cast(CFXJS_Engine::GetObjectPrivate(pDocObj)); + + auto* pJSField = + static_cast(CFXJS_Engine::GetObjectPrivate(pFieldObj)); pJSDocument->SetFormFillEnv(m_pTargetFormFillEnv ? m_pTargetFormFillEnv.Get() diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp index 0a322477ec..cdbe0dfc4d 100644 --- a/fxjs/cjs_field.cpp +++ b/fxjs/cjs_field.cpp @@ -2279,10 +2279,8 @@ CJS_Return CJS_Field::buttonGetIcon( if (pObj.IsEmpty()) return CJS_Return(false); - CJS_Icon* pJS_Icon = static_cast(pRuntime->GetObjectPrivate(pObj)); - if (!pJS_Icon) - return CJS_Return(false); - return CJS_Return(pJS_Icon->ToV8Object()); + auto* pJS_Icon = static_cast(CFXJS_Engine::GetObjectPrivate(pObj)); + return pJS_Icon ? CJS_Return(pJS_Icon->ToV8Object()) : CJS_Return(false); } CJS_Return CJS_Field::buttonImportIcon( @@ -2399,8 +2397,8 @@ CJS_Return CJS_Field::getArray( if (pObj.IsEmpty()) return CJS_Return(false); - CJS_Field* pJSField = - static_cast(pRuntime->GetObjectPrivate(pObj)); + auto* pJSField = + static_cast(CFXJS_Engine::GetObjectPrivate(pObj)); pJSField->AttachField(m_pJSDoc, *pStr); pRuntime->PutArrayElement(FormFieldArray, j++, pJSField diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp index 9a1f541212..5834ab9600 100644 --- a/fxjs/cjs_global.cpp +++ b/fxjs/cjs_global.cpp @@ -32,18 +32,17 @@ template void JSSpecialPropQuery(const char*, v8::Local property, const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); + if (!pJSObj) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) + CJS_Runtime* pRuntime = pJSObj->GetRuntime(); + if (!pRuntime) return; - Alt* pObj = static_cast(pJSObj); - CJS_Return result = - pObj->QueryProperty(PropFromV8Prop(info.GetIsolate(), property).c_str()); + CJS_Return result = static_cast(pJSObj)->QueryProperty( + PropFromV8Prop(info.GetIsolate(), property).c_str()); + info.GetReturnValue().Set(!result.HasError() ? 4 : 0); } @@ -51,24 +50,22 @@ template void JSSpecialPropGet(const char* class_name, v8::Local property, const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); + if (!pJSObj) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) + CJS_Runtime* pRuntime = pJSObj->GetRuntime(); + if (!pRuntime) return; - Alt* pObj = static_cast(pJSObj); - CJS_Return result = pObj->GetProperty( + CJS_Return result = static_cast(pJSObj)->GetProperty( pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); + if (result.HasError()) { pRuntime->Error( JSFormatErrorString(class_name, "GetProperty", result.Error())); return; } - if (result.HasReturn()) info.GetReturnValue().Set(result.Return()); } @@ -78,18 +75,17 @@ void JSSpecialPropPut(const char* class_name, v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); + if (!pJSObj) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) + CJS_Runtime* pRuntime = pJSObj->GetRuntime(); + if (!pRuntime) return; - Alt* pObj = static_cast(pJSObj); - CJS_Return result = pObj->SetProperty( + CJS_Return result = static_cast(pJSObj)->SetProperty( pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str(), value); + if (result.HasError()) { pRuntime->Error( JSFormatErrorString(class_name, "PutProperty", result.Error())); @@ -100,17 +96,15 @@ template void JSSpecialPropDel(const char* class_name, v8::Local property, const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); + if (!pJSObj) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) + CJS_Runtime* pRuntime = pJSObj->GetRuntime(); + if (!pRuntime) return; - Alt* pObj = static_cast(pJSObj); - CJS_Return result = pObj->DelProperty( + CJS_Return result = static_cast(pJSObj)->DelProperty( pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); if (result.HasError()) { // TODO(dsinclair): Should this set the pRuntime->Error result? diff --git a/fxjs/cjs_runtime.cpp b/fxjs/cjs_runtime.cpp index a8640eaa34..2896c5f701 100644 --- a/fxjs/cjs_runtime.cpp +++ b/fxjs/cjs_runtime.cpp @@ -168,14 +168,12 @@ void CJS_Runtime::SetFormFillEnvToDocument() { v8::Context::Scope context_scope(context); v8::Local pThis = GetThisObj(); - if (pThis.IsEmpty()) + if (pThis.IsEmpty() || + CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::GetObjDefnID()) { return; - - if (CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::GetObjDefnID()) - return; - - CJS_Document* pJSDocument = - static_cast(GetObjectPrivate(pThis)); + } + auto* pJSDocument = + static_cast(CFXJS_Engine::GetObjectPrivate(pThis)); if (!pJSDocument) return; diff --git a/fxjs/js_define.h b/fxjs/js_define.h index 325642d8ce..93dfd49099 100644 --- a/fxjs/js_define.h +++ b/fxjs/js_define.h @@ -63,13 +63,12 @@ void JSPropGetter(const char* prop_name_string, const char* class_name_string, v8::Local property, const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); + if (!pJSObj) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) + CJS_Runtime* pRuntime = pJSObj->GetRuntime(); + if (!pRuntime) return; C* pObj = static_cast(pJSObj); @@ -90,13 +89,12 @@ void JSPropSetter(const char* prop_name_string, v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); + if (!pJSObj) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) + CJS_Runtime* pRuntime = pJSObj->GetRuntime(); + if (!pRuntime) return; C* pObj = static_cast(pJSObj); @@ -113,13 +111,12 @@ template & info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) + CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); + if (!pJSObj) return; - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) + CJS_Runtime* pRuntime = pJSObj->GetRuntime(); + if (!pRuntime) return; std::vector> parameters; -- cgit v1.2.3