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 PassRefPtr<StructureID> StructureID::addPropertyTransition(StructureID* structureID, const Identifier& propertyName, JSValue* value, unsigned attributes, JSObject* slotBase, PutPropertySlot& slot, PropertyStorage& propertyStorage)
52 ASSERT(!structureID->m_isDictionary);
53 ASSERT(structureID->m_type == ObjectType);
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());
59 size_t offset = existingTransition->propertyMap().getOffset(propertyName);
60 ASSERT(offset != WTF::notFound);
61 propertyStorage[offset] = value;
62 slot.setNewProperty(slotBase, offset);
64 return existingTransition;
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();
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;
81 transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
83 structureID->m_transitionTable.add(make_pair(propertyName.ustring().rep(), attributes), transition.get());
84 return transition.release();
87 PassRefPtr<StructureID> StructureID::toDictionaryTransition(StructureID* structureID)
89 ASSERT(!structureID->m_isDictionary);
91 RefPtr<StructureID> transition = create(structureID->m_prototype);
92 transition->m_isDictionary = true;
93 transition->m_propertyMap = structureID->m_propertyMap;
94 return transition.release();
97 PassRefPtr<StructureID> StructureID::fromDictionaryTransition(StructureID* structureID)
99 ASSERT(structureID->m_isDictionary);
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;
108 PassRefPtr<StructureID> StructureID::changePrototypeTransition(StructureID* structureID, JSValue* prototype)
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();
116 PassRefPtr<StructureID> StructureID::getterSetterTransition(StructureID* structureID)
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();
124 StructureID::~StructureID()
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));
132 StructureIDChain::StructureIDChain(StructureID* structureID)
136 StructureID* tmp = structureID;
137 while (!tmp->storedPrototype()->isNull()) {
139 tmp = static_cast<JSCell*>(tmp->storedPrototype())->structureID();
142 m_vector.set(new RefPtr<StructureID>[size]);
145 for (i = 0; i < size - 1; ++i) {
146 m_vector[i] = structureID;
147 structureID = static_cast<JSObject*>(structureID->storedPrototype())->structureID();
149 m_vector[i] = structureID;