Line data Source code
1 : // Copyright 2016 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/builtins/builtins.h"
6 : #include "src/builtins/builtins-utils.h"
7 :
8 : #include "src/code-factory.h"
9 : #include "src/code-stub-assembler.h"
10 : #include "src/contexts.h"
11 : #include "src/counters.h"
12 : #include "src/elements.h"
13 : #include "src/isolate.h"
14 : #include "src/lookup.h"
15 : #include "src/objects-inl.h"
16 : #include "src/prototype.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 : namespace {
22 :
23 701935 : inline bool ClampedToInteger(Isolate* isolate, Object* object, int* out) {
24 : // This is an extended version of ECMA-262 7.1.11 handling signed values
25 : // Try to convert object to a number and clamp values to [kMinInt, kMaxInt]
26 701935 : if (object->IsSmi()) {
27 699679 : *out = Smi::ToInt(object);
28 699679 : return true;
29 2256 : } else if (object->IsHeapNumber()) {
30 : double value = HeapNumber::cast(object)->value();
31 840 : if (std::isnan(value)) {
32 0 : *out = 0;
33 840 : } else if (value > kMaxInt) {
34 140 : *out = kMaxInt;
35 700 : } else if (value < kMinInt) {
36 140 : *out = kMinInt;
37 : } else {
38 560 : *out = static_cast<int>(value);
39 : }
40 : return true;
41 1416 : } else if (object->IsNullOrUndefined(isolate)) {
42 456 : *out = 0;
43 456 : return true;
44 960 : } else if (object->IsBoolean()) {
45 0 : *out = object->IsTrue(isolate);
46 0 : return true;
47 : }
48 : return false;
49 : }
50 :
51 270430 : inline bool GetSloppyArgumentsLength(Isolate* isolate, Handle<JSObject> object,
52 : int* out) {
53 540860 : Context* context = *isolate->native_context();
54 : Map* map = object->map();
55 270841 : if (map != context->sloppy_arguments_map() &&
56 270778 : map != context->strict_arguments_map() &&
57 : map != context->fast_aliased_arguments_map()) {
58 : return false;
59 : }
60 : DCHECK(object->HasFastElements() || object->HasFastArgumentsElements());
61 : Object* len_obj = object->InObjectPropertyAt(JSArgumentsObject::kLengthIndex);
62 270238 : if (!len_obj->IsSmi()) return false;
63 270228 : *out = Max(0, Smi::ToInt(len_obj));
64 :
65 : FixedArray* parameters = FixedArray::cast(object->elements());
66 270228 : if (object->HasSloppyArgumentsElements()) {
67 : FixedArray* arguments = FixedArray::cast(parameters->get(1));
68 292 : return *out <= arguments->length();
69 : }
70 540164 : return *out <= parameters->length();
71 : }
72 :
73 : inline bool IsJSArrayFastElementMovingAllowed(Isolate* isolate,
74 : JSArray* receiver) {
75 6756217 : return JSObject::PrototypeHasNoElements(isolate, receiver);
76 : }
77 :
78 600025 : inline bool HasSimpleElements(JSObject* current) {
79 1200040 : return current->map()->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER &&
80 1200040 : !current->GetElementsAccessor()->HasAccessors(current);
81 : }
82 :
83 579108 : inline bool HasOnlySimpleReceiverElements(Isolate* isolate,
84 : JSObject* receiver) {
85 : // Check that we have no accessors on the receiver's elements.
86 579108 : if (!HasSimpleElements(receiver)) return false;
87 578908 : return JSObject::PrototypeHasNoElements(isolate, receiver);
88 : }
89 :
90 6844 : inline bool HasOnlySimpleElements(Isolate* isolate, JSReceiver* receiver) {
91 : DisallowHeapAllocation no_gc;
92 : PrototypeIterator iter(isolate, receiver, kStartAtReceiver);
93 20701 : for (; !iter.IsAtEnd(); iter.Advance()) {
94 41834 : if (iter.GetCurrent()->IsJSProxy()) return false;
95 20917 : JSObject* current = iter.GetCurrent<JSObject>();
96 20917 : if (!HasSimpleElements(current)) return false;
97 : }
98 : return true;
99 : }
100 :
101 : // Returns |false| if not applicable.
102 : MUST_USE_RESULT
103 6524016 : inline bool EnsureJSArrayWithWritableFastElements(Isolate* isolate,
104 : Handle<Object> receiver,
105 : BuiltinArguments* args,
106 : int first_added_arg) {
107 6524016 : if (!receiver->IsJSArray()) return false;
108 : Handle<JSArray> array = Handle<JSArray>::cast(receiver);
109 : ElementsKind origin_kind = array->GetElementsKind();
110 6519604 : if (IsDictionaryElementsKind(origin_kind)) return false;
111 6516111 : if (!array->map()->is_extensible()) return false;
112 6516111 : if (args == nullptr) return true;
113 :
114 : // If there may be elements accessors in the prototype chain, the fast path
115 : // cannot be used if there arguments to add to the array.
116 6269958 : if (!IsJSArrayFastElementMovingAllowed(isolate, *array)) return false;
117 :
118 : // Adding elements to the array prototype would break code that makes sure
119 : // it has no elements. Handle that elsewhere.
120 6263063 : if (isolate->IsAnyInitialArrayPrototype(array)) return false;
121 :
122 : // Need to ensure that the arguments passed in args can be contained in
123 : // the array.
124 : int args_length = args->length();
125 6263013 : if (first_added_arg >= args_length) return true;
126 :
127 6258830 : if (IsObjectElementsKind(origin_kind)) return true;
128 : ElementsKind target_kind = origin_kind;
129 : {
130 : DisallowHeapAllocation no_gc;
131 5924153 : for (int i = first_added_arg; i < args_length; i++) {
132 5925748 : Object* arg = (*args)[i];
133 5925748 : if (arg->IsHeapObject()) {
134 1747 : if (arg->IsHeapNumber()) {
135 : target_kind = PACKED_DOUBLE_ELEMENTS;
136 : } else {
137 : target_kind = PACKED_ELEMENTS;
138 : break;
139 : }
140 : }
141 : }
142 : }
143 5854219 : if (target_kind != origin_kind) {
144 : // Use a short-lived HandleScope to avoid creating several copies of the
145 : // elements handle which would cause issues when left-trimming later-on.
146 : HandleScope scope(isolate);
147 1614 : JSObject::TransitionElementsKind(array, target_kind);
148 : }
149 : return true;
150 : }
151 :
152 18009 : MUST_USE_RESULT static Object* CallJsIntrinsic(Isolate* isolate,
153 : Handle<JSFunction> function,
154 : BuiltinArguments args) {
155 : HandleScope handleScope(isolate);
156 18009 : int argc = args.length() - 1;
157 : ScopedVector<Handle<Object>> argv(argc);
158 55257 : for (int i = 0; i < argc; ++i) {
159 57717 : argv[i] = args.at(i + 1);
160 : }
161 50801 : RETURN_RESULT_OR_FAILURE(
162 : isolate,
163 : Execution::Call(isolate, function, args.receiver(), argc, argv.start()));
164 : }
165 : } // namespace
166 :
167 18781074 : BUILTIN(ArrayPush) {
168 : HandleScope scope(isolate);
169 : Handle<Object> receiver = args.receiver();
170 6260358 : if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1)) {
171 8346 : return CallJsIntrinsic(isolate, isolate->array_push(), args);
172 : }
173 : // Fast Elements Path
174 6252012 : int to_add = args.length() - 1;
175 : Handle<JSArray> array = Handle<JSArray>::cast(receiver);
176 : int len = Smi::ToInt(array->length());
177 6252062 : if (to_add == 0) return Smi::FromInt(len);
178 :
179 : // Currently fixed arrays cannot grow too big, so we should never hit this.
180 : DCHECK_LE(to_add, Smi::kMaxValue - Smi::ToInt(array->length()));
181 :
182 6251962 : if (JSArray::HasReadOnlyLength(array)) {
183 80 : return CallJsIntrinsic(isolate, isolate->array_push(), args);
184 : }
185 :
186 6251882 : ElementsAccessor* accessor = array->GetElementsAccessor();
187 6251882 : int new_length = accessor->Push(array, &args, to_add);
188 6251882 : return Smi::FromInt(new_length);
189 : }
190 :
191 187122 : BUILTIN(ArrayPop) {
192 : HandleScope scope(isolate);
193 : Handle<Object> receiver = args.receiver();
194 62374 : if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, nullptr, 0)) {
195 3309 : return CallJsIntrinsic(isolate, isolate->array_pop(), args);
196 : }
197 :
198 : Handle<JSArray> array = Handle<JSArray>::cast(receiver);
199 :
200 59065 : uint32_t len = static_cast<uint32_t>(Smi::ToInt(array->length()));
201 59065 : if (len == 0) return isolate->heap()->undefined_value();
202 :
203 59009 : if (JSArray::HasReadOnlyLength(array)) {
204 240 : return CallJsIntrinsic(isolate, isolate->array_pop(), args);
205 : }
206 :
207 : Handle<Object> result;
208 58769 : if (IsJSArrayFastElementMovingAllowed(isolate, JSArray::cast(*receiver))) {
209 : // Fast Elements Path
210 55391 : result = array->GetElementsAccessor()->Pop(array);
211 : } else {
212 : // Use Slow Lookup otherwise
213 3378 : uint32_t new_length = len - 1;
214 6756 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
215 : isolate, result, JSReceiver::GetElement(isolate, array, new_length));
216 3378 : JSArray::SetLength(array, new_length);
217 : }
218 58769 : return *result;
219 : }
220 :
221 565140 : BUILTIN(ArrayShift) {
222 : HandleScope scope(isolate);
223 22 : Heap* heap = isolate->heap();
224 : Handle<Object> receiver = args.receiver();
225 375468 : if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, nullptr, 0) ||
226 : !IsJSArrayFastElementMovingAllowed(isolate, JSArray::cast(*receiver))) {
227 1518 : return CallJsIntrinsic(isolate, isolate->array_shift(), args);
228 : }
229 : Handle<JSArray> array = Handle<JSArray>::cast(receiver);
230 :
231 : int len = Smi::ToInt(array->length());
232 186884 : if (len == 0) return heap->undefined_value();
233 :
234 186840 : if (JSArray::HasReadOnlyLength(array)) {
235 240 : return CallJsIntrinsic(isolate, isolate->array_shift(), args);
236 : }
237 :
238 186600 : Handle<Object> first = array->GetElementsAccessor()->Shift(array);
239 186600 : return *first;
240 : }
241 :
242 21129 : BUILTIN(ArrayUnshift) {
243 : HandleScope scope(isolate);
244 : Handle<Object> receiver = args.receiver();
245 7043 : if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1)) {
246 926 : return CallJsIntrinsic(isolate, isolate->array_unshift(), args);
247 : }
248 : Handle<JSArray> array = Handle<JSArray>::cast(receiver);
249 6117 : int to_add = args.length() - 1;
250 6141 : if (to_add == 0) return array->length();
251 :
252 : // Currently fixed arrays cannot grow too big, so we should never hit this.
253 : DCHECK_LE(to_add, Smi::kMaxValue - Smi::ToInt(array->length()));
254 :
255 6093 : if (JSArray::HasReadOnlyLength(array)) {
256 80 : return CallJsIntrinsic(isolate, isolate->array_unshift(), args);
257 : }
258 :
259 6013 : ElementsAccessor* accessor = array->GetElementsAccessor();
260 6013 : int new_length = accessor->Unshift(array, &args, to_add);
261 6013 : return Smi::FromInt(new_length);
262 : }
263 :
264 1533390 : BUILTIN(ArraySlice) {
265 : HandleScope scope(isolate);
266 : Handle<Object> receiver = args.receiver();
267 511130 : int len = -1;
268 511130 : int relative_start = 0;
269 511130 : int relative_end = 0;
270 :
271 511130 : if (receiver->IsJSArray()) {
272 : DisallowHeapAllocation no_gc;
273 : JSArray* array = JSArray::cast(*receiver);
274 721167 : if (V8_UNLIKELY(!array->HasFastElements() ||
275 : !IsJSArrayFastElementMovingAllowed(isolate, array) ||
276 : !isolate->IsArraySpeciesLookupChainIntact() ||
277 : // If this is a subclass of Array, then call out to JS
278 : !array->HasArrayPrototype(isolate))) {
279 : AllowHeapAllocation allow_allocation;
280 340 : return CallJsIntrinsic(isolate, isolate->array_slice(), args);
281 : }
282 240111 : len = Smi::ToInt(array->length());
283 541109 : } else if (receiver->IsJSObject() &&
284 : GetSloppyArgumentsLength(isolate, Handle<JSObject>::cast(receiver),
285 270430 : &len)) {
286 : // Array.prototype.slice.call(arguments, ...) is quite a common idiom
287 : // (notably more than 50% of invocations in Web apps).
288 : // Treat it in C++ as well.
289 : DCHECK(JSObject::cast(*receiver)->HasFastElements() ||
290 : JSObject::cast(*receiver)->HasFastArgumentsElements());
291 : } else {
292 : AllowHeapAllocation allow_allocation;
293 461 : return CallJsIntrinsic(isolate, isolate->array_slice(), args);
294 : }
295 : DCHECK_LE(0, len);
296 510329 : int argument_count = args.length() - 1;
297 : // Note carefully chosen defaults---if argument is missing,
298 : // it's undefined which gets converted to 0 for relative_start
299 : // and to len for relative_end.
300 510329 : relative_start = 0;
301 510329 : relative_end = len;
302 510329 : if (argument_count > 0) {
303 : DisallowHeapAllocation no_gc;
304 484714 : if (!ClampedToInteger(isolate, args[1], &relative_start)) {
305 : AllowHeapAllocation allow_allocation;
306 270 : return CallJsIntrinsic(isolate, isolate->array_slice(), args);
307 : }
308 484444 : if (argument_count > 1) {
309 209772 : Object* end_arg = args[2];
310 : // slice handles the end_arg specially
311 209772 : if (end_arg->IsUndefined(isolate)) {
312 70 : relative_end = len;
313 209702 : } else if (!ClampedToInteger(isolate, end_arg, &relative_end)) {
314 : AllowHeapAllocation allow_allocation;
315 70 : return CallJsIntrinsic(isolate, isolate->array_slice(), args);
316 : }
317 : }
318 : }
319 :
320 : // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
321 510217 : uint32_t actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
322 1019978 : : Min(relative_start, len);
323 :
324 : // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
325 : uint32_t actual_end =
326 1019978 : (relative_end < 0) ? Max(len + relative_end, 0) : Min(relative_end, len);
327 :
328 : Handle<JSObject> object = Handle<JSObject>::cast(receiver);
329 509989 : ElementsAccessor* accessor = object->GetElementsAccessor();
330 1019978 : return *accessor->Slice(object, actual_start, actual_end);
331 : }
332 :
333 17583 : BUILTIN(ArraySplice) {
334 : HandleScope scope(isolate);
335 : Handle<Object> receiver = args.receiver();
336 15514 : if (V8_UNLIKELY(
337 : !EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 3) ||
338 : // If this is a subclass of Array, then call out to JS.
339 : !Handle<JSArray>::cast(receiver)->HasArrayPrototype(isolate) ||
340 : // If anything with @@species has been messed with, call out to JS.
341 : !isolate->IsArraySpeciesLookupChainIntact())) {
342 1189 : return CallJsIntrinsic(isolate, isolate->array_splice(), args);
343 : }
344 : Handle<JSArray> array = Handle<JSArray>::cast(receiver);
345 :
346 4672 : int argument_count = args.length() - 1;
347 4672 : int relative_start = 0;
348 4672 : if (argument_count > 0) {
349 : DisallowHeapAllocation no_gc;
350 4554 : if (!ClampedToInteger(isolate, args[1], &relative_start)) {
351 : AllowHeapAllocation allow_allocation;
352 480 : return CallJsIntrinsic(isolate, isolate->array_splice(), args);
353 : }
354 : }
355 : int len = Smi::ToInt(array->length());
356 : // clip relative start to [0, len]
357 4621 : int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
358 4192 : : Min(relative_start, len);
359 :
360 : int actual_delete_count;
361 4192 : if (argument_count == 1) {
362 : // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
363 : // given as a request to delete all the elements from the start.
364 : // And it differs from the case of undefined delete count.
365 : // This does not follow ECMA-262, but we do the same for compatibility.
366 : DCHECK_GE(len - actual_start, 0);
367 1109 : actual_delete_count = len - actual_start;
368 : } else {
369 3083 : int delete_count = 0;
370 : DisallowHeapAllocation no_gc;
371 3083 : if (argument_count > 1) {
372 2965 : if (!ClampedToInteger(isolate, args[2], &delete_count)) {
373 : AllowHeapAllocation allow_allocation;
374 140 : return CallJsIntrinsic(isolate, isolate->array_splice(), args);
375 : }
376 : }
377 2943 : actual_delete_count = Min(Max(delete_count, 0), len - actual_start);
378 : }
379 :
380 4052 : int add_count = (argument_count > 1) ? (argument_count - 2) : 0;
381 4052 : int new_length = len - actual_delete_count + add_count;
382 :
383 4052 : if (new_length != len && JSArray::HasReadOnlyLength(array)) {
384 : AllowHeapAllocation allow_allocation;
385 320 : return CallJsIntrinsic(isolate, isolate->array_splice(), args);
386 : }
387 3732 : ElementsAccessor* accessor = array->GetElementsAccessor();
388 : Handle<JSArray> result_array = accessor->Splice(
389 3732 : array, actual_start, actual_delete_count, &args, add_count);
390 3732 : return *result_array;
391 : }
392 :
393 : // Array Concat -------------------------------------------------------------
394 :
395 : namespace {
396 :
397 : /**
398 : * A simple visitor visits every element of Array's.
399 : * The backend storage can be a fixed array for fast elements case,
400 : * or a dictionary for sparse array. Since Dictionary is a subtype
401 : * of FixedArray, the class can be used by both fast and slow cases.
402 : * The second parameter of the constructor, fast_elements, specifies
403 : * whether the storage is a FixedArray or Dictionary.
404 : *
405 : * An index limit is used to deal with the situation that a result array
406 : * length overflows 32-bit non-negative integer.
407 : */
408 : class ArrayConcatVisitor {
409 : public:
410 12124 : ArrayConcatVisitor(Isolate* isolate, Handle<HeapObject> storage,
411 : bool fast_elements)
412 : : isolate_(isolate),
413 : storage_(isolate->global_handles()->Create(*storage)),
414 : index_offset_(0u),
415 : bit_field_(
416 : FastElementsField::encode(fast_elements) |
417 6062 : ExceedsLimitField::encode(false) |
418 6062 : IsFixedArrayField::encode(storage->IsFixedArray()) |
419 6360 : HasSimpleElementsField::encode(storage->IsFixedArray() ||
420 : storage->map()->instance_type() >
421 18186 : LAST_CUSTOM_ELEMENTS_RECEIVER)) {
422 : DCHECK(!(this->fast_elements() && !is_fixed_array()));
423 6062 : }
424 :
425 : ~ArrayConcatVisitor() { clear_storage(); }
426 :
427 2770948 : MUST_USE_RESULT bool visit(uint32_t i, Handle<Object> elm) {
428 1385494 : uint32_t index = index_offset_ + i;
429 :
430 1385494 : if (i >= JSObject::kMaxElementCount - index_offset_) {
431 : set_exceeds_array_limit(true);
432 : // Exception hasn't been thrown at this point. Return true to
433 : // break out, and caller will throw. !visit would imply that
434 : // there is already a pending exception.
435 40 : return true;
436 : }
437 :
438 1385454 : if (!is_fixed_array()) {
439 336 : LookupIterator it(isolate_, storage_, index, LookupIterator::OWN);
440 336 : MAYBE_RETURN(
441 : JSReceiver::CreateDataProperty(&it, elm, Object::THROW_ON_ERROR),
442 : false);
443 287 : return true;
444 : }
445 :
446 1385118 : if (fast_elements()) {
447 1006789 : if (index < static_cast<uint32_t>(storage_fixed_array()->length())) {
448 1006769 : storage_fixed_array()->set(index, *elm);
449 1006769 : return true;
450 : }
451 : // Our initial estimate of length was foiled, possibly by
452 : // getters on the arrays increasing the length of later arrays
453 : // during iteration.
454 : // This shouldn't happen in anything but pathological cases.
455 20 : SetDictionaryMode();
456 : // Fall-through to dictionary mode.
457 : }
458 : DCHECK(!fast_elements());
459 : Handle<SeededNumberDictionary> dict(
460 : SeededNumberDictionary::cast(*storage_));
461 : // The object holding this backing store has just been allocated, so
462 : // it cannot yet be used as a prototype.
463 : Handle<JSObject> not_a_prototype_holder;
464 : Handle<SeededNumberDictionary> result =
465 378349 : SeededNumberDictionary::Set(dict, index, elm, not_a_prototype_holder);
466 378349 : if (!result.is_identical_to(dict)) {
467 : // Dictionary needed to grow.
468 : clear_storage();
469 : set_storage(*result);
470 : }
471 : return true;
472 : }
473 :
474 2024138 : void increase_index_offset(uint32_t delta) {
475 1012069 : if (JSObject::kMaxElementCount - index_offset_ < delta) {
476 50 : index_offset_ = JSObject::kMaxElementCount;
477 : } else {
478 1012019 : index_offset_ += delta;
479 : }
480 : // If the initial length estimate was off (see special case in visit()),
481 : // but the array blowing the limit didn't contain elements beyond the
482 : // provided-for index range, go to dictionary mode now.
483 2016677 : if (fast_elements() &&
484 2009216 : index_offset_ >
485 : static_cast<uint32_t>(FixedArrayBase::cast(*storage_)->length())) {
486 10 : SetDictionaryMode();
487 : }
488 1012069 : }
489 :
490 : bool exceeds_array_limit() const {
491 : return ExceedsLimitField::decode(bit_field_);
492 : }
493 :
494 11176 : Handle<JSArray> ToArray() {
495 : DCHECK(is_fixed_array());
496 5588 : Handle<JSArray> array = isolate_->factory()->NewJSArray(0);
497 : Handle<Object> length =
498 5588 : isolate_->factory()->NewNumber(static_cast<double>(index_offset_));
499 : Handle<Map> map = JSObject::GetElementsTransitionMap(
500 11176 : array, fast_elements() ? HOLEY_ELEMENTS : DICTIONARY_ELEMENTS);
501 5588 : array->set_length(*length);
502 5588 : array->set_elements(*storage_fixed_array());
503 5588 : array->synchronized_set_map(*map);
504 5588 : return array;
505 : }
506 :
507 239 : MUST_USE_RESULT MaybeHandle<JSReceiver> ToJSReceiver() {
508 : DCHECK(!is_fixed_array());
509 : Handle<JSReceiver> result = Handle<JSReceiver>::cast(storage_);
510 : Handle<Object> length =
511 239 : isolate_->factory()->NewNumber(static_cast<double>(index_offset_));
512 717 : RETURN_ON_EXCEPTION(
513 : isolate_,
514 : JSReceiver::SetProperty(result, isolate_->factory()->length_string(),
515 : length, LanguageMode::kStrict),
516 : JSReceiver);
517 229 : return result;
518 : }
519 : bool has_simple_elements() const {
520 : return HasSimpleElementsField::decode(bit_field_);
521 : }
522 :
523 : private:
524 : // Convert storage to dictionary mode.
525 30 : void SetDictionaryMode() {
526 : DCHECK(fast_elements() && is_fixed_array());
527 : Handle<FixedArray> current_storage = storage_fixed_array();
528 : Handle<SeededNumberDictionary> slow_storage(
529 30 : SeededNumberDictionary::New(isolate_, current_storage->length()));
530 30 : uint32_t current_length = static_cast<uint32_t>(current_storage->length());
531 540 : FOR_WITH_HANDLE_SCOPE(
532 : isolate_, uint32_t, i = 0, i, i < current_length, i++, {
533 : Handle<Object> element(current_storage->get(i), isolate_);
534 : if (!element->IsTheHole(isolate_)) {
535 : // The object holding this backing store has just been allocated, so
536 : // it cannot yet be used as a prototype.
537 : Handle<JSObject> not_a_prototype_holder;
538 : Handle<SeededNumberDictionary> new_storage =
539 : SeededNumberDictionary::Set(slow_storage, i, element,
540 : not_a_prototype_holder);
541 : if (!new_storage.is_identical_to(slow_storage)) {
542 : slow_storage = loop_scope.CloseAndEscape(new_storage);
543 : }
544 : }
545 : });
546 : clear_storage();
547 : set_storage(*slow_storage);
548 : set_fast_elements(false);
549 30 : }
550 :
551 7952 : inline void clear_storage() { GlobalHandles::Destroy(storage_.location()); }
552 :
553 : inline void set_storage(FixedArray* storage) {
554 : DCHECK(is_fixed_array());
555 : DCHECK(has_simple_elements());
556 3780 : storage_ = isolate_->global_handles()->Create(storage);
557 : }
558 :
559 : class FastElementsField : public BitField<bool, 0, 1> {};
560 : class ExceedsLimitField : public BitField<bool, 1, 1> {};
561 : class IsFixedArrayField : public BitField<bool, 2, 1> {};
562 : class HasSimpleElementsField : public BitField<bool, 3, 1> {};
563 :
564 : bool fast_elements() const { return FastElementsField::decode(bit_field_); }
565 : void set_fast_elements(bool fast) {
566 60 : bit_field_ = FastElementsField::update(bit_field_, fast);
567 : }
568 : void set_exceeds_array_limit(bool exceeds) {
569 80 : bit_field_ = ExceedsLimitField::update(bit_field_, exceeds);
570 : }
571 : bool is_fixed_array() const { return IsFixedArrayField::decode(bit_field_); }
572 : Handle<FixedArray> storage_fixed_array() {
573 : DCHECK(is_fixed_array());
574 : DCHECK(has_simple_elements());
575 : return Handle<FixedArray>::cast(storage_);
576 : }
577 :
578 : Isolate* isolate_;
579 : Handle<Object> storage_; // Always a global handle.
580 : // Index after last seen index. Always less than or equal to
581 : // JSObject::kMaxElementCount.
582 : uint32_t index_offset_;
583 : uint32_t bit_field_;
584 : };
585 :
586 7527 : uint32_t EstimateElementCount(Handle<JSArray> array) {
587 : DisallowHeapAllocation no_gc;
588 7527 : uint32_t length = static_cast<uint32_t>(array->length()->Number());
589 : int element_count = 0;
590 7527 : switch (array->GetElementsKind()) {
591 : case PACKED_SMI_ELEMENTS:
592 : case HOLEY_SMI_ELEMENTS:
593 : case PACKED_ELEMENTS:
594 : case HOLEY_ELEMENTS: {
595 : // Fast elements can't have lengths that are not representable by
596 : // a 32-bit signed integer.
597 : DCHECK_GE(static_cast<int32_t>(FixedArray::kMaxLength), 0);
598 6834 : int fast_length = static_cast<int>(length);
599 : Isolate* isolate = array->GetIsolate();
600 : FixedArray* elements = FixedArray::cast(array->elements());
601 51939 : for (int i = 0; i < fast_length; i++) {
602 45105 : if (!elements->get(i)->IsTheHole(isolate)) element_count++;
603 : }
604 : break;
605 : }
606 : case PACKED_DOUBLE_ELEMENTS:
607 : case HOLEY_DOUBLE_ELEMENTS: {
608 : // Fast elements can't have lengths that are not representable by
609 : // a 32-bit signed integer.
610 : DCHECK_GE(static_cast<int32_t>(FixedDoubleArray::kMaxLength), 0);
611 70 : int fast_length = static_cast<int>(length);
612 70 : if (array->elements()->IsFixedArray()) {
613 : DCHECK_EQ(FixedArray::cast(array->elements())->length(), 0);
614 : break;
615 : }
616 : FixedDoubleArray* elements = FixedDoubleArray::cast(array->elements());
617 656720 : for (int i = 0; i < fast_length; i++) {
618 656660 : if (!elements->is_the_hole(i)) element_count++;
619 : }
620 : break;
621 : }
622 : case DICTIONARY_ELEMENTS: {
623 : SeededNumberDictionary* dictionary =
624 : SeededNumberDictionary::cast(array->elements());
625 : Isolate* isolate = dictionary->GetIsolate();
626 : int capacity = dictionary->Capacity();
627 12631 : for (int i = 0; i < capacity; i++) {
628 : Object* key = dictionary->KeyAt(i);
629 12008 : if (dictionary->IsKey(isolate, key)) {
630 4687 : element_count++;
631 : }
632 : }
633 : break;
634 : }
635 : #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) case TYPE##_ELEMENTS:
636 :
637 : TYPED_ARRAYS(TYPED_ARRAY_CASE)
638 : #undef TYPED_ARRAY_CASE
639 : // External arrays are always dense.
640 0 : return length;
641 : case NO_ELEMENTS:
642 : return 0;
643 : case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
644 : case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
645 : case FAST_STRING_WRAPPER_ELEMENTS:
646 : case SLOW_STRING_WRAPPER_ELEMENTS:
647 0 : UNREACHABLE();
648 : }
649 : // As an estimate, we assume that the prototype doesn't contain any
650 : // inherited elements.
651 7527 : return element_count;
652 : }
653 :
654 1571 : void CollectElementIndices(Handle<JSObject> object, uint32_t range,
655 : std::vector<uint32_t>* indices) {
656 : Isolate* isolate = object->GetIsolate();
657 : ElementsKind kind = object->GetElementsKind();
658 1571 : switch (kind) {
659 : case PACKED_SMI_ELEMENTS:
660 : case PACKED_ELEMENTS:
661 : case HOLEY_SMI_ELEMENTS:
662 : case HOLEY_ELEMENTS: {
663 : DisallowHeapAllocation no_gc;
664 : FixedArray* elements = FixedArray::cast(object->elements());
665 1035 : uint32_t length = static_cast<uint32_t>(elements->length());
666 1035 : if (range < length) length = range;
667 43253 : for (uint32_t i = 0; i < length; i++) {
668 84436 : if (!elements->get(i)->IsTheHole(isolate)) {
669 190 : indices->push_back(i);
670 : }
671 : }
672 : break;
673 : }
674 : case HOLEY_DOUBLE_ELEMENTS:
675 : case PACKED_DOUBLE_ELEMENTS: {
676 10 : if (object->elements()->IsFixedArray()) {
677 : DCHECK_EQ(object->elements()->length(), 0);
678 : break;
679 : }
680 : Handle<FixedDoubleArray> elements(
681 : FixedDoubleArray::cast(object->elements()));
682 10 : uint32_t length = static_cast<uint32_t>(elements->length());
683 10 : if (range < length) length = range;
684 20 : for (uint32_t i = 0; i < length; i++) {
685 20 : if (!elements->is_the_hole(i)) {
686 10 : indices->push_back(i);
687 : }
688 : }
689 10 : break;
690 : }
691 : case DICTIONARY_ELEMENTS: {
692 : DisallowHeapAllocation no_gc;
693 : SeededNumberDictionary* dict =
694 : SeededNumberDictionary::cast(object->elements());
695 466 : uint32_t capacity = dict->Capacity();
696 5834 : FOR_WITH_HANDLE_SCOPE(isolate, uint32_t, j = 0, j, j < capacity, j++, {
697 : Object* k = dict->KeyAt(j);
698 : if (!dict->IsKey(isolate, k)) continue;
699 : DCHECK(k->IsNumber());
700 : uint32_t index = static_cast<uint32_t>(k->Number());
701 : if (index < range) {
702 : indices->push_back(index);
703 : }
704 : });
705 : break;
706 : }
707 : #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) case TYPE##_ELEMENTS:
708 :
709 : TYPED_ARRAYS(TYPED_ARRAY_CASE)
710 : #undef TYPED_ARRAY_CASE
711 : {
712 : uint32_t length = static_cast<uint32_t>(
713 10 : FixedArrayBase::cast(object->elements())->length());
714 10 : if (range <= length) {
715 : length = range;
716 : // We will add all indices, so we might as well clear it first
717 : // and avoid duplicates.
718 : indices->clear();
719 : }
720 30 : for (uint32_t i = 0; i < length; i++) {
721 20 : indices->push_back(i);
722 : }
723 10 : if (length == range) return; // All indices accounted for already.
724 : break;
725 : }
726 : case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
727 : case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
728 : DisallowHeapAllocation no_gc;
729 : FixedArrayBase* elements = object->elements();
730 : JSObject* raw_object = *object;
731 50 : ElementsAccessor* accessor = object->GetElementsAccessor();
732 300080 : for (uint32_t i = 0; i < range; i++) {
733 300030 : if (accessor->HasElement(raw_object, i, elements)) {
734 60 : indices->push_back(i);
735 : }
736 : }
737 : break;
738 : }
739 : case FAST_STRING_WRAPPER_ELEMENTS:
740 : case SLOW_STRING_WRAPPER_ELEMENTS: {
741 : DCHECK(object->IsJSValue());
742 : Handle<JSValue> js_value = Handle<JSValue>::cast(object);
743 : DCHECK(js_value->value()->IsString());
744 : Handle<String> string(String::cast(js_value->value()), isolate);
745 0 : uint32_t length = static_cast<uint32_t>(string->length());
746 0 : uint32_t i = 0;
747 : uint32_t limit = Min(length, range);
748 0 : for (; i < limit; i++) {
749 0 : indices->push_back(i);
750 : }
751 0 : ElementsAccessor* accessor = object->GetElementsAccessor();
752 0 : for (; i < range; i++) {
753 0 : if (accessor->HasElement(*object, i)) {
754 0 : indices->push_back(i);
755 : }
756 : }
757 : break;
758 : }
759 : case NO_ELEMENTS:
760 : break;
761 : }
762 :
763 1571 : PrototypeIterator iter(isolate, object);
764 1571 : if (!iter.IsAtEnd()) {
765 : // The prototype will usually have no inherited element indices,
766 : // but we have to check.
767 : CollectElementIndices(PrototypeIterator::GetCurrent<JSObject>(iter), range,
768 1105 : indices);
769 : }
770 : }
771 :
772 1160 : bool IterateElementsSlow(Isolate* isolate, Handle<JSReceiver> receiver,
773 : uint32_t length, ArrayConcatVisitor* visitor) {
774 5755208 : FOR_WITH_HANDLE_SCOPE(isolate, uint32_t, i = 0, i, i < length, ++i, {
775 : Maybe<bool> maybe = JSReceiver::HasElement(receiver, i);
776 : if (!maybe.IsJust()) return false;
777 : if (maybe.FromJust()) {
778 : Handle<Object> element_value;
779 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
780 : isolate, element_value, JSReceiver::GetElement(isolate, receiver, i),
781 : false);
782 : if (!visitor->visit(i, element_value)) return false;
783 : }
784 : });
785 1141 : visitor->increase_index_offset(length);
786 1141 : return true;
787 : }
788 : /**
789 : * A helper function that visits "array" elements of a JSReceiver in numerical
790 : * order.
791 : *
792 : * The visitor argument called for each existing element in the array
793 : * with the element index and the element's value.
794 : * Afterwards it increments the base-index of the visitor by the array
795 : * length.
796 : * Returns false if any access threw an exception, otherwise true.
797 : */
798 7841 : bool IterateElements(Isolate* isolate, Handle<JSReceiver> receiver,
799 6628 : ArrayConcatVisitor* visitor) {
800 7841 : uint32_t length = 0;
801 :
802 7841 : if (receiver->IsJSArray()) {
803 : Handle<JSArray> array = Handle<JSArray>::cast(receiver);
804 6844 : length = static_cast<uint32_t>(array->length()->Number());
805 : } else {
806 : Handle<Object> val;
807 1994 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
808 : isolate, val, Object::GetLengthFromArrayLike(isolate, receiver), false);
809 : // TODO(caitp): Support larger element indexes (up to 2^53-1).
810 914 : if (!val->ToUint32(&length)) {
811 0 : length = 0;
812 : }
813 : // TODO(cbruni): handle other element kind as well
814 914 : return IterateElementsSlow(isolate, receiver, length, visitor);
815 : }
816 :
817 13472 : if (!HasOnlySimpleElements(isolate, *receiver) ||
818 : !visitor->has_simple_elements()) {
819 246 : return IterateElementsSlow(isolate, receiver, length, visitor);
820 : }
821 : Handle<JSObject> array = Handle<JSObject>::cast(receiver);
822 :
823 6598 : switch (array->GetElementsKind()) {
824 : case PACKED_SMI_ELEMENTS:
825 : case PACKED_ELEMENTS:
826 : case HOLEY_SMI_ELEMENTS:
827 : case HOLEY_ELEMENTS: {
828 : // Run through the elements FixedArray and use HasElement and GetElement
829 : // to check the prototype for missing elements.
830 : Handle<FixedArray> elements(FixedArray::cast(array->elements()));
831 6102 : int fast_length = static_cast<int>(length);
832 : DCHECK(fast_length <= elements->length());
833 191260 : FOR_WITH_HANDLE_SCOPE(isolate, int, j = 0, j, j < fast_length, j++, {
834 : Handle<Object> element_value(elements->get(j), isolate);
835 : if (!element_value->IsTheHole(isolate)) {
836 : if (!visitor->visit(j, element_value)) return false;
837 : } else {
838 : Maybe<bool> maybe = JSReceiver::HasElement(array, j);
839 : if (!maybe.IsJust()) return false;
840 : if (maybe.FromJust()) {
841 : // Call GetElement on array, not its prototype, or getters won't
842 : // have the correct receiver.
843 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
844 : isolate, element_value,
845 : JSReceiver::GetElement(isolate, array, j), false);
846 : if (!visitor->visit(j, element_value)) return false;
847 : }
848 : }
849 : });
850 : break;
851 : }
852 : case HOLEY_DOUBLE_ELEMENTS:
853 : case PACKED_DOUBLE_ELEMENTS: {
854 : // Empty array is FixedArray but not FixedDoubleArray.
855 30 : if (length == 0) break;
856 : // Run through the elements FixedArray and use HasElement and GetElement
857 : // to check the prototype for missing elements.
858 20 : if (array->elements()->IsFixedArray()) {
859 : DCHECK_EQ(array->elements()->length(), 0);
860 : break;
861 : }
862 : Handle<FixedDoubleArray> elements(
863 : FixedDoubleArray::cast(array->elements()));
864 20 : int fast_length = static_cast<int>(length);
865 : DCHECK(fast_length <= elements->length());
866 310 : FOR_WITH_HANDLE_SCOPE(isolate, int, j = 0, j, j < fast_length, j++, {
867 : if (!elements->is_the_hole(j)) {
868 : double double_value = elements->get_scalar(j);
869 : Handle<Object> element_value =
870 : isolate->factory()->NewNumber(double_value);
871 : if (!visitor->visit(j, element_value)) return false;
872 : } else {
873 : Maybe<bool> maybe = JSReceiver::HasElement(array, j);
874 : if (!maybe.IsJust()) return false;
875 : if (maybe.FromJust()) {
876 : // Call GetElement on array, not its prototype, or getters won't
877 : // have the correct receiver.
878 : Handle<Object> element_value;
879 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
880 : isolate, element_value,
881 : JSReceiver::GetElement(isolate, array, j), false);
882 : if (!visitor->visit(j, element_value)) return false;
883 : }
884 : }
885 : });
886 : break;
887 : }
888 :
889 : case DICTIONARY_ELEMENTS: {
890 : Handle<SeededNumberDictionary> dict(array->element_dictionary());
891 : std::vector<uint32_t> indices;
892 466 : indices.reserve(dict->Capacity() / 2);
893 :
894 : // Collect all indices in the object and the prototypes less
895 : // than length. This might introduce duplicates in the indices list.
896 466 : CollectElementIndices(array, length, &indices);
897 466 : std::sort(indices.begin(), indices.end());
898 466 : size_t n = indices.size();
899 7038 : FOR_WITH_HANDLE_SCOPE(isolate, size_t, j = 0, j, j < n, (void)0, {
900 : uint32_t index = indices[j];
901 : Handle<Object> element;
902 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
903 : isolate, element, JSReceiver::GetElement(isolate, array, index),
904 : false);
905 : if (!visitor->visit(index, element)) return false;
906 : // Skip to next different index (i.e., omit duplicates).
907 : do {
908 : j++;
909 : } while (j < n && indices[j] == index);
910 : });
911 : break;
912 : }
913 : case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
914 : case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
915 0 : FOR_WITH_HANDLE_SCOPE(
916 : isolate, uint32_t, index = 0, index, index < length, index++, {
917 : Handle<Object> element;
918 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
919 : isolate, element, JSReceiver::GetElement(isolate, array, index),
920 : false);
921 : if (!visitor->visit(index, element)) return false;
922 : });
923 : break;
924 : }
925 : case NO_ELEMENTS:
926 : break;
927 : #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) case TYPE##_ELEMENTS:
928 : TYPED_ARRAYS(TYPED_ARRAY_CASE)
929 : #undef TYPED_ARRAY_CASE
930 0 : return IterateElementsSlow(isolate, receiver, length, visitor);
931 : case FAST_STRING_WRAPPER_ELEMENTS:
932 : case SLOW_STRING_WRAPPER_ELEMENTS:
933 : // |array| is guaranteed to be an array or typed array.
934 0 : UNREACHABLE();
935 : break;
936 : }
937 6559 : visitor->increase_index_offset(length);
938 6559 : return true;
939 : }
940 :
941 1012264 : static Maybe<bool> IsConcatSpreadable(Isolate* isolate, Handle<Object> obj) {
942 : HandleScope handle_scope(isolate);
943 1012264 : if (!obj->IsJSReceiver()) return Just(false);
944 10298 : if (!isolate->IsIsConcatSpreadableLookupChainIntact(JSReceiver::cast(*obj))) {
945 : // Slow path if @@isConcatSpreadable has been used.
946 : Handle<Symbol> key(isolate->factory()->is_concat_spreadable_symbol());
947 : Handle<Object> value;
948 : MaybeHandle<Object> maybeValue =
949 6079 : i::Runtime::GetObjectProperty(isolate, obj, key);
950 8467 : if (!maybeValue.ToHandle(&value)) return Nothing<bool>();
951 8395 : if (!value->IsUndefined(isolate)) return Just(value->BooleanValue());
952 : }
953 : return Object::IsArray(obj);
954 : }
955 :
956 6072 : Object* Slow_ArrayConcat(BuiltinArguments* args, Handle<Object> species,
957 6072 : Isolate* isolate) {
958 : int argument_count = args->length();
959 :
960 6072 : bool is_array_species = *species == isolate->context()->array_function();
961 :
962 : // Pass 1: estimate the length and number of elements of the result.
963 : // The actual length can be larger if any of the arguments have getters
964 : // that mutate other arguments (but will otherwise be precise).
965 : // The number of elements is precise if there are no inherited elements.
966 :
967 : ElementsKind kind = PACKED_SMI_ELEMENTS;
968 :
969 : uint32_t estimate_result_length = 0;
970 : uint32_t estimate_nof = 0;
971 5080970 : FOR_WITH_HANDLE_SCOPE(isolate, int, i = 0, i, i < argument_count, i++, {
972 : Handle<Object> obj((*args)[i], isolate);
973 : uint32_t length_estimate;
974 : uint32_t element_estimate;
975 : if (obj->IsJSArray()) {
976 : Handle<JSArray> array(Handle<JSArray>::cast(obj));
977 : length_estimate = static_cast<uint32_t>(array->length()->Number());
978 : if (length_estimate != 0) {
979 : ElementsKind array_kind =
980 : GetPackedElementsKind(array->GetElementsKind());
981 : kind = GetMoreGeneralElementsKind(kind, array_kind);
982 : }
983 : element_estimate = EstimateElementCount(array);
984 : } else {
985 : if (obj->IsHeapObject()) {
986 : kind = GetMoreGeneralElementsKind(
987 : kind, obj->IsNumber() ? PACKED_DOUBLE_ELEMENTS : PACKED_ELEMENTS);
988 : }
989 : length_estimate = 1;
990 : element_estimate = 1;
991 : }
992 : // Avoid overflows by capping at kMaxElementCount.
993 : if (JSObject::kMaxElementCount - estimate_result_length < length_estimate) {
994 : estimate_result_length = JSObject::kMaxElementCount;
995 : } else {
996 : estimate_result_length += length_estimate;
997 : }
998 : if (JSObject::kMaxElementCount - estimate_nof < element_estimate) {
999 : estimate_nof = JSObject::kMaxElementCount;
1000 : } else {
1001 : estimate_nof += element_estimate;
1002 : }
1003 : });
1004 :
1005 : // If estimated number of elements is more than half of length, a
1006 : // fixed array (fast case) is more time and space-efficient than a
1007 : // dictionary.
1008 5774 : bool fast_case = is_array_species &&
1009 11110 : (estimate_nof * 2) >= estimate_result_length &&
1010 5038 : isolate->IsIsConcatSpreadableLookupChainIntact();
1011 :
1012 6072 : if (fast_case && kind == PACKED_DOUBLE_ELEMENTS) {
1013 : Handle<FixedArrayBase> storage =
1014 20 : isolate->factory()->NewFixedDoubleArray(estimate_result_length);
1015 : int j = 0;
1016 : bool failure = false;
1017 20 : if (estimate_result_length > 0) {
1018 : Handle<FixedDoubleArray> double_storage =
1019 : Handle<FixedDoubleArray>::cast(storage);
1020 40 : for (int i = 0; i < argument_count; i++) {
1021 30 : Handle<Object> obj((*args)[i], isolate);
1022 30 : if (obj->IsSmi()) {
1023 0 : double_storage->set(j, Smi::ToInt(*obj));
1024 0 : j++;
1025 30 : } else if (obj->IsNumber()) {
1026 : double_storage->set(j, obj->Number());
1027 10 : j++;
1028 : } else {
1029 : DisallowHeapAllocation no_gc;
1030 : JSArray* array = JSArray::cast(*obj);
1031 20 : uint32_t length = static_cast<uint32_t>(array->length()->Number());
1032 20 : switch (array->GetElementsKind()) {
1033 : case HOLEY_DOUBLE_ELEMENTS:
1034 : case PACKED_DOUBLE_ELEMENTS: {
1035 : // Empty array is FixedArray but not FixedDoubleArray.
1036 10 : if (length == 0) break;
1037 : FixedDoubleArray* elements =
1038 : FixedDoubleArray::cast(array->elements());
1039 10 : for (uint32_t i = 0; i < length; i++) {
1040 20 : if (elements->is_the_hole(i)) {
1041 : // TODO(jkummerow/verwaest): We could be a bit more clever
1042 : // here: Check if there are no elements/getters on the
1043 : // prototype chain, and if so, allow creation of a holey
1044 : // result array.
1045 : // Same thing below (holey smi case).
1046 : failure = true;
1047 : break;
1048 : }
1049 : double double_value = elements->get_scalar(i);
1050 : double_storage->set(j, double_value);
1051 0 : j++;
1052 : }
1053 : break;
1054 : }
1055 : case HOLEY_SMI_ELEMENTS:
1056 : case PACKED_SMI_ELEMENTS: {
1057 0 : Object* the_hole = isolate->heap()->the_hole_value();
1058 : FixedArray* elements(FixedArray::cast(array->elements()));
1059 0 : for (uint32_t i = 0; i < length; i++) {
1060 0 : Object* element = elements->get(i);
1061 0 : if (element == the_hole) {
1062 : failure = true;
1063 : break;
1064 : }
1065 : int32_t int_value = Smi::ToInt(element);
1066 0 : double_storage->set(j, int_value);
1067 0 : j++;
1068 : }
1069 : break;
1070 : }
1071 : case HOLEY_ELEMENTS:
1072 : case PACKED_ELEMENTS:
1073 : case DICTIONARY_ELEMENTS:
1074 : case NO_ELEMENTS:
1075 : DCHECK_EQ(0u, length);
1076 : break;
1077 : default:
1078 0 : UNREACHABLE();
1079 : }
1080 : }
1081 30 : if (failure) break;
1082 : }
1083 : }
1084 20 : if (!failure) {
1085 20 : return *isolate->factory()->NewJSArrayWithElements(storage, kind, j);
1086 : }
1087 : // In case of failure, fall through.
1088 : }
1089 :
1090 : Handle<HeapObject> storage;
1091 6062 : if (fast_case) {
1092 : // The backing storage array must have non-existing elements to preserve
1093 : // holes across concat operations.
1094 : storage =
1095 2322 : isolate->factory()->NewFixedArrayWithHoles(estimate_result_length);
1096 3740 : } else if (is_array_species) {
1097 3442 : storage = SeededNumberDictionary::New(isolate, estimate_nof);
1098 : } else {
1099 : DCHECK(species->IsConstructor());
1100 : Handle<Object> length(Smi::kZero, isolate);
1101 : Handle<Object> storage_object;
1102 596 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1103 : isolate, storage_object,
1104 : Execution::New(isolate, species, species, 1, &length));
1105 298 : storage = Handle<HeapObject>::cast(storage_object);
1106 : }
1107 :
1108 6062 : ArrayConcatVisitor visitor(isolate, storage, fast_case);
1109 :
1110 1018131 : for (int i = 0; i < argument_count; i++) {
1111 1012264 : Handle<Object> obj((*args)[i], isolate);
1112 1012264 : Maybe<bool> spreadable = IsConcatSpreadable(isolate, obj);
1113 1012459 : MAYBE_RETURN(spreadable, isolate->heap()->exception());
1114 1012210 : if (spreadable.FromJust()) {
1115 7841 : Handle<JSReceiver> object = Handle<JSReceiver>::cast(obj);
1116 7841 : if (!IterateElements(isolate, object, &visitor)) {
1117 141 : return isolate->heap()->exception();
1118 : }
1119 : } else {
1120 1004369 : if (!visitor.visit(0, obj)) return isolate->heap()->exception();
1121 1004369 : visitor.increase_index_offset(1);
1122 : }
1123 : }
1124 :
1125 11734 : if (visitor.exceeds_array_limit()) {
1126 80 : THROW_NEW_ERROR_RETURN_FAILURE(
1127 : isolate, NewRangeError(MessageTemplate::kInvalidArrayLength));
1128 : }
1129 :
1130 5827 : if (is_array_species) {
1131 11176 : return *visitor.ToArray();
1132 : } else {
1133 707 : RETURN_RESULT_OR_FAILURE(isolate, visitor.ToJSReceiver());
1134 : }
1135 : }
1136 :
1137 577968 : bool IsSimpleArray(Isolate* isolate, Handle<JSArray> obj) {
1138 : DisallowHeapAllocation no_gc;
1139 : Map* map = obj->map();
1140 : // If there is only the 'length' property we are fine.
1141 577968 : if (map->prototype() ==
1142 1733848 : isolate->native_context()->initial_array_prototype() &&
1143 : map->NumberOfOwnDescriptors() == 1) {
1144 : return true;
1145 : }
1146 : // TODO(cbruni): slower lookup for array subclasses and support slow
1147 : // @@IsConcatSpreadable lookup.
1148 56 : return false;
1149 : }
1150 :
1151 296929 : MaybeHandle<JSArray> Fast_ArrayConcat(Isolate* isolate,
1152 : BuiltinArguments* args) {
1153 296929 : if (!isolate->IsIsConcatSpreadableLookupChainIntact()) {
1154 5097 : return MaybeHandle<JSArray>();
1155 : }
1156 : // We shouldn't overflow when adding another len.
1157 : const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
1158 : STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
1159 : STATIC_ASSERT(FixedDoubleArray::kMaxLength < kHalfOfMaxInt);
1160 : USE(kHalfOfMaxInt);
1161 :
1162 : int n_arguments = args->length();
1163 : int result_len = 0;
1164 : {
1165 : DisallowHeapAllocation no_gc;
1166 : // Iterate through all the arguments performing checks
1167 : // and calculating total length.
1168 869734 : for (int i = 0; i < n_arguments; i++) {
1169 583151 : Object* arg = (*args)[i];
1170 583151 : if (!arg->IsJSArray()) return MaybeHandle<JSArray>();
1171 579108 : if (!HasOnlySimpleReceiverElements(isolate, JSObject::cast(arg))) {
1172 1020 : return MaybeHandle<JSArray>();
1173 : }
1174 : // TODO(cbruni): support fast concatenation of DICTIONARY_ELEMENTS.
1175 578088 : if (!JSObject::cast(arg)->HasFastElements()) {
1176 120 : return MaybeHandle<JSArray>();
1177 : }
1178 : Handle<JSArray> array(JSArray::cast(arg), isolate);
1179 577968 : if (!IsSimpleArray(isolate, array)) {
1180 56 : return MaybeHandle<JSArray>();
1181 : }
1182 : // The Array length is guaranted to be <= kHalfOfMaxInt thus we won't
1183 : // overflow.
1184 577912 : result_len += Smi::ToInt(array->length());
1185 : DCHECK_GE(result_len, 0);
1186 : // Throw an Error if we overflow the FixedArray limits
1187 577912 : if (FixedDoubleArray::kMaxLength < result_len ||
1188 : FixedArray::kMaxLength < result_len) {
1189 : AllowHeapAllocation gc;
1190 20 : THROW_NEW_ERROR(isolate,
1191 : NewRangeError(MessageTemplate::kInvalidArrayLength),
1192 : JSArray);
1193 : }
1194 : }
1195 : }
1196 286583 : return ElementsAccessor::Concat(isolate, args, n_arguments, result_len);
1197 : }
1198 :
1199 : } // namespace
1200 :
1201 : // ES6 22.1.3.1 Array.prototype.concat
1202 878862 : BUILTIN(ArrayConcat) {
1203 : HandleScope scope(isolate);
1204 :
1205 : Handle<Object> receiver = args.receiver();
1206 585908 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1207 : isolate, receiver,
1208 : Object::ToObject(isolate, args.receiver(), "Array.prototype.concat"));
1209 292665 : args[0] = *receiver;
1210 :
1211 : Handle<JSArray> result_array;
1212 :
1213 : // Avoid a real species read to avoid extra lookups to the array constructor
1214 875829 : if (V8_LIKELY(receiver->IsJSArray() &&
1215 : Handle<JSArray>::cast(receiver)->HasArrayPrototype(isolate) &&
1216 : isolate->IsArraySpeciesLookupChainIntact())) {
1217 582214 : if (Fast_ArrayConcat(isolate, &args).ToHandle(&result_array)) {
1218 286535 : return *result_array;
1219 : }
1220 4572 : if (isolate->has_pending_exception()) return isolate->heap()->exception();
1221 : }
1222 : // Reading @@species happens before anything else with a side effect, so
1223 : // we can do it here to determine whether to take the fast path.
1224 : Handle<Object> species;
1225 12240 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1226 : isolate, species, Object::ArraySpeciesConstructor(isolate, receiver));
1227 12240 : if (*species == *isolate->array_function()) {
1228 11644 : if (Fast_ArrayConcat(isolate, &args).ToHandle(&result_array)) {
1229 48 : return *result_array;
1230 : }
1231 5774 : if (isolate->has_pending_exception()) return isolate->heap()->exception();
1232 : }
1233 6072 : return Slow_ArrayConcat(&args, species, isolate);
1234 : }
1235 :
1236 : } // namespace internal
1237 : } // namespace v8
|