Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: src/runtime/runtime-array.cc

Issue 2232063002: [builtins] WIP: Array indexOf in TurboFan/Runtime (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add elements fastpaths Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 // If the receiver is not a special receiver type, and the length is a valid
605 // element index, perform fast operation tailored to specific ElementsKinds.
606 if (object->map()->instance_type() > LAST_SPECIAL_RECEIVER_TYPE &&
607 len < kMaxUInt32 &&
608 JSObject::PrototypeHasNoElements(isolate, JSObject::cast(*object))) {
609 Handle<JSObject> obj = Handle<JSObject>::cast(object);
610 ElementsAccessor* elements = obj->GetElementsAccessor();
611 Maybe<int32_t> result = elements->IndexOfValue(isolate, obj, search_element,
612 static_cast<uint32_t>(index),
613 static_cast<uint32_t>(len));
614 MAYBE_RETURN(result, isolate->heap()->exception());
615 return Smi::FromInt(result.FromJust());
Jakob Kummerow 2016/08/17 15:42:59 What if the result is outside Smi range?
mattloring 2016/08/18 19:03:11 Done.
616 }
617
618 // Otherwise, perform slow lookups for special receiver types
619 for (; index < len; ++index) {
620 // Let elementK be the result of ? Get(O, ! ToString(k)).
621 Handle<Object> element_k;
622 {
623 Handle<Object> index_obj = isolate->factory()->NewNumberFromInt64(index);
624 bool success;
625 LookupIterator it = LookupIterator::PropertyOrElement(
626 isolate, object, index_obj, &success);
627 DCHECK(success);
628 if (!JSReceiver::HasProperty(&it).FromJust()) {
629 continue;
630 }
631 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k,
632 Object::GetProperty(&it));
633 if (search_element->StrictEquals(*element_k)) {
634 return *index_obj;
635 }
636 }
637 }
638 return Smi::FromInt(-1);
639 }
640
547 } // namespace internal 641 } // namespace internal
548 } // namespace v8 642 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698