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/code-stubs.h" | 8 #include "src/code-stubs.h" |
9 #include "src/conversions-inl.h" | 9 #include "src/conversions-inl.h" |
10 #include "src/elements.h" | 10 #include "src/elements.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); | 63 InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
64 } else { | 64 } else { |
65 FastArrayPushStub stub(isolate); | 65 FastArrayPushStub stub(isolate); |
66 InstallCode(isolate, holder, "push", stub.GetCode()); | 66 InstallCode(isolate, holder, "push", stub.GetCode()); |
67 } | 67 } |
68 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); | 68 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
69 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); | 69 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
70 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); | 70 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
71 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); | 71 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
72 InstallBuiltin(isolate, holder, "includes", Builtins::kArrayIncludes, 2); | 72 InstallBuiltin(isolate, holder, "includes", Builtins::kArrayIncludes, 2); |
| 73 InstallBuiltin(isolate, holder, "indexOf", Builtins::kArrayIndexOf, 2); |
73 | 74 |
74 return *holder; | 75 return *holder; |
75 } | 76 } |
76 | 77 |
77 | 78 |
78 RUNTIME_FUNCTION(Runtime_FixedArrayGet) { | 79 RUNTIME_FUNCTION(Runtime_FixedArrayGet) { |
79 SealHandleScope shs(isolate); | 80 SealHandleScope shs(isolate); |
80 DCHECK(args.length() == 2); | 81 DCHECK(args.length() == 2); |
81 CONVERT_ARG_CHECKED(FixedArray, object, 0); | 82 CONVERT_ARG_CHECKED(FixedArray, object, 0); |
82 CONVERT_SMI_ARG_CHECKED(index, 1); | 83 CONVERT_SMI_ARG_CHECKED(index, 1); |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 } | 538 } |
538 | 539 |
539 // If SameValueZero(searchElement, elementK) is true, return true. | 540 // If SameValueZero(searchElement, elementK) is true, return true. |
540 if (search_element->SameValueZero(*element_k)) { | 541 if (search_element->SameValueZero(*element_k)) { |
541 return isolate->heap()->true_value(); | 542 return isolate->heap()->true_value(); |
542 } | 543 } |
543 } | 544 } |
544 return isolate->heap()->false_value(); | 545 return isolate->heap()->false_value(); |
545 } | 546 } |
546 | 547 |
| 548 RUNTIME_FUNCTION(Runtime_ArrayIndexOf) { |
| 549 HandleScope shs(isolate); |
| 550 DCHECK(args.length() == 3); |
| 551 CONVERT_ARG_HANDLE_CHECKED(Object, search_element, 1); |
| 552 CONVERT_ARG_HANDLE_CHECKED(Object, from_index, 2); |
| 553 |
| 554 // Let O be ? ToObject(this value). |
| 555 Handle<Object> receiver_obj = args.at<Object>(0); |
| 556 if (receiver_obj->IsNull(isolate) || receiver_obj->IsUndefined(isolate)) { |
| 557 THROW_NEW_ERROR_RETURN_FAILURE( |
| 558 isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, |
| 559 isolate->factory()->NewStringFromAsciiChecked( |
| 560 "Array.prototype.indexOf"))); |
| 561 } |
| 562 Handle<JSReceiver> object; |
| 563 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 564 isolate, object, Object::ToObject(isolate, args.at<Object>(0))); |
| 565 |
| 566 // Let len be ? ToLength(? Get(O, "length")). |
| 567 int64_t len; |
| 568 { |
| 569 if (object->IsJSArray()) { |
| 570 uint32_t len32 = 0; |
| 571 bool success = JSArray::cast(*object)->length()->ToArrayLength(&len32); |
| 572 DCHECK(success); |
| 573 USE(success); |
| 574 len = len32; |
| 575 } else { |
| 576 Handle<Object> len_; |
| 577 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 578 isolate, len_, |
| 579 Object::GetProperty(object, isolate->factory()->length_string())); |
| 580 |
| 581 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, len_, |
| 582 Object::ToLength(isolate, len_)); |
| 583 len = static_cast<int64_t>(len_->Number()); |
| 584 DCHECK_EQ(len, len_->Number()); |
| 585 } |
| 586 } |
| 587 |
| 588 if (len == 0) return Smi::FromInt(-1); |
| 589 |
| 590 // Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step |
| 591 // produces the value 0.) |
| 592 int64_t start_from; |
| 593 { |
| 594 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, from_index, |
| 595 Object::ToInteger(isolate, from_index)); |
| 596 double fp = from_index->Number(); |
| 597 if (fp > len) return Smi::FromInt(-1); |
| 598 start_from = static_cast<int64_t>(fp); |
| 599 } |
| 600 |
| 601 int64_t index; |
| 602 if (start_from >= 0) { |
| 603 index = start_from; |
| 604 } else { |
| 605 index = len + start_from; |
| 606 if (index < 0) { |
| 607 index = 0; |
| 608 } |
| 609 } |
| 610 |
| 611 // If the receiver is not a special receiver type, and the length is a valid |
| 612 // element index, perform fast operation tailored to specific ElementsKinds. |
| 613 if (object->map()->instance_type() > LAST_SPECIAL_RECEIVER_TYPE && |
| 614 len < kMaxUInt32 && |
| 615 JSObject::PrototypeHasNoElements(isolate, JSObject::cast(*object))) { |
| 616 Handle<JSObject> obj = Handle<JSObject>::cast(object); |
| 617 ElementsAccessor* elements = obj->GetElementsAccessor(); |
| 618 Maybe<int64_t> result = elements->IndexOfValue(isolate, obj, search_element, |
| 619 static_cast<uint32_t>(index), |
| 620 static_cast<uint32_t>(len)); |
| 621 MAYBE_RETURN(result, isolate->heap()->exception()); |
| 622 return *isolate->factory()->NewNumberFromInt64(result.FromJust()); |
| 623 } |
| 624 |
| 625 // Otherwise, perform slow lookups for special receiver types |
| 626 for (; index < len; ++index) { |
| 627 // Let elementK be the result of ? Get(O, ! ToString(k)). |
| 628 Handle<Object> element_k; |
| 629 { |
| 630 Handle<Object> index_obj = isolate->factory()->NewNumberFromInt64(index); |
| 631 bool success; |
| 632 LookupIterator it = LookupIterator::PropertyOrElement( |
| 633 isolate, object, index_obj, &success); |
| 634 DCHECK(success); |
| 635 if (!JSReceiver::HasProperty(&it).FromJust()) { |
| 636 continue; |
| 637 } |
| 638 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k, |
| 639 Object::GetProperty(&it)); |
| 640 if (search_element->StrictEquals(*element_k)) { |
| 641 return *index_obj; |
| 642 } |
| 643 } |
| 644 } |
| 645 return Smi::FromInt(-1); |
| 646 } |
| 647 |
547 } // namespace internal | 648 } // namespace internal |
548 } // namespace v8 | 649 } // namespace v8 |
OLD | NEW |