OLD | NEW |
---|---|
1 // Copyright 2006-2008 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 |
11 // with the distribution. | 11 // with the distribution. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 } | 45 } |
46 | 46 |
47 pattern = IS_UNDEFINED(pattern) ? '' : ToString(pattern); | 47 pattern = IS_UNDEFINED(pattern) ? '' : ToString(pattern); |
48 flags = IS_UNDEFINED(flags) ? '' : ToString(flags); | 48 flags = IS_UNDEFINED(flags) ? '' : ToString(flags); |
49 | 49 |
50 var global = false; | 50 var global = false; |
51 var ignoreCase = false; | 51 var ignoreCase = false; |
52 var multiline = false; | 52 var multiline = false; |
53 | 53 |
54 for (var i = 0; i < flags.length; i++) { | 54 for (var i = 0; i < flags.length; i++) { |
55 var c = flags.charAt(i); | 55 var c = StringCharAt.call(flags, i); |
56 switch (c) { | 56 switch (c) { |
57 case 'g': | 57 case 'g': |
58 // Allow duplicate flags to be consistent with JSC and others. | 58 // Allow duplicate flags to be consistent with JSC and others. |
59 global = true; | 59 global = true; |
60 break; | 60 break; |
61 case 'i': | 61 case 'i': |
62 ignoreCase = true; | 62 ignoreCase = true; |
63 break; | 63 break; |
64 case 'm': | 64 case 'm': |
65 multiline = true; | 65 multiline = true; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) { | 110 if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) { |
111 return pattern; | 111 return pattern; |
112 } | 112 } |
113 return new $RegExp(pattern, flags); | 113 return new $RegExp(pattern, flags); |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 | 117 |
118 // Deprecated RegExp.prototype.compile method. We behave like the constructor | 118 // Deprecated RegExp.prototype.compile method. We behave like the constructor |
119 // were called again. In SpiderMonkey, this method returns the regexp object. | 119 // were called again. In SpiderMonkey, this method returns the regexp object. |
120 // In KJS, it returns undefined. For compatibility with KJS, we match their | 120 // In JSC, it returns undefined. For compatibility with JSC, we match their |
121 // behavior. | 121 // behavior. |
122 function CompileRegExp(pattern, flags) { | 122 function CompileRegExp(pattern, flags) { |
123 // Both KJS and SpiderMonkey treat a missing pattern argument as the | 123 // Both JSC and SpiderMonkey treat a missing pattern argument as the |
124 // empty subject string, and an actual undefined value passed as the | 124 // empty subject string, and an actual undefined value passed as the |
125 // patter as the string 'undefined'. Note that KJS is inconsistent | 125 // pattern as the string 'undefined'. Note that JSC is inconsistent |
126 // here, treating undefined values differently in | 126 // here, treating undefined values differently in |
127 // RegExp.prototype.compile and in the constructor, where they are | 127 // RegExp.prototype.compile and in the constructor, where they are |
128 // the empty string. For compatibility with KJS, we match their | 128 // the empty string. For compatibility with JSC, we match their |
129 // behavior. | 129 // behavior. |
130 if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) { | 130 if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) { |
131 DoConstructRegExp(this, 'undefined', flags, false); | 131 DoConstructRegExp(this, 'undefined', flags, false); |
132 } else { | 132 } else { |
133 DoConstructRegExp(this, pattern, flags, false); | 133 DoConstructRegExp(this, pattern, flags, false); |
134 } | 134 } |
135 } | 135 } |
136 | 136 |
137 | 137 |
138 // DoRegExpExec and DoRegExpExecGlobal are wrappers around the runtime | |
139 // %RegExp and %RegExpGlobal functions that ensure that the static | |
140 // properties of the RegExp constructor are set. | |
141 function DoRegExpExec(regexp, string, index) { | 138 function DoRegExpExec(regexp, string, index) { |
Lasse Reichstein
2009/03/11 13:49:41
Is this used any more?
Lasse Reichstein
2009/03/11 13:51:53
Yes it is, just not in this file. It's called from
| |
142 var matchIndices = %RegExpExec(regexp, string, index); | 139 return %RegExpExec(regexp, string, index, lastMatchInfo); |
143 if (!IS_NULL(matchIndices)) { | |
144 regExpCaptures = matchIndices; | |
145 regExpSubject = regExpInput = string; | |
146 } | |
147 return matchIndices; | |
148 } | 140 } |
149 | 141 |
150 | 142 |
151 function DoRegExpExecGlobal(regexp, string) { | 143 function DoRegExpExecGlobal(regexp, string) { |
152 // Here, matchIndices is an array of arrays of substring indices. | 144 // Returns an array of arrays of substring indices. |
153 var matchIndices = %RegExpExecGlobal(regexp, string); | 145 return %RegExpExecGlobal(regexp, string, lastMatchInfo); |
154 if (matchIndices.length != 0) { | |
155 regExpCaptures = matchIndices[matchIndices.length - 1]; | |
156 regExpSubject = regExpInput = string; | |
157 } | |
158 return matchIndices; | |
159 } | 146 } |
160 | 147 |
161 | 148 |
162 function RegExpExec(string) { | 149 function RegExpExec(string) { |
163 if (!IS_REGEXP(this)) { | 150 if (!IS_REGEXP(this)) { |
164 throw MakeTypeError('method_called_on_incompatible', ['RegExp.prototype.exec ', this]); | 151 throw MakeTypeError('method_called_on_incompatible', ['RegExp.prototype.exec ', this]); |
165 } | 152 } |
166 if (%_ArgumentsLength() == 0) { | 153 if (%_ArgumentsLength() == 0) { |
154 var regExpInput = LAST_INPUT(lastMatchInfo); | |
167 if (IS_UNDEFINED(regExpInput)) { | 155 if (IS_UNDEFINED(regExpInput)) { |
168 throw MakeError('no_input_to_regexp', [this]); | 156 throw MakeError('no_input_to_regexp', [this]); |
169 } | 157 } |
170 string = regExpInput; | 158 string = regExpInput; |
171 } | 159 } |
172 var s = ToString(string); | 160 var s = ToString(string); |
173 var length = s.length; | 161 var length = s.length; |
174 var lastIndex = this.lastIndex; | 162 var lastIndex = this.lastIndex; |
175 var i = this.global ? TO_INTEGER(lastIndex) : 0; | 163 var i = this.global ? TO_INTEGER(lastIndex) : 0; |
176 | 164 |
177 if (i < 0 || i > s.length) { | 165 if (i < 0 || i > s.length) { |
178 this.lastIndex = 0; | 166 this.lastIndex = 0; |
179 return null; | 167 return null; |
180 } | 168 } |
181 | 169 |
182 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); | 170 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
183 // matchIndices is an array of integers with length of captures*2, | 171 // matchIndices is either null or the lastMatchInfo array. |
184 // each pair of integers specified the start and the end of index | 172 var matchIndices = %RegExpExec(this, s, i, lastMatchInfo); |
185 // in the string. | |
186 var matchIndices = DoRegExpExec(this, s, i); | |
187 | 173 |
188 if (matchIndices == null) { | 174 if (matchIndices == null) { |
189 if (this.global) this.lastIndex = 0; | 175 if (this.global) this.lastIndex = 0; |
190 return matchIndices; // no match | 176 return matchIndices; // no match |
191 } | 177 } |
192 | 178 |
193 var numResults = matchIndices.length >> 1; | 179 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
194 var result = new $Array(numResults); | 180 var result = new $Array(numResults); |
195 for (var i = 0; i < numResults; i++) { | 181 for (var i = 0; i < numResults; i++) { |
196 var matchStart = matchIndices[2*i]; | 182 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; |
197 var matchEnd = matchIndices[2*i + 1]; | 183 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; |
198 if (matchStart != -1 && matchEnd != -1) { | 184 if (matchStart != -1 && matchEnd != -1) { |
199 result[i] = s.slice(matchStart, matchEnd); | 185 result[i] = SubString(s, matchStart, matchEnd); |
200 } else { | 186 } else { |
201 // Make sure the element is present. Avoid reading the undefined | 187 // Make sure the element is present. Avoid reading the undefined |
202 // property from the global object since this may change. | 188 // property from the global object since this may change. |
203 result[i] = void 0; | 189 result[i] = void 0; |
204 } | 190 } |
205 } | 191 } |
206 | 192 |
207 if (this.global) | 193 if (this.global) |
208 this.lastIndex = matchIndices[1]; | 194 this.lastIndex = lastMatchInfo[CAPTURE1]; |
209 result.index = matchIndices[0]; | 195 result.index = lastMatchInfo[CAPTURE0]; |
210 result.input = s; | 196 result.input = s; |
211 return result; | 197 return result; |
212 } | 198 } |
213 | 199 |
214 | 200 |
201 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be | |
202 // that test is defined in terms of String.prototype.exec even if the method is | |
203 // called on a non-RegExp object. However, it probably means the original | |
204 // value of String.prototype.exec, which is what everybody else implements. | |
215 function RegExpTest(string) { | 205 function RegExpTest(string) { |
216 var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string); | 206 if (!IS_REGEXP(this)) { |
217 return result != null; | 207 throw MakeTypeError('method_called_on_incompatible', ['RegExp.prototype.test ', this]); |
Mads Ager (chromium)
2009/03/11 13:49:17
Long line, break it?
Erik Corry
2009/03/11 14:01:06
Yes. It seems none of our linters catch >80 chara
| |
208 } | |
209 if (%_ArgumentsLength() == 0) { | |
210 var regExpInput = LAST_INPUT(lastMatchInfo); | |
211 if (IS_UNDEFINED(regExpInput)) { | |
212 throw MakeError('no_input_to_regexp', [this]); | |
213 } | |
214 string = regExpInput; | |
215 } | |
216 var s = ToString(string); | |
217 var length = s.length; | |
218 var lastIndex = this.lastIndex; | |
219 var i = this.global ? TO_INTEGER(lastIndex) : 0; | |
220 | |
221 if (i < 0 || i > s.length) { | |
222 this.lastIndex = 0; | |
223 return false; | |
224 } | |
225 | |
226 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); | |
227 // matchIndices is either null or the lastMatchInfo array. | |
228 var matchIndices = %RegExpExec(this, s, i, lastMatchInfo); | |
229 | |
230 if (matchIndices == null) { | |
231 if (this.global) this.lastIndex = 0; | |
232 return false; | |
233 } | |
234 | |
235 if (this.global) this.lastIndex = lastMatchInfo[CAPTURE1]; | |
236 return true; | |
218 } | 237 } |
219 | 238 |
220 | 239 |
221 function RegExpToString() { | 240 function RegExpToString() { |
222 // If this.source is an empty string, output /(?:)/. | 241 // If this.source is an empty string, output /(?:)/. |
223 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 | 242 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 |
224 // ecma_2/RegExp/properties-001.js. | 243 // ecma_2/RegExp/properties-001.js. |
225 var src = this.source ? this.source : '(?:)'; | 244 var src = this.source ? this.source : '(?:)'; |
226 var result = '/' + src + '/'; | 245 var result = '/' + src + '/'; |
227 if (this.global) | 246 if (this.global) |
228 result += 'g'; | 247 result += 'g'; |
229 if (this.ignoreCase) | 248 if (this.ignoreCase) |
230 result += 'i'; | 249 result += 'i'; |
231 if (this.multiline) | 250 if (this.multiline) |
232 result += 'm'; | 251 result += 'm'; |
233 return result; | 252 return result; |
234 } | 253 } |
235 | 254 |
236 | 255 |
237 // Getters for the static properties lastMatch, lastParen, leftContext, and | 256 // Getters for the static properties lastMatch, lastParen, leftContext, and |
238 // rightContext of the RegExp constructor. The properties are computed based | 257 // rightContext of the RegExp constructor. The properties are computed based |
239 // on the captures array of the last successful match and the subject string | 258 // on the captures array of the last successful match and the subject string |
240 // of the last successful match. | 259 // of the last successful match. |
241 function RegExpGetLastMatch() { | 260 function RegExpGetLastMatch() { |
242 return regExpSubject.slice(regExpCaptures[0], regExpCaptures[1]); | 261 var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
262 return SubString(regExpSubject, | |
263 lastMatchInfo[CAPTURE0], | |
264 lastMatchInfo[CAPTURE1]); | |
243 } | 265 } |
244 | 266 |
245 | 267 |
246 function RegExpGetLastParen() { | 268 function RegExpGetLastParen() { |
247 var length = regExpCaptures.length; | 269 var length = NUMBER_OF_CAPTURES(lastMatchInfo); |
248 if (length <= 2) return ''; // There were no captures. | 270 if (length <= 2) return ''; // There were no captures. |
249 // We match the SpiderMonkey behavior: return the substring defined by the | 271 // We match the SpiderMonkey behavior: return the substring defined by the |
250 // last pair (after the first pair) of elements of the capture array even if | 272 // last pair (after the first pair) of elements of the capture array even if |
251 // it is empty. | 273 // it is empty. |
252 return regExpSubject.slice(regExpCaptures[length - 2], | 274 var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
253 regExpCaptures[length - 1]); | 275 var start = lastMatchInfo[CAPTURE(length - 2)]; |
276 var end = lastMatchInfo[CAPTURE(length - 1)]; | |
277 if (start != -1 && end != -1) { | |
278 return SubString(regExpSubject, start, end); | |
279 } | |
280 return ""; | |
254 } | 281 } |
255 | 282 |
256 | 283 |
257 function RegExpGetLeftContext() { | 284 function RegExpGetLeftContext() { |
258 return regExpSubject.slice(0, regExpCaptures[0]); | 285 return SubString(LAST_SUBJECT(lastMatchInfo), |
286 0, | |
287 lastMatchInfo[CAPTURE0]); | |
259 } | 288 } |
260 | 289 |
261 | 290 |
262 function RegExpGetRightContext() { | 291 function RegExpGetRightContext() { |
263 return regExpSubject.slice(regExpCaptures[1], regExpSubject.length); | 292 var subject = LAST_SUBJECT(lastMatchInfo); |
293 return SubString(subject, | |
294 lastMatchInfo[CAPTURE1], | |
295 subject.length); | |
264 } | 296 } |
265 | 297 |
266 | 298 |
267 // The properties $1..$9 are the first nine capturing substrings of the last | 299 // The properties $1..$9 are the first nine capturing substrings of the last |
268 // successful match, or ''. The function RegExpMakeCaptureGetter will be | 300 // successful match, or ''. The function RegExpMakeCaptureGetter will be |
269 // called with an index greater than or equal to 1 but it actually works for | 301 // called with indeces from 1 to 9. |
270 // any non-negative index. | |
271 function RegExpMakeCaptureGetter(n) { | 302 function RegExpMakeCaptureGetter(n) { |
272 return function() { | 303 return function() { |
273 var index = n * 2; | 304 var index = n * 2; |
274 if (index >= regExpCaptures.length) return ''; | 305 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; |
275 var matchStart = regExpCaptures[index]; | 306 var matchStart = lastMatchInfo[CAPTURE(index)]; |
276 var matchEnd = regExpCaptures[index + 1]; | 307 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; |
277 if (matchStart == -1 || matchEnd == -1) return ''; | 308 if (matchStart == -1 || matchEnd == -1) return ''; |
278 return regExpSubject.slice(matchStart, matchEnd); | 309 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); |
279 }; | 310 }; |
280 } | 311 } |
281 | 312 |
282 | 313 |
283 // Properties of the builtins object for recording the result of the last | 314 // Property of the builtins object for recording the result of the last |
284 // regexp match. The property regExpCaptures is the matchIndices array of the | 315 // regexp match. The property lastMatchInfo includes the matchIndices |
285 // last successful regexp match (an array of start/end index pairs for the | 316 // array of the last successful regexp match (an array of start/end index |
286 // match and all the captured substrings), the invariant is that there is at | 317 // pairs for the match and all the captured substrings), the invariant is |
287 // least two elements. The property regExpSubject is the subject string for | 318 // that there are at least two capture indeces. The array also contains |
288 // the last successful match. | 319 // the subject string for the last successful match. |
289 var regExpCaptures = [0, 0]; | 320 var lastMatchInfo = [ |
290 var regExpSubject = ''; | 321 2, // REGEXP_NUMBER_OF_CAPTURES |
291 var regExpInput; | 322 0, // REGEXP_FIRST_CAPTURE + 0 |
323 0, // REGEXP_FIRST_CAPTURE + 1 | |
324 "", // Last subject. | |
325 void 0, // Last input - settable with RegExpSetInput. | |
326 ]; | |
292 | 327 |
293 // ------------------------------------------------------------------- | 328 // ------------------------------------------------------------------- |
294 | 329 |
295 function SetupRegExp() { | 330 function SetupRegExp() { |
296 %FunctionSetInstanceClassName($RegExp, 'RegExp'); | 331 %FunctionSetInstanceClassName($RegExp, 'RegExp'); |
297 %FunctionSetPrototype($RegExp, new $Object()); | 332 %FunctionSetPrototype($RegExp, new $Object()); |
298 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); | 333 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); |
299 %SetCode($RegExp, RegExpConstructor); | 334 %SetCode($RegExp, RegExpConstructor); |
300 | 335 |
301 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( | 336 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( |
302 "exec", RegExpExec, | 337 "exec", RegExpExec, |
303 "test", RegExpTest, | 338 "test", RegExpTest, |
304 "toString", RegExpToString, | 339 "toString", RegExpToString, |
305 "compile", CompileRegExp | 340 "compile", CompileRegExp |
306 )); | 341 )); |
307 | 342 |
308 // The length of compile is 1 in SpiderMonkey. | 343 // The length of compile is 1 in SpiderMonkey. |
309 %FunctionSetLength($RegExp.prototype.compile, 1); | 344 %FunctionSetLength($RegExp.prototype.compile, 1); |
310 | 345 |
311 // The properties input, $input, and $_ are aliases for each other. When this | 346 // The properties input, $input, and $_ are aliases for each other. When this |
312 // value is set the value it is set to is coerced to a string. | 347 // value is set the value it is set to is coerced to a string. |
313 // Getter and setter for the input. | 348 // Getter and setter for the input. |
314 function RegExpGetInput() { | 349 function RegExpGetInput() { |
350 var regExpInput = LAST_INPUT(lastMatchInfo); | |
315 return IS_UNDEFINED(regExpInput) ? "" : regExpInput; | 351 return IS_UNDEFINED(regExpInput) ? "" : regExpInput; |
316 } | 352 } |
317 function RegExpSetInput(string) { regExpInput = ToString(string); } | 353 function RegExpSetInput(string) { |
354 lastMatchInfo[lastMatchInfo[REGEXP_NUMBER_OF_CAPTURES] + 2] = | |
355 ToString(string); | |
356 }; | |
318 | 357 |
319 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); | 358 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); |
320 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE); | 359 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE); |
321 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE ); | 360 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE ); |
322 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE ); | 361 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE ); |
323 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE); | 362 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE); |
324 %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DE LETE); | 363 %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DE LETE); |
325 | 364 |
326 // The properties multiline and $* are aliases for each other. When this | 365 // The properties multiline and $* are aliases for each other. When this |
327 // value is set in SpiderMonkey, the value it is set to is coerced to a | 366 // value is set in SpiderMonkey, the value it is set to is coerced to a |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); | 402 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
364 | 403 |
365 for (var i = 1; i < 10; ++i) { | 404 for (var i = 1; i < 10; ++i) { |
366 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); | 405 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); |
367 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); | 406 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); |
368 } | 407 } |
369 } | 408 } |
370 | 409 |
371 | 410 |
372 SetupRegExp(); | 411 SetupRegExp(); |
OLD | NEW |