OLD | NEW |
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_tolength; | 12 var FLAG_harmony_tolength; |
13 var GlobalObject = global.Object; | 13 var GlobalObject = global.Object; |
14 var GlobalRegExp = global.RegExp; | 14 var GlobalRegExp = global.RegExp; |
| 15 var InternalArray = utils.InternalArray; |
15 var InternalPackedArray = utils.InternalPackedArray; | 16 var InternalPackedArray = utils.InternalPackedArray; |
16 var MakeTypeError; | 17 var MakeTypeError; |
| 18 var splitSymbol = utils.ImportNow("split_symbol"); |
17 | 19 |
18 utils.ImportFromExperimental(function(from) { | 20 utils.ImportFromExperimental(function(from) { |
19 FLAG_harmony_tolength = from.FLAG_harmony_tolength; | 21 FLAG_harmony_tolength = from.FLAG_harmony_tolength; |
20 }); | 22 }); |
21 | 23 |
22 utils.Import(function(from) { | 24 utils.Import(function(from) { |
23 MakeTypeError = from.MakeTypeError; | 25 MakeTypeError = from.MakeTypeError; |
24 }); | 26 }); |
25 | 27 |
26 // ------------------------------------------------------------------- | 28 // ------------------------------------------------------------------- |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 var result = '/' + REGEXP_SOURCE(this) + '/'; | 263 var result = '/' + REGEXP_SOURCE(this) + '/'; |
262 if (REGEXP_GLOBAL(this)) result += 'g'; | 264 if (REGEXP_GLOBAL(this)) result += 'g'; |
263 if (REGEXP_IGNORE_CASE(this)) result += 'i'; | 265 if (REGEXP_IGNORE_CASE(this)) result += 'i'; |
264 if (REGEXP_MULTILINE(this)) result += 'm'; | 266 if (REGEXP_MULTILINE(this)) result += 'm'; |
265 if (REGEXP_UNICODE(this)) result += 'u'; | 267 if (REGEXP_UNICODE(this)) result += 'u'; |
266 if (REGEXP_STICKY(this)) result += 'y'; | 268 if (REGEXP_STICKY(this)) result += 'y'; |
267 return result; | 269 return result; |
268 } | 270 } |
269 | 271 |
270 | 272 |
| 273 // ES6 21.2.5.11. |
| 274 function RegExpSplit(string, limit) { |
| 275 // TODO(yangguo): allow non-regexp receivers. |
| 276 if (!IS_REGEXP(this)) { |
| 277 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 278 "RegExp.prototype.@@split", this); |
| 279 } |
| 280 var separator = this; |
| 281 var subject = TO_STRING(string); |
| 282 |
| 283 limit = (IS_UNDEFINED(limit)) ? kMaxUint32 : TO_UINT32(limit); |
| 284 var length = subject.length; |
| 285 |
| 286 if (limit === 0) return []; |
| 287 |
| 288 if (length === 0) { |
| 289 if (DoRegExpExec(separator, subject, 0, 0) !== null) return []; |
| 290 return [subject]; |
| 291 } |
| 292 |
| 293 var currentIndex = 0; |
| 294 var startIndex = 0; |
| 295 var startMatch = 0; |
| 296 var result = new InternalArray(); |
| 297 |
| 298 outer_loop: |
| 299 while (true) { |
| 300 if (startIndex === length) { |
| 301 result[result.length] = %_SubString(subject, currentIndex, length); |
| 302 break; |
| 303 } |
| 304 |
| 305 var matchInfo = DoRegExpExec(separator, subject, startIndex); |
| 306 if (matchInfo === null || length === (startMatch = matchInfo[CAPTURE0])) { |
| 307 result[result.length] = %_SubString(subject, currentIndex, length); |
| 308 break; |
| 309 } |
| 310 var endIndex = matchInfo[CAPTURE1]; |
| 311 |
| 312 // We ignore a zero-length match at the currentIndex. |
| 313 if (startIndex === endIndex && endIndex === currentIndex) { |
| 314 startIndex++; |
| 315 continue; |
| 316 } |
| 317 |
| 318 result[result.length] = %_SubString(subject, currentIndex, startMatch); |
| 319 |
| 320 if (result.length === limit) break; |
| 321 |
| 322 var matchinfo_len = NUMBER_OF_CAPTURES(matchInfo) + REGEXP_FIRST_CAPTURE; |
| 323 for (var i = REGEXP_FIRST_CAPTURE + 2; i < matchinfo_len; ) { |
| 324 var start = matchInfo[i++]; |
| 325 var end = matchInfo[i++]; |
| 326 if (end != -1) { |
| 327 result[result.length] = %_SubString(subject, start, end); |
| 328 } else { |
| 329 result[result.length] = UNDEFINED; |
| 330 } |
| 331 if (result.length === limit) break outer_loop; |
| 332 } |
| 333 |
| 334 startIndex = currentIndex = endIndex; |
| 335 } |
| 336 |
| 337 var array_result = []; |
| 338 %MoveArrayContents(result, array_result); |
| 339 return array_result; |
| 340 } |
| 341 |
| 342 |
271 // Getters for the static properties lastMatch, lastParen, leftContext, and | 343 // Getters for the static properties lastMatch, lastParen, leftContext, and |
272 // rightContext of the RegExp constructor. The properties are computed based | 344 // rightContext of the RegExp constructor. The properties are computed based |
273 // on the captures array of the last successful match and the subject string | 345 // on the captures array of the last successful match and the subject string |
274 // of the last successful match. | 346 // of the last successful match. |
275 function RegExpGetLastMatch() { | 347 function RegExpGetLastMatch() { |
276 var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo); | 348 var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo); |
277 return %_SubString(regExpSubject, | 349 return %_SubString(regExpSubject, |
278 RegExpLastMatchInfo[CAPTURE0], | 350 RegExpLastMatchInfo[CAPTURE0], |
279 RegExpLastMatchInfo[CAPTURE1]); | 351 RegExpLastMatchInfo[CAPTURE1]); |
280 } | 352 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 %FunctionSetInstanceClassName(GlobalRegExp, 'RegExp'); | 449 %FunctionSetInstanceClassName(GlobalRegExp, 'RegExp'); |
378 %FunctionSetPrototype(GlobalRegExp, new GlobalObject()); | 450 %FunctionSetPrototype(GlobalRegExp, new GlobalObject()); |
379 %AddNamedProperty( | 451 %AddNamedProperty( |
380 GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM); | 452 GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM); |
381 %SetCode(GlobalRegExp, RegExpConstructor); | 453 %SetCode(GlobalRegExp, RegExpConstructor); |
382 | 454 |
383 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ | 455 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ |
384 "exec", RegExpExecJS, | 456 "exec", RegExpExecJS, |
385 "test", RegExpTest, | 457 "test", RegExpTest, |
386 "toString", RegExpToString, | 458 "toString", RegExpToString, |
387 "compile", RegExpCompileJS | 459 "compile", RegExpCompileJS, |
| 460 splitSymbol, RegExpSplit, |
388 ]); | 461 ]); |
389 | 462 |
390 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "global", | 463 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "global", |
391 RegExpGetGlobal, DONT_ENUM); | 464 RegExpGetGlobal, DONT_ENUM); |
392 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "ignoreCase", | 465 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "ignoreCase", |
393 RegExpGetIgnoreCase, DONT_ENUM); | 466 RegExpGetIgnoreCase, DONT_ENUM); |
394 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "multiline", | 467 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "multiline", |
395 RegExpGetMultiline, DONT_ENUM); | 468 RegExpGetMultiline, DONT_ENUM); |
396 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "source", | 469 %DefineGetterPropertyUnchecked(GlobalRegExp.prototype, "source", |
397 RegExpGetSource, DONT_ENUM); | 470 RegExpGetSource, DONT_ENUM); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 // Exports | 523 // Exports |
451 | 524 |
452 utils.Export(function(to) { | 525 utils.Export(function(to) { |
453 to.RegExpExec = DoRegExpExec; | 526 to.RegExpExec = DoRegExpExec; |
454 to.RegExpExecNoTests = RegExpExecNoTests; | 527 to.RegExpExecNoTests = RegExpExecNoTests; |
455 to.RegExpLastMatchInfo = RegExpLastMatchInfo; | 528 to.RegExpLastMatchInfo = RegExpLastMatchInfo; |
456 to.RegExpTest = RegExpTest; | 529 to.RegExpTest = RegExpTest; |
457 }); | 530 }); |
458 | 531 |
459 }) | 532 }) |
OLD | NEW |