Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: src/key-accumulator.cc

Issue 1425403002: [runtime] Support Symbols in KeyAccumulator (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: adressing nits + adding separate file for KeyAccumulator Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/key-accumulator.h"
6
7 #include "src/elements.h"
8 #include "src/factory.h"
9 #include "src/objects-inl.h"
10
11
12 namespace v8 {
13 namespace internal {
14
15
16 KeyAccumulator::~KeyAccumulator() {
17 for (size_t i = 0; i < elements_.size(); i++) {
18 delete elements_[i];
19 }
20 }
21
22
23 Handle<FixedArray> KeyAccumulator::GetKeys(GetKeysConversion convert) {
24 if (length_ == 0) {
25 return isolate_->factory()->empty_fixed_array();
26 }
27 // Make sure we have all the lengths collected.
28 NextPrototype();
29
30 // Assemble the result array by first adding the element keys and then
31 // the property keys. We use the total number of keys per level in
32 // |protoLengths_| and the available element keys in the corresponding bucket
33 // in |elements_| to deduce the number of keys to take from the |properties_|
34 // set.
35 Handle<FixedArray> result = isolate_->factory()->NewFixedArray(length_);
36 int insertion_index = 0;
37 int string_properties_index = 0;
38 int symbol_properties_index = 0;
39 // String and Symbol lengths always come in pairs:
40 size_t max_level = level_lengths_.size() / 2;
41 for (size_t level = 0; level < max_level; level++) {
42 int num_string_properties = level_lengths_[level * 2];
43 int num_symbol_properties = level_lengths_[level * 2 + 1];
44 if (num_string_properties < 0) {
45 // If the num_string_properties is negative, the current level contains
46 // properties from a proxy, hence we skip the integer keys in |elements_|
47 // since proxies define the complete ordering.
48 num_string_properties = -num_string_properties;
49 } else if (level < elements_.size()) {
50 // Add the element indices for this prototype level.
51 std::vector<uint32_t>* elements = elements_[level];
52 int num_elements = static_cast<int>(elements->size());
53 for (int i = 0; i < num_elements; i++) {
54 Handle<Object> key;
55 if (convert == KEEP_NUMBERS) {
56 key = isolate_->factory()->NewNumberFromUint(elements->at(i));
57 } else {
58 key = isolate_->factory()->Uint32ToString(elements->at(i));
59 }
60 result->set(insertion_index, *key);
61 insertion_index++;
62 }
63 }
64 // Add the string property keys for this prototype level.
65 for (int i = 0; i < num_string_properties; i++) {
66 Object* key = string_properties_->KeyAt(string_properties_index);
67 result->set(insertion_index, key);
68 insertion_index++;
69 string_properties_index++;
70 }
71 // Add the symbol property keys for this prototype level.
72 for (int i = 0; i < num_symbol_properties; i++) {
73 Object* key = symbol_properties_->KeyAt(symbol_properties_index);
74 result->set(insertion_index, key);
75 insertion_index++;
76 symbol_properties_index++;
77 }
78 }
79
80 DCHECK_EQ(insertion_index, length_);
81 return result;
82 }
83
84
85 namespace {
86
87 bool AccumulatorHasKey(std::vector<uint32_t>* sub_elements, uint32_t key) {
88 return std::binary_search(sub_elements->begin(), sub_elements->end(), key);
89 }
90
91 } // namespace
92
93
94 bool KeyAccumulator::AddKey(uint32_t key) {
95 // Make sure we do not add keys to a proxy-level (see AddKeysFromProxy).
96 // We mark proxy-levels with a negative length
97 DCHECK_LE(0, level_string_length_);
98 // Binary search over all but the last level. The last one might not be
99 // sorted yet.
100 for (size_t i = 1; i < elements_.size(); i++) {
101 if (AccumulatorHasKey(elements_[i - 1], key)) return false;
102 }
103 elements_.back()->push_back(key);
104 length_++;
105 return true;
106 }
107
108
109 bool KeyAccumulator::AddKey(Object* key, AddKeyConversion convert) {
110 return AddKey(handle(key, isolate_), convert);
111 }
112
113
114 bool KeyAccumulator::AddKey(Handle<Object> key, AddKeyConversion convert) {
115 if (key->IsSymbol()) {
116 if (filter_ == SKIP_SYMBOLS) return false;
117 return AddSymbolKey(key);
118 }
119 // Make sure we do not add keys to a proxy-level (see AddKeysFromProxy).
120 DCHECK_LE(0, level_string_length_);
121 // In some cases (e.g. proxies) we might get in String-converted ints which
122 // should be added to the elements list instead of the properties. For
123 // proxies we have to convert as well but also respect the original order.
124 // Therefore we add a converted key to both sides
125 if (convert == CONVERT_TO_ARRAY_INDEX || convert == PROXY_MAGIC) {
126 uint32_t index = 0;
127 int prev_length = length_;
128 int prev_proto = level_string_length_;
129 bool was_array_index = false;
130 bool key_was_added = false;
131 if ((key->IsString() && Handle<String>::cast(key)->AsArrayIndex(&index)) ||
132 key->ToArrayIndex(&index)) {
133 key_was_added = AddKey(index);
134 was_array_index = true;
135 if (convert == CONVERT_TO_ARRAY_INDEX) return key_was_added;
136 }
137 if (was_array_index && convert == PROXY_MAGIC) {
138 // If we had an array index (number) and it wasn't added, the key
139 // already existed before, hence we cannot add it to the properties
140 // keys as it would lead to duplicate entries.
141 if (!key_was_added) {
142 return false;
143 }
144 length_ = prev_length;
145 level_string_length_ = prev_proto;
146 }
147 }
148 return AddStringKey(key, convert);
149 }
150
151
152 bool KeyAccumulator::AddStringKey(Handle<Object> key,
153 AddKeyConversion convert) {
154 if (string_properties_.is_null()) {
155 string_properties_ = OrderedHashSet::Allocate(isolate_, 16);
156 }
157 // TODO(cbruni): remove this conversion once we throw the correct TypeError
158 // for non-string/symbol elements returned by proxies
159 if (convert == PROXY_MAGIC && key->IsNumber()) {
160 key = isolate_->factory()->NumberToString(key);
161 }
162 int prev_size = string_properties_->NumberOfElements();
163 string_properties_ = OrderedHashSet::Add(string_properties_, key);
164 if (prev_size < string_properties_->NumberOfElements()) {
165 length_++;
166 level_string_length_++;
167 return true;
168 } else {
169 return false;
170 }
171 }
172
173
174 bool KeyAccumulator::AddSymbolKey(Handle<Object> key) {
175 if (symbol_properties_.is_null()) {
176 symbol_properties_ = OrderedHashSet::Allocate(isolate_, 16);
177 }
178 int prev_size = symbol_properties_->NumberOfElements();
179 symbol_properties_ = OrderedHashSet::Add(symbol_properties_, key);
180 if (prev_size < symbol_properties_->NumberOfElements()) {
181 length_++;
182 level_symbol_length_++;
183 return true;
184 } else {
185 return false;
186 }
187 }
188
189
190 void KeyAccumulator::AddKeys(Handle<FixedArray> array,
191 AddKeyConversion convert) {
192 int add_length = array->length();
193 if (add_length == 0) return;
194 for (int i = 0; i < add_length; i++) {
195 Handle<Object> current(array->get(i), isolate_);
196 AddKey(current, convert);
197 }
198 }
199
200
201 void KeyAccumulator::AddKeys(Handle<JSObject> array_like,
202 AddKeyConversion convert) {
203 DCHECK(array_like->IsJSArray() || array_like->HasSloppyArgumentsElements());
204 ElementsAccessor* accessor = array_like->GetElementsAccessor();
205 accessor->AddElementsToKeyAccumulator(array_like, this, convert);
206 }
207
208
209 void KeyAccumulator::AddKeysFromProxy(Handle<JSObject> array_like) {
210 // Proxies define a complete list of keys with no distinction of
211 // elements and properties, which breaks the normal assumption for the
212 // KeyAccumulator.
213 AddKeys(array_like, PROXY_MAGIC);
214 // Invert the current length to indicate a present proxy, so we can ignore
215 // element keys for this level. Otherwise we would not fully respect the order
216 // given by the proxy.
217 level_string_length_ = -level_string_length_;
218 }
219
220
221 void KeyAccumulator::AddElementKeysFromInterceptor(
222 Handle<JSObject> array_like) {
223 AddKeys(array_like, CONVERT_TO_ARRAY_INDEX);
224 // The interceptor might introduce duplicates for the current level, since
225 // these keys get added after the objects's normal element keys.
226 SortCurrentElementsListRemoveDuplicates();
227 }
228
229
230 void KeyAccumulator::SortCurrentElementsListRemoveDuplicates() {
231 // Sort and remove duplicates from the current elements level and adjust.
232 // the lengths accordingly.
233 auto last_level = elements_.back();
234 size_t nof_removed_keys = last_level->size();
235 std::sort(last_level->begin(), last_level->end());
236 last_level->erase(std::unique(last_level->begin(), last_level->end()),
237 last_level->end());
238 // Adjust total length by the number of removed duplicates.
239 nof_removed_keys -= last_level->size();
240 length_ -= static_cast<int>(nof_removed_keys);
241 }
242
243
244 void KeyAccumulator::SortCurrentElementsList() {
245 if (elements_.empty()) return;
246 auto element_keys = elements_.back();
247 std::sort(element_keys->begin(), element_keys->end());
248 }
249
250
251 void KeyAccumulator::NextPrototype() {
252 // Store the protoLength on the first call of this method.
253 if (!elements_.empty()) {
254 level_lengths_.push_back(level_string_length_);
255 level_lengths_.push_back(level_symbol_length_);
256 }
257 elements_.push_back(new std::vector<uint32_t>());
258 level_string_length_ = 0;
259 level_symbol_length_ = 0;
260 }
261
262
263 } // namespace internal
264 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698