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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 throw MakeTypeError('incompatible_method_receiver', | 145 throw MakeTypeError('incompatible_method_receiver', |
146 ['RegExp.prototype.exec', this]); | 146 ['RegExp.prototype.exec', this]); |
147 } | 147 } |
148 if (%_ArgumentsLength() == 0) { | 148 if (%_ArgumentsLength() == 0) { |
149 var regExpInput = LAST_INPUT(lastMatchInfo); | 149 var regExpInput = LAST_INPUT(lastMatchInfo); |
150 if (IS_UNDEFINED(regExpInput)) { | 150 if (IS_UNDEFINED(regExpInput)) { |
151 throw MakeError('no_input_to_regexp', [this]); | 151 throw MakeError('no_input_to_regexp', [this]); |
152 } | 152 } |
153 string = regExpInput; | 153 string = regExpInput; |
154 } | 154 } |
155 var s = ToString(string); | 155 var s; |
156 var length = s.length; | 156 if (IS_STRING(string)) { |
157 s = string; | |
158 } else { | |
159 s = ToString(string); | |
160 } | |
157 var lastIndex = this.lastIndex; | 161 var lastIndex = this.lastIndex; |
158 var i = this.global ? TO_INTEGER(lastIndex) : 0; | 162 var i = this.global ? TO_INTEGER(lastIndex) : 0; |
159 | 163 |
160 if (i < 0 || i > s.length) { | 164 if (i < 0 || i > s.length) { |
161 this.lastIndex = 0; | 165 this.lastIndex = 0; |
162 return null; | 166 return null; |
163 } | 167 } |
164 | 168 |
165 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); | 169 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
166 // matchIndices is either null or the lastMatchInfo array. | 170 // matchIndices is either null or the lastMatchInfo array. |
167 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); | 171 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); |
168 | 172 |
169 if (matchIndices == null) { | 173 if (matchIndices == null) { |
170 if (this.global) this.lastIndex = 0; | 174 if (this.global) this.lastIndex = 0; |
171 return matchIndices; // no match | 175 return matchIndices; // no match |
172 } | 176 } |
173 | 177 |
174 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; | 178 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
175 var result = new $Array(numResults); | 179 if (numResults === 1) { |
176 for (var i = 0; i < numResults; i++) { | 180 var result = new $Array(1); |
Kasper Lund
2010/03/05 14:50:29
I wonder if
result = [ SubString(s, matchStar
| |
177 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; | 181 var matchStart = lastMatchInfo[CAPTURE(0)]; |
178 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; | 182 var matchEnd = lastMatchInfo[CAPTURE(1)]; |
179 if (matchStart != -1 && matchEnd != -1) { | 183 result[0] = SubString(s, matchStart, matchEnd); |
180 result[i] = SubString(s, matchStart, matchEnd); | 184 } else { |
181 } else { | 185 var result = new $Array(numResults); |
182 // Make sure the element is present. Avoid reading the undefined | 186 for (var i = 0; i < numResults; i++) { |
183 // property from the global object since this may change. | 187 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; |
184 result[i] = void 0; | 188 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; |
189 if (matchStart != -1 && matchEnd != -1) { | |
190 result[i] = SubString(s, matchStart, matchEnd); | |
191 } else { | |
192 // Make sure the element is present. Avoid reading the undefined | |
193 // property from the global object since this may change. | |
194 result[i] = void 0; | |
195 } | |
185 } | 196 } |
186 } | 197 } |
187 | 198 |
188 if (this.global) | 199 if (this.global) |
189 this.lastIndex = lastMatchInfo[CAPTURE1]; | 200 this.lastIndex = lastMatchInfo[CAPTURE1]; |
190 result.index = lastMatchInfo[CAPTURE0]; | 201 result.index = lastMatchInfo[CAPTURE0]; |
191 result.input = s; | 202 result.input = s; |
192 return result; | 203 return result; |
193 } | 204 } |
194 | 205 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
397 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); | 408 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
398 | 409 |
399 for (var i = 1; i < 10; ++i) { | 410 for (var i = 1; i < 10; ++i) { |
400 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); | 411 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); |
401 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); | 412 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); |
402 } | 413 } |
403 } | 414 } |
404 | 415 |
405 | 416 |
406 SetupRegExp(); | 417 SetupRegExp(); |
OLD | NEW |