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

Side by Side Diff: src/builtins/builtins-typedarray.cc

Issue 2744283002: [typedarrays] Implement %TypedArray%.prototype.lastIndexOf in C++ (Closed)
Patch Set: Correct integral value check Created 3 years, 9 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
« no previous file with comments | « src/builtins/builtins.h ('k') | src/elements.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 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/builtins/builtins-utils.h" 5 #include "src/builtins/builtins-utils.h"
6 #include "src/builtins/builtins.h" 6 #include "src/builtins/builtins.h"
7 #include "src/code-stub-assembler.h" 7 #include "src/code-stub-assembler.h"
8 #include "src/counters.h" 8 #include "src/counters.h"
9 #include "src/elements.h" 9 #include "src/elements.h"
10 #include "src/objects-inl.h" 10 #include "src/objects-inl.h"
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 578
579 Handle<Object> search_element = args.at<Object>(1); 579 Handle<Object> search_element = args.at<Object>(1);
580 ElementsAccessor* elements = array->GetElementsAccessor(); 580 ElementsAccessor* elements = array->GetElementsAccessor();
581 Maybe<int64_t> result = elements->IndexOfValue(isolate, array, search_element, 581 Maybe<int64_t> result = elements->IndexOfValue(isolate, array, search_element,
582 static_cast<uint32_t>(index), 582 static_cast<uint32_t>(index),
583 static_cast<uint32_t>(len)); 583 static_cast<uint32_t>(len));
584 MAYBE_RETURN(result, isolate->heap()->exception()); 584 MAYBE_RETURN(result, isolate->heap()->exception());
585 return *isolate->factory()->NewNumberFromInt64(result.FromJust()); 585 return *isolate->factory()->NewNumberFromInt64(result.FromJust());
586 } 586 }
587 587
588 BUILTIN(TypedArrayPrototypeLastIndexOf) {
589 HandleScope scope(isolate);
590
591 Handle<JSTypedArray> array;
592 const char* method = "%TypedArray%.prototype.lastIndexOf";
593 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
594 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method));
595
596 int64_t len = array->length_value();
597 if (len == 0) return Smi::FromInt(-1);
598
599 int64_t index = len - 1;
600 if (args.length() > 2) {
601 Handle<Object> num;
602 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
603 isolate, num, Object::ToInteger(isolate, args.at<Object>(2)));
604 // Set a negative value (-1) for returning -1 if num is negative and
605 // len + num is still negative. Upper bound is len - 1.
606 index = std::min<int64_t>(CapRelativeIndex(num, -1, len), len - 1);
607 }
608
609 if (index < 0) return Smi::FromInt(-1);
610
611 // TODO(cwhan.tunz): throw. See the above comment in CopyWithin.
612 if (V8_UNLIKELY(array->WasNeutered())) return Smi::FromInt(-1);
613
614 Handle<Object> search_element = args.atOrUndefined(isolate, 1);
615 ElementsAccessor* elements = array->GetElementsAccessor();
616 Maybe<int64_t> result = elements->LastIndexOfValue(
617 isolate, array, search_element, static_cast<uint32_t>(index));
618 MAYBE_RETURN(result, isolate->heap()->exception());
619 return *isolate->factory()->NewNumberFromInt64(result.FromJust());
620 }
621
588 } // namespace internal 622 } // namespace internal
589 } // namespace v8 623 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins.h ('k') | src/elements.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698