OLD | NEW |
(Empty) | |
| 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 #include "v8.h" |
| 29 |
| 30 #include "objects.h" |
| 31 #include "transitions-inl.h" |
| 32 #include "transitions.h" |
| 33 #include "utils.h" |
| 34 |
| 35 namespace v8 { |
| 36 namespace internal { |
| 37 |
| 38 |
| 39 MaybeObject* TransitionArray::Allocate(int number_of_transitions) { |
| 40 Heap* heap = Isolate::Current()->heap(); |
| 41 // Use FixedArray to not use DescriptorArray::cast on incomplete object. |
| 42 FixedArray* array; |
| 43 { MaybeObject* maybe_array = |
| 44 heap->AllocateFixedArray(ToKeyIndex(number_of_transitions)); |
| 45 if (!maybe_array->To(&array)) return maybe_array; |
| 46 } |
| 47 |
| 48 array->set(kElementsTransitionIndex, Smi::FromInt(0)); |
| 49 return array; |
| 50 } |
| 51 |
| 52 |
| 53 void TransitionArray::CopyFrom(TransitionArray* origin, |
| 54 int origin_transition, |
| 55 int target_transition) { |
| 56 this->Set(target_transition, |
| 57 origin->GetKey(origin_transition), |
| 58 origin->GetValue(origin_transition)); |
| 59 } |
| 60 |
| 61 |
| 62 static bool InsertionPointFound(String* key1, String* key2) { |
| 63 return key1->Hash() > key2->Hash(); |
| 64 } |
| 65 |
| 66 |
| 67 MaybeObject* TransitionArray::NewWith(String* name, Object* value) { |
| 68 TransitionArray* result; |
| 69 |
| 70 { MaybeObject* maybe_array; |
| 71 maybe_array = TransitionArray::Allocate(1); |
| 72 if (!maybe_array->To(&result)) return maybe_array; |
| 73 } |
| 74 |
| 75 result->Set(0, name, value); |
| 76 return result; |
| 77 } |
| 78 |
| 79 |
| 80 MaybeObject* TransitionArray::CopyInsert(String* name, Object* value) { |
| 81 TransitionArray* result; |
| 82 |
| 83 int number_of_transitions = this->number_of_transitions(); |
| 84 int new_size = number_of_transitions; |
| 85 |
| 86 int insertion_index = this->Search(name); |
| 87 if (insertion_index == kNotFound) ++new_size; |
| 88 |
| 89 { MaybeObject* maybe_array; |
| 90 maybe_array = TransitionArray::Allocate(new_size); |
| 91 if (!maybe_array->To(&result)) return maybe_array; |
| 92 } |
| 93 |
| 94 if (HasElementsTransition()) { |
| 95 result->set_elements_transition(elements_transition()); |
| 96 } |
| 97 |
| 98 if (insertion_index != kNotFound) { |
| 99 for (int i = 0; i < number_of_transitions; ++i) { |
| 100 if (i != insertion_index) result->CopyFrom(this, i, i); |
| 101 } |
| 102 result->Set(insertion_index, name, value); |
| 103 return result; |
| 104 } |
| 105 |
| 106 insertion_index = 0; |
| 107 for (; insertion_index < number_of_transitions; ++insertion_index) { |
| 108 if (InsertionPointFound(GetKey(insertion_index), name)) break; |
| 109 result->CopyFrom(this, insertion_index, insertion_index); |
| 110 } |
| 111 |
| 112 result->Set(insertion_index, name, value); |
| 113 |
| 114 for (; insertion_index < number_of_transitions; ++insertion_index) { |
| 115 result->CopyFrom(this, insertion_index, insertion_index + 1); |
| 116 } |
| 117 |
| 118 return result; |
| 119 } |
| 120 |
| 121 |
| 122 } } // namespace v8::internal |
OLD | NEW |