Chromium Code Reviews| Index: src/regexp-delay.js |
| =================================================================== |
| --- src/regexp-delay.js (revision 4060) |
| +++ src/regexp-delay.js (working copy) |
| @@ -140,7 +140,36 @@ |
| } |
| +var cachedRegexp; |
| +var cachedSubject; |
| +var cachedLastIndex; |
| +var cachedAnswer; |
| + |
| + |
| +function CloneRegexpAnswer(array) { |
| + var len = array.length; |
| + var answer = new $Array(len); |
| + for (var i = 0; i < len; i++) { |
| + answer[i] = array[i]; |
| + } |
| + answer.index = array.index; |
| + answer.input = array.input; |
| + return answer; |
| +} |
| + |
| + |
| function RegExpExec(string) { |
| + if (cachedLastIndex === this.lastIndex && |
|
Lasse Reichstein
2010/03/10 08:53:24
lastIndex is only relevant if the regexp is global
|
| + cachedRegexp === this && |
|
Lasse Reichstein
2010/03/10 08:53:24
Checking the regexp itself is unsafe. Its behavior
|
| + cachedSubject === string) { |
|
Mads Ager (chromium)
2010/03/10 08:12:22
I would use %_ObjectEquals for the string and rege
|
| + var last = cachedAnswer; |
| + if (last == null) { |
|
Lasse Reichstein
2010/03/10 08:53:24
Use ===?
|
| + return last; |
| + } else { |
| + return CloneRegexpAnswer(last); |
| + } |
| + } |
| + |
| if (!IS_REGEXP(this)) { |
| throw MakeTypeError('incompatible_method_receiver', |
| ['RegExp.prototype.exec', this]); |
| @@ -159,6 +188,7 @@ |
| s = ToString(string); |
| } |
| var lastIndex = this.lastIndex; |
| + |
| var i = this.global ? TO_INTEGER(lastIndex) : 0; |
| if (i < 0 || i > s.length) { |
| @@ -172,7 +202,11 @@ |
| if (matchIndices == null) { |
| if (this.global) this.lastIndex = 0; |
| - return matchIndices; // no match |
| + cachedLastIndex = lastIndex; |
| + cachedRegexp = this; |
| + cachedSubject = s; |
| + cachedAnswer = matchIndices; // Null. |
| + return matchIndices; // No match. |
| } |
| var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
| @@ -196,11 +230,18 @@ |
| } |
| } |
| - if (this.global) |
| - this.lastIndex = lastMatchInfo[CAPTURE1]; |
| result.index = lastMatchInfo[CAPTURE0]; |
| result.input = s; |
| - return result; |
| + if (this.global) { |
| + this.lastIndex = lastMatchInfo[CAPTURE1]; |
| + return result; |
| + } else { |
| + cachedRegexp = this; |
| + cachedSubject = s; |
| + cachedLastIndex = lastIndex; |
| + cachedAnswer = result; |
| + return CloneRegexpAnswer(result); |
| + } |
| } |