blob: f2e49ecafec3af954a24602e42a85f5f2f1a7707 [file] [log] [blame]
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00001// 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#include "v8.h"
29
30#include "objects.h"
31#include "transitions-inl.h"
32#include "utils.h"
33
34namespace v8 {
35namespace internal {
36
37
38MaybeObject* TransitionArray::Allocate(int number_of_transitions) {
39 Heap* heap = Isolate::Current()->heap();
40 // Use FixedArray to not use DescriptorArray::cast on incomplete object.
41 FixedArray* array;
42 { MaybeObject* maybe_array =
43 heap->AllocateFixedArray(ToKeyIndex(number_of_transitions));
44 if (!maybe_array->To(&array)) return maybe_array;
45 }
46
47 array->set(kElementsTransitionIndex, Smi::FromInt(0));
danno@chromium.org81cac2b2012-07-10 11:28:27 +000048 array->set(kPrototypeTransitionsIndex, Smi::FromInt(0));
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000049 return array;
50}
51
52
53void TransitionArray::CopyFrom(TransitionArray* origin,
54 int origin_transition,
55 int target_transition,
56 const WhitenessWitness& witness) {
verwaest@chromium.org753aee42012-07-17 16:15:42 +000057 Set(target_transition,
58 origin->GetKey(origin_transition),
59 origin->GetTarget(origin_transition),
60 witness);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000061}
62
63
64static bool InsertionPointFound(String* key1, String* key2) {
65 return key1->Hash() > key2->Hash();
66}
67
68
verwaest@chromium.org753aee42012-07-17 16:15:42 +000069MaybeObject* TransitionArray::NewWith(String* name, Map* target) {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000070 TransitionArray* result;
71
72 { MaybeObject* maybe_array;
73 maybe_array = TransitionArray::Allocate(1);
74 if (!maybe_array->To(&result)) return maybe_array;
75 }
76
77 FixedArray::WhitenessWitness witness(result);
78
verwaest@chromium.org753aee42012-07-17 16:15:42 +000079 result->Set(0, name, target, witness);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000080 return result;
81}
82
83
verwaest@chromium.org753aee42012-07-17 16:15:42 +000084MaybeObject* TransitionArray::CopyInsert(String* name, Map* target) {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000085 TransitionArray* result;
86
87 int number_of_transitions = this->number_of_transitions();
88 int new_size = number_of_transitions;
89
90 int insertion_index = this->Search(name);
91 if (insertion_index == kNotFound) ++new_size;
92
verwaest@chromium.org753aee42012-07-17 16:15:42 +000093 MaybeObject* maybe_array;
94 maybe_array = TransitionArray::Allocate(new_size);
95 if (!maybe_array->To(&result)) return maybe_array;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000096
97 if (HasElementsTransition()) {
98 result->set_elements_transition(elements_transition());
99 }
100
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000101 if (HasPrototypeTransitions()) {
102 result->SetPrototypeTransitions(GetPrototypeTransitions());
103 }
104
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000105 FixedArray::WhitenessWitness witness(result);
106
107 if (insertion_index != kNotFound) {
108 for (int i = 0; i < number_of_transitions; ++i) {
109 if (i != insertion_index) result->CopyFrom(this, i, i, witness);
110 }
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000111 result->Set(insertion_index, name, target, witness);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000112 return result;
113 }
114
115 insertion_index = 0;
116 for (; insertion_index < number_of_transitions; ++insertion_index) {
117 if (InsertionPointFound(GetKey(insertion_index), name)) break;
118 result->CopyFrom(this, insertion_index, insertion_index, witness);
119 }
120
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000121 result->Set(insertion_index, name, target, witness);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000122
123 for (; insertion_index < number_of_transitions; ++insertion_index) {
124 result->CopyFrom(this, insertion_index, insertion_index + 1, witness);
125 }
126
127 return result;
128}
129
130
131} } // namespace v8::internal