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

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

Issue 1410753002: Do not coerce lastIndex of a global RegExp in @@match and @@replace. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 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 | « no previous file | test/mjsunit/es6/regexp-match-lastindex.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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 (function(global, utils) { 5 (function(global, utils) {
6 6
7 %CheckIsBootstrapping(); 7 %CheckIsBootstrapping();
8 8
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 // Imports 10 // Imports
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 return %StringLocaleCompare(TO_STRING(this), TO_STRING(other)); 144 return %StringLocaleCompare(TO_STRING(this), TO_STRING(other));
145 } 145 }
146 146
147 147
148 // ECMA-262 section 15.5.4.10 148 // ECMA-262 section 15.5.4.10
149 function StringMatchJS(regexp) { 149 function StringMatchJS(regexp) {
150 CHECK_OBJECT_COERCIBLE(this, "String.prototype.match"); 150 CHECK_OBJECT_COERCIBLE(this, "String.prototype.match");
151 151
152 var subject = TO_STRING(this); 152 var subject = TO_STRING(this);
153 if (IS_REGEXP(regexp)) { 153 if (IS_REGEXP(regexp)) {
154 // Emulate RegExp.prototype.exec's side effect in step 5, even though
155 // value is discarded.
156 var lastIndex = TO_INTEGER(regexp.lastIndex);
157 if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0); 154 if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0);
158 var result = %StringMatch(subject, regexp, RegExpLastMatchInfo); 155 var result = %StringMatch(subject, regexp, RegExpLastMatchInfo);
159 if (result !== null) $regexpLastMatchInfoOverride = null; 156 if (result !== null) $regexpLastMatchInfoOverride = null;
160 regexp.lastIndex = 0; 157 regexp.lastIndex = 0;
161 return result; 158 return result;
162 } 159 }
163 // Non-regexp argument. 160 // Non-regexp argument.
164 regexp = new GlobalRegExp(regexp); 161 regexp = new GlobalRegExp(regexp);
165 return RegExpExecNoTests(regexp, subject, 0); 162 return RegExpExecNoTests(regexp, subject, 0);
166 } 163 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 // ........ need to circument last match info override 212 // ........ need to circument last match info override
216 // .... function replace 213 // .... function replace
217 // ...... global search 214 // ...... global search
218 // ...... non-global search 215 // ...... non-global search
219 // .. string search 216 // .. string search
220 // .... special case that replaces with one single character 217 // .... special case that replaces with one single character
221 // ...... function replace 218 // ...... function replace
222 // ...... string replace (with $-expansion) 219 // ...... string replace (with $-expansion)
223 220
224 if (IS_REGEXP(search)) { 221 if (IS_REGEXP(search)) {
225 // Emulate RegExp.prototype.exec's side effect in step 5, even if
226 // value is discarded.
227 var lastIndex = TO_INTEGER(search.lastIndex);
228
229 if (!IS_CALLABLE(replace)) { 222 if (!IS_CALLABLE(replace)) {
230 replace = TO_STRING(replace); 223 replace = TO_STRING(replace);
231 224
232 if (!search.global) { 225 if (!search.global) {
233 // Non-global regexp search, string replace. 226 // Non-global regexp search, string replace.
234 var match = RegExpExec(search, subject, 0); 227 var match = RegExpExec(search, subject, 0);
235 if (match == null) { 228 if (match == null) {
236 search.lastIndex = 0 229 search.lastIndex = 0
237 return subject; 230 return subject;
238 } 231 }
(...skipping 958 matching lines...) Expand 10 before | Expand all | Expand 10 after
1197 to.StringLastIndexOf = StringLastIndexOfJS; 1190 to.StringLastIndexOf = StringLastIndexOfJS;
1198 to.StringMatch = StringMatchJS; 1191 to.StringMatch = StringMatchJS;
1199 to.StringReplace = StringReplace; 1192 to.StringReplace = StringReplace;
1200 to.StringSlice = StringSlice; 1193 to.StringSlice = StringSlice;
1201 to.StringSplit = StringSplitJS; 1194 to.StringSplit = StringSplitJS;
1202 to.StringSubstr = StringSubstr; 1195 to.StringSubstr = StringSubstr;
1203 to.StringSubstring = StringSubstring; 1196 to.StringSubstring = StringSubstring;
1204 }); 1197 });
1205 1198
1206 }) 1199 })
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/es6/regexp-match-lastindex.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698