OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/builtins.h" | 5 #include "src/builtins.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/api-natives.h" | 8 #include "src/api-natives.h" |
9 #include "src/arguments.h" | 9 #include "src/arguments.h" |
10 #include "src/base/once.h" | 10 #include "src/base/once.h" |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 HandleScope scope(isolate); | 465 HandleScope scope(isolate); |
466 Handle<Object> receiver = args.receiver(); | 466 Handle<Object> receiver = args.receiver(); |
467 MaybeHandle<FixedArrayBase> maybe_elms_obj = | 467 MaybeHandle<FixedArrayBase> maybe_elms_obj = |
468 EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1); | 468 EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1); |
469 Handle<FixedArrayBase> elms_obj; | 469 Handle<FixedArrayBase> elms_obj; |
470 if (!maybe_elms_obj.ToHandle(&elms_obj)) { | 470 if (!maybe_elms_obj.ToHandle(&elms_obj)) { |
471 return CallJsIntrinsic(isolate, isolate->array_unshift(), args); | 471 return CallJsIntrinsic(isolate, isolate->array_unshift(), args); |
472 } | 472 } |
473 Handle<JSArray> array = Handle<JSArray>::cast(receiver); | 473 Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
474 DCHECK(!array->map()->is_observed()); | 474 DCHECK(!array->map()->is_observed()); |
475 if (!array->HasFastSmiOrObjectElements()) { | |
476 return CallJsIntrinsic(isolate, isolate->array_unshift(), args); | |
477 } | |
478 int len = Smi::cast(array->length())->value(); | |
479 int to_add = args.length() - 1; | 475 int to_add = args.length() - 1; |
480 int new_length = len + to_add; | |
481 // Currently fixed arrays cannot grow too big, so | 476 // Currently fixed arrays cannot grow too big, so |
482 // we should never hit this case. | 477 // we should never hit this case. |
483 DCHECK(to_add <= (Smi::kMaxValue - len)); | 478 DCHECK(to_add <= (Smi::kMaxValue - Smi::cast(array->length())->value())); |
484 | 479 |
485 if (to_add > 0 && JSArray::WouldChangeReadOnlyLength(array, len + to_add)) { | 480 if (to_add > 0 && JSArray::HasReadOnlyLength(array)) { |
486 return CallJsIntrinsic(isolate, isolate->array_unshift(), args); | 481 return CallJsIntrinsic(isolate, isolate->array_unshift(), args); |
487 } | 482 } |
488 | 483 |
489 Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); | 484 ElementsAccessor* accessor = array->GetElementsAccessor(); |
490 | 485 int new_length = accessor->Unshift(array, elms_obj, &args, to_add); |
491 if (new_length > elms->length()) { | |
492 // New backing storage is needed. | |
493 int capacity = new_length + (new_length >> 1) + 16; | |
494 Handle<FixedArray> new_elms = | |
495 isolate->factory()->NewUninitializedFixedArray(capacity); | |
496 | |
497 ElementsKind kind = array->GetElementsKind(); | |
498 ElementsAccessor* accessor = array->GetElementsAccessor(); | |
499 accessor->CopyElements( | |
500 elms, 0, kind, new_elms, to_add, | |
501 ElementsAccessor::kCopyToEndAndInitializeToHole); | |
502 | |
503 elms = new_elms; | |
504 array->set_elements(*elms); | |
505 } else { | |
506 DisallowHeapAllocation no_gc; | |
507 Heap* heap = isolate->heap(); | |
508 heap->MoveElements(*elms, to_add, 0, len); | |
509 } | |
510 | |
511 // Add the provided values. | |
512 DisallowHeapAllocation no_gc; | |
513 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); | |
514 for (int i = 0; i < to_add; i++) { | |
515 elms->set(i, args[i + 1], mode); | |
516 } | |
517 | |
518 // Set the length. | |
519 array->set_length(Smi::FromInt(new_length)); | |
520 return Smi::FromInt(new_length); | 486 return Smi::FromInt(new_length); |
521 } | 487 } |
522 | 488 |
523 | 489 |
524 BUILTIN(ArraySlice) { | 490 BUILTIN(ArraySlice) { |
525 HandleScope scope(isolate); | 491 HandleScope scope(isolate); |
526 Handle<Object> receiver = args.receiver(); | 492 Handle<Object> receiver = args.receiver(); |
527 Handle<JSObject> object; | 493 Handle<JSObject> object; |
528 Handle<FixedArrayBase> elms_obj; | 494 Handle<FixedArrayBase> elms_obj; |
529 int len = -1; | 495 int len = -1; |
(...skipping 840 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1370 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) | 1336 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) |
1371 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) | 1337 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) |
1372 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 1338 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
1373 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 1339 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
1374 #undef DEFINE_BUILTIN_ACCESSOR_C | 1340 #undef DEFINE_BUILTIN_ACCESSOR_C |
1375 #undef DEFINE_BUILTIN_ACCESSOR_A | 1341 #undef DEFINE_BUILTIN_ACCESSOR_A |
1376 | 1342 |
1377 | 1343 |
1378 } // namespace internal | 1344 } // namespace internal |
1379 } // namespace v8 | 1345 } // namespace v8 |
OLD | NEW |