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

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

Issue 2399423003: [builtins] Move StringIncludes to a builtin. (Closed)
Patch Set: Add another test. Re-use IsRegExp logic Created 4 years, 2 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-regexp.cc ('k') | src/js/string.js » ('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.h" 5 #include "src/builtins/builtins.h"
6 #include "src/builtins/builtins-utils.h" 6 #include "src/builtins/builtins-utils.h"
7 7
8 #include "src/code-factory.h" 8 #include "src/code-factory.h"
9 9
10 namespace v8 { 10 namespace v8 {
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after
774 774
775 assembler->Bind(&if_positioninbounds); 775 assembler->Bind(&if_positioninbounds);
776 } 776 }
777 777
778 // Load the character at the {position} from the {receiver}. 778 // Load the character at the {position} from the {receiver}.
779 Node* value = assembler->StringCharCodeAt(receiver, position); 779 Node* value = assembler->StringCharCodeAt(receiver, position);
780 Node* result = assembler->SmiFromWord32(value); 780 Node* result = assembler->SmiFromWord32(value);
781 assembler->Return(result); 781 assembler->Return(result);
782 } 782 }
783 783
784 // ES6 section 21.1.3.7
785 // String.prototype.includes ( searchString [ , position ] )
786 BUILTIN(StringPrototypeIncludes) {
787 HandleScope handle_scope(isolate);
788 TO_THIS_STRING(str, "String.prototype.includes");
789
790 // Check if the search string is a regExp and fail if it is.
791 Handle<Object> search = args.atOrUndefined(isolate, 1);
792 Maybe<bool> is_reg_exp = Object::IsRegExp(isolate, search);
793 if (is_reg_exp.IsNothing()) {
794 DCHECK(isolate->has_pending_exception());
795 return isolate->heap()->exception();
796 }
797 if (is_reg_exp.FromJust()) {
798 THROW_NEW_ERROR_RETURN_FAILURE(
799 isolate, NewTypeError(MessageTemplate::kFirstArgumentNotRegExp,
800 isolate->factory()->NewStringFromStaticChars(
801 "String.prototype.includes")));
802 }
803 Handle<String> search_string;
804 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, search_string,
805 Object::ToString(isolate, search));
806 Handle<Object> position;
807 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
808 isolate, position,
809 Object::ToInteger(isolate, args.atOrUndefined(isolate, 2)));
810
811 double index = std::max(position->Number(), 0.0);
812 index = std::min(index, static_cast<double>(str->length()));
813
814 int index_in_str = String::IndexOf(isolate, str, search_string,
815 static_cast<uint32_t>(index));
816 return *isolate->factory()->ToBoolean(index_in_str != -1);
817 }
818
784 // ES6 section 21.1.3.8 String.prototype.indexOf ( searchString [ , position ] ) 819 // ES6 section 21.1.3.8 String.prototype.indexOf ( searchString [ , position ] )
785 BUILTIN(StringPrototypeIndexOf) { 820 BUILTIN(StringPrototypeIndexOf) {
786 HandleScope handle_scope(isolate); 821 HandleScope handle_scope(isolate);
787 822
788 return String::IndexOf(isolate, args.receiver(), 823 return String::IndexOf(isolate, args.receiver(),
789 args.atOrUndefined(isolate, 1), 824 args.atOrUndefined(isolate, 1),
790 args.atOrUndefined(isolate, 2)); 825 args.atOrUndefined(isolate, 2));
791 } 826 }
792 827
793 // ES6 section 21.1.3.9 828 // ES6 section 21.1.3.9
(...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after
1350 Runtime::kThrowIncompatibleMethodReceiver, context, 1385 Runtime::kThrowIncompatibleMethodReceiver, context,
1351 assembler->HeapConstant(assembler->factory()->NewStringFromAsciiChecked( 1386 assembler->HeapConstant(assembler->factory()->NewStringFromAsciiChecked(
1352 "String Iterator.prototype.next", TENURED)), 1387 "String Iterator.prototype.next", TENURED)),
1353 iterator); 1388 iterator);
1354 assembler->Return(result); // Never reached. 1389 assembler->Return(result); // Never reached.
1355 } 1390 }
1356 } 1391 }
1357 1392
1358 } // namespace internal 1393 } // namespace internal
1359 } // namespace v8 1394 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins-regexp.cc ('k') | src/js/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698