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