Changeset 236018 in webkit


Ignore:
Timestamp:
Sep 14, 2018 2:00:21 PM (6 years ago)
Author:
mark.lam@apple.com
Message:

Refactor some ForInContext code for better encapsulation.
https://bugs.webkit.org/show_bug.cgi?id=189626
<rdar://problem/44466415>

Reviewed by Keith Miller.

  1. Add a ForInContext::m_type field to store the context type. This does not increase the class size, but eliminates the need for a virtual call to get the type.

Note: we still need a virtual destructor because we'll be mingling
IndexedForInContexts and StructureForInContexts in the BytecodeGenerator::m_forInContextStack.

  1. Add ForInContext::isIndexedForInContext() and ForInContext::isStructureForInContext() convenience methods.
  1. Add ForInContext::asIndexedForInContext() and ForInContext::asStructureForInContext() to do the casting to the subclass types. This ensures that we'll properly assert that the casting is legal.
  • bytecompiler/BytecodeGenerator.cpp:

(JSC::BytecodeGenerator::emitGetByVal):
(JSC::BytecodeGenerator::popIndexedForInScope):
(JSC::BytecodeGenerator::popStructureForInScope):

  • bytecompiler/BytecodeGenerator.h:

(JSC::ForInContext::type const):
(JSC::ForInContext::isIndexedForInContext const):
(JSC::ForInContext::isStructureForInContext const):
(JSC::ForInContext::asIndexedForInContext):
(JSC::ForInContext::asStructureForInContext):
(JSC::ForInContext::ForInContext):
(JSC::StructureForInContext::StructureForInContext):
(JSC::IndexedForInContext::IndexedForInContext):
(JSC::ForInContext::~ForInContext): Deleted.

Location:
trunk/Source/JavaScriptCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r236008 r236018  
     12018-09-14  Mark Lam  <mark.lam@apple.com>
     2
     3        Refactor some ForInContext code for better encapsulation.
     4        https://bugs.webkit.org/show_bug.cgi?id=189626
     5        <rdar://problem/44466415>
     6
     7        Reviewed by Keith Miller.
     8
     9        1. Add a ForInContext::m_type field to store the context type.  This does not
     10           increase the class size, but eliminates the need for a virtual call to get the
     11           type.
     12
     13           Note: we still need a virtual destructor because we'll be mingling
     14           IndexedForInContexts and StructureForInContexts in the BytecodeGenerator::m_forInContextStack.
     15
     16        2. Add ForInContext::isIndexedForInContext() and ForInContext::isStructureForInContext()
     17           convenience methods.
     18
     19        3. Add ForInContext::asIndexedForInContext() and ForInContext::asStructureForInContext()
     20           to do the casting to the subclass types.  This ensures that we'll properly
     21           assert that the casting is legal.
     22
     23        * bytecompiler/BytecodeGenerator.cpp:
     24        (JSC::BytecodeGenerator::emitGetByVal):
     25        (JSC::BytecodeGenerator::popIndexedForInScope):
     26        (JSC::BytecodeGenerator::popStructureForInScope):
     27        * bytecompiler/BytecodeGenerator.h:
     28        (JSC::ForInContext::type const):
     29        (JSC::ForInContext::isIndexedForInContext const):
     30        (JSC::ForInContext::isStructureForInContext const):
     31        (JSC::ForInContext::asIndexedForInContext):
     32        (JSC::ForInContext::asStructureForInContext):
     33        (JSC::ForInContext::ForInContext):
     34        (JSC::StructureForInContext::StructureForInContext):
     35        (JSC::IndexedForInContext::IndexedForInContext):
     36        (JSC::ForInContext::~ForInContext): Deleted.
     37
    1382018-09-14  Devin Rousso  <webkit@devinrousso.com>
    239
  • trunk/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp

    r234082 r236018  
    29132913        unsigned instIndex = instructions().size();
    29142914
    2915         if (context.type() == ForInContext::IndexedForInContextType) {
    2916             static_cast<IndexedForInContext&>(context).addGetInst(instIndex, property->index());
    2917             property = static_cast<IndexedForInContext&>(context).index();
     2915        if (context.isIndexedForInContext()) {
     2916            auto& indexedContext = context.asIndexedForInContext();
     2917            indexedContext.addGetInst(instIndex, property->index());
     2918            property = indexedContext.index();
    29182919            break;
    29192920        }
    29202921
    2921         ASSERT(context.type() == ForInContext::StructureForInContextType);
    2922         StructureForInContext& structureContext = static_cast<StructureForInContext&>(context);
     2922        StructureForInContext& structureContext = context.asStructureForInContext();
    29232923        UnlinkedValueProfile profile = emitProfiledOpcode(op_get_direct_pname);
    29242924        instructions().append(kill(dst));
     
    46324632    if (!localRegister)
    46334633        return;
    4634 
    4635     ASSERT(m_forInContextStack.last()->type() == ForInContext::IndexedForInContextType);
    4636     static_cast<IndexedForInContext&>(m_forInContextStack.last().get()).finalize(*this);
     4634    m_forInContextStack.last()->asIndexedForInContext().finalize(*this);
    46374635    m_forInContextStack.removeLast();
    46384636}
     
    47444742    if (!localRegister)
    47454743        return;
    4746     ASSERT(m_forInContextStack.last()->type() == ForInContext::StructureForInContextType);
    4747     static_cast<StructureForInContext&>(m_forInContextStack.last().get()).finalize(*this);
     4744    m_forInContextStack.last()->asStructureForInContext().finalize(*this);
    47484745    m_forInContextStack.removeLast();
    47494746}
  • trunk/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h

    r234082 r236018  
    5757    class JSImmutableButterfly;
    5858    class Identifier;
     59    class IndexedForInContext;
     60    class StructureForInContext;
    5961
    6062    enum ExpectedFunction {
     
    181183        WTF_MAKE_NONCOPYABLE(ForInContext);
    182184    public:
    183         ForInContext(RegisterID* localRegister)
    184             : m_localRegister(localRegister)
    185             , m_isValid(true)
    186         {
    187         }
    188 
    189         virtual ~ForInContext()
    190         {
    191         }
     185        virtual ~ForInContext() = default;
    192186
    193187        bool isValid() const { return m_isValid; }
    194188        void invalidate() { m_isValid = false; }
    195189
    196         enum ForInContextType {
    197             StructureForInContextType,
    198             IndexedForInContextType
     190        enum class Type : uint8_t {
     191            IndexedForIn,
     192            StructureForIn
    199193        };
    200         virtual ForInContextType type() const = 0;
     194
     195        Type type() const { return m_type; }
     196        bool isIndexedForInContext() const { return m_type == Type::IndexedForIn; }
     197        bool isStructureForInContext() const { return m_type == Type::StructureForIn; }
     198
     199        IndexedForInContext& asIndexedForInContext()
     200        {
     201            ASSERT(isIndexedForInContext());
     202            return *reinterpret_cast<IndexedForInContext*>(this);
     203        }
     204
     205        StructureForInContext& asStructureForInContext()
     206        {
     207            ASSERT(isStructureForInContext());
     208            return *reinterpret_cast<StructureForInContext*>(this);
     209        }
    201210
    202211        RegisterID* local() const { return m_localRegister.get(); }
     212
     213    protected:
     214        ForInContext(RegisterID* localRegister, Type type)
     215            : m_localRegister(localRegister)
     216            , m_type(type)
     217        { }
    203218
    204219    private:
    205220        RefPtr<RegisterID> m_localRegister;
    206         bool m_isValid;
     221        bool m_isValid { true };
     222        Type m_type;
    207223    };
    208224
     
    212228
    213229        StructureForInContext(RegisterID* localRegister, RegisterID* indexRegister, RegisterID* propertyRegister, RegisterID* enumeratorRegister)
    214             : ForInContext(localRegister)
     230            : ForInContext(localRegister, Type::StructureForIn)
    215231            , m_indexRegister(indexRegister)
    216232            , m_propertyRegister(propertyRegister)
    217233            , m_enumeratorRegister(enumeratorRegister)
    218234        {
    219         }
    220 
    221         ForInContextType type() const override
    222         {
    223             return StructureForInContextType;
    224235        }
    225236
     
    245256    public:
    246257        IndexedForInContext(RegisterID* localRegister, RegisterID* indexRegister)
    247             : ForInContext(localRegister)
     258            : ForInContext(localRegister, Type::IndexedForIn)
    248259            , m_indexRegister(indexRegister)
    249260        {
    250         }
    251 
    252         ForInContextType type() const override
    253         {
    254             return IndexedForInContextType;
    255261        }
    256262
Note: See TracChangeset for help on using the changeset viewer.