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

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

Issue 1427573005: [es6] Implement @@split subclassing. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: added test for name Created 5 years, 1 month 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/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 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
11 11
12 var FLAG_harmony_regexps; 12 var FLAG_harmony_regexps;
13 var FLAG_harmony_tolength; 13 var FLAG_harmony_tolength;
14 var FLAG_harmony_unicode_regexps; 14 var FLAG_harmony_unicode_regexps;
15 var GlobalObject = global.Object; 15 var GlobalObject = global.Object;
16 var GlobalRegExp = global.RegExp; 16 var GlobalRegExp = global.RegExp;
17 var InternalArray = utils.InternalArray;
17 var InternalPackedArray = utils.InternalPackedArray; 18 var InternalPackedArray = utils.InternalPackedArray;
18 var MakeTypeError; 19 var MakeTypeError;
20 var splitSymbol = utils.ImportNow("split_symbol");
19 21
20 utils.ImportFromExperimental(function(from) { 22 utils.ImportFromExperimental(function(from) {
21 FLAG_harmony_regexps = from.FLAG_harmony_regexps; 23 FLAG_harmony_regexps = from.FLAG_harmony_regexps;
22 FLAG_harmony_tolength = from.FLAG_harmony_tolength; 24 FLAG_harmony_tolength = from.FLAG_harmony_tolength;
23 FLAG_harmony_unicode_regexps = from.FLAG_harmony_unicode_regexps; 25 FLAG_harmony_unicode_regexps = from.FLAG_harmony_unicode_regexps;
24 }); 26 });
25 27
26 utils.Import(function(from) { 28 utils.Import(function(from) {
27 MakeTypeError = from.MakeTypeError; 29 MakeTypeError = from.MakeTypeError;
28 }); 30 });
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 var result = '/' + this.source + '/'; 269 var result = '/' + this.source + '/';
268 if (this.global) result += 'g'; 270 if (this.global) result += 'g';
269 if (this.ignoreCase) result += 'i'; 271 if (this.ignoreCase) result += 'i';
270 if (this.multiline) result += 'm'; 272 if (this.multiline) result += 'm';
271 if (FLAG_harmony_unicode_regexps && this.unicode) result += 'u'; 273 if (FLAG_harmony_unicode_regexps && this.unicode) result += 'u';
272 if (FLAG_harmony_regexps && this.sticky) result += 'y'; 274 if (FLAG_harmony_regexps && this.sticky) result += 'y';
273 return result; 275 return result;
274 } 276 }
275 277
276 278
279 // ES6 21.2.5.11.
280 function RegExpSplit(string, limit) {
281 // TODO(yangguo): allow non-regexp receivers.
282 if (!IS_REGEXP(this)) {
283 throw MakeTypeError(kIncompatibleMethodReceiver,
284 "RegExp.prototype.@@split", this);
285 }
286 var separator = this;
287 var subject = TO_STRING(string);
288
289 limit = (IS_UNDEFINED(limit)) ? 0xffffffff : TO_UINT32(limit);
Dan Ehrenberg 2015/11/05 21:18:48 I added a kUint32Max to macros.py; maybe it could
290 var length = subject.length;
291
292 if (limit === 0) return [];
293
294 if (length === 0) {
295 if (DoRegExpExec(separator, subject, 0, 0) !== null) return [];
296 return [subject];
297 }
298
299 var currentIndex = 0;
300 var startIndex = 0;
301 var startMatch = 0;
302 var result = new InternalArray();
303
304 outer_loop:
305 while (true) {
306 if (startIndex === length) {
307 result[result.length] = %_SubString(subject, currentIndex, length);
308 break;
309 }
310
311 var matchInfo = DoRegExpExec(separator, subject, startIndex);
312 if (matchInfo === null || length === (startMatch = matchInfo[CAPTURE0])) {
313 result[result.length] = %_SubString(subject, currentIndex, length);
314 break;
315 }
316 var endIndex = matchInfo[CAPTURE1];
317
318 // We ignore a zero-length match at the currentIndex.
319 if (startIndex === endIndex && endIndex === currentIndex) {
320 startIndex++;
321 continue;
322 }
323
324 result[result.length] = %_SubString(subject, currentIndex, startMatch);
325
326 if (result.length === limit) break;
327
328 var matchinfo_len = NUMBER_OF_CAPTURES(matchInfo) + REGEXP_FIRST_CAPTURE;
329 for (var i = REGEXP_FIRST_CAPTURE + 2; i < matchinfo_len; ) {
330 var start = matchInfo[i++];
331 var end = matchInfo[i++];
332 if (end != -1) {
333 result[result.length] = %_SubString(subject, start, end);
334 } else {
335 result[result.length] = UNDEFINED;
336 }
337 if (result.length === limit) break outer_loop;
338 }
339
340 startIndex = currentIndex = endIndex;
341 }
342
343 var array_result = [];
344 %MoveArrayContents(result, array_result);
345 return array_result;
346 }
347
348
277 // Getters for the static properties lastMatch, lastParen, leftContext, and 349 // Getters for the static properties lastMatch, lastParen, leftContext, and
278 // rightContext of the RegExp constructor. The properties are computed based 350 // rightContext of the RegExp constructor. The properties are computed based
279 // on the captures array of the last successful match and the subject string 351 // on the captures array of the last successful match and the subject string
280 // of the last successful match. 352 // of the last successful match.
281 function RegExpGetLastMatch() { 353 function RegExpGetLastMatch() {
282 var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo); 354 var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo);
283 return %_SubString(regExpSubject, 355 return %_SubString(regExpSubject,
284 RegExpLastMatchInfo[CAPTURE0], 356 RegExpLastMatchInfo[CAPTURE0],
285 RegExpLastMatchInfo[CAPTURE1]); 357 RegExpLastMatchInfo[CAPTURE1]);
286 } 358 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 %FunctionSetInstanceClassName(GlobalRegExp, 'RegExp'); 411 %FunctionSetInstanceClassName(GlobalRegExp, 'RegExp');
340 %FunctionSetPrototype(GlobalRegExp, new GlobalObject()); 412 %FunctionSetPrototype(GlobalRegExp, new GlobalObject());
341 %AddNamedProperty( 413 %AddNamedProperty(
342 GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM); 414 GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM);
343 %SetCode(GlobalRegExp, RegExpConstructor); 415 %SetCode(GlobalRegExp, RegExpConstructor);
344 416
345 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ 417 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [
346 "exec", RegExpExecJS, 418 "exec", RegExpExecJS,
347 "test", RegExpTest, 419 "test", RegExpTest,
348 "toString", RegExpToString, 420 "toString", RegExpToString,
349 "compile", RegExpCompileJS 421 "compile", RegExpCompileJS,
422 splitSymbol, RegExpSplit,
350 ]); 423 ]);
351 424
352 // The length of compile is 1 in SpiderMonkey. 425 // The length of compile is 1 in SpiderMonkey.
353 %FunctionSetLength(GlobalRegExp.prototype.compile, 1); 426 %FunctionSetLength(GlobalRegExp.prototype.compile, 1);
354 427
355 // The properties `input` and `$_` are aliases for each other. When this 428 // The properties `input` and `$_` are aliases for each other. When this
356 // value is set the value it is set to is coerced to a string. 429 // value is set the value it is set to is coerced to a string.
357 // Getter and setter for the input. 430 // Getter and setter for the input.
358 var RegExpGetInput = function() { 431 var RegExpGetInput = function() {
359 var regExpInput = LAST_INPUT(RegExpLastMatchInfo); 432 var regExpInput = LAST_INPUT(RegExpLastMatchInfo);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 // Exports 495 // Exports
423 496
424 utils.Export(function(to) { 497 utils.Export(function(to) {
425 to.RegExpExec = DoRegExpExec; 498 to.RegExpExec = DoRegExpExec;
426 to.RegExpExecNoTests = RegExpExecNoTests; 499 to.RegExpExecNoTests = RegExpExecNoTests;
427 to.RegExpLastMatchInfo = RegExpLastMatchInfo; 500 to.RegExpLastMatchInfo = RegExpLastMatchInfo;
428 to.RegExpTest = RegExpTest; 501 to.RegExpTest = RegExpTest;
429 }); 502 });
430 503
431 }) 504 })
OLDNEW
« no previous file with comments | « no previous file | src/js/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698