2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "PropertyMap.h"
33 #include <wtf/HashFunctions.h>
34 #include <wtf/HashTraits.h>
35 #include <wtf/OwnArrayPtr.h>
36 #include <wtf/PassRefPtr.h>
37 #include <wtf/RefCounted.h>
42 class StructureIDChain;
44 struct TransitionTableHash {
45 typedef std::pair<RefPtr<UString::Rep>, unsigned> TransitionTableKey;
46 static unsigned hash(const TransitionTableKey& p)
48 return p.first->computedHash();
51 static bool equal(const TransitionTableKey& a, const TransitionTableKey& b)
56 static const bool safeToCompareToEmptyOrDeleted = true;
59 struct TransitionTableHashTraits {
60 typedef WTF::HashTraits<RefPtr<UString::Rep> > FirstTraits;
61 typedef WTF::GenericHashTraits<unsigned> SecondTraits;
62 typedef std::pair<FirstTraits::TraitType, SecondTraits::TraitType> TraitType;
64 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
65 static TraitType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
67 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
69 static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); }
70 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); }
73 class StructureID : public RefCounted<StructureID> {
75 static PassRefPtr<StructureID> create(JSValue* prototype, JSType type = ObjectType)
77 return adoptRef(new StructureID(prototype, type));
80 static PassRefPtr<StructureID> changePrototypeTransition(StructureID*, JSValue* prototype);
81 static PassRefPtr<StructureID> addPropertyTransition(StructureID*, const Identifier& propertyName, JSValue*, unsigned attributes, JSObject* slotBase, PutPropertySlot&, PropertyStorage&);
82 static PassRefPtr<StructureID> getterSetterTransition(StructureID*);
83 static PassRefPtr<StructureID> toDictionaryTransition(StructureID*);
84 static PassRefPtr<StructureID> fromDictionaryTransition(StructureID*);
90 if (!m_prototype->marked())
94 bool isDictionary() const { return m_isDictionary; }
96 JSType type() const { return m_type; }
98 JSValue* storedPrototype() const { return m_prototype; }
99 JSValue* prototypeForLookup(ExecState*);
101 void setCachedPrototypeChain(PassRefPtr<StructureIDChain> cachedPrototypeChain) { m_cachedPrototypeChain = cachedPrototypeChain; }
102 StructureIDChain* cachedPrototypeChain() const { return m_cachedPrototypeChain.get(); }
104 const PropertyMap& propertyMap() const { return m_propertyMap; }
105 PropertyMap& propertyMap() { return m_propertyMap; }
108 typedef std::pair<RefPtr<UString::Rep>, unsigned> TransitionTableKey;
109 typedef HashMap<TransitionTableKey, StructureID*, TransitionTableHash, TransitionTableHashTraits> TransitionTable;
111 StructureID(JSValue* prototype, JSType);
113 static const size_t s_maxTransitionLength = 64;
118 JSValue* m_prototype;
119 RefPtr<StructureIDChain> m_cachedPrototypeChain;
121 RefPtr<StructureID> m_previous;
122 UString::Rep* m_nameInPrevious;
123 unsigned m_attributesInPrevious;
125 size_t m_transitionCount;
126 TransitionTable m_transitionTable;
128 PropertyMap m_propertyMap;
131 class StructureIDChain : public RefCounted<StructureIDChain> {
133 static PassRefPtr<StructureIDChain> create(StructureID* structureID) { return adoptRef(new StructureIDChain(structureID)); }
135 RefPtr<StructureID>* head() { return m_vector.get(); }
138 StructureIDChain(StructureID* structureID);
140 OwnArrayPtr<RefPtr<StructureID> > m_vector;
145 #endif // StructureID_h