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.
27 #include "StructureID.h"
29 #include "identifier.h"
31 #include <wtf/RefPtr.h>
37 StructureID::StructureID(JSValue* prototype, JSType type)
38 : m_isDictionary(false)
40 , m_prototype(prototype)
41 , m_cachedPrototypeChain(0)
44 , m_transitionCount(0)
47 ASSERT(m_prototype->isObject() || m_prototype->isNull());
50 void StructureID::transitionTo(StructureID* oldStructureID, StructureID* newStructureID, JSObject* slotBase)
52 if (!slotBase->usingInlineStorage() && oldStructureID->m_propertyMap.size() != newStructureID->m_propertyMap.size())
53 slotBase->allocatePropertyStorage(oldStructureID->m_propertyMap.size(), newStructureID->m_propertyMap.size());
56 PassRefPtr<StructureID> StructureID::addPropertyTransition(StructureID* structureID, const Identifier& propertyName, JSValue* value, unsigned attributes, JSObject* slotBase, PutPropertySlot& slot, PropertyStorage& propertyStorage)
58 ASSERT(!structureID->m_isDictionary);
59 ASSERT(structureID->m_type == ObjectType);
61 if (StructureID* existingTransition = structureID->m_transitionTable.get(make_pair(propertyName.ustring().rep(), attributes))) {
62 if (!slotBase->usingInlineStorage() && structureID->m_propertyMap.size() != existingTransition->m_propertyMap.size())
63 slotBase->allocatePropertyStorage(structureID->m_propertyMap.size(), existingTransition->m_propertyMap.size());
65 size_t offset = existingTransition->propertyMap().getOffset(propertyName);
66 ASSERT(offset != WTF::notFound);
67 propertyStorage[offset] = value;
68 slot.setNewProperty(slotBase, offset);
70 return existingTransition;
73 if (structureID->m_transitionCount > s_maxTransitionLength) {
74 RefPtr<StructureID> transition = toDictionaryTransition(structureID);
75 transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
76 return transition.release();
79 RefPtr<StructureID> transition = create(structureID->m_prototype);
80 transition->m_cachedPrototypeChain = structureID->m_cachedPrototypeChain;
81 transition->m_previous = structureID;
82 transition->m_nameInPrevious = propertyName.ustring().rep();
83 transition->m_attributesInPrevious = attributes;
84 transition->m_transitionCount = structureID->m_transitionCount + 1;
85 transition->m_propertyMap = structureID->m_propertyMap;
87 transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
89 structureID->m_transitionTable.add(make_pair(propertyName.ustring().rep(), attributes), transition.get());
90 return transition.release();
93 PassRefPtr<StructureID> StructureID::toDictionaryTransition(StructureID* structureID)
95 ASSERT(!structureID->m_isDictionary);
97 RefPtr<StructureID> transition = create(structureID->m_prototype);
98 transition->m_isDictionary = true;
99 transition->m_propertyMap = structureID->m_propertyMap;
100 return transition.release();
103 PassRefPtr<StructureID> StructureID::fromDictionaryTransition(StructureID* structureID)
105 ASSERT(structureID->m_isDictionary);
107 // Since dictionary StructureIDs are not shared, and no opcodes specialize
108 // for them, we don't need to allocate a new StructureID when transitioning
109 // to non-dictionary status.
110 structureID->m_isDictionary = false;
114 PassRefPtr<StructureID> StructureID::changePrototypeTransition(StructureID* structureID, JSValue* prototype)
116 RefPtr<StructureID> transition = create(prototype);
117 transition->m_transitionCount = structureID->m_transitionCount + 1;
118 transition->m_propertyMap = structureID->m_propertyMap;
119 return transition.release();
122 PassRefPtr<StructureID> StructureID::getterSetterTransition(StructureID* structureID)
124 RefPtr<StructureID> transition = create(structureID->storedPrototype());
125 transition->m_transitionCount = structureID->m_transitionCount + 1;
126 transition->m_propertyMap = structureID->m_propertyMap;
127 return transition.release();
130 StructureID::~StructureID()
133 ASSERT(m_previous->m_transitionTable.contains(make_pair(m_nameInPrevious, m_attributesInPrevious)));
134 m_previous->m_transitionTable.remove(make_pair(m_nameInPrevious, m_attributesInPrevious));
138 StructureIDChain::StructureIDChain(StructureID* structureID)
142 StructureID* tmp = structureID;
143 while (!tmp->storedPrototype()->isNull()) {
145 tmp = static_cast<JSCell*>(tmp->storedPrototype())->structureID();
148 m_vector.set(new RefPtr<StructureID>[size + 1]);
151 for (i = 0; i < size - 1; ++i) {
152 m_vector[i] = structureID;
153 structureID = static_cast<JSObject*>(structureID->storedPrototype())->structureID();
155 m_vector[i] = structureID;