1 | /*
|
---|
2 | * Copyright (C) 2008 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
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.
|
---|
12 | *
|
---|
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.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef StructureID_h
|
---|
27 | #define StructureID_h
|
---|
28 |
|
---|
29 | #include "JSValue.h"
|
---|
30 | #include "PropertyMap.h"
|
---|
31 | #include "ustring.h"
|
---|
32 | #include <wtf/HashFunctions.h>
|
---|
33 | #include <wtf/HashTraits.h>
|
---|
34 | #include <wtf/OwnArrayPtr.h>
|
---|
35 | #include <wtf/PassRefPtr.h>
|
---|
36 | #include <wtf/RefCounted.h>
|
---|
37 |
|
---|
38 | namespace JSC {
|
---|
39 |
|
---|
40 | class JSValue;
|
---|
41 | class StructureIDChain;
|
---|
42 |
|
---|
43 | struct TransitionTableHash {
|
---|
44 | typedef std::pair<RefPtr<UString::Rep>, unsigned> TransitionTableKey;
|
---|
45 | static unsigned hash(const TransitionTableKey& p)
|
---|
46 | {
|
---|
47 | return p.first->computedHash();
|
---|
48 | }
|
---|
49 |
|
---|
50 | static bool equal(const TransitionTableKey& a, const TransitionTableKey& b)
|
---|
51 | {
|
---|
52 | return a == b;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static const bool safeToCompareToEmptyOrDeleted = true;
|
---|
56 | };
|
---|
57 |
|
---|
58 | struct TransitionTableHashTraits {
|
---|
59 | typedef WTF::HashTraits<RefPtr<UString::Rep> > FirstTraits;
|
---|
60 | typedef WTF::GenericHashTraits<unsigned> SecondTraits;
|
---|
61 | typedef std::pair<FirstTraits::TraitType, SecondTraits::TraitType> TraitType;
|
---|
62 |
|
---|
63 | static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
|
---|
64 | static TraitType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
|
---|
65 |
|
---|
66 | static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
|
---|
67 |
|
---|
68 | static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); }
|
---|
69 | static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); }
|
---|
70 | };
|
---|
71 |
|
---|
72 | class StructureID : public RefCounted<StructureID> {
|
---|
73 | public:
|
---|
74 | static PassRefPtr<StructureID> create(JSValue* prototype)
|
---|
75 | {
|
---|
76 | return adoptRef(new StructureID(prototype));
|
---|
77 | }
|
---|
78 |
|
---|
79 | static PassRefPtr<StructureID> changePrototypeTransition(StructureID*, JSValue* prototype);
|
---|
80 | static PassRefPtr<StructureID> addPropertyTransition(StructureID*, const Identifier& propertyName, JSValue*, unsigned attributes, bool checkReadOnly, JSObject* slotBase, PutPropertySlot&, PropertyStorage&);
|
---|
81 | static PassRefPtr<StructureID> getterSetterTransition(StructureID*);
|
---|
82 | static PassRefPtr<StructureID> toDictionaryTransition(StructureID*);
|
---|
83 | static PassRefPtr<StructureID> fromDictionaryTransition(StructureID*);
|
---|
84 |
|
---|
85 | ~StructureID();
|
---|
86 |
|
---|
87 | void mark()
|
---|
88 | {
|
---|
89 | if (!m_prototype->marked())
|
---|
90 | m_prototype->mark();
|
---|
91 | }
|
---|
92 |
|
---|
93 | bool isDictionary() const { return m_isDictionary; }
|
---|
94 |
|
---|
95 | JSValue* prototype() const { return m_prototype; }
|
---|
96 |
|
---|
97 | void setCachedPrototypeChain(PassRefPtr<StructureIDChain> cachedPrototypeChain) { m_cachedPrototypeChain = cachedPrototypeChain; }
|
---|
98 | StructureIDChain* cachedPrototypeChain() const { return m_cachedPrototypeChain.get(); }
|
---|
99 |
|
---|
100 | const PropertyMap& propertyMap() const { return m_propertyMap; }
|
---|
101 | PropertyMap& propertyMap() { return m_propertyMap; }
|
---|
102 |
|
---|
103 | private:
|
---|
104 | typedef std::pair<RefPtr<UString::Rep>, unsigned> TransitionTableKey;
|
---|
105 | typedef HashMap<TransitionTableKey, StructureID*, TransitionTableHash, TransitionTableHashTraits> TransitionTable;
|
---|
106 |
|
---|
107 | StructureID(JSValue* prototype);
|
---|
108 |
|
---|
109 | static const size_t s_maxTransitionLength = 64;
|
---|
110 |
|
---|
111 | bool m_isDictionary;
|
---|
112 |
|
---|
113 | JSValue* m_prototype;
|
---|
114 | RefPtr<StructureIDChain> m_cachedPrototypeChain;
|
---|
115 |
|
---|
116 | RefPtr<StructureID> m_previous;
|
---|
117 | UString::Rep* m_nameInPrevious;
|
---|
118 | unsigned m_attributesInPrevious;
|
---|
119 |
|
---|
120 | size_t m_transitionCount;
|
---|
121 | TransitionTable m_transitionTable;
|
---|
122 |
|
---|
123 | PropertyMap m_propertyMap;
|
---|
124 | };
|
---|
125 |
|
---|
126 | class StructureIDChain : public RefCounted<StructureIDChain> {
|
---|
127 | public:
|
---|
128 | static PassRefPtr<StructureIDChain> create(StructureID* structureID) { return adoptRef(new StructureIDChain(structureID)); }
|
---|
129 |
|
---|
130 | RefPtr<StructureID>* head() { return m_vector.get(); }
|
---|
131 |
|
---|
132 | private:
|
---|
133 | StructureIDChain(StructureID* structureID);
|
---|
134 |
|
---|
135 | OwnArrayPtr<RefPtr<StructureID> > m_vector;
|
---|
136 | };
|
---|
137 |
|
---|
138 | } // namespace JSC
|
---|
139 |
|
---|
140 | #endif // StructureID_h
|
---|