| 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 var $regexpLastMatchInfoOverride; | 5 var $regexpLastMatchInfoOverride; |
| 6 var harmony_regexps = false; | |
| 7 var harmony_unicode_regexps = false; | |
| 8 | 6 |
| 9 (function(global, utils) { | 7 (function(global, utils) { |
| 10 | 8 |
| 11 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
| 12 | 10 |
| 13 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
| 14 // Imports | 12 // Imports |
| 15 | 13 |
| 14 var FLAG_harmony_regexps; |
| 15 var FLAG_harmony_unicode_regexps; |
| 16 var GlobalRegExp = global.RegExp; | 16 var GlobalRegExp = global.RegExp; |
| 17 var InternalPackedArray = utils.InternalPackedArray; | 17 var InternalPackedArray = utils.InternalPackedArray; |
| 18 var ToNumber; | 18 var ToNumber; |
| 19 | 19 |
| 20 utils.Import(function(from) { | 20 utils.Import(function(from) { |
| 21 ToNumber = from.ToNumber; | 21 ToNumber = from.ToNumber; |
| 22 }); | 22 }); |
| 23 | 23 |
| 24 utils.ImportFromExperimental(function(from) { |
| 25 FLAG_harmony_regexps = from.FLAG_harmony_regexps; |
| 26 FLAG_harmony_unicode_regexps = from.FLAG_harmony_unicode_regexps; |
| 27 }); |
| 28 |
| 24 // ------------------------------------------------------------------- | 29 // ------------------------------------------------------------------- |
| 25 | 30 |
| 26 // Property of the builtins object for recording the result of the last | 31 // Property of the builtins object for recording the result of the last |
| 27 // regexp match. The property RegExpLastMatchInfo includes the matchIndices | 32 // regexp match. The property RegExpLastMatchInfo includes the matchIndices |
| 28 // array of the last successful regexp match (an array of start/end index | 33 // array of the last successful regexp match (an array of start/end index |
| 29 // pairs for the match and all the captured substrings), the invariant is | 34 // pairs for the match and all the captured substrings), the invariant is |
| 30 // that there are at least two capture indeces. The array also contains | 35 // that there are at least two capture indeces. The array also contains |
| 31 // the subject string for the last successful match. | 36 // the subject string for the last successful match. |
| 32 var RegExpLastMatchInfo = new InternalPackedArray( | 37 var RegExpLastMatchInfo = new InternalPackedArray( |
| 33 2, // REGEXP_NUMBER_OF_CAPTURES | 38 2, // REGEXP_NUMBER_OF_CAPTURES |
| (...skipping 13 matching lines...) Expand all Loading... |
| 47 | 52 |
| 48 // A recursive descent parser for Patterns according to the grammar of | 53 // A recursive descent parser for Patterns according to the grammar of |
| 49 // ECMA-262 15.10.1, with deviations noted below. | 54 // ECMA-262 15.10.1, with deviations noted below. |
| 50 function DoConstructRegExp(object, pattern, flags) { | 55 function DoConstructRegExp(object, pattern, flags) { |
| 51 // RegExp : Called as constructor; see ECMA-262, section 15.10.4. | 56 // RegExp : Called as constructor; see ECMA-262, section 15.10.4. |
| 52 if (IS_REGEXP(pattern)) { | 57 if (IS_REGEXP(pattern)) { |
| 53 if (!IS_UNDEFINED(flags)) throw MakeTypeError(kRegExpFlags); | 58 if (!IS_UNDEFINED(flags)) throw MakeTypeError(kRegExpFlags); |
| 54 flags = (pattern.global ? 'g' : '') | 59 flags = (pattern.global ? 'g' : '') |
| 55 + (pattern.ignoreCase ? 'i' : '') | 60 + (pattern.ignoreCase ? 'i' : '') |
| 56 + (pattern.multiline ? 'm' : ''); | 61 + (pattern.multiline ? 'm' : ''); |
| 57 if (harmony_unicode_regexps) | 62 if (FLAG_harmony_unicode_regexps) |
| 58 flags += (pattern.unicode ? 'u' : ''); | 63 flags += (pattern.unicode ? 'u' : ''); |
| 59 if (harmony_regexps) | 64 if (FLAG_harmony_regexps) |
| 60 flags += (pattern.sticky ? 'y' : ''); | 65 flags += (pattern.sticky ? 'y' : ''); |
| 61 pattern = pattern.source; | 66 pattern = pattern.source; |
| 62 } | 67 } |
| 63 | 68 |
| 64 pattern = IS_UNDEFINED(pattern) ? '' : $toString(pattern); | 69 pattern = IS_UNDEFINED(pattern) ? '' : $toString(pattern); |
| 65 flags = IS_UNDEFINED(flags) ? '' : $toString(flags); | 70 flags = IS_UNDEFINED(flags) ? '' : $toString(flags); |
| 66 | 71 |
| 67 %RegExpInitializeAndCompile(object, pattern, flags); | 72 %RegExpInitializeAndCompile(object, pattern, flags); |
| 68 } | 73 } |
| 69 | 74 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 'RegExp.prototype.exec', this); | 161 'RegExp.prototype.exec', this); |
| 157 } | 162 } |
| 158 | 163 |
| 159 string = TO_STRING_INLINE(string); | 164 string = TO_STRING_INLINE(string); |
| 160 var lastIndex = this.lastIndex; | 165 var lastIndex = this.lastIndex; |
| 161 | 166 |
| 162 // Conversion is required by the ES5 specification (RegExp.prototype.exec | 167 // Conversion is required by the ES5 specification (RegExp.prototype.exec |
| 163 // algorithm, step 5) even if the value is discarded for non-global RegExps. | 168 // algorithm, step 5) even if the value is discarded for non-global RegExps. |
| 164 var i = TO_INTEGER(lastIndex); | 169 var i = TO_INTEGER(lastIndex); |
| 165 | 170 |
| 166 var updateLastIndex = this.global || (harmony_regexps && this.sticky); | 171 var updateLastIndex = this.global || (FLAG_harmony_regexps && this.sticky); |
| 167 if (updateLastIndex) { | 172 if (updateLastIndex) { |
| 168 if (i < 0 || i > string.length) { | 173 if (i < 0 || i > string.length) { |
| 169 this.lastIndex = 0; | 174 this.lastIndex = 0; |
| 170 return null; | 175 return null; |
| 171 } | 176 } |
| 172 } else { | 177 } else { |
| 173 i = 0; | 178 i = 0; |
| 174 } | 179 } |
| 175 | 180 |
| 176 // matchIndices is either null or the RegExpLastMatchInfo array. | 181 // matchIndices is either null or the RegExpLastMatchInfo array. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 204 'RegExp.prototype.test', this); | 209 'RegExp.prototype.test', this); |
| 205 } | 210 } |
| 206 string = TO_STRING_INLINE(string); | 211 string = TO_STRING_INLINE(string); |
| 207 | 212 |
| 208 var lastIndex = this.lastIndex; | 213 var lastIndex = this.lastIndex; |
| 209 | 214 |
| 210 // Conversion is required by the ES5 specification (RegExp.prototype.exec | 215 // Conversion is required by the ES5 specification (RegExp.prototype.exec |
| 211 // algorithm, step 5) even if the value is discarded for non-global RegExps. | 216 // algorithm, step 5) even if the value is discarded for non-global RegExps. |
| 212 var i = TO_INTEGER(lastIndex); | 217 var i = TO_INTEGER(lastIndex); |
| 213 | 218 |
| 214 if (this.global || (harmony_regexps && this.sticky)) { | 219 if (this.global || (FLAG_harmony_regexps && this.sticky)) { |
| 215 if (i < 0 || i > string.length) { | 220 if (i < 0 || i > string.length) { |
| 216 this.lastIndex = 0; | 221 this.lastIndex = 0; |
| 217 return false; | 222 return false; |
| 218 } | 223 } |
| 219 // matchIndices is either null or the RegExpLastMatchInfo array. | 224 // matchIndices is either null or the RegExpLastMatchInfo array. |
| 220 var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); | 225 var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); |
| 221 if (IS_NULL(matchIndices)) { | 226 if (IS_NULL(matchIndices)) { |
| 222 this.lastIndex = 0; | 227 this.lastIndex = 0; |
| 223 return false; | 228 return false; |
| 224 } | 229 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 267 |
| 263 function RegExpToString() { | 268 function RegExpToString() { |
| 264 if (!IS_REGEXP(this)) { | 269 if (!IS_REGEXP(this)) { |
| 265 throw MakeTypeError(kIncompatibleMethodReceiver, | 270 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 266 'RegExp.prototype.toString', this); | 271 'RegExp.prototype.toString', this); |
| 267 } | 272 } |
| 268 var result = '/' + this.source + '/'; | 273 var result = '/' + this.source + '/'; |
| 269 if (this.global) result += 'g'; | 274 if (this.global) result += 'g'; |
| 270 if (this.ignoreCase) result += 'i'; | 275 if (this.ignoreCase) result += 'i'; |
| 271 if (this.multiline) result += 'm'; | 276 if (this.multiline) result += 'm'; |
| 272 if (harmony_unicode_regexps && this.unicode) result += 'u'; | 277 if (FLAG_harmony_unicode_regexps && this.unicode) result += 'u'; |
| 273 if (harmony_regexps && this.sticky) result += 'y'; | 278 if (FLAG_harmony_regexps && this.sticky) result += 'y'; |
| 274 return result; | 279 return result; |
| 275 } | 280 } |
| 276 | 281 |
| 277 | 282 |
| 278 // Getters for the static properties lastMatch, lastParen, leftContext, and | 283 // Getters for the static properties lastMatch, lastParen, leftContext, and |
| 279 // rightContext of the RegExp constructor. The properties are computed based | 284 // rightContext of the RegExp constructor. The properties are computed based |
| 280 // on the captures array of the last successful match and the subject string | 285 // on the captures array of the last successful match and the subject string |
| 281 // of the last successful match. | 286 // of the last successful match. |
| 282 function RegExpGetLastMatch() { | 287 function RegExpGetLastMatch() { |
| 283 if ($regexpLastMatchInfoOverride !== null) { | 288 if ($regexpLastMatchInfoOverride !== null) { |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 // Exports | 454 // Exports |
| 450 | 455 |
| 451 utils.Export(function(to) { | 456 utils.Export(function(to) { |
| 452 to.RegExpExec = DoRegExpExec; | 457 to.RegExpExec = DoRegExpExec; |
| 453 to.RegExpExecNoTests = RegExpExecNoTests; | 458 to.RegExpExecNoTests = RegExpExecNoTests; |
| 454 to.RegExpLastMatchInfo = RegExpLastMatchInfo; | 459 to.RegExpLastMatchInfo = RegExpLastMatchInfo; |
| 455 to.RegExpTest = RegExpTest; | 460 to.RegExpTest = RegExpTest; |
| 456 }); | 461 }); |
| 457 | 462 |
| 458 }) | 463 }) |
| OLD | NEW |