OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/conversions-inl.h" | 8 #include "src/conversions-inl.h" |
9 #include "src/elements.h" | 9 #include "src/elements.h" |
10 #include "src/factory.h" | 10 #include "src/factory.h" |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // Returns an array that tells you where in the [0, length) interval an array | 190 // Returns an array that tells you where in the [0, length) interval an array |
191 // might have elements. Can either return an array of keys (positive integers | 191 // might have elements. Can either return an array of keys (positive integers |
192 // or undefined) or a number representing the positive length of an interval | 192 // or undefined) or a number representing the positive length of an interval |
193 // starting at index 0. | 193 // starting at index 0. |
194 // Intervals can span over some keys that are not in the object. | 194 // Intervals can span over some keys that are not in the object. |
195 RUNTIME_FUNCTION(Runtime_GetArrayKeys) { | 195 RUNTIME_FUNCTION(Runtime_GetArrayKeys) { |
196 HandleScope scope(isolate); | 196 HandleScope scope(isolate); |
197 DCHECK(args.length() == 2); | 197 DCHECK(args.length() == 2); |
198 CONVERT_ARG_HANDLE_CHECKED(JSObject, array, 0); | 198 CONVERT_ARG_HANDLE_CHECKED(JSObject, array, 0); |
199 CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); | 199 CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
200 if (array->elements()->IsDictionary()) { | 200 |
201 Handle<FixedArray> keys = isolate->factory()->empty_fixed_array(); | 201 if (!array->elements()->IsDictionary()) { |
202 for (PrototypeIterator iter(isolate, array, | |
203 PrototypeIterator::START_AT_RECEIVER); | |
204 !iter.IsAtEnd(); iter.Advance()) { | |
205 if (PrototypeIterator::GetCurrent(iter)->IsJSProxy() || | |
206 PrototypeIterator::GetCurrent<JSObject>(iter) | |
207 ->HasIndexedInterceptor()) { | |
208 // Bail out if we find a proxy or interceptor, likely not worth | |
209 // collecting keys in that case. | |
210 return *isolate->factory()->NewNumberFromUint(length); | |
211 } | |
212 Handle<JSObject> current = PrototypeIterator::GetCurrent<JSObject>(iter); | |
213 Handle<FixedArray> current_keys = | |
214 isolate->factory()->NewFixedArray(current->NumberOfOwnElements(NONE)); | |
215 current->GetOwnElementKeys(*current_keys, NONE); | |
216 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
217 isolate, keys, FixedArray::UnionOfKeys(keys, current_keys)); | |
218 } | |
219 // Erase any keys >= length. | |
220 // TODO(adamk): Remove this step when the contract of %GetArrayKeys | |
221 // is changed to let this happen on the JS side. | |
222 for (int i = 0; i < keys->length(); i++) { | |
223 if (NumberToUint32(keys->get(i)) >= length) keys->set_undefined(i); | |
224 } | |
225 return *isolate->factory()->NewJSArrayWithElements(keys); | |
226 } else { | |
227 RUNTIME_ASSERT(array->HasFastSmiOrObjectElements() || | 202 RUNTIME_ASSERT(array->HasFastSmiOrObjectElements() || |
228 array->HasFastDoubleElements()); | 203 array->HasFastDoubleElements()); |
229 uint32_t actual_length = static_cast<uint32_t>(array->elements()->length()); | 204 uint32_t actual_length = static_cast<uint32_t>(array->elements()->length()); |
230 return *isolate->factory()->NewNumberFromUint(Min(actual_length, length)); | 205 return *isolate->factory()->NewNumberFromUint(Min(actual_length, length)); |
231 } | 206 } |
| 207 |
| 208 KeyAccumulator accumulator(isolate); |
| 209 for (PrototypeIterator iter(isolate, array, |
| 210 PrototypeIterator::START_AT_RECEIVER); |
| 211 !iter.IsAtEnd(); iter.Advance()) { |
| 212 if (PrototypeIterator::GetCurrent(iter)->IsJSProxy() || |
| 213 PrototypeIterator::GetCurrent<JSObject>(iter) |
| 214 ->HasIndexedInterceptor()) { |
| 215 // Bail out if we find a proxy or interceptor, likely not worth |
| 216 // collecting keys in that case. |
| 217 return *isolate->factory()->NewNumberFromUint(length); |
| 218 } |
| 219 Handle<JSObject> current = PrototypeIterator::GetCurrent<JSObject>(iter); |
| 220 Handle<FixedArray> current_keys = |
| 221 isolate->factory()->NewFixedArray(current->NumberOfOwnElements(NONE)); |
| 222 current->GetOwnElementKeys(*current_keys, NONE); |
| 223 accumulator.AddKeys(current_keys, FixedArray::ALL_KEYS); |
| 224 } |
| 225 // Erase any keys >= length. |
| 226 // TODO(adamk): Remove this step when the contract of %GetArrayKeys |
| 227 // is changed to let this happen on the JS side. |
| 228 Handle<FixedArray> keys = accumulator.GetKeys(); |
| 229 for (int i = 0; i < keys->length(); i++) { |
| 230 if (NumberToUint32(keys->get(i)) >= length) keys->set_undefined(i); |
| 231 } |
| 232 return *isolate->factory()->NewJSArrayWithElements(keys); |
232 } | 233 } |
233 | 234 |
234 | 235 |
235 static Object* ArrayConstructorCommon(Isolate* isolate, | 236 static Object* ArrayConstructorCommon(Isolate* isolate, |
236 Handle<JSFunction> constructor, | 237 Handle<JSFunction> constructor, |
237 Handle<JSFunction> original_constructor, | 238 Handle<JSFunction> original_constructor, |
238 Handle<AllocationSite> site, | 239 Handle<AllocationSite> site, |
239 Arguments* caller_args) { | 240 Arguments* caller_args) { |
240 Factory* factory = isolate->factory(); | 241 Factory* factory = isolate->factory(); |
241 | 242 |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 | 492 |
492 RUNTIME_FUNCTION(Runtime_FastOneByteArrayJoin) { | 493 RUNTIME_FUNCTION(Runtime_FastOneByteArrayJoin) { |
493 SealHandleScope shs(isolate); | 494 SealHandleScope shs(isolate); |
494 DCHECK(args.length() == 2); | 495 DCHECK(args.length() == 2); |
495 // Returning undefined means that this fast path fails and one has to resort | 496 // Returning undefined means that this fast path fails and one has to resort |
496 // to a slow path. | 497 // to a slow path. |
497 return isolate->heap()->undefined_value(); | 498 return isolate->heap()->undefined_value(); |
498 } | 499 } |
499 } // namespace internal | 500 } // namespace internal |
500 } // namespace v8 | 501 } // namespace v8 |
OLD | NEW |