OLD | NEW |
---|---|
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 } | 119 } |
120 | 120 |
121 | 121 |
122 function RegExpCache() { | 122 function RegExpCache() { |
123 this.type = 'none'; | 123 this.type = 'none'; |
124 this.regExp = 0; | 124 this.regExp = 0; |
125 this.subject = 0; | 125 this.subject = 0; |
126 this.replaceString = 0; | 126 this.replaceString = 0; |
127 this.lastIndex = 0; | 127 this.lastIndex = 0; |
128 this.answer = 0; | 128 this.answer = 0; |
129 // answerSaved marks whether the contents of answer is valid for a cache | |
130 // hit in RegExpExec, StringMatch and StringSplit. | |
131 this.answerSaved = false; | |
129 } | 132 } |
130 | 133 |
131 | 134 |
132 var regExpCache = new RegExpCache(); | 135 var regExpCache = new RegExpCache(); |
133 | 136 |
134 | 137 |
135 function CloneRegexpAnswer(array) { | 138 function CloneRegexpAnswer(array) { |
139 if (array == null) return null; | |
136 var len = array.length; | 140 var len = array.length; |
137 var answer = new $Array(len); | 141 var answer = new $Array(len); |
138 for (var i = 0; i < len; i++) { | 142 for (var i = 0; i < len; i++) { |
139 answer[i] = array[i]; | 143 answer[i] = array[i]; |
140 } | 144 } |
141 answer.index = array.index; | 145 answer.index = array.index; |
142 answer.input = array.input; | 146 answer.input = array.input; |
143 return answer; | 147 return answer; |
144 } | 148 } |
145 | 149 |
146 | 150 |
147 function RegExpExec(string) { | 151 function RegExpExec(string) { |
148 if (!IS_REGEXP(this)) { | 152 if (!IS_REGEXP(this)) { |
149 throw MakeTypeError('incompatible_method_receiver', | 153 throw MakeTypeError('incompatible_method_receiver', |
150 ['RegExp.prototype.exec', this]); | 154 ['RegExp.prototype.exec', this]); |
151 } | 155 } |
152 | 156 |
153 var cache = regExpCache; | 157 var cache = regExpCache; |
158 var saveAnswer = false; | |
154 | 159 |
155 if (%_ObjectEquals(cache.type, 'exec') && | 160 if (%_ObjectEquals(cache.type, 'exec') && |
156 %_ObjectEquals(cache.lastIndex, this.lastIndex) && | 161 %_ObjectEquals(cache.lastIndex, this.lastIndex) && |
157 %_ObjectEquals(cache.regExp, this) && | 162 %_ObjectEquals(cache.regExp, this) && |
158 %_ObjectEquals(cache.subject, string)) { | 163 %_ObjectEquals(cache.subject, string)) { |
159 var last = cache.answer; | 164 if (cache.answerSaved) { |
160 if (last == null) { | 165 return CloneRegexpAnswer(cache.answer); |
161 return last; | |
162 } else { | 166 } else { |
163 return CloneRegexpAnswer(last); | 167 saveAnswer = true; |
164 } | 168 } |
169 } else { | |
170 cache.answerSaved = false; | |
Lasse Reichstein
2010/04/08 14:50:52
No need to set answerSaved here. Set it, unconditi
| |
165 } | 171 } |
166 | 172 |
167 if (%_ArgumentsLength() == 0) { | 173 if (%_ArgumentsLength() == 0) { |
168 var regExpInput = LAST_INPUT(lastMatchInfo); | 174 var regExpInput = LAST_INPUT(lastMatchInfo); |
169 if (IS_UNDEFINED(regExpInput)) { | 175 if (IS_UNDEFINED(regExpInput)) { |
170 throw MakeError('no_input_to_regexp', [this]); | 176 throw MakeError('no_input_to_regexp', [this]); |
171 } | 177 } |
172 string = regExpInput; | 178 string = regExpInput; |
173 } | 179 } |
174 var s; | 180 var s; |
(...skipping 14 matching lines...) Expand all Loading... | |
189 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); | 195 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
190 // matchIndices is either null or the lastMatchInfo array. | 196 // matchIndices is either null or the lastMatchInfo array. |
191 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); | 197 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); |
192 | 198 |
193 if (matchIndices == null) { | 199 if (matchIndices == null) { |
194 if (this.global) this.lastIndex = 0; | 200 if (this.global) this.lastIndex = 0; |
195 cache.lastIndex = lastIndex; | 201 cache.lastIndex = lastIndex; |
196 cache.regExp = this; | 202 cache.regExp = this; |
197 cache.subject = s; | 203 cache.subject = s; |
198 cache.answer = matchIndices; // Null. | 204 cache.answer = matchIndices; // Null. |
205 cache.answerSaved = true; // Safe since no cloning is needed. | |
199 cache.type = 'exec'; | 206 cache.type = 'exec'; |
200 return matchIndices; // No match. | 207 return matchIndices; // No match. |
201 } | 208 } |
202 | 209 |
203 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; | 210 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
204 var result; | 211 var result; |
205 if (numResults === 1) { | 212 if (numResults === 1) { |
206 var matchStart = lastMatchInfo[CAPTURE(0)]; | 213 var matchStart = lastMatchInfo[CAPTURE(0)]; |
207 var matchEnd = lastMatchInfo[CAPTURE(1)]; | 214 var matchEnd = lastMatchInfo[CAPTURE(1)]; |
208 result = [SubString(s, matchStart, matchEnd)]; | 215 result = [SubString(s, matchStart, matchEnd)]; |
209 } else { | 216 } else { |
210 result = new $Array(numResults); | 217 result = new $Array(numResults); |
211 for (var i = 0; i < numResults; i++) { | 218 for (var i = 0; i < numResults; i++) { |
212 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; | 219 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; |
213 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; | 220 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; |
214 if (matchStart != -1 && matchEnd != -1) { | 221 if (matchStart != -1 && matchEnd != -1) { |
215 result[i] = SubString(s, matchStart, matchEnd); | 222 result[i] = SubString(s, matchStart, matchEnd); |
216 } else { | 223 } else { |
217 // Make sure the element is present. Avoid reading the undefined | 224 // Make sure the element is present. Avoid reading the undefined |
218 // property from the global object since this may change. | 225 // property from the global object since this may change. |
219 result[i] = void 0; | 226 result[i] = void 0; |
220 } | 227 } |
221 } | 228 } |
222 } | 229 } |
223 | 230 |
224 result.index = lastMatchInfo[CAPTURE0]; | 231 result.index = lastMatchInfo[CAPTURE0]; |
225 result.input = s; | 232 result.input = s; |
226 if (this.global) { | 233 if (this.global) { |
227 this.lastIndex = lastMatchInfo[CAPTURE1]; | 234 this.lastIndex = lastMatchInfo[CAPTURE1]; |
228 return result; | |
229 } else { | 235 } else { |
230 cache.regExp = this; | 236 cache.regExp = this; |
231 cache.subject = s; | 237 cache.subject = s; |
232 cache.lastIndex = lastIndex; | 238 cache.lastIndex = lastIndex; |
233 cache.answer = result; | 239 if (saveAnswer) { |
240 cache.answer = CloneRegexpAnswer(result); | |
241 cache.answerSaved = true; | |
Lasse Reichstein
2010/04/08 14:50:52
I.e., always set answerSaved to saveAnswer here.
| |
242 } | |
234 cache.type = 'exec'; | 243 cache.type = 'exec'; |
235 return CloneRegexpAnswer(result); | |
236 } | 244 } |
245 return result; | |
246 | |
237 } | 247 } |
238 | 248 |
239 | 249 |
240 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be | 250 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be |
241 // that test is defined in terms of String.prototype.exec. However, it probably | 251 // that test is defined in terms of String.prototype.exec. However, it probably |
242 // means the original value of String.prototype.exec, which is what everybody | 252 // means the original value of String.prototype.exec, which is what everybody |
243 // else implements. | 253 // else implements. |
244 function RegExpTest(string) { | 254 function RegExpTest(string) { |
245 if (!IS_REGEXP(this)) { | 255 if (!IS_REGEXP(this)) { |
246 throw MakeTypeError('incompatible_method_receiver', | 256 throw MakeTypeError('incompatible_method_receiver', |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
497 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); | 507 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
498 | 508 |
499 for (var i = 1; i < 10; ++i) { | 509 for (var i = 1; i < 10; ++i) { |
500 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); | 510 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); |
501 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); | 511 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); |
502 } | 512 } |
503 } | 513 } |
504 | 514 |
505 | 515 |
506 SetupRegExp(); | 516 SetupRegExp(); |
OLD | NEW |