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

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: try lots of tight loops Created 4 years, 5 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/code-stub-assembler.cc ('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 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 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 438
432 439
433 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) { 440 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) {
434 HandleScope scope(isolate); 441 HandleScope scope(isolate);
435 DCHECK(args.length() == 1); 442 DCHECK(args.length() == 1);
436 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0); 443 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0);
437 RETURN_RESULT_OR_FAILURE( 444 RETURN_RESULT_OR_FAILURE(
438 isolate, Object::ArraySpeciesConstructor(isolate, original_array)); 445 isolate, Object::ArraySpeciesConstructor(isolate, original_array));
439 } 446 }
440 447
448 // ES7 22.1.3.11 Array.prototype.includes
449 RUNTIME_FUNCTION(Runtime_ArrayIncludes_Slow) {
450 HandleScope shs(isolate);
451 DCHECK(args.length() == 3);
452 CONVERT_ARG_HANDLE_CHECKED(Object, search_element, 1);
453 CONVERT_ARG_HANDLE_CHECKED(Object, from_index_, 2);
454
455 // 1. Let O be ? ToObject(this value).
456 Handle<JSReceiver> object;
457 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
458 isolate, object, Object::ToObject(isolate, handle(args[0], isolate)));
459
460 // 2. Let len be ? ToLength(? Get(O, "length")).
461 Handle<Object> len_;
462 Handle<String> length_string =
463 handle(isolate->heap()->length_string(), isolate);
464 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
465 isolate, len_, Object::GetProperty(object, length_string));
466 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, len_,
467 Object::ToLength(isolate, len_));
468 int64_t len = static_cast<int64_t>(len_->Number());
469 DCHECK_EQ(len, len_->Number());
470
471 // 3. If len is 0, return false.
472 if (len == 0) return isolate->heap()->false_value();
473
474 // 4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined,
475 // this step produces the value 0.)
476 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, from_index_,
477 Object::ToInteger(isolate, from_index_));
478 if (std::isinf(from_index_->Number()) && from_index_->Number() > 0.0) {
479 return isolate->heap()->false_value();
480 }
481 int64_t from_index = static_cast<int64_t>(from_index_->Number());
482
483 int64_t k;
484 if (from_index >= 0) {
485 k = from_index;
486 } else {
487 k = len + from_index;
488 if (k < 0) {
489 k = 0;
490 }
491 }
492
493 if (IsFastPackedElementsKind(object->map()->elements_kind()) &&
494 len < std::numeric_limits<uint32_t>::max()) {
495 uint32_t i = static_cast<uint32_t>(k);
496 for (; i < static_cast<uint32_t>(len); ++i) {
497 Handle<Object> element_k;
498 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
499 isolate, element_k, Object::GetElement(isolate, object, i));
500 if (search_element->SameValueZero(*element_k)) {
501 return isolate->heap()->true_value();
502 }
503 }
504 return isolate->heap()->false_value();
505 }
506
507 bool stable = !object->IsJSProxy() &&
508 IsFastElementsKind(object->map()->elements_kind());
509 Handle<Map> original_map = handle(object->map(), isolate);
510 for (; k < len; ++k) {
511 Handle<Object> element_k;
512 if (stable && k < std::numeric_limits<uint32_t>::max()) {
513 uint32_t index = static_cast<uint32_t>(k);
514 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
515 isolate, element_k, Object::GetElement(isolate, object, index));
516 stable = object->map() == *original_map;
517 } else {
518 Handle<String> name;
519 Handle<Object> num =
520 isolate->factory()->NewNumber(static_cast<double>(k));
521 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
522 Object::ToString(isolate, num));
523 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
524 isolate, element_k, Object::GetPropertyOrElement(object, name));
525 }
526
527 if (search_element->SameValueZero(*element_k)) {
528 return isolate->heap()->true_value();
529 }
530 }
531 return isolate->heap()->false_value();
532 }
533
441 } // namespace internal 534 } // namespace internal
442 } // namespace v8 535 } // namespace v8
OLDNEW
« src/code-stub-assembler.cc ('K') | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698