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<JSReceiver> object; |
| 556 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 557 isolate, object, Object::ToObject(isolate, args.at<Object>(0))); |
| 558 |
| 559 // Let len be ? ToLength(? Get(O, "length")). |
| 560 int64_t len; |
| 561 { |
| 562 if (object->IsJSArray()) { |
| 563 uint32_t len32 = 0; |
| 564 bool success = JSArray::cast(*object)->length()->ToArrayLength(&len32); |
| 565 DCHECK(success); |
| 566 USE(success); |
| 567 len = len32; |
| 568 } else { |
| 569 Handle<Object> len_; |
| 570 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 571 isolate, len_, |
| 572 Object::GetProperty(object, isolate->factory()->length_string())); |
| 573 |
| 574 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, len_, |
| 575 Object::ToLength(isolate, len_)); |
| 576 len = static_cast<int64_t>(len_->Number()); |
| 577 DCHECK_EQ(len, len_->Number()); |
| 578 } |
| 579 } |
| 580 |
| 581 if (len == 0) return Smi::FromInt(-1); |
| 582 |
| 583 // Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step |
| 584 // produces the value 0.) |
| 585 int64_t start_from; |
| 586 { |
| 587 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, from_index, |
| 588 Object::ToInteger(isolate, from_index)); |
| 589 double fp = from_index->Number(); |
| 590 if (fp > len) return Smi::FromInt(-1); |
| 591 start_from = static_cast<int64_t>(fp); |
| 592 } |
| 593 |
| 594 int64_t index; |
| 595 if (start_from >= 0) { |
| 596 index = start_from; |
| 597 } else { |
| 598 index = len + start_from; |
| 599 if (index < 0) { |
| 600 index = 0; |
| 601 } |
| 602 } |
| 603 |
| 604 // TODO(mattloring): specialize for reciever types as with include. |
| 605 |
| 606 // Otherwise, perform slow lookups for special receiver types |
| 607 for (; index < len; ++index) { |
| 608 // Let elementK be the result of ? Get(O, ! ToString(k)). |
| 609 Handle<Object> element_k; |
| 610 { |
| 611 Handle<Object> index_obj = isolate->factory()->NewNumberFromInt64(index); |
| 612 bool success; |
| 613 LookupIterator it = LookupIterator::PropertyOrElement( |
| 614 isolate, object, index_obj, &success); |
| 615 DCHECK(success); |
| 616 if (!JSReceiver::HasProperty(&it).FromJust()) { |
| 617 continue; |
| 618 } |
| 619 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k, |
| 620 Object::GetProperty(&it)); |
| 621 if (search_element->StrictEquals(*element_k)) { |
| 622 return *index_obj; |
| 623 } |
| 624 } |
| 625 } |
| 626 return Smi::FromInt(-1); |
| 627 } |
| 628 |
547 } // namespace internal | 629 } // namespace internal |
548 } // namespace v8 | 630 } // namespace v8 |
OLD | NEW |