fbf7daa2c40263be2371ad53c6b329d5f6312c09
[WebKit-https.git] / JavaScriptCore / kjs / StructureID.cpp
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 #include "config.h"
27 #include "StructureID.h"
28
29 #include "identifier.h"
30 #include "JSObject.h"
31 #include <wtf/RefPtr.h>
32
33 using namespace std;
34
35 namespace JSC {
36
37     StructureID::StructureID(JSValue* prototype, JSType type)
38     : m_isDictionary(false)
39     , m_type(type)
40     , m_prototype(prototype)
41     , m_cachedPrototypeChain(0)
42     , m_previous(0)
43     , m_nameInPrevious(0)
44     , m_transitionCount(0)
45 {
46     ASSERT(m_prototype);
47     ASSERT(m_prototype->isObject() || m_prototype->isNull());
48 }
49
50 PassRefPtr<StructureID> StructureID::addPropertyTransition(StructureID* structureID, const Identifier& propertyName, JSValue* value, unsigned attributes, JSObject* slotBase, PutPropertySlot& slot, PropertyStorage& propertyStorage)
51 {
52     ASSERT(!structureID->m_isDictionary);
53     ASSERT(structureID->m_type == ObjectType);
54
55     if (StructureID* existingTransition = structureID->m_transitionTable.get(make_pair(propertyName.ustring().rep(), attributes))) {
56         if (!slotBase->usingInlineStorage() && structureID->m_propertyMap.size() != existingTransition->m_propertyMap.size())
57             slotBase->allocatePropertyStorage(structureID->m_propertyMap.size(), existingTransition->m_propertyMap.size());
58
59         size_t offset = existingTransition->propertyMap().getOffset(propertyName);
60         ASSERT(offset != WTF::notFound);
61         propertyStorage[offset] = value;
62         slot.setNewProperty(slotBase, offset);
63
64         return existingTransition;
65     }
66
67     if (structureID->m_transitionCount > s_maxTransitionLength) {
68         RefPtr<StructureID> transition = toDictionaryTransition(structureID);
69         transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
70         return transition.release();
71     }
72
73     RefPtr<StructureID> transition = create(structureID->m_prototype);
74     transition->m_cachedPrototypeChain = structureID->m_cachedPrototypeChain;
75     transition->m_previous = structureID;
76     transition->m_nameInPrevious = propertyName.ustring().rep();
77     transition->m_attributesInPrevious = attributes;
78     transition->m_transitionCount = structureID->m_transitionCount + 1;
79     transition->m_propertyMap = structureID->m_propertyMap;
80
81     transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
82
83     structureID->m_transitionTable.add(make_pair(propertyName.ustring().rep(), attributes), transition.get());
84     return transition.release();
85 }
86
87 PassRefPtr<StructureID> StructureID::toDictionaryTransition(StructureID* structureID)
88 {
89     ASSERT(!structureID->m_isDictionary);
90
91     RefPtr<StructureID> transition = create(structureID->m_prototype);
92     transition->m_isDictionary = true;
93     transition->m_propertyMap = structureID->m_propertyMap;
94     return transition.release();
95 }
96
97 PassRefPtr<StructureID> StructureID::fromDictionaryTransition(StructureID* structureID)
98 {
99     ASSERT(structureID->m_isDictionary);
100
101     // Since dictionary StructureIDs are not shared, and no opcodes specialize
102     // for them, we don't need to allocate a new StructureID when transitioning
103     // to non-dictionary status.
104     structureID->m_isDictionary = false;
105     return structureID;
106 }
107
108 PassRefPtr<StructureID> StructureID::changePrototypeTransition(StructureID* structureID, JSValue* prototype)
109 {
110     RefPtr<StructureID> transition = create(prototype);
111     transition->m_transitionCount = structureID->m_transitionCount + 1;
112     transition->m_propertyMap = structureID->m_propertyMap;
113     return transition.release();
114 }
115
116 PassRefPtr<StructureID> StructureID::getterSetterTransition(StructureID* structureID)
117 {
118     RefPtr<StructureID> transition = create(structureID->storedPrototype());
119     transition->m_transitionCount = structureID->m_transitionCount + 1;
120     transition->m_propertyMap = structureID->m_propertyMap;
121     return transition.release();
122 }
123
124 StructureID::~StructureID()
125 {
126     if (m_previous) {
127         ASSERT(m_previous->m_transitionTable.contains(make_pair(m_nameInPrevious, m_attributesInPrevious)));
128         m_previous->m_transitionTable.remove(make_pair(m_nameInPrevious, m_attributesInPrevious));
129     }
130 }
131
132 StructureIDChain::StructureIDChain(StructureID* structureID)
133 {
134     size_t size = 1;
135
136     StructureID* tmp = structureID;
137     while (!tmp->storedPrototype()->isNull()) {
138         ++size;
139         tmp = static_cast<JSCell*>(tmp->storedPrototype())->structureID();
140     }
141     
142     m_vector.set(new RefPtr<StructureID>[size]);
143
144     size_t i;
145     for (i = 0; i < size - 1; ++i) {
146         m_vector[i] = structureID;
147         structureID = static_cast<JSObject*>(structureID->storedPrototype())->structureID();
148     }
149     m_vector[i] = structureID;
150 }
151
152 } // namespace JSC