From 721f5fa5061f2dec8282b7bde48c9d4721208b7a Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 23 Jul 2018 18:03:44 +0000 Subject: Replace CFXJSE_HostObject::type_ with polymorphic conversion methods More foolproof technique, and saves one word in every instance at a cost of two pointers in every subclass vtable. Change-Id: Ied4f91d4d203b5e6d83abe751159eb7e55489bb5 Reviewed-on: https://pdfium-review.googlesource.com/38530 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- fxjs/cfxjse_engine.cpp | 20 ++++++-------------- fxjs/cfxjse_formcalc_context.cpp | 14 +++++++------- fxjs/cfxjse_formcalc_context.h | 3 +++ fxjs/fxjse.cpp | 19 +++++++++++++++++++ fxjs/fxjse.h | 15 +++++++-------- 5 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 fxjs/fxjse.cpp (limited to 'fxjs') diff --git a/fxjs/cfxjse_engine.cpp b/fxjs/cfxjse_engine.cpp index 9b54555a21..62376dd822 100644 --- a/fxjs/cfxjse_engine.cpp +++ b/fxjs/cfxjse_engine.cpp @@ -77,20 +77,16 @@ CXFA_Object* CFXJSE_Engine::ToObject( if (!info.Holder()->IsObject()) return nullptr; - CFXJSE_HostObject* hostObj = + CFXJSE_HostObject* pHostObj = FXJSE_RetrieveObjectBinding(info.Holder().As(), nullptr); - if (!hostObj || hostObj->type() != CFXJSE_HostObject::kXFA) - return nullptr; - return static_cast(hostObj); + return pHostObj ? pHostObj->AsCXFAObject() : nullptr; } // static. CXFA_Object* CFXJSE_Engine::ToObject(CFXJSE_Value* pValue, CFXJSE_Class* pClass) { CFXJSE_HostObject* pHostObj = pValue->ToHostObject(pClass); - if (!pHostObj || pHostObj->type() != CFXJSE_HostObject::kXFA) - return nullptr; - return static_cast(pHostObj); + return pHostObj ? pHostObj->AsCXFAObject() : nullptr; } CFXJSE_Engine::CFXJSE_Engine(CXFA_Document* pDocument, @@ -791,16 +787,12 @@ void CFXJSE_Engine::AddNodesOfRunScript(CXFA_Node* pNode) { } CXFA_Object* CFXJSE_Engine::ToXFAObject(v8::Local obj) { - ASSERT(!obj.IsEmpty()); - - if (!obj->IsObject()) + if (obj.IsEmpty() || !obj->IsObject()) return nullptr; - CFXJSE_HostObject* hostObj = + CFXJSE_HostObject* pHostObj = FXJSE_RetrieveObjectBinding(obj.As(), nullptr); - if (!hostObj || hostObj->type() != CFXJSE_HostObject::kXFA) - return nullptr; - return static_cast(hostObj); + return pHostObj ? pHostObj->AsCXFAObject() : nullptr; } v8::Local CFXJSE_Engine::NewXFAObject( diff --git a/fxjs/cfxjse_formcalc_context.cpp b/fxjs/cfxjse_formcalc_context.cpp index c7433b4b2b..b0a26d86a4 100644 --- a/fxjs/cfxjse_formcalc_context.cpp +++ b/fxjs/cfxjse_formcalc_context.cpp @@ -450,9 +450,7 @@ bool PatternStringType(const ByteStringView& szPattern, uint32_t& patternType) { CFXJSE_FormCalcContext* ToFormCalcContext(CFXJSE_Value* pValue) { CFXJSE_HostObject* pHostObj = pValue->ToHostObject(nullptr); - if (!pHostObj || pHostObj->type() != CFXJSE_HostObject::kFM2JS) - return nullptr; - return static_cast(pHostObj); + return pHostObj ? pHostObj->AsFormCalcContext() : nullptr; } bool IsWhitespace(char c) { @@ -6177,15 +6175,13 @@ bool CFXJSE_FormCalcContext::Translate(const WideStringView& wsFormcalc, return false; wsJavascript->AppendChar(0); - return !CXFA_IsTooBig(wsJavascript); } CFXJSE_FormCalcContext::CFXJSE_FormCalcContext(v8::Isolate* pScriptIsolate, CFXJSE_Context* pScriptContext, CXFA_Document* pDoc) - : CFXJSE_HostObject(kFM2JS), - m_pIsolate(pScriptIsolate), + : m_pIsolate(pScriptIsolate), m_pFMClass(CFXJSE_Class::Create(pScriptContext, &kFormCalcFM2JSDescriptor, false)), @@ -6194,7 +6190,11 @@ CFXJSE_FormCalcContext::CFXJSE_FormCalcContext(v8::Isolate* pScriptIsolate, m_pValue.get()->SetObject(this, m_pFMClass); } -CFXJSE_FormCalcContext::~CFXJSE_FormCalcContext() {} +CFXJSE_FormCalcContext::~CFXJSE_FormCalcContext() = default; + +CFXJSE_FormCalcContext* CFXJSE_FormCalcContext::AsFormCalcContext() { + return this; +} void CFXJSE_FormCalcContext::GlobalPropertyGetter(CFXJSE_Value* pValue) { pValue->Assign(m_pValue.get()); diff --git a/fxjs/cfxjse_formcalc_context.h b/fxjs/cfxjse_formcalc_context.h index 052bba394a..8a92d9b0f1 100644 --- a/fxjs/cfxjse_formcalc_context.h +++ b/fxjs/cfxjse_formcalc_context.h @@ -25,6 +25,9 @@ class CFXJSE_FormCalcContext : public CFXJSE_HostObject { CXFA_Document* pDoc); ~CFXJSE_FormCalcContext() override; + // CFXJSE_HostObject: + CFXJSE_FormCalcContext* AsFormCalcContext() override; + static void Abs(CFXJSE_Value* pThis, const ByteStringView& szFuncName, CFXJSE_Arguments& args); diff --git a/fxjs/fxjse.cpp b/fxjs/fxjse.cpp new file mode 100644 index 0000000000..68adaa5dd3 --- /dev/null +++ b/fxjs/fxjse.cpp @@ -0,0 +1,19 @@ +// Copyright 2018 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fxjs/fxjse.h" + +CFXJSE_HostObject::CFXJSE_HostObject() = default; + +CFXJSE_HostObject::~CFXJSE_HostObject() = default; + +CFXJSE_FormCalcContext* CFXJSE_HostObject::AsFormCalcContext() { + return nullptr; +} + +CXFA_Object* CFXJSE_HostObject::AsCXFAObject() { + return nullptr; +} diff --git a/fxjs/fxjse.h b/fxjs/fxjse.h index addd148117..3f2eb1d836 100644 --- a/fxjs/fxjse.h +++ b/fxjs/fxjse.h @@ -12,23 +12,22 @@ #include "v8/include/v8.h" class CFXJSE_Arguments; +class CFXJSE_FormCalcContext; class CFXJSE_Value; class CJS_Return; +class CXFA_Object; // C++ object which is retrieved from v8 object's slot. class CFXJSE_HostObject { public: - virtual ~CFXJSE_HostObject() {} + virtual ~CFXJSE_HostObject(); - // Small layering violation here, but we need to distinguish between the - // two kinds of subclasses. - enum Type { kXFA, kFM2JS }; - Type type() const { return type_; } + // Two subclasses. + virtual CFXJSE_FormCalcContext* AsFormCalcContext(); + virtual CXFA_Object* AsCXFAObject(); protected: - explicit CFXJSE_HostObject(Type type) { type_ = type; } - - Type type_; + CFXJSE_HostObject(); }; typedef CJS_Return (*FXJSE_MethodCallback)( -- cgit v1.2.3