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

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

Issue 2146293003: [builtins] implement Array.prototype.includes in TurboFan (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: fix some things, rebase, rename some variables in TF, etc 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 14 matching lines...) Expand all
25 CHECK(length->IsSmi()); 25 CHECK(length->IsSmi());
26 CHECK(Smi::cast(length)->value() == 0); 26 CHECK(Smi::cast(length)->value() == 0);
27 CHECK(prototype->HasFastSmiOrObjectElements()); 27 CHECK(prototype->HasFastSmiOrObjectElements());
28 // This is necessary to enable fast checks for absence of elements 28 // This is necessary to enable fast checks for absence of elements
29 // on Array.prototype and below. 29 // on Array.prototype and below.
30 prototype->set_elements(isolate->heap()->empty_fixed_array()); 30 prototype->set_elements(isolate->heap()->empty_fixed_array());
31 return Smi::FromInt(0); 31 return Smi::FromInt(0);
32 } 32 }
33 33
34 static void InstallCode(Isolate* isolate, Handle<JSObject> holder, 34 static void InstallCode(Isolate* isolate, Handle<JSObject> holder,
35 const char* name, Handle<Code> code) { 35 const char* name, Handle<Code> code, int argc = -1) {
36 Handle<String> key = isolate->factory()->InternalizeUtf8String(name); 36 Handle<String> key = isolate->factory()->InternalizeUtf8String(name);
37 Handle<JSFunction> optimized = 37 Handle<JSFunction> optimized =
38 isolate->factory()->NewFunctionWithoutPrototype(key, code); 38 isolate->factory()->NewFunctionWithoutPrototype(key, code);
39 optimized->shared()->DontAdaptArguments(); 39 if (argc < 0) {
40 optimized->shared()->DontAdaptArguments();
41 } else {
42 optimized->shared()->set_internal_formal_parameter_count(argc);
43 }
40 JSObject::AddProperty(holder, key, optimized, NONE); 44 JSObject::AddProperty(holder, key, optimized, NONE);
41 } 45 }
42 46
43 static void InstallBuiltin(Isolate* isolate, Handle<JSObject> holder, 47 static void InstallBuiltin(Isolate* isolate, Handle<JSObject> holder,
44 const char* name, Builtins::Name builtin_name) { 48 const char* name, Builtins::Name builtin_name,
49 int argc = -1) {
45 InstallCode(isolate, holder, name, 50 InstallCode(isolate, holder, name,
46 handle(isolate->builtins()->builtin(builtin_name), isolate)); 51 handle(isolate->builtins()->builtin(builtin_name), isolate),
52 argc);
47 } 53 }
48 54
49 RUNTIME_FUNCTION(Runtime_SpecialArrayFunctions) { 55 RUNTIME_FUNCTION(Runtime_SpecialArrayFunctions) {
50 HandleScope scope(isolate); 56 HandleScope scope(isolate);
51 DCHECK(args.length() == 0); 57 DCHECK(args.length() == 0);
52 Handle<JSObject> holder = 58 Handle<JSObject> holder =
53 isolate->factory()->NewJSObject(isolate->object_function()); 59 isolate->factory()->NewJSObject(isolate->object_function());
54 60
55 InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); 61 InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop);
56 FastArrayPushStub stub(isolate); 62 FastArrayPushStub stub(isolate);
57 InstallCode(isolate, holder, "push", stub.GetCode()); 63 InstallCode(isolate, holder, "push", stub.GetCode());
58 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); 64 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift);
59 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); 65 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift);
60 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); 66 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice);
61 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); 67 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice);
68 InstallBuiltin(isolate, holder, "includes", Builtins::kArrayIncludes, 2);
62 69
63 return *holder; 70 return *holder;
64 } 71 }
65 72
66 73
67 RUNTIME_FUNCTION(Runtime_FixedArrayGet) { 74 RUNTIME_FUNCTION(Runtime_FixedArrayGet) {
68 SealHandleScope shs(isolate); 75 SealHandleScope shs(isolate);
69 DCHECK(args.length() == 2); 76 DCHECK(args.length() == 2);
70 CONVERT_ARG_CHECKED(FixedArray, object, 0); 77 CONVERT_ARG_CHECKED(FixedArray, object, 0);
71 CONVERT_SMI_ARG_CHECKED(index, 1); 78 CONVERT_SMI_ARG_CHECKED(index, 1);
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 439
433 440
434 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) { 441 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) {
435 HandleScope scope(isolate); 442 HandleScope scope(isolate);
436 DCHECK(args.length() == 1); 443 DCHECK(args.length() == 1);
437 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0); 444 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0);
438 RETURN_RESULT_OR_FAILURE( 445 RETURN_RESULT_OR_FAILURE(
439 isolate, Object::ArraySpeciesConstructor(isolate, original_array)); 446 isolate, Object::ArraySpeciesConstructor(isolate, original_array));
440 } 447 }
441 448
449 static inline Object* FastArrayIncludes(Isolate* isolate,
450 Handle<JSObject> object,
451 Handle<Object> value, uint32_t k,
452 uint32_t len) {
453 ElementsAccessor* elements = object->GetElementsAccessor();
454 Maybe<bool> result = elements->IncludesValue(isolate, object, value, k, len);
455 MAYBE_RETURN(result, isolate->heap()->exception());
456 return *isolate->factory()->ToBoolean(result.FromJust());
457 }
458
459 // ES7 22.1.3.11 Array.prototype.includes
460 RUNTIME_FUNCTION(Runtime_ArrayIncludes_Slow) {
461 HandleScope shs(isolate);
462 DCHECK(args.length() == 3);
463 CONVERT_ARG_HANDLE_CHECKED(Object, search_element, 1);
464 CONVERT_ARG_HANDLE_CHECKED(Object, start_from_, 2);
465
466 // 1. Let O be ? ToObject(this value).
467 Handle<JSReceiver> object;
468 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
469 isolate, object, Object::ToObject(isolate, handle(args[0], isolate)));
470
471 // 2. Let len be ? ToLength(? Get(O, "length")).
472 Handle<Object> len_;
473 int64_t len;
474 if (object->map()->instance_type() == JS_ARRAY_TYPE) {
475 len_ = handle(JSArray::cast(*object)->length(), isolate);
476 } else {
477 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
478 isolate, len_,
479 Object::GetProperty(object, isolate->factory()->length_string()));
480 }
481 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, len_,
482 Object::ToLength(isolate, len_));
483 len = static_cast<int64_t>(len_->Number());
484 DCHECK_EQ(len, len_->Number());
485
486 // 3. If len is 0, return false.
487 if (len == 0) return isolate->heap()->false_value();
488
489 // 4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined,
490 // this step produces the value 0.)
491 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, start_from_,
492 Object::ToInteger(isolate, start_from_));
493 if (std::isinf(start_from_->Number()) && start_from_->Number() > 0.0) {
494 return isolate->heap()->false_value();
495 }
496 int64_t start_from = static_cast<int64_t>(start_from_->Number());
497
498 int64_t index;
499 if (start_from >= 0) {
500 index = start_from;
501 } else {
502 index = len + start_from;
503 if (index < 0) {
504 index = 0;
505 }
506 }
507
508 // In the slow path, we can still take a fast path similar to the TurboFan
509 // implementation if there are no elements in the prototype, and if the length
510 // is within elements boundaries.
511 //
512 // This would apply to Array-like Objects with no elements in the prototype,
513 // for example.
514 if (object->map()->instance_type() > LAST_SPECIAL_RECEIVER_TYPE &&
515 len < std::numeric_limits<uint32_t>::max() &&
516 JSObject::PrototypeHasNoElements(isolate, JSObject::cast(*object))) {
517 return FastArrayIncludes(isolate, Handle<JSObject>::cast(object),
518 search_element, static_cast<uint32_t>(index),
519 static_cast<uint32_t>(len));
520 }
521
522 // Otherwise, perform slow lookups for JSProxies and API objects, or for
523 // objects with long `length` values.
524 for (; index < len; ++index) {
525 Handle<Object> element_k;
526
527 Handle<Object> index_object = isolate->factory()->NewNumberFromInt64(index);
528
529 bool success;
530 LookupIterator it = LookupIterator::PropertyOrElement(
531 isolate, object, index_object, &success);
532 DCHECK(success);
533
534 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k,
535 Object::GetProperty(&it));
536
537 if (search_element->SameValueZero(*element_k)) {
538 return isolate->heap()->true_value();
539 }
540 }
541 return isolate->heap()->false_value();
542 }
543
442 } // namespace internal 544 } // namespace internal
443 } // namespace v8 545 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698