From f743552fbdb17f974c9b1675af81210fe0ffcc50 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 5 Feb 2018 22:27:22 +0000 Subject: Fold CJS_EmbedObj classes into CJS_Object classes This CL removes the CJS_EmbedObj class and various subclasses and folds the subclasses into their CJS_Object counterparts. Change-Id: If6b882a4995c0b1bf83ac783f5c27ba9216c2d5c Reviewed-on: https://pdfium-review.googlesource.com/25410 Commit-Queue: dsinclair Reviewed-by: Tom Sepez --- fxjs/cjs_global.cpp | 187 ++++++++++++++++++---------------------------------- 1 file changed, 64 insertions(+), 123 deletions(-) (limited to 'fxjs/cjs_global.cpp') diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp index 8816cf1b07..fbc6bf94e8 100644 --- a/fxjs/cjs_global.cpp +++ b/fxjs/cjs_global.cpp @@ -41,7 +41,7 @@ void JSSpecialPropQuery(const char*, if (!pJSObj) return; - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + Alt* pObj = static_cast(pJSObj); CJS_Return result = pObj->QueryProperty(PropFromV8Prop(info.GetIsolate(), property).c_str()); info.GetReturnValue().Set(!result.HasError() ? 4 : 0); @@ -60,7 +60,7 @@ void JSSpecialPropGet(const char* class_name, if (!pJSObj) return; - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + Alt* pObj = static_cast(pJSObj); CJS_Return result = pObj->GetProperty( pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); if (result.HasError()) { @@ -87,7 +87,7 @@ void JSSpecialPropPut(const char* class_name, if (!pJSObj) return; - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + Alt* pObj = static_cast(pJSObj); CJS_Return result = pObj->SetProperty( pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str(), value); if (result.HasError()) { @@ -109,7 +109,7 @@ void JSSpecialPropDel(const char* class_name, if (!pJSObj) return; - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + Alt* pObj = static_cast(pJSObj); CJS_Return result = pObj->DelProperty( pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); if (result.HasError()) { @@ -119,58 +119,20 @@ void JSSpecialPropDel(const char* class_name, } } -struct JSGlobalData { - JSGlobalData(); - ~JSGlobalData(); - - JS_GlobalDataType nType; - double dData; - bool bData; - ByteString sData; - v8::Global pData; - bool bPersistent; - bool bDeleted; -}; - -class JSGlobalAlternate : public CJS_EmbedObj { - public: - explicit JSGlobalAlternate(CJS_Object* pJSObject); - ~JSGlobalAlternate() override; - - CJS_Return setPersistent(CJS_Runtime* pRuntime, - const std::vector>& params); - CJS_Return QueryProperty(const wchar_t* propname); - CJS_Return GetProperty(CJS_Runtime* pRuntime, const wchar_t* propname); - CJS_Return SetProperty(CJS_Runtime* pRuntime, - const wchar_t* propname, - v8::Local vp); - CJS_Return DelProperty(CJS_Runtime* pRuntime, const wchar_t* propname); - void Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv); - - private: - void UpdateGlobalPersistentVariables(); - void CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime); - void DestroyGlobalPersisitentVariables(); - CJS_Return SetGlobalVariables(const ByteString& propname, - JS_GlobalDataType nType, - double dData, - bool bData, - const ByteString& sData, - v8::Local pData, - bool bDefaultPersistent); - void ObjectToArray(CJS_Runtime* pRuntime, - v8::Local pObj, - CJS_GlobalVariableArray& array); - void PutObjectProperty(v8::Local obj, CJS_KeyValue* pData); - - std::map> m_MapGlobal; - WideString m_sFilePath; - CJS_GlobalData* m_pGlobalData; - CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; -}; - } // namespace +CJS_Global::JSGlobalData::JSGlobalData() + : nType(JS_GlobalDataType::NUMBER), + dData(0), + bData(false), + sData(""), + bPersistent(false), + bDeleted(false) {} + +CJS_Global::JSGlobalData::~JSGlobalData() { + pData.Reset(); +} + const JSMethodSpec CJS_Global::MethodSpecs[] = { {"setPersistent", setPersistent_static}}; @@ -179,22 +141,22 @@ int CJS_Global::ObjDefnID = -1; // static void CJS_Global::setPersistent_static( const v8::FunctionCallbackInfo& info) { - JSMethod( - "setPersistent", "global", info); + JSMethod("setPersistent", "global", + info); } // static void CJS_Global::queryprop_static( v8::Local property, const v8::PropertyCallbackInfo& info) { - JSSpecialPropQuery("global", property, info); + JSSpecialPropQuery("global", property, info); } // static void CJS_Global::getprop_static( v8::Local property, const v8::PropertyCallbackInfo& info) { - JSSpecialPropGet("global", property, info); + JSSpecialPropGet("global", property, info); } // static @@ -202,14 +164,14 @@ void CJS_Global::putprop_static( v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) { - JSSpecialPropPut("global", property, value, info); + JSSpecialPropPut("global", property, value, info); } // static void CJS_Global::delprop_static( v8::Local property, const v8::PropertyCallbackInfo& info) { - JSSpecialPropDel("global", property, info); + JSSpecialPropDel("global", property, info); } // static @@ -227,49 +189,31 @@ void CJS_Global::DefineJSObjects(CFXJS_Engine* pEngine) { DefineAllProperties(pEngine); } -CJS_Global::CJS_Global(v8::Local pObject) : CJS_Object(pObject) { - m_pEmbedObj = pdfium::MakeUnique(this); +CJS_Global::CJS_Global(v8::Local pObject) + : CJS_Object(pObject), m_pFormFillEnv(nullptr) {} + +CJS_Global::~CJS_Global() { + DestroyGlobalPersisitentVariables(); + m_pGlobalData->Release(); } void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { CJS_Runtime* pRuntime = static_cast(pIRuntime); - JSGlobalAlternate* pGlobal = - static_cast(GetEmbedObject()); - pGlobal->Initial(pRuntime->GetFormFillEnv()); -} - -JSGlobalData::JSGlobalData() - : nType(JS_GlobalDataType::NUMBER), - dData(0), - bData(false), - sData(""), - bPersistent(false), - bDeleted(false) {} - -JSGlobalData::~JSGlobalData() { - pData.Reset(); + Initial(pRuntime->GetFormFillEnv()); } -JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) - : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {} - -JSGlobalAlternate::~JSGlobalAlternate() { - DestroyGlobalPersisitentVariables(); - m_pGlobalData->Release(); -} - -void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { +void CJS_Global::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { m_pFormFillEnv.Reset(pFormFillEnv); m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv); UpdateGlobalPersistentVariables(); } -CJS_Return JSGlobalAlternate::QueryProperty(const wchar_t* propname) { +CJS_Return CJS_Global::QueryProperty(const wchar_t* propname) { return CJS_Return(WideString(propname) != L"setPersistent"); } -CJS_Return JSGlobalAlternate::DelProperty(CJS_Runtime* pRuntime, - const wchar_t* propname) { +CJS_Return CJS_Global::DelProperty(CJS_Runtime* pRuntime, + const wchar_t* propname) { auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); if (it == m_MapGlobal.end()) return CJS_Return(false); @@ -278,8 +222,8 @@ CJS_Return JSGlobalAlternate::DelProperty(CJS_Runtime* pRuntime, return CJS_Return(true); } -CJS_Return JSGlobalAlternate::GetProperty(CJS_Runtime* pRuntime, - const wchar_t* propname) { +CJS_Return CJS_Global::GetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname) { auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); if (it == m_MapGlobal.end()) return CJS_Return(true); @@ -307,9 +251,9 @@ CJS_Return JSGlobalAlternate::GetProperty(CJS_Runtime* pRuntime, return CJS_Return(false); } -CJS_Return JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime, - const wchar_t* propname, - v8::Local vp) { +CJS_Return CJS_Global::SetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname, + v8::Local vp) { ByteString sPropName = ByteString::FromUnicode(propname); if (vp->IsNumber()) { return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, @@ -342,7 +286,7 @@ CJS_Return JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime, return CJS_Return(false); } -CJS_Return JSGlobalAlternate::setPersistent( +CJS_Return CJS_Global::setPersistent( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 2) @@ -357,10 +301,10 @@ CJS_Return JSGlobalAlternate::setPersistent( return CJS_Return(true); } -void JSGlobalAlternate::UpdateGlobalPersistentVariables() { +void CJS_Global::UpdateGlobalPersistentVariables() { CJS_Runtime* pRuntime = static_cast(CFXJS_Engine::EngineFromIsolateCurrentContext( - m_pJSObject->ToV8Object()->GetIsolate())); + ToV8Object()->GetIsolate())); for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); @@ -369,8 +313,7 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NUMBER, pData->data.dData, false, "", v8::Local(), pData->bPersistent == 1); - pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode(), + pRuntime->PutObjectProperty(ToV8Object(), pData->data.sKey.UTF8Decode(), pRuntime->NewNumber(pData->data.dData)); break; case JS_GlobalDataType::BOOLEAN: @@ -378,7 +321,7 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { pData->data.bData == 1, "", v8::Local(), pData->bPersistent == 1); pRuntime->PutObjectProperty( - m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), + ToV8Object(), pData->data.sKey.UTF8Decode(), pRuntime->NewBoolean(pData->data.bData == 1)); break; case JS_GlobalDataType::STRING: @@ -386,7 +329,7 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { false, pData->data.sData, v8::Local(), pData->bPersistent == 1); pRuntime->PutObjectProperty( - m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), + ToV8Object(), pData->data.sKey.UTF8Decode(), pRuntime->NewString(pData->data.sData.UTF8Decode().AsStringView())); break; case JS_GlobalDataType::OBJECT: { @@ -395,7 +338,7 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { PutObjectProperty(pObj, &pData->data); SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::OBJECT, 0, false, "", pObj, pData->bPersistent == 1); - pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), + pRuntime->PutObjectProperty(ToV8Object(), pData->data.sKey.UTF8Decode(), pObj); } } break; @@ -403,16 +346,14 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NULLOBJ, 0, false, "", v8::Local(), pData->bPersistent == 1); - pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode(), + pRuntime->PutObjectProperty(ToV8Object(), pData->data.sKey.UTF8Decode(), pRuntime->NewNull()); break; } } } -void JSGlobalAlternate::CommitGlobalPersisitentVariables( - CJS_Runtime* pRuntime) { +void CJS_Global::CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime) { for (const auto& iter : m_MapGlobal) { ByteString name = iter.first; JSGlobalData* pData = iter.second.get(); @@ -435,8 +376,8 @@ void JSGlobalAlternate::CommitGlobalPersisitentVariables( break; case JS_GlobalDataType::OBJECT: { CJS_GlobalVariableArray array; - v8::Local obj = v8::Local::New( - GetJSObject()->GetIsolate(), pData->pData); + v8::Local obj = + v8::Local::New(GetIsolate(), pData->pData); ObjectToArray(pRuntime, obj, array); m_pGlobalData->SetGlobalVariableObject(name, array); m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); @@ -449,9 +390,9 @@ void JSGlobalAlternate::CommitGlobalPersisitentVariables( } } -void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, - v8::Local pObj, - CJS_GlobalVariableArray& array) { +void CJS_Global::ObjectToArray(CJS_Runtime* pRuntime, + v8::Local pObj, + CJS_GlobalVariableArray& array) { std::vector pKeyList = pRuntime->GetObjectPropertyNames(pObj); for (const auto& ws : pKeyList) { ByteString sKey = ws.UTF8Encode(); @@ -498,10 +439,10 @@ void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, } } -void JSGlobalAlternate::PutObjectProperty(v8::Local pObj, - CJS_KeyValue* pData) { - CJS_Runtime* pRuntime = CJS_Runtime::RuntimeFromIsolateCurrentContext( - m_pJSObject->ToV8Object()->GetIsolate()); +void CJS_Global::PutObjectProperty(v8::Local pObj, + CJS_KeyValue* pData) { + CJS_Runtime* pRuntime = + CJS_Runtime::RuntimeFromIsolateCurrentContext(ToV8Object()->GetIsolate()); for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { CJS_KeyValue* pObjData = pData->objData.GetAt(i); @@ -535,17 +476,17 @@ void JSGlobalAlternate::PutObjectProperty(v8::Local pObj, } } -void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { +void CJS_Global::DestroyGlobalPersisitentVariables() { m_MapGlobal.clear(); } -CJS_Return JSGlobalAlternate::SetGlobalVariables(const ByteString& propname, - JS_GlobalDataType nType, - double dData, - bool bData, - const ByteString& sData, - v8::Local pData, - bool bDefaultPersistent) { +CJS_Return CJS_Global::SetGlobalVariables(const ByteString& propname, + JS_GlobalDataType nType, + double dData, + bool bData, + const ByteString& sData, + v8::Local pData, + bool bDefaultPersistent) { if (propname.IsEmpty()) return CJS_Return(false); -- cgit v1.2.3