Ensure that all descriptors have a valid enumeration index, and replace NextEnumIndex...
[platform/upstream/v8.git] / src / transitions-inl.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_TRANSITIONS_INL_H_
29 #define V8_TRANSITIONS_INL_H_
30
31 #include "objects-inl.h"
32 #include "transitions.h"
33
34 namespace v8 {
35 namespace internal {
36
37
38 #define FIELD_ADDR(p, offset) \
39   (reinterpret_cast<byte*>(p) + offset - kHeapObjectTag)
40
41 #define WRITE_FIELD(p, offset, value) \
42   (*reinterpret_cast<Object**>(FIELD_ADDR(p, offset)) = value)
43
44 #define CONDITIONAL_WRITE_BARRIER(heap, object, offset, value, mode)    \
45   if (mode == UPDATE_WRITE_BARRIER) {                                   \
46     heap->incremental_marking()->RecordWrite(                           \
47       object, HeapObject::RawField(object, offset), value);             \
48     if (heap->InNewSpace(value)) {                                      \
49       heap->RecordWrite(object->address(), offset);                     \
50     }                                                                   \
51   }
52
53
54 TransitionArray* TransitionArray::cast(Object* object) {
55   ASSERT(object->IsTransitionArray());
56   return reinterpret_cast<TransitionArray*>(object);
57 }
58
59
60 Map* TransitionArray::elements_transition() {
61   Object* transition_map = get(kElementsTransitionIndex);
62   return Map::cast(transition_map);
63 }
64
65
66 void TransitionArray::ClearElementsTransition() {
67   WRITE_FIELD(this, kElementsTransitionOffset, Smi::FromInt(0));
68 }
69
70
71 bool TransitionArray::HasElementsTransition() {
72   return get(kElementsTransitionIndex) != Smi::FromInt(0);
73 }
74
75
76 void TransitionArray::set_elements_transition(Map* transition_map,
77                                               WriteBarrierMode mode) {
78   Heap* heap = GetHeap();
79   WRITE_FIELD(this, kElementsTransitionOffset, transition_map);
80   CONDITIONAL_WRITE_BARRIER(
81       heap, this, kElementsTransitionOffset, transition_map, mode);
82 }
83
84
85 bool TransitionArray::HasPrototypeTransitions() {
86   Object* prototype_transitions = get(kPrototypeTransitionsIndex);
87   return prototype_transitions != Smi::FromInt(0);
88 }
89
90
91 FixedArray* TransitionArray::GetPrototypeTransitions() {
92   Object* prototype_transitions = get(kPrototypeTransitionsIndex);
93   return FixedArray::cast(prototype_transitions);
94 }
95
96
97 HeapObject* TransitionArray::UncheckedPrototypeTransitions() {
98   Object* prototype_transitions = get(kPrototypeTransitionsIndex);
99   if (prototype_transitions == Smi::FromInt(0)) return NULL;
100   return reinterpret_cast<HeapObject*>(prototype_transitions);
101 }
102
103
104 void TransitionArray::SetPrototypeTransitions(FixedArray* transitions,
105                                               WriteBarrierMode mode) {
106   ASSERT(this != NULL);
107   ASSERT(transitions->IsFixedArray());
108   Heap* heap = GetHeap();
109   WRITE_FIELD(this, kPrototypeTransitionsOffset, transitions);
110   CONDITIONAL_WRITE_BARRIER(
111       heap, this, kPrototypeTransitionsOffset, transitions, mode);
112 }
113
114
115 Object** TransitionArray::GetPrototypeTransitionsSlot() {
116   return HeapObject::RawField(reinterpret_cast<HeapObject*>(this),
117                               kPrototypeTransitionsOffset);
118 }
119
120
121 Object** TransitionArray::GetKeySlot(int transition_number) {
122   ASSERT(transition_number < number_of_transitions());
123   return HeapObject::RawField(
124       reinterpret_cast<HeapObject*>(this),
125       OffsetOfElementAt(ToKeyIndex(transition_number)));
126 }
127
128
129 String* TransitionArray::GetKey(int transition_number) {
130   ASSERT(transition_number < number_of_transitions());
131   return String::cast(get(ToKeyIndex(transition_number)));
132 }
133
134
135 void TransitionArray::SetKey(int transition_number, String* key) {
136   ASSERT(transition_number < number_of_transitions());
137   set(ToKeyIndex(transition_number), key);
138 }
139
140
141 Object* TransitionArray::GetValue(int transition_number) {
142   ASSERT(transition_number < number_of_transitions());
143   return get(ToValueIndex(transition_number));
144 }
145
146
147 Object** TransitionArray::GetValueSlot(int transition_number) {
148   ASSERT(transition_number < number_of_transitions());
149   return HeapObject::RawField(
150       reinterpret_cast<HeapObject*>(this),
151       OffsetOfElementAt(ToValueIndex(transition_number)));
152 }
153
154
155 void TransitionArray::SetValue(int transition_number, Object* value) {
156   ASSERT(transition_number < number_of_transitions());
157   set(ToValueIndex(transition_number), value);
158 }
159
160
161 Map* TransitionArray::GetTargetMap(int transition_number) {
162   Object* value = GetValue(transition_number);
163   if (value->IsAccessorPair()) {
164     // TODO(verwaest): Currently details are always taken from the getter if it
165     // is a transition, otherwise from the setter which in that case has to be a
166     // transition. Details should be dependent on which component is requested.
167     AccessorPair* accessors = AccessorPair::cast(value);
168     if (accessors->getter()->IsMap()) {
169       return Map::cast(accessors->getter());
170     }
171     return Map::cast(accessors->setter());
172   }
173   return Map::cast(value);
174 }
175
176
177 PropertyDetails TransitionArray::GetTargetDetails(int transition_number) {
178   Map* map = GetTargetMap(transition_number);
179   DescriptorArray* descriptors = map->instance_descriptors();
180   int descriptor = descriptors->LastAdded();
181   ASSERT(descriptor != DescriptorArray::kNotFound);
182   return descriptors->GetDetails(descriptor);
183 }
184
185
186 Object** TransitionArray::GetElementsTransitionSlot() {
187   return HeapObject::RawField(reinterpret_cast<HeapObject*>(this),
188                               kElementsTransitionOffset);
189 }
190
191
192 int TransitionArray::Search(String* name) {
193   return internal::Search(this, name);
194 }
195
196
197 void TransitionArray::Set(int transition_number,
198                           String* key,
199                           Object* value,
200                           const WhitenessWitness&) {
201   NoIncrementalWriteBarrierSet(this,
202                                ToKeyIndex(transition_number),
203                                key);
204   NoIncrementalWriteBarrierSet(this,
205                                ToValueIndex(transition_number),
206                                value);
207 }
208
209
210 #undef FIELD_ADDR
211 #undef WRITE_FIELD
212 #undef CONDITIONAL_WRITE_BARRIER
213
214
215 } }  // namespace v8::internal
216
217 #endif  // V8_TRANSITIONS_INL_H_