OLD | NEW |
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 Loading... |
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, int argc = -1) { | 35 const char* name, Handle<Code> code) { |
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 if (argc < 0) { | 39 optimized->shared()->DontAdaptArguments(); |
40 optimized->shared()->DontAdaptArguments(); | |
41 } else { | |
42 optimized->shared()->set_internal_formal_parameter_count(argc); | |
43 } | |
44 JSObject::AddProperty(holder, key, optimized, NONE); | 40 JSObject::AddProperty(holder, key, optimized, NONE); |
45 } | 41 } |
46 | 42 |
47 static void InstallBuiltin(Isolate* isolate, Handle<JSObject> holder, | 43 static void InstallBuiltin(Isolate* isolate, Handle<JSObject> holder, |
48 const char* name, Builtins::Name builtin_name, | 44 const char* name, Builtins::Name builtin_name) { |
49 int argc = -1) { | |
50 InstallCode(isolate, holder, name, | 45 InstallCode(isolate, holder, name, |
51 handle(isolate->builtins()->builtin(builtin_name), isolate), | 46 handle(isolate->builtins()->builtin(builtin_name), isolate)); |
52 argc); | |
53 } | 47 } |
54 | 48 |
55 RUNTIME_FUNCTION(Runtime_SpecialArrayFunctions) { | 49 RUNTIME_FUNCTION(Runtime_SpecialArrayFunctions) { |
56 HandleScope scope(isolate); | 50 HandleScope scope(isolate); |
57 DCHECK(args.length() == 0); | 51 DCHECK(args.length() == 0); |
58 Handle<JSObject> holder = | 52 Handle<JSObject> holder = |
59 isolate->factory()->NewJSObject(isolate->object_function()); | 53 isolate->factory()->NewJSObject(isolate->object_function()); |
60 | 54 |
61 InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); | 55 InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
62 if (FLAG_minimal) { | 56 if (FLAG_minimal) { |
63 InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); | 57 InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
64 } else { | 58 } else { |
65 FastArrayPushStub stub(isolate); | 59 FastArrayPushStub stub(isolate); |
66 InstallCode(isolate, holder, "push", stub.GetCode()); | 60 InstallCode(isolate, holder, "push", stub.GetCode()); |
67 } | 61 } |
68 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); | 62 InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
69 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); | 63 InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
70 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); | 64 InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
71 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); | 65 InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
72 InstallBuiltin(isolate, holder, "includes", Builtins::kArrayIncludes, 2); | |
73 | 66 |
74 return *holder; | 67 return *holder; |
75 } | 68 } |
76 | 69 |
77 | 70 |
78 RUNTIME_FUNCTION(Runtime_FixedArrayGet) { | 71 RUNTIME_FUNCTION(Runtime_FixedArrayGet) { |
79 SealHandleScope shs(isolate); | 72 SealHandleScope shs(isolate); |
80 DCHECK(args.length() == 2); | 73 DCHECK(args.length() == 2); |
81 CONVERT_ARG_CHECKED(FixedArray, object, 0); | 74 CONVERT_ARG_CHECKED(FixedArray, object, 0); |
82 CONVERT_SMI_ARG_CHECKED(index, 1); | 75 CONVERT_SMI_ARG_CHECKED(index, 1); |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 | 436 |
444 | 437 |
445 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) { | 438 RUNTIME_FUNCTION(Runtime_ArraySpeciesConstructor) { |
446 HandleScope scope(isolate); | 439 HandleScope scope(isolate); |
447 DCHECK(args.length() == 1); | 440 DCHECK(args.length() == 1); |
448 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0); | 441 CONVERT_ARG_HANDLE_CHECKED(Object, original_array, 0); |
449 RETURN_RESULT_OR_FAILURE( | 442 RETURN_RESULT_OR_FAILURE( |
450 isolate, Object::ArraySpeciesConstructor(isolate, original_array)); | 443 isolate, Object::ArraySpeciesConstructor(isolate, original_array)); |
451 } | 444 } |
452 | 445 |
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; | |
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 | |
546 } // namespace internal | 446 } // namespace internal |
547 } // namespace v8 | 447 } // namespace v8 |
OLD | NEW |