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 'use strict'; | 7 'use strict'; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var AddIndexedProperty; | |
14 var ExpandReplacement; | 15 var ExpandReplacement; |
16 var GlobalArray = global.Array; | |
15 var GlobalObject = global.Object; | 17 var GlobalObject = global.Object; |
16 var GlobalRegExp = global.RegExp; | 18 var GlobalRegExp = global.RegExp; |
17 var GlobalRegExpPrototype; | 19 var GlobalRegExpPrototype; |
18 var InternalArray = utils.InternalArray; | 20 var InternalArray = utils.InternalArray; |
19 var InternalPackedArray = utils.InternalPackedArray; | 21 var InternalPackedArray = utils.InternalPackedArray; |
20 var MakeTypeError; | 22 var MakeTypeError; |
23 var MaxSimple; | |
24 var MinSimple; | |
21 var matchSymbol = utils.ImportNow("match_symbol"); | 25 var matchSymbol = utils.ImportNow("match_symbol"); |
22 var replaceSymbol = utils.ImportNow("replace_symbol"); | 26 var replaceSymbol = utils.ImportNow("replace_symbol"); |
23 var searchSymbol = utils.ImportNow("search_symbol"); | 27 var searchSymbol = utils.ImportNow("search_symbol"); |
24 var splitSymbol = utils.ImportNow("split_symbol"); | 28 var splitSymbol = utils.ImportNow("split_symbol"); |
29 var SpeciesConstructor; | |
25 | 30 |
26 utils.Import(function(from) { | 31 utils.Import(function(from) { |
32 AddIndexedProperty = from.AddIndexedProperty; | |
27 ExpandReplacement = from.ExpandReplacement; | 33 ExpandReplacement = from.ExpandReplacement; |
28 MakeTypeError = from.MakeTypeError; | 34 MakeTypeError = from.MakeTypeError; |
35 MaxSimple = from.MaxSimple; | |
36 MinSimple = from.MinSimple; | |
37 SpeciesConstructor = from.SpeciesConstructor; | |
29 }); | 38 }); |
30 | 39 |
31 // ------------------------------------------------------------------- | 40 // ------------------------------------------------------------------- |
32 | 41 |
33 // Property of the builtins object for recording the result of the last | 42 // Property of the builtins object for recording the result of the last |
34 // regexp match. The property RegExpLastMatchInfo includes the matchIndices | 43 // regexp match. The property RegExpLastMatchInfo includes the matchIndices |
35 // array of the last successful regexp match (an array of start/end index | 44 // array of the last successful regexp match (an array of start/end index |
36 // pairs for the match and all the captured substrings), the invariant is | 45 // pairs for the match and all the captured substrings), the invariant is |
37 // that there are at least two capture indeces. The array also contains | 46 // that there are at least two capture indeces. The array also contains |
38 // the subject string for the last successful match. | 47 // the subject string for the last successful match. |
39 var RegExpLastMatchInfo = new InternalPackedArray( | 48 var RegExpLastMatchInfo = new InternalPackedArray( |
40 2, // REGEXP_NUMBER_OF_CAPTURES | 49 2, // REGEXP_NUMBER_OF_CAPTURES |
41 "", // Last subject. | 50 "", // Last subject. |
42 UNDEFINED, // Last input - settable with RegExpSetInput. | 51 UNDEFINED, // Last input - settable with RegExpSetInput. |
43 0, // REGEXP_FIRST_CAPTURE + 0 | 52 0, // REGEXP_FIRST_CAPTURE + 0 |
44 0 // REGEXP_FIRST_CAPTURE + 1 | 53 0 // REGEXP_FIRST_CAPTURE + 1 |
45 ); | 54 ); |
46 | 55 |
47 // ------------------------------------------------------------------- | 56 // ------------------------------------------------------------------- |
48 | 57 |
58 // ES#sec-isregexp IsRegExp ( argument ) | |
49 function IsRegExp(o) { | 59 function IsRegExp(o) { |
50 if (!IS_RECEIVER(o)) return false; | 60 if (!IS_RECEIVER(o)) return false; |
51 var is_regexp = o[matchSymbol]; | 61 var is_regexp = o[matchSymbol]; |
52 if (!IS_UNDEFINED(is_regexp)) return TO_BOOLEAN(is_regexp); | 62 if (!IS_UNDEFINED(is_regexp)) return TO_BOOLEAN(is_regexp); |
53 return IS_REGEXP(o); | 63 return IS_REGEXP(o); |
54 } | 64 } |
55 | 65 |
56 | 66 |
57 // ES6 section 21.2.3.2.2 | 67 // ES#sec-regexpinitialize |
68 // Runtime Semantics: RegExpInitialize ( obj, pattern, flags ) | |
58 function RegExpInitialize(object, pattern, flags) { | 69 function RegExpInitialize(object, pattern, flags) { |
59 pattern = IS_UNDEFINED(pattern) ? '' : TO_STRING(pattern); | 70 pattern = IS_UNDEFINED(pattern) ? '' : TO_STRING(pattern); |
60 flags = IS_UNDEFINED(flags) ? '' : TO_STRING(flags); | 71 flags = IS_UNDEFINED(flags) ? '' : TO_STRING(flags); |
61 %RegExpInitializeAndCompile(object, pattern, flags); | 72 %RegExpInitializeAndCompile(object, pattern, flags); |
62 return object; | 73 return object; |
63 } | 74 } |
64 | 75 |
65 | 76 |
66 function PatternFlags(pattern) { | 77 function PatternFlags(pattern) { |
67 return (REGEXP_GLOBAL(pattern) ? 'g' : '') + | 78 return (REGEXP_GLOBAL(pattern) ? 'g' : '') + |
68 (REGEXP_IGNORE_CASE(pattern) ? 'i' : '') + | 79 (REGEXP_IGNORE_CASE(pattern) ? 'i' : '') + |
69 (REGEXP_MULTILINE(pattern) ? 'm' : '') + | 80 (REGEXP_MULTILINE(pattern) ? 'm' : '') + |
70 (REGEXP_UNICODE(pattern) ? 'u' : '') + | 81 (REGEXP_UNICODE(pattern) ? 'u' : '') + |
71 (REGEXP_STICKY(pattern) ? 'y' : ''); | 82 (REGEXP_STICKY(pattern) ? 'y' : ''); |
72 } | 83 } |
73 | 84 |
74 | 85 |
86 // ES#sec-regexp-pattern-flags | |
87 // RegExp ( pattern, flags ) | |
75 function RegExpConstructor(pattern, flags) { | 88 function RegExpConstructor(pattern, flags) { |
76 var newtarget = new.target; | 89 var newtarget = new.target; |
77 var pattern_is_regexp = IsRegExp(pattern); | 90 var pattern_is_regexp = IsRegExp(pattern); |
78 | 91 |
79 if (IS_UNDEFINED(newtarget)) { | 92 if (IS_UNDEFINED(newtarget)) { |
80 newtarget = GlobalRegExp; | 93 newtarget = GlobalRegExp; |
81 | 94 |
82 // ES6 section 21.2.3.1 step 3.b | 95 // ES6 section 21.2.3.1 step 3.b |
83 if (pattern_is_regexp && IS_UNDEFINED(flags) && | 96 if (pattern_is_regexp && IS_UNDEFINED(flags) && |
84 pattern.constructor === newtarget) { | 97 pattern.constructor === newtarget) { |
85 return pattern; | 98 return pattern; |
86 } | 99 } |
87 } | 100 } |
88 | 101 |
89 if (IS_REGEXP(pattern)) { | 102 if (IS_REGEXP(pattern)) { |
90 if (IS_UNDEFINED(flags)) flags = PatternFlags(pattern); | 103 if (IS_UNDEFINED(flags)) flags = PatternFlags(pattern); |
91 pattern = REGEXP_SOURCE(pattern); | 104 pattern = REGEXP_SOURCE(pattern); |
92 | 105 |
93 } else if (pattern_is_regexp) { | 106 } else if (pattern_is_regexp) { |
94 var input_pattern = pattern; | 107 var input_pattern = pattern; |
95 pattern = pattern.source; | 108 pattern = pattern.source; |
96 if (IS_UNDEFINED(flags)) flags = input_pattern.flags; | 109 if (IS_UNDEFINED(flags)) flags = input_pattern.flags; |
97 } | 110 } |
98 | 111 |
99 var object = %NewObject(GlobalRegExp, newtarget); | 112 var object = %NewObject(GlobalRegExp, newtarget); |
100 return RegExpInitialize(object, pattern, flags); | 113 return RegExpInitialize(object, pattern, flags); |
101 } | 114 } |
102 | 115 |
103 | 116 |
117 // ES#sec-regexp.prototype.compile RegExp.prototype.compile (pattern, flags) | |
104 function RegExpCompileJS(pattern, flags) { | 118 function RegExpCompileJS(pattern, flags) { |
105 if (!IS_REGEXP(this)) { | 119 if (!IS_REGEXP(this)) { |
106 throw MakeTypeError(kIncompatibleMethodReceiver, | 120 throw MakeTypeError(kIncompatibleMethodReceiver, |
107 "RegExp.prototype.compile", this); | 121 "RegExp.prototype.compile", this); |
108 } | 122 } |
109 | 123 |
110 if (IS_REGEXP(pattern)) { | 124 if (IS_REGEXP(pattern)) { |
111 if (!IS_UNDEFINED(flags)) throw MakeTypeError(kRegExpFlags); | 125 if (!IS_UNDEFINED(flags)) throw MakeTypeError(kRegExpFlags); |
112 | 126 |
113 flags = PatternFlags(pattern); | 127 flags = PatternFlags(pattern); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 if (matchInfo !== null) { | 172 if (matchInfo !== null) { |
159 // ES6 21.2.5.2.2 step 18. | 173 // ES6 21.2.5.2.2 step 18. |
160 if (REGEXP_STICKY(regexp)) regexp.lastIndex = matchInfo[CAPTURE1]; | 174 if (REGEXP_STICKY(regexp)) regexp.lastIndex = matchInfo[CAPTURE1]; |
161 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string); | 175 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string); |
162 } | 176 } |
163 regexp.lastIndex = 0; | 177 regexp.lastIndex = 0; |
164 return null; | 178 return null; |
165 } | 179 } |
166 | 180 |
167 | 181 |
182 // ES#sec-regexp.prototype.exec | |
183 // RegExp.prototype.exec ( string ) | |
184 function RegExpSubclassExecJS(string) { | |
185 if (!IS_REGEXP(this)) { | |
186 throw MakeTypeError(kIncompatibleMethodReceiver, | |
187 'RegExp.prototype.exec', this); | |
188 } | |
189 | |
190 string = TO_STRING(string); | |
191 var lastIndex = this.lastIndex; | |
192 | |
193 // Conversion is required by the ES2015 specification (RegExpBuiltinExec | |
194 // algorithm, step 4) even if the value is discarded for non-global RegExps. | |
195 var i = TO_LENGTH(lastIndex); | |
196 | |
197 var global = TO_BOOLEAN(this.global); | |
adamk
2016/03/22 22:23:42
Are these the only calls that differ from the exis
Dan Ehrenberg
2016/03/22 23:09:31
Yes. They showed up as relatively big items in the
| |
198 var sticky = TO_BOOLEAN(this.sticky); | |
199 var updateLastIndex = global || sticky; | |
200 if (updateLastIndex) { | |
201 if (i < 0 || i > string.length) { | |
adamk
2016/03/22 22:23:42
I see where the > length is checked in the spec, b
Dan Ehrenberg
2016/03/22 23:09:32
My mistake, the < 0 path should be unreachable now
| |
202 this.lastIndex = 0; | |
203 return null; | |
204 } | |
205 } else { | |
206 i = 0; | |
207 } | |
208 | |
209 // matchIndices is either null or the RegExpLastMatchInfo array. | |
210 // TODO(littledan): Whether a RegExp is sticky is compiled into the RegExp | |
211 // itself, but ES2015 allows monkey-patching this property to differ from | |
212 // the internal flags. If it differs, recompile a different RegExp? | |
213 var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); | |
214 | |
215 if (IS_NULL(matchIndices)) { | |
216 this.lastIndex = 0; | |
217 return null; | |
218 } | |
219 | |
220 // Successful match. | |
221 if (updateLastIndex) { | |
222 this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; | |
223 } | |
224 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); | |
225 } | |
226 %FunctionRemovePrototype(RegExpSubclassExecJS); | |
227 | |
228 | |
229 // Legacy implementation of RegExp.prototype.exec | |
168 function RegExpExecJS(string) { | 230 function RegExpExecJS(string) { |
169 if (!IS_REGEXP(this)) { | 231 if (!IS_REGEXP(this)) { |
170 throw MakeTypeError(kIncompatibleMethodReceiver, | 232 throw MakeTypeError(kIncompatibleMethodReceiver, |
171 'RegExp.prototype.exec', this); | 233 'RegExp.prototype.exec', this); |
172 } | 234 } |
173 | 235 |
174 string = TO_STRING(string); | 236 string = TO_STRING(string); |
175 var lastIndex = this.lastIndex; | 237 var lastIndex = this.lastIndex; |
176 | 238 |
177 // Conversion is required by the ES2015 specification (RegExpBuiltinExec | 239 // Conversion is required by the ES2015 specification (RegExpBuiltinExec |
(...skipping 19 matching lines...) Expand all Loading... | |
197 } | 259 } |
198 | 260 |
199 // Successful match. | 261 // Successful match. |
200 if (updateLastIndex) { | 262 if (updateLastIndex) { |
201 this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; | 263 this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; |
202 } | 264 } |
203 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); | 265 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); |
204 } | 266 } |
205 | 267 |
206 | 268 |
269 // ES#sec-regexpexec Runtime Semantics: RegExpExec ( R, S ) | |
270 function RegExpSubclassExec(regexp, string) { | |
271 var exec = regexp.exec; | |
272 if (IS_CALLABLE(exec)) { | |
273 var result = %_Call(exec, regexp, string); | |
274 if (!IS_OBJECT(result) && !IS_NULL(result)) { | |
275 throw MakeTypeError(kInvalidRegExpExecResult); | |
276 } | |
277 return result; | |
278 } | |
279 return %_Call(RegExpExecJS, regexp, string); | |
280 } | |
281 | |
282 | |
207 // One-element cache for the simplified test regexp. | 283 // One-element cache for the simplified test regexp. |
208 var regexp_key; | 284 var regexp_key; |
209 var regexp_val; | 285 var regexp_val; |
210 | 286 |
287 // Legacy implementation of RegExp.prototype.test | |
211 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be | 288 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be |
212 // that test is defined in terms of String.prototype.exec. However, it probably | 289 // that test is defined in terms of String.prototype.exec. However, it probably |
213 // means the original value of String.prototype.exec, which is what everybody | 290 // means the original value of String.prototype.exec, which is what everybody |
214 // else implements. | 291 // else implements. |
215 function RegExpTest(string) { | 292 function RegExpTest(string) { |
216 if (!IS_REGEXP(this)) { | 293 if (!IS_REGEXP(this)) { |
217 throw MakeTypeError(kIncompatibleMethodReceiver, | 294 throw MakeTypeError(kIncompatibleMethodReceiver, |
218 'RegExp.prototype.test', this); | 295 'RegExp.prototype.test', this); |
219 } | 296 } |
220 string = TO_STRING(string); | 297 string = TO_STRING(string); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 // matchIndices is either null or the RegExpLastMatchInfo array. | 331 // matchIndices is either null or the RegExpLastMatchInfo array. |
255 var matchIndices = %_RegExpExec(regexp, string, 0, RegExpLastMatchInfo); | 332 var matchIndices = %_RegExpExec(regexp, string, 0, RegExpLastMatchInfo); |
256 if (IS_NULL(matchIndices)) { | 333 if (IS_NULL(matchIndices)) { |
257 this.lastIndex = 0; | 334 this.lastIndex = 0; |
258 return false; | 335 return false; |
259 } | 336 } |
260 return true; | 337 return true; |
261 } | 338 } |
262 } | 339 } |
263 | 340 |
341 | |
342 // ES#sec-regexp.prototype.test RegExp.prototype.test ( S ) | |
343 function RegExpSubclassTest(string) { | |
344 if (!IS_OBJECT(this)) { | |
345 throw MakeTypeError(kIncompatibleMethodReceiver, | |
346 'RegExp.prototype.test', this); | |
347 } | |
348 string = TO_STRING(string); | |
349 var match = RegExpSubclassExec(this, string); | |
350 return !IS_NULL(match); | |
351 } | |
352 %FunctionRemovePrototype(RegExpSubclassTest); | |
353 | |
264 function TrimRegExp(regexp) { | 354 function TrimRegExp(regexp) { |
265 if (regexp_key !== regexp) { | 355 if (regexp_key !== regexp) { |
266 regexp_key = regexp; | 356 regexp_key = regexp; |
267 regexp_val = | 357 regexp_val = |
268 new GlobalRegExp( | 358 new GlobalRegExp( |
269 %_SubString(REGEXP_SOURCE(regexp), 2, REGEXP_SOURCE(regexp).length), | 359 %_SubString(REGEXP_SOURCE(regexp), 2, REGEXP_SOURCE(regexp).length), |
270 (REGEXP_IGNORE_CASE(regexp) ? REGEXP_MULTILINE(regexp) ? "im" : "i" | 360 (REGEXP_IGNORE_CASE(regexp) ? REGEXP_MULTILINE(regexp) ? "im" : "i" |
271 : REGEXP_MULTILINE(regexp) ? "m" : "")); | 361 : REGEXP_MULTILINE(regexp) ? "m" : "")); |
272 } | 362 } |
273 return regexp_val; | 363 return regexp_val; |
(...skipping 27 matching lines...) Expand all Loading... | |
301 | 391 |
302 function AtSurrogatePair(subject, index) { | 392 function AtSurrogatePair(subject, index) { |
303 if (index + 1 >= subject.length) return false; | 393 if (index + 1 >= subject.length) return false; |
304 var first = %_StringCharCodeAt(subject, index); | 394 var first = %_StringCharCodeAt(subject, index); |
305 if (first < 0xD800 || first > 0xDBFF) return false; | 395 if (first < 0xD800 || first > 0xDBFF) return false; |
306 var second = %_StringCharCodeAt(subject, index + 1); | 396 var second = %_StringCharCodeAt(subject, index + 1); |
307 return second >= 0xDC00 || second <= 0xDFFF; | 397 return second >= 0xDC00 || second <= 0xDFFF; |
308 } | 398 } |
309 | 399 |
310 | 400 |
311 // ES6 21.2.5.11. | 401 // Legacy implementation of RegExp.prototype[Symbol.split] which |
402 // doesn't properly call the underlying exec, @@species methods | |
312 function RegExpSplit(string, limit) { | 403 function RegExpSplit(string, limit) { |
313 // TODO(yangguo): allow non-regexp receivers. | 404 // TODO(yangguo): allow non-regexp receivers. |
314 if (!IS_REGEXP(this)) { | 405 if (!IS_REGEXP(this)) { |
315 throw MakeTypeError(kIncompatibleMethodReceiver, | 406 throw MakeTypeError(kIncompatibleMethodReceiver, |
316 "RegExp.prototype.@@split", this); | 407 "RegExp.prototype.@@split", this); |
317 } | 408 } |
318 var separator = this; | 409 var separator = this; |
319 var subject = TO_STRING(string); | 410 var subject = TO_STRING(string); |
320 | 411 |
321 limit = (IS_UNDEFINED(limit)) ? kMaxUint32 : TO_UINT32(limit); | 412 limit = (IS_UNDEFINED(limit)) ? kMaxUint32 : TO_UINT32(limit); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
375 | 466 |
376 startIndex = currentIndex = endIndex; | 467 startIndex = currentIndex = endIndex; |
377 } | 468 } |
378 | 469 |
379 var array_result = []; | 470 var array_result = []; |
380 %MoveArrayContents(result, array_result); | 471 %MoveArrayContents(result, array_result); |
381 return array_result; | 472 return array_result; |
382 } | 473 } |
383 | 474 |
384 | 475 |
385 // ES6 21.2.5.6. | 476 // ES#sec-regexp.prototype-@@split |
477 // RegExp.prototype [ @@split ] ( string, limit ) | |
478 function RegExpSubclassSplit(string, limit) { | |
479 if (!IS_RECEIVER(this)) { | |
480 throw MakeTypeError(kIncompatibleMethodReceiver, | |
481 "RegExp.prototype.@@split", this); | |
482 } | |
483 string = TO_STRING(string); | |
484 var constructor = SpeciesConstructor(this, GlobalRegExp); | |
485 var flags = TO_STRING(this.flags); | |
486 var unicode = %StringIndexOf(flags, 'u', 0) >= 0; | |
487 var sticky = %StringIndexOf(flags, 'y', 0) >= 0; | |
488 var new_flags = sticky ? flags : flags + "y"; | |
adamk
2016/03/23 01:07:19
I'd prefer if new code added in this patch used ca
Dan Ehrenberg
2016/03/24 00:50:54
fixed
| |
489 var splitter = new constructor(this, new_flags); | |
490 var array = new GlobalArray(); | |
adamk
2016/03/22 22:23:42
It seems like the main reason to use an Array here
Dan Ehrenberg
2016/03/22 23:09:31
Good idea for optimization; I was just blindly fol
adamk
2016/03/23 01:07:19
Given that we already know there's lots of optimiz
| |
491 var array_index = 0; | |
492 var lim = (IS_UNDEFINED(limit)) ? kMaxUint32 : TO_UINT32(limit); | |
493 var size = string.length; | |
494 var prev_string_index = 0; | |
495 if (lim === 0) return array; | |
496 var result; | |
497 if (size === 0) { | |
498 result = RegExpSubclassExec(splitter, string); | |
499 if (IS_NULL(result)) AddIndexedProperty(array, 0, string); | |
500 return array; | |
501 } | |
502 var string_index = prev_string_index; | |
503 while (string_index < size) { | |
504 splitter.lastIndex = string_index; | |
505 result = RegExpSubclassExec(splitter, string); | |
506 if (IS_NULL(result)) { | |
507 string_index += GetUnicodeAdvancedIncrement(string, string_index, | |
508 unicode); | |
509 } else { | |
510 var end = MinSimple(splitter.lastIndex, size); | |
adamk
2016/03/22 22:23:42
Missing TO_LENGTH? Hard to tell it's required, but
Dan Ehrenberg
2016/03/22 23:09:31
Oops, definitely needed around splitter.lastIndex.
| |
511 if (end === prev_string_index) { | |
512 string_index += GetUnicodeAdvancedIncrement(string, string_index, | |
513 unicode); | |
514 } else { | |
515 AddIndexedProperty( | |
516 array, array_index, | |
517 %_SubString(string, prev_string_index, string_index)); | |
518 array_index++; | |
519 if (array_index === lim) return array; | |
520 prev_string_index = end; | |
521 var number_of_captures = MaxSimple(TO_LENGTH(result.length), 0); | |
522 for (var i = 1; i < number_of_captures; i++) { | |
523 AddIndexedProperty(array, array_index, result[i]); | |
524 array_index++; | |
525 if (array_index === lim) return array; | |
526 } | |
527 string_index = prev_string_index; | |
528 } | |
529 } | |
530 } | |
531 AddIndexedProperty(array, array_index, | |
532 %_SubString(string, prev_string_index, size)); | |
533 return array; | |
534 } | |
535 %FunctionRemovePrototype(RegExpSubclassSplit); | |
536 | |
537 | |
538 // Legacy implementation of RegExp.prototype[Symbol.match] which | |
539 // doesn't properly call the underlying exec method | |
386 function RegExpMatch(string) { | 540 function RegExpMatch(string) { |
387 // TODO(yangguo): allow non-regexp receivers. | |
388 if (!IS_REGEXP(this)) { | 541 if (!IS_REGEXP(this)) { |
389 throw MakeTypeError(kIncompatibleMethodReceiver, | 542 throw MakeTypeError(kIncompatibleMethodReceiver, |
390 "RegExp.prototype.@@match", this); | 543 "RegExp.prototype.@@match", this); |
391 } | 544 } |
392 var subject = TO_STRING(string); | 545 var subject = TO_STRING(string); |
393 | 546 |
394 if (!REGEXP_GLOBAL(this)) return RegExpExecNoTests(this, subject, 0); | 547 if (!REGEXP_GLOBAL(this)) return RegExpExecNoTests(this, subject, 0); |
395 this.lastIndex = 0; | 548 this.lastIndex = 0; |
396 var result = %StringMatch(subject, this, RegExpLastMatchInfo); | 549 var result = %StringMatch(subject, this, RegExpLastMatchInfo); |
397 return result; | 550 return result; |
398 } | 551 } |
399 | 552 |
400 | 553 |
401 // ES6 21.2.5.8. | 554 // ES#sec-regexp.prototype-@@match |
555 // RegExp.prototype [ @@match ] ( string ) | |
556 function RegExpSubclassMatch(string) { | |
557 if (!IS_OBJECT(this)) { | |
558 throw MakeTypeError(kIncompatibleMethodReceiver, | |
559 "RegExp.prototype.@@match", this); | |
560 } | |
561 string = TO_STRING(string); | |
562 var global = this.global; | |
563 if (!global) return RegExpSubclassExec(this, string); | |
564 var unicode = this.unicode; | |
565 this.lastIndex = 0; | |
566 var array = []; | |
567 var n = 0; | |
568 var result; | |
569 while (true) { | |
570 result = RegExpSubclassExec(this, string); | |
571 if (IS_NULL(result)) { | |
572 if (n === 0) return null; | |
573 return array; | |
574 } | |
575 var matchStr = TO_STRING(result[0]); | |
576 %AddElement(array, n, matchStr); | |
577 if (matchStr === "") AdvanceStringIndex(this, string, unicode); | |
adamk
2016/03/22 22:23:42
I found it quite confusing that what the spec call
Dan Ehrenberg
2016/03/22 23:09:32
Fixed it to have more meaningful names.
| |
578 n++; | |
579 } | |
580 } | |
581 %FunctionRemovePrototype(RegExpSubclassMatch); | |
582 | |
583 | |
584 // Legacy implementation of RegExp.prototype[Symbol.replace] which | |
585 // doesn't properly call the underlying exec method. | |
402 | 586 |
403 // TODO(lrn): This array will survive indefinitely if replace is never | 587 // TODO(lrn): This array will survive indefinitely if replace is never |
404 // called again. However, it will be empty, since the contents are cleared | 588 // called again. However, it will be empty, since the contents are cleared |
405 // in the finally block. | 589 // in the finally block. |
406 var reusableReplaceArray = new InternalArray(4); | 590 var reusableReplaceArray = new InternalArray(4); |
407 | 591 |
408 // Helper function for replacing regular expressions with the result of a | 592 // Helper function for replacing regular expressions with the result of a |
409 // function application in String.prototype.replace. | 593 // function application in String.prototype.replace. |
410 function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) { | 594 function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) { |
411 var resultArray = reusableReplaceArray; | 595 var resultArray = reusableReplaceArray; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 } | 702 } |
519 | 703 |
520 result += replacement; // The add method converts to string if necessary. | 704 result += replacement; // The add method converts to string if necessary. |
521 // Can't use matchInfo any more from here, since the function could | 705 // Can't use matchInfo any more from here, since the function could |
522 // overwrite it. | 706 // overwrite it. |
523 return result + %_SubString(subject, endOfMatch, subject.length); | 707 return result + %_SubString(subject, endOfMatch, subject.length); |
524 } | 708 } |
525 | 709 |
526 | 710 |
527 function RegExpReplace(string, replace) { | 711 function RegExpReplace(string, replace) { |
528 // TODO(littledan): allow non-regexp receivers. | |
529 if (!IS_REGEXP(this)) { | 712 if (!IS_REGEXP(this)) { |
530 throw MakeTypeError(kIncompatibleMethodReceiver, | 713 throw MakeTypeError(kIncompatibleMethodReceiver, |
531 "RegExp.prototype.@@replace", this); | 714 "RegExp.prototype.@@replace", this); |
532 } | 715 } |
533 var subject = TO_STRING(string); | 716 var subject = TO_STRING(string); |
534 var search = this; | 717 var search = this; |
535 | 718 |
536 if (!IS_CALLABLE(replace)) { | 719 if (!IS_CALLABLE(replace)) { |
537 replace = TO_STRING(replace); | 720 replace = TO_STRING(replace); |
538 | 721 |
(...skipping 21 matching lines...) Expand all Loading... | |
560 | 743 |
561 if (REGEXP_GLOBAL(search)) { | 744 if (REGEXP_GLOBAL(search)) { |
562 // Global regexp search, function replace. | 745 // Global regexp search, function replace. |
563 return StringReplaceGlobalRegExpWithFunction(subject, search, replace); | 746 return StringReplaceGlobalRegExpWithFunction(subject, search, replace); |
564 } | 747 } |
565 // Non-global regexp search, function replace. | 748 // Non-global regexp search, function replace. |
566 return StringReplaceNonGlobalRegExpWithFunction(subject, search, replace); | 749 return StringReplaceNonGlobalRegExpWithFunction(subject, search, replace); |
567 } | 750 } |
568 | 751 |
569 | 752 |
570 // ES6 21.2.5.9. | 753 // ES#sec-getsubstitution |
754 // GetSubstitution(matched, str, position, captures, replacement) | |
755 // Expand the $-expressions in the string and return a new string with | |
756 // the result. | |
757 // TODO(littledan): Call this function from String.prototype.replace instead | |
adamk
2016/03/22 22:23:42
I agree :)
This one's really big; what's the diff
Dan Ehrenberg
2016/03/22 23:09:32
They take the captures/replacement in different fo
| |
758 // of the very similar ExpandReplacement in src/js/string.js | |
759 function GetSubstitution(matched, string, position, captures, replacement) { | |
760 var match_length = matched.length; | |
761 var string_length = string.length; | |
762 var captures_length = captures.length; | |
763 var tail_pos = position + match_length; | |
764 var result = ""; | |
765 var pos, expansion, peek, next, scaled_index, advance, new_scaled_index; | |
766 | |
767 var next = %StringIndexOf(replacement, '$', 0); | |
768 if (next < 0) { | |
769 result += replacement; | |
770 return result; | |
771 } | |
772 | |
773 if (next > 0) result += %_SubString(replacement, 0, next); | |
774 | |
775 while (true) { | |
776 expansion = '$'; | |
777 pos = next + 1; | |
778 if (pos < replacement.length) { | |
779 peek = %_StringCharCodeAt(replacement, pos); | |
780 if (peek == 36) { // $$ | |
781 ++pos; | |
782 result += '$'; | |
783 } else if (peek == 38) { // $& - match | |
784 ++pos; | |
785 result += matched; | |
786 } else if (peek == 96) { // $` - prefix | |
787 ++pos; | |
788 result += %_SubString(string, 0, position); | |
789 } else if (peek == 39) { // $' - suffix | |
790 ++pos; | |
791 result += %_SubString(string, tail_pos, string_length); | |
792 } else if (peek >= 48 && peek <= 57) { | |
793 // Valid indices are $1 .. $9, $01 .. $09 and $10 .. $99 | |
794 scaled_index = (peek - 48); | |
795 advance = 1; | |
796 if (pos + 1 < replacement.length) { | |
797 next = %_StringCharCodeAt(replacement, pos + 1); | |
798 if (next >= 48 && next <= 57) { | |
799 new_scaled_index = scaled_index * 10 + ((next - 48)); | |
800 if (new_scaled_index < captures_length) { | |
801 scaled_index = new_scaled_index; | |
802 advance = 2; | |
803 } | |
804 } | |
805 } | |
806 if (scaled_index != 0 && scaled_index < captures_length) { | |
807 var capture = captures[scaled_index]; | |
808 if (!IS_UNDEFINED(capture)) result += capture; | |
809 pos += advance; | |
810 } else { | |
811 result += '$'; | |
812 } | |
813 } else { | |
814 result += '$'; | |
815 } | |
816 } else { | |
817 result += '$'; | |
818 } | |
819 | |
820 // Go the the next $ in the replacement. | |
821 next = %StringIndexOf(replacement, '$', pos); | |
822 | |
823 // Return if there are no more $ characters in the replacement. If we | |
824 // haven't reached the end, we need to append the suffix. | |
825 if (next < 0) { | |
826 if (pos < replacement.length) { | |
827 result += %_SubString(replacement, pos, replacement.length); | |
828 } | |
829 return result; | |
830 } | |
831 | |
832 // Append substring between the previous and the next $ character. | |
833 if (next > pos) { | |
834 result += %_SubString(replacement, pos, next); | |
835 } | |
836 } | |
837 return result; | |
838 } | |
839 | |
840 | |
841 function GetUnicodeAdvancedIncrement(string, index, unicode) { | |
842 var increment = 1; | |
843 if (unicode) { | |
844 var first = %_StringCharCodeAt(string, index); | |
845 if (first >= 0xD800 && first <= 0xDBFF && string.length > index + 1) { | |
846 var second = %_StringCharCodeAt(string, index + 1); | |
847 if (second >= 0xDC00 && second <= 0xDFFF) { | |
848 increment = 2; | |
849 } | |
850 } | |
851 } | |
852 return increment; | |
853 } | |
854 | |
855 | |
856 // ES#sec-advancestringindex | |
857 // AdvanceStringIndex ( S, index, unicode ) | |
858 function AdvanceStringIndex(regexp, string, unicode) { | |
859 var last_index = regexp.lastIndex; | |
860 regexp.lastIndex = last_index + | |
861 GetUnicodeAdvancedIncrement(string, last_index, unicode); | |
862 } | |
863 | |
864 | |
865 // ES#sec-regexp.prototype-@@replace | |
866 // RegExp.prototype [ @@replace ] ( string, replaceValue ) | |
867 function RegExpSubclassReplace(string, replace) { | |
868 if (!IS_OBJECT(this)) { | |
869 throw MakeTypeError(kIncompatibleMethodReceiver, | |
870 "RegExp.prototype.@@replace", this); | |
871 } | |
872 string = TO_STRING(string); | |
873 var length = string.length; | |
874 var functional_replace = IS_CALLABLE(replace); | |
875 if (!functional_replace) replace = TO_STRING(replace); | |
876 var global = this.global; | |
877 if (global) { | |
878 var unicode = this.unicode; | |
879 this.lastIndex = 0; | |
880 } | |
881 var results = new InternalArray(); | |
882 var result, replacement; | |
883 while (true) { | |
884 result = RegExpSubclassExec(this, string); | |
885 if (IS_NULL(result)) { | |
886 break; | |
887 } else { | |
888 results.push(result); | |
889 if (!global) break; | |
890 var match_str = TO_STRING(result[0]); | |
891 if (match_str === "") AdvanceStringIndex(this, string, unicode); | |
892 } | |
893 } | |
894 var accumulated_result = ""; | |
895 var next_source_position = 0; | |
896 for (var i = 0; i < results.length; i++) { | |
897 result = results[i]; | |
898 var captures_length = MaxSimple(TO_LENGTH(result.length), 0); | |
899 var matched = TO_STRING(result[0]); | |
900 var matched_length = matched.length; | |
901 var position = MaxSimple(MinSimple(TO_INTEGER(result.index), length), 0); | |
902 var captures = new InternalArray(); | |
903 for (var n = 0; n < captures_length; n++) { | |
904 var capture = result[n]; | |
905 if (!IS_UNDEFINED(capture)) capture = TO_STRING(capture); | |
906 captures[n] = capture; | |
907 } | |
908 if (functional_replace) { | |
909 var parameters = new InternalArray(captures_length + 2); | |
910 for (var j = 0; j < captures_length; j++) { | |
911 parameters[j] = captures[j]; | |
912 } | |
913 parameters[j] = position; | |
914 parameters[j + 1] = string; | |
915 replacement = %reflect_apply(replace, UNDEFINED, parameters, 0, | |
916 parameters.length); | |
917 } else { | |
918 replacement = GetSubstitution(matched, string, position, captures, replace ); | |
adamk
2016/03/23 01:07:19
Nit: 80 cols.
Dan Ehrenberg
2016/03/24 00:50:54
fixed
| |
919 } | |
920 if (position >= next_source_position) { | |
921 accumulated_result += | |
922 %_SubString(string, next_source_position, position) + replacement; | |
923 next_source_position = position + matched_length; | |
924 } | |
925 } | |
926 if (next_source_position >= length) return accumulated_result; | |
927 return accumulated_result + %_SubString(string, next_source_position, length); | |
928 } | |
929 %FunctionRemovePrototype(RegExpSubclassReplace); | |
930 | |
931 | |
932 // Legacy implementation of RegExp.prototype[Symbol.search] which | |
933 // doesn't properly use the overridden exec method | |
571 function RegExpSearch(string) { | 934 function RegExpSearch(string) { |
572 // TODO(yangguo): allow non-regexp receivers. | |
573 if (!IS_REGEXP(this)) { | 935 if (!IS_REGEXP(this)) { |
574 throw MakeTypeError(kIncompatibleMethodReceiver, | 936 throw MakeTypeError(kIncompatibleMethodReceiver, |
575 "RegExp.prototype.@@search", this); | 937 "RegExp.prototype.@@search", this); |
576 } | 938 } |
577 var match = DoRegExpExec(this, TO_STRING(string), 0); | 939 var match = DoRegExpExec(this, TO_STRING(string), 0); |
578 if (match) return match[CAPTURE0]; | 940 if (match) return match[CAPTURE0]; |
579 return -1; | 941 return -1; |
580 } | 942 } |
581 | 943 |
582 | 944 |
945 // ES#sec-regexp.prototype-@@search | |
946 // RegExp.prototype [ @@search ] ( string ) | |
947 function RegExpSubclassSearch(string) { | |
948 if (!IS_OBJECT(this)) { | |
949 throw MakeTypeError(kIncompatibleMethodReceiver, | |
950 "RegExp.prototype.@@search", this); | |
951 } | |
952 string = TO_STRING(string); | |
953 var previousLastIndex = this.lastIndex; | |
954 this.lastIndex = 0; | |
955 var result = RegExpSubclassExec(this, string); | |
956 this.lastIndex = previousLastIndex; | |
957 if (IS_NULL(result)) return -1; | |
958 return result.index; | |
959 } | |
960 %FunctionRemovePrototype(RegExpSubclassSearch); | |
961 | |
962 | |
583 // Getters for the static properties lastMatch, lastParen, leftContext, and | 963 // Getters for the static properties lastMatch, lastParen, leftContext, and |
584 // rightContext of the RegExp constructor. The properties are computed based | 964 // rightContext of the RegExp constructor. The properties are computed based |
585 // on the captures array of the last successful match and the subject string | 965 // on the captures array of the last successful match and the subject string |
586 // of the last successful match. | 966 // of the last successful match. |
587 function RegExpGetLastMatch() { | 967 function RegExpGetLastMatch() { |
588 var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo); | 968 var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo); |
589 return %_SubString(regExpSubject, | 969 return %_SubString(regExpSubject, |
590 RegExpLastMatchInfo[CAPTURE0], | 970 RegExpLastMatchInfo[CAPTURE0], |
591 RegExpLastMatchInfo[CAPTURE1]); | 971 RegExpLastMatchInfo[CAPTURE1]); |
592 } | 972 } |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
774 } | 1154 } |
775 %ToFastProperties(GlobalRegExp); | 1155 %ToFastProperties(GlobalRegExp); |
776 | 1156 |
777 // ------------------------------------------------------------------- | 1157 // ------------------------------------------------------------------- |
778 // Exports | 1158 // Exports |
779 | 1159 |
780 utils.Export(function(to) { | 1160 utils.Export(function(to) { |
781 to.RegExpExec = DoRegExpExec; | 1161 to.RegExpExec = DoRegExpExec; |
782 to.RegExpExecNoTests = RegExpExecNoTests; | 1162 to.RegExpExecNoTests = RegExpExecNoTests; |
783 to.RegExpLastMatchInfo = RegExpLastMatchInfo; | 1163 to.RegExpLastMatchInfo = RegExpLastMatchInfo; |
1164 to.RegExpSubclassExecJS = RegExpSubclassExecJS; | |
1165 to.RegExpSubclassMatch = RegExpSubclassMatch; | |
1166 to.RegExpSubclassReplace = RegExpSubclassReplace; | |
1167 to.RegExpSubclassSearch = RegExpSubclassSearch; | |
1168 to.RegExpSubclassSplit = RegExpSubclassSplit; | |
1169 to.RegExpSubclassTest = RegExpSubclassTest; | |
784 to.RegExpTest = RegExpTest; | 1170 to.RegExpTest = RegExpTest; |
785 to.IsRegExp = IsRegExp; | 1171 to.IsRegExp = IsRegExp; |
786 }); | 1172 }); |
787 | 1173 |
788 }) | 1174 }) |
OLD | NEW |