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

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

Issue 2205883003: Reland [builtins] implement Array.prototype.includes in TurboFan (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix compiler warning 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 if (FLAG_minimal) { 62 if (FLAG_minimal) {
57 InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); 63 InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush);
58 } else { 64 } else {
59 FastArrayPushStub stub(isolate); 65 FastArrayPushStub stub(isolate);
60 InstallCode(isolate, holder, "push", stub.GetCode()); 66 InstallCode(isolate, holder, "push", stub.GetCode());
61 } 67 }
62 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); 68 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift);
63 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); 69 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift);
64 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); 70 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice);
65 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); 71 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice);
72 InstallBuiltin(isolate, holder, "includes", Builtins::kArrayIncludes, 2);
66 73
67 return *holder; 74 return *holder;
68 } 75 }
69 76
70 77
71 RUNTIME_FUNCTION(Runtime_FixedArrayGet) { 78 RUNTIME_FUNCTION(Runtime_FixedArrayGet) {
72 SealHandleScope shs(isolate); 79 SealHandleScope shs(isolate);
73 DCHECK(args.length() == 2); 80 DCHECK(args.length() == 2);
74 CONVERT_ARG_CHECKED(FixedArray, object, 0); 81 CONVERT_ARG_CHECKED(FixedArray, object, 0);
75 CONVERT_SMI_ARG_CHECKED(index, 1); 82 CONVERT_SMI_ARG_CHECKED(index, 1);
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 443
437 444
438 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) { 445 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) {
439 HandleScope scope(isolate); 446 HandleScope scope(isolate);
440 DCHECK(args.length() == 1); 447 DCHECK(args.length() == 1);
441 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0); 448 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0);
442 RETURN_RESULT_OR_FAILURE( 449 RETURN_RESULT_OR_FAILURE(
443 isolate, Object::ArraySpeciesConstructor(isolate, original_array)); 450 isolate, Object::ArraySpeciesConstructor(isolate, original_array));
444 } 451 }
445 452
453 // ES7 22.1.3.11 Array.prototype.includes
454 RUNTIME_FUNCTION(Runtime_ArrayIncludes_Slow) {
455 HandleScope shs(isolate);
456 DCHECK(args.length() == 3);
457 CONVERT_ARG_HANDLE_CHECKED(Object, search_element, 1);
458 CONVERT_ARG_HANDLE_CHECKED(Object, from_index, 2);
459
460 // Let O be ? ToObject(this value).
461 Handle<JSReceiver> object;
462 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
463 isolate, object, Object::ToObject(isolate, handle(args[0], isolate)));
464
465 // Let len be ? ToLength(? Get(O, "length")).
466 int64_t len;
467 {
468 if (object->map()->instance_type() == JS_ARRAY_TYPE) {
469 uint32_t len32 = 0;
470 bool success = JSArray::cast(*object)->length()->ToArrayLength(&len32);
471 DCHECK(success);
472 USE(success);
473 len = len32;
474 } else {
475 Handle<Object> len_;
476 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
477 isolate, len_,
478 Object::GetProperty(object, isolate->factory()->length_string()));
479
480 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, len_,
481 Object::ToLength(isolate, len_));
482 len = static_cast<int64_t>(len_->Number());
483 DCHECK_EQ(len, len_->Number());
484 }
485 }
486
487 if (len == 0) return isolate->heap()->false_value();
488
489 // Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step
490 // produces the value 0.)
491 int64_t start_from;
492 {
493 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, from_index,
494 Object::ToInteger(isolate, from_index));
495 double fp = from_index->Number();
496 if (fp > len) return isolate->heap()->false_value();
497 start_from = static_cast<int64_t>(fp);
498 }
499
500 int64_t index;
501 if (start_from >= 0) {
502 index = start_from;
503 } else {
504 index = len + start_from;
505 if (index < 0) {
506 index = 0;
507 }
508 }
509
510 // If the receiver is not a special receiver type, and the length is a valid
511 // element index, perform fast operation tailored to specific ElementsKinds.
512 if (object->map()->instance_type() > LAST_SPECIAL_RECEIVER_TYPE &&
513 len < kMaxUInt32 &&
514 JSObject::PrototypeHasNoElements(isolate, JSObject::cast(*object))) {
515 Handle<JSObject> obj = Handle<JSObject>::cast(object);
516 ElementsAccessor* elements = obj->GetElementsAccessor();
517 Maybe<bool> result = elements->IncludesValue(isolate, obj, search_element,
518 static_cast<uint32_t>(index),
519 static_cast<uint32_t>(len));
520 MAYBE_RETURN(result, isolate->heap()->exception());
521 return *isolate->factory()->ToBoolean(result.FromJust());
522 }
523
524 // Otherwise, perform slow lookups for special receiver types
525 for (; index < len; ++index) {
526 // Let elementK be the result of ? Get(O, ! ToString(k)).
527 Handle<Object> element_k;
528 {
529 Handle<Object> index_obj = isolate->factory()->NewNumberFromInt64(index);
530 bool success;
531 LookupIterator it = LookupIterator::PropertyOrElement(
532 isolate, object, index_obj, &success);
533 DCHECK(success);
534 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k,
535 Object::GetProperty(&it));
536 }
537
538 // If SameValueZero(searchElement, elementK) is true, return true.
539 if (search_element->SameValueZero(*element_k)) {
540 return isolate->heap()->true_value();
541 }
542 }
543 return isolate->heap()->false_value();
544 }
545
446 } // namespace internal 546 } // namespace internal
447 } // namespace v8 547 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698