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

Side by Side Diff: src/harmony-string.js

Issue 120683002: Make `String.prototype.{starts,ends}With` throw when passing a regular expression (Closed) Base URL: git@github.com:v8/v8.git@master
Patch Set: Created 7 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 | « no previous file | src/messages.js » ('j') | test/mjsunit/harmony/string-endswith.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 57
58 58
59 // ES6 draft 07-15-13, section 15.5.3.22 59 // ES6 draft 07-15-13, section 15.5.3.22
60 function StringStartsWith(searchString /* position */) { // length == 1 60 function StringStartsWith(searchString /* position */) { // length == 1
61 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 61 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
62 throw MakeTypeError("called_on_null_or_undefined", 62 throw MakeTypeError("called_on_null_or_undefined",
63 ["String.prototype.startsWith"]); 63 ["String.prototype.startsWith"]);
64 } 64 }
65 65
66 var s = TO_STRING_INLINE(this); 66 var s = TO_STRING_INLINE(this);
67
68 if (IS_REGEXP(searchString)) {
69 throw MakeTypeError("first_argument_not_regexp",
70 ["String.prototype.startsWith"]);
71 }
72
67 var ss = TO_STRING_INLINE(searchString); 73 var ss = TO_STRING_INLINE(searchString);
68 var pos = 0; 74 var pos = 0;
69 if (%_ArgumentsLength() > 1) { 75 if (%_ArgumentsLength() > 1) {
70 pos = %_Arguments(1); // position 76 pos = %_Arguments(1); // position
71 pos = ToInteger(pos); 77 pos = ToInteger(pos);
72 } 78 }
73 79
74 var s_len = s.length; 80 var s_len = s.length;
75 var start = MathMin(MathMax(pos, 0), s_len); 81 var start = MathMin(MathMax(pos, 0), s_len);
76 var ss_len = ss.length; 82 var ss_len = ss.length;
77 if (ss_len + start > s_len) { 83 if (ss_len + start > s_len) {
78 return false; 84 return false;
79 } 85 }
80 86
81 return %StringIndexOf(s, ss, start) === start; 87 return %StringIndexOf(s, ss, start) === start;
82 } 88 }
83 89
84 90
85 // ES6 draft 07-15-13, section 15.5.3.23 91 // ES6 draft 07-15-13, section 15.5.3.23
86 function StringEndsWith(searchString /* position */) { // length == 1 92 function StringEndsWith(searchString /* position */) { // length == 1
87 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 93 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
88 throw MakeTypeError("called_on_null_or_undefined", 94 throw MakeTypeError("called_on_null_or_undefined",
89 ["String.prototype.endsWith"]); 95 ["String.prototype.endsWith"]);
90 } 96 }
91 97
92 var s = TO_STRING_INLINE(this); 98 var s = TO_STRING_INLINE(this);
99
100 if (IS_REGEXP(searchString)) {
101 throw MakeTypeError("first_argument_not_regexp",
102 ["String.prototype.endsWith"]);
103 }
104
93 var ss = TO_STRING_INLINE(searchString); 105 var ss = TO_STRING_INLINE(searchString);
94 var s_len = s.length; 106 var s_len = s.length;
95 var pos = s_len; 107 var pos = s_len;
96 if (%_ArgumentsLength() > 1) { 108 if (%_ArgumentsLength() > 1) {
97 var arg = %_Arguments(1); // position 109 var arg = %_Arguments(1); // position
98 if (!IS_UNDEFINED(arg)) { 110 if (!IS_UNDEFINED(arg)) {
99 pos = ToInteger(arg); 111 pos = ToInteger(arg);
100 } 112 }
101 } 113 }
102 114
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 // Set up the non-enumerable functions on the String prototype object. 157 // Set up the non-enumerable functions on the String prototype object.
146 InstallFunctions($String.prototype, DONT_ENUM, $Array( 158 InstallFunctions($String.prototype, DONT_ENUM, $Array(
147 "repeat", StringRepeat, 159 "repeat", StringRepeat,
148 "startsWith", StringStartsWith, 160 "startsWith", StringStartsWith,
149 "endsWith", StringEndsWith, 161 "endsWith", StringEndsWith,
150 "contains", StringContains 162 "contains", StringContains
151 )); 163 ));
152 } 164 }
153 165
154 ExtendStringPrototype(); 166 ExtendStringPrototype();
OLDNEW
« no previous file with comments | « no previous file | src/messages.js » ('j') | test/mjsunit/harmony/string-endswith.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698