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

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: 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
« src/js/typedarray.js ('K') | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 CONVERT_NUMBER_CHECKED(int, key, Int32, args[1]); 368 CONVERT_NUMBER_CHECKED(int, key, Int32, args[1]);
368 369
369 if (key < 0) { 370 if (key < 0) {
370 return object->elements(); 371 return object->elements();
371 } 372 }
372 373
373 uint32_t capacity = static_cast<uint32_t>(object->elements()->length()); 374 uint32_t capacity = static_cast<uint32_t>(object->elements()->length());
374 uint32_t index = static_cast<uint32_t>(key); 375 uint32_t index = static_cast<uint32_t>(key);
375 376
376 if (index >= capacity) { 377 if (index >= capacity) {
377 if (object->map()->is_prototype_map() || 378 if (object->WouldConvertToSlowElements(index)) {
Benedikt Meurer 2016/08/11 04:02:24 You seem to undo a correctness fix here.
mattloring 2016/08/15 17:31:09 Done.
378 object->WouldConvertToSlowElements(index)) {
379 // We don't want to allow operations that cause lazy deopt. Return a Smi 379 // We don't want to allow operations that cause lazy deopt. Return a Smi
380 // as a signal that optimized code should eagerly deoptimize. 380 // as a signal that optimized code should eagerly deoptimize.
381 return Smi::FromInt(0); 381 return Smi::FromInt(0);
382 } 382 }
383 383
384 uint32_t new_capacity = JSObject::NewElementsCapacity(index + 1); 384 uint32_t new_capacity = JSObject::NewElementsCapacity(index + 1);
385 object->GetElementsAccessor()->GrowCapacityAndConvert(object, new_capacity); 385 object->GetElementsAccessor()->GrowCapacityAndConvert(object, new_capacity);
386 } 386 }
387 387
388 // On success, return the fixed array elements. 388 // On success, return the fixed array elements.
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 } 537 }
538 538
539 // If SameValueZero(searchElement, elementK) is true, return true. 539 // If SameValueZero(searchElement, elementK) is true, return true.
540 if (search_element->SameValueZero(*element_k)) { 540 if (search_element->SameValueZero(*element_k)) {
541 return isolate->heap()->true_value(); 541 return isolate->heap()->true_value();
542 } 542 }
543 } 543 }
544 return isolate->heap()->false_value(); 544 return isolate->heap()->false_value();
545 } 545 }
546 546
547 RUNTIME_FUNCTION(Runtime_ArrayIndexOf) {
548 HandleScope shs(isolate);
549 DCHECK(args.length() == 3);
550 CONVERT_ARG_HANDLE_CHECKED(Object, search_element, 1);
551 CONVERT_ARG_HANDLE_CHECKED(Object, from_index, 2);
552
553 // Let O be ? ToObject(this value).
554 Handle<JSReceiver> object;
555 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
556 isolate, object, Object::ToObject(isolate, handle(args[0], isolate)));
Camillo Bruni 2016/08/12 10:46:49 nit: use args.at<Object>(0)
mattloring 2016/08/15 17:31:09 Done.
557
558 // Let len be ? ToLength(? Get(O, "length")).
559 int64_t len;
560 {
561 if (object->map()->instance_type() == JS_ARRAY_TYPE) {
Camillo Bruni 2016/08/12 10:46:49 nit: object->IsJSArray()
mattloring 2016/08/15 17:31:09 Done.
562 uint32_t len32 = 0;
563 bool success = JSArray::cast(*object)->length()->ToArrayLength(&len32);
564 DCHECK(success);
565 USE(success);
566 len = len32;
567 } else {
568 Handle<Object> len_;
569 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
570 isolate, len_,
571 Object::GetProperty(object, isolate->factory()->length_string()));
572
573 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, len_,
574 Object::ToLength(isolate, len_));
575 len = static_cast<int64_t>(len_->Number());
576 DCHECK_EQ(len, len_->Number());
577 }
578 }
579
580 if (len == 0) return *isolate->factory()->NewNumberFromInt64(-1);
Camillo Bruni 2016/08/12 10:46:49 nit: return Smi::FromInt(-1);
mattloring 2016/08/15 17:31:09 Done.
581
582 // Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step
583 // produces the value 0.)
584 int64_t start_from;
585 {
586 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, from_index,
587 Object::ToInteger(isolate, from_index));
588 double fp = from_index->Number();
589 if (fp > len) return *isolate->factory()->NewNumberFromInt64(-1);
Camillo Bruni 2016/08/12 10:46:49 nit: return Smi::FromInt(-1);
mattloring 2016/08/15 17:31:09 Done.
590 start_from = static_cast<int64_t>(fp);
591 }
592
593 int64_t index;
594 if (start_from >= 0) {
595 index = start_from;
596 } else {
597 index = len + start_from;
598 if (index < 0) {
599 index = 0;
600 }
601 }
602
603 // TODO(mattloring): specialize for reciever types as with include.
Camillo Bruni 2016/08/12 10:46:49 Pretty please ;)
mattloring 2016/08/17 23:27:20 Added in the most recent patch set.
604
605 // Otherwise, perform slow lookups for special receiver types
606 for (; index < len; ++index) {
607 // Let elementK be the result of ? Get(O, ! ToString(k)).
608 Handle<Object> element_k;
609 {
610 Handle<Object> index_obj = isolate->factory()->NewNumberFromInt64(index);
611 bool success;
612 LookupIterator it = LookupIterator::PropertyOrElement(
613 isolate, object, index_obj, &success);
614 DCHECK(success);
615 if (!JSReceiver::HasProperty(&it).FromJust()) {
616 continue;
617 }
618 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k,
619 Object::GetProperty(&it));
620 if (search_element->StrictEquals(*element_k)) {
621 return *index_obj;
622 }
623 }
624 }
625 return *isolate->factory()->NewNumberFromInt64(-1);
Camillo Bruni 2016/08/12 10:46:49 nit: return Smi::FromInt(-1);
mattloring 2016/08/15 17:31:09 Done.
626 }
627
547 } // namespace internal 628 } // namespace internal
548 } // namespace v8 629 } // namespace v8
OLDNEW
« src/js/typedarray.js ('K') | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698