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

Side by Side Diff: src/objects.cc

Issue 2577143002: [runtime] Add PositiveNumberToUint32 helper to avoid double to uint roundtrip (Closed)
Patch Set: avoid overflows Created 4 years 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/objects.h ('k') | src/objects-inl.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 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 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/objects.h" 5 #include "src/objects.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <iomanip> 8 #include <iomanip>
9 #include <memory> 9 #include <memory>
10 #include <sstream> 10 #include <sstream>
(...skipping 11716 matching lines...) Expand 10 before | Expand all | Expand 10 after
11727 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver_string, 11727 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver_string,
11728 Object::ToString(isolate, receiver)); 11728 Object::ToString(isolate, receiver));
11729 11729
11730 Handle<String> search_string; 11730 Handle<String> search_string;
11731 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, search_string, 11731 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, search_string,
11732 Object::ToString(isolate, search)); 11732 Object::ToString(isolate, search));
11733 11733
11734 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position, 11734 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position,
11735 Object::ToInteger(isolate, position)); 11735 Object::ToInteger(isolate, position));
11736 11736
11737 double index = std::max(position->Number(), 0.0); 11737 uint32_t index = receiver_string->ToValidIndex(*position);
11738 index = std::min(index, static_cast<double>(receiver_string->length())); 11738 return Smi::FromInt(
11739 11739 String::IndexOf(isolate, receiver_string, search_string, index));
11740 return Smi::FromInt(String::IndexOf(isolate, receiver_string, search_string,
11741 static_cast<uint32_t>(index)));
11742 } 11740 }
11743 11741
11744 namespace { 11742 namespace {
11745 11743
11746 template <typename T> 11744 template <typename T>
11747 int SearchString(Isolate* isolate, String::FlatContent receiver_content, 11745 int SearchString(Isolate* isolate, String::FlatContent receiver_content,
11748 Vector<T> pat_vector, int start_index) { 11746 Vector<T> pat_vector, int start_index) {
11749 if (receiver_content.IsOneByte()) { 11747 if (receiver_content.IsOneByte()) {
11750 return SearchString(isolate, receiver_content.ToOneByteVector(), pat_vector, 11748 return SearchString(isolate, receiver_content.ToOneByteVector(), pat_vector,
11751 start_index); 11749 start_index);
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
11937 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position, 11935 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position,
11938 Object::ToNumber(position)); 11936 Object::ToNumber(position));
11939 11937
11940 uint32_t start_index; 11938 uint32_t start_index;
11941 11939
11942 if (position->IsNaN()) { 11940 if (position->IsNaN()) {
11943 start_index = receiver_string->length(); 11941 start_index = receiver_string->length();
11944 } else { 11942 } else {
11945 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position, 11943 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position,
11946 Object::ToInteger(isolate, position)); 11944 Object::ToInteger(isolate, position));
11947 11945 start_index = receiver_string->ToValidIndex(*position);
11948 double position_number = std::max(position->Number(), 0.0);
11949 position_number = std::min(position_number,
11950 static_cast<double>(receiver_string->length()));
11951 start_index = static_cast<uint32_t>(position_number);
11952 } 11946 }
11953 11947
11954 uint32_t pattern_length = search_string->length(); 11948 uint32_t pattern_length = search_string->length();
11955 uint32_t receiver_length = receiver_string->length(); 11949 uint32_t receiver_length = receiver_string->length();
11956 11950
11957 if (start_index + pattern_length > receiver_length) { 11951 if (start_index + pattern_length > receiver_length) {
11958 start_index = receiver_length - pattern_length; 11952 start_index = receiver_length - pattern_length;
11959 } 11953 }
11960 11954
11961 if (pattern_length == 0) { 11955 if (pattern_length == 0) {
(...skipping 8449 matching lines...) Expand 10 before | Expand all | Expand 10 after
20411 // depend on this. 20405 // depend on this.
20412 return DICTIONARY_ELEMENTS; 20406 return DICTIONARY_ELEMENTS;
20413 } 20407 }
20414 DCHECK_LE(kind, LAST_ELEMENTS_KIND); 20408 DCHECK_LE(kind, LAST_ELEMENTS_KIND);
20415 return kind; 20409 return kind;
20416 } 20410 }
20417 } 20411 }
20418 20412
20419 } // namespace internal 20413 } // namespace internal
20420 } // namespace v8 20414 } // namespace v8
OLDNEW
« no previous file with comments | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698