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) { | 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 Loading... | |
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 static inline Object* FastArrayIncludes(Isolate* isolate, | |
449 Handle<JSObject> object, | |
450 Handle<Object> value, uint32_t k, | |
451 uint32_t len) { | |
452 ElementsAccessor* elements = object->GetElementsAccessor(); | |
453 Maybe<bool> result = elements->IncludesValue(isolate, object, value, k, len); | |
454 MAYBE_RETURN(result, isolate->heap()->exception()); | |
455 if (result.FromJust()) { | |
456 return isolate->heap()->true_value(); | |
457 } else { | |
458 return isolate->heap()->false_value(); | |
459 } | |
Camillo Bruni
2016/07/21 08:01:55
nit: isolate->heap()->ToBoolean(result.FromJust())
| |
460 } | |
461 | |
462 // ES7 22.1.3.11 Array.prototype.includes | |
463 RUNTIME_FUNCTION(Runtime_ArrayIncludes_Slow) { | |
464 HandleScope shs(isolate); | |
Camillo Bruni
2016/07/21 08:01:55
uber-nit: shs => sh (it's not a SealedHandleScope)
| |
465 DCHECK(args.length() == 3); | |
Camillo Bruni
2016/07/21 08:01:55
nit: DCHECK_EQ
| |
466 CONVERT_ARG_HANDLE_CHECKED(Object, search_element, 1); | |
467 CONVERT_ARG_HANDLE_CHECKED(Object, from_index_, 2); | |
468 | |
469 // 1. Let O be ? ToObject(this value). | |
470 Handle<JSReceiver> object; | |
471 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
472 isolate, object, Object::ToObject(isolate, handle(args[0], isolate))); | |
473 | |
474 // 2. Let len be ? ToLength(? Get(O, "length")). | |
Camillo Bruni
2016/07/21 08:01:55
Maybe add a shortcut for JS_ARRAY instance types d
| |
475 Handle<Object> len_; | |
476 Handle<String> length_string = | |
477 handle(isolate->heap()->length_string(), isolate); | |
Camillo Bruni
2016/07/21 08:01:55
nit: isolate->factory()->length_string() returns d
| |
478 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
479 isolate, len_, Object::GetProperty(object, length_string)); | |
480 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, len_, | |
481 Object::ToLength(isolate, len_)); | |
482 int64_t len = static_cast<int64_t>(len_->Number()); | |
483 DCHECK_EQ(len, len_->Number()); | |
484 | |
485 // 3. If len is 0, return false. | |
486 if (len == 0) return isolate->heap()->false_value(); | |
487 | |
488 // 4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, | |
489 // this step produces the value 0.) | |
490 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, from_index_, | |
491 Object::ToInteger(isolate, from_index_)); | |
492 if (std::isinf(from_index_->Number()) && from_index_->Number() > 0.0) { | |
493 return isolate->heap()->false_value(); | |
494 } | |
495 int64_t from_index = static_cast<int64_t>(from_index_->Number()); | |
496 | |
497 int64_t k; | |
Camillo Bruni
2016/07/21 08:01:55
nit: despite the spec, can you give reasonable var
caitp
2016/07/21 20:32:29
Done.
| |
498 if (from_index >= 0) { | |
499 k = from_index; | |
500 } else { | |
501 k = len + from_index; | |
502 if (k < 0) { | |
503 k = 0; | |
504 } | |
505 } | |
506 | |
507 // In the slow path, we can still take a fast path similar to the TurboFan | |
508 // implementation if there are no elements in the prototype, and if the length | |
509 // is within elements boundaries. | |
510 // | |
511 // This would apply to Array-like Objects with no elements in the prototype, | |
512 // for example. | |
513 if (object->map()->instance_type() > LAST_SPECIAL_RECEIVER_TYPE && | |
514 len < std::numeric_limits<uint32_t>::max() && | |
515 JSObject::PrototypeHasNoElements(isolate, JSObject::cast(*object))) { | |
516 return FastArrayIncludes(isolate, Handle<JSObject>::cast(object), | |
517 search_element, static_cast<uint32_t>(k), | |
518 static_cast<uint32_t>(len)); | |
519 } | |
520 | |
521 // Otherwise, perform slow lookups for JSProxies and API objects, or for | |
522 // objects with long `length` values. | |
523 for (; k < len; ++k) { | |
524 Handle<Object> element_k; | |
525 | |
526 Handle<Object> index = isolate->factory()->NewNumberFromInt64(k); | |
527 | |
528 bool success; | |
529 LookupIterator it = | |
530 LookupIterator::PropertyOrElement(isolate, object, index, &success); | |
531 DCHECK(success); | |
532 | |
533 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k, | |
534 Object::GetProperty(&it)); | |
535 | |
536 if (search_element->SameValueZero(*element_k)) { | |
537 return isolate->heap()->true_value(); | |
538 } | |
539 } | |
540 return isolate->heap()->false_value(); | |
541 } | |
542 | |
441 } // namespace internal | 543 } // namespace internal |
442 } // namespace v8 | 544 } // namespace v8 |
OLD | NEW |