| 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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 | 113 |
| 114 function DoRegExpExec(regexp, string, index) { | 114 function DoRegExpExec(regexp, string, index) { |
| 115 var result = %_RegExpExec(regexp, string, index, lastMatchInfo); | 115 var result = %_RegExpExec(regexp, string, index, lastMatchInfo); |
| 116 if (result !== null) lastMatchInfoOverride = null; | 116 if (result !== null) lastMatchInfoOverride = null; |
| 117 return result; | 117 return result; |
| 118 } | 118 } |
| 119 | 119 |
| 120 | 120 |
| 121 function BuildResultFromMatchInfo(lastMatchInfo, s) { | 121 function BuildResultFromMatchInfo(lastMatchInfo, s) { |
| 122 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; | 122 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
| 123 var start = lastMatchInfo[CAPTURE0]; | 123 var result = %_RegExpConstructResult(numResults, lastMatchInfo[CAPTURE0], s); |
| 124 var end = lastMatchInfo[CAPTURE1]; | 124 if (numResults === 1) { |
| 125 var result = %_RegExpConstructResult(numResults, start, s); | 125 var matchStart = lastMatchInfo[CAPTURE(0)]; |
| 126 if (start + 1 == end) { | 126 var matchEnd = lastMatchInfo[CAPTURE(1)]; |
| 127 result[0] = %_StringCharAt(s, start); | 127 result[0] = SubString(s, matchStart, matchEnd); |
| 128 } else { | 128 } else { |
| 129 result[0] = %_SubString(s, start, end); | 129 for (var i = 0; i < numResults; i++) { |
| 130 } | 130 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; |
| 131 var j = REGEXP_FIRST_CAPTURE + 2; | 131 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; |
| 132 for (var i = 1; i < numResults; i++) { | 132 if (matchStart != -1 && matchEnd != -1) { |
| 133 start = lastMatchInfo[j++]; | 133 result[i] = SubString(s, matchStart, matchEnd); |
| 134 end = lastMatchInfo[j++]; | |
| 135 if (end != -1) { | |
| 136 if (start + 1 == end) { | |
| 137 result[i] = %_StringCharAt(s, start); | |
| 138 } else { | 134 } else { |
| 139 result[i] = %_SubString(s, start, end); | 135 // Make sure the element is present. Avoid reading the undefined |
| 136 // property from the global object since this may change. |
| 137 result[i] = void 0; |
| 140 } | 138 } |
| 141 } else { | |
| 142 // Make sure the element is present. Avoid reading the undefined | |
| 143 // property from the global object since this may change. | |
| 144 result[i] = void 0; | |
| 145 } | 139 } |
| 146 } | 140 } |
| 147 return result; | 141 return result; |
| 148 } | 142 } |
| 149 | 143 |
| 150 | 144 |
| 151 function RegExpExecNoTests(regexp, string, start) { | 145 function RegExpExecNoTests(regexp, string, start) { |
| 152 // Must be called with RegExp, string and positive integer as arguments. | 146 // Must be called with RegExp, string and positive integer as arguments. |
| 153 var matchInfo = DoRegExpExec(regexp, string, start); | 147 var matchInfo = DoRegExpExec(regexp, string, start); |
| 154 var result = null; | 148 var result = null; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 165 ['RegExp.prototype.exec', this]); | 159 ['RegExp.prototype.exec', this]); |
| 166 } | 160 } |
| 167 | 161 |
| 168 if (%_ArgumentsLength() === 0) { | 162 if (%_ArgumentsLength() === 0) { |
| 169 var regExpInput = LAST_INPUT(lastMatchInfo); | 163 var regExpInput = LAST_INPUT(lastMatchInfo); |
| 170 if (IS_UNDEFINED(regExpInput)) { | 164 if (IS_UNDEFINED(regExpInput)) { |
| 171 throw MakeError('no_input_to_regexp', [this]); | 165 throw MakeError('no_input_to_regexp', [this]); |
| 172 } | 166 } |
| 173 string = regExpInput; | 167 string = regExpInput; |
| 174 } | 168 } |
| 175 string = TO_STRING_INLINE(string); | 169 var s; |
| 170 if (IS_STRING(string)) { |
| 171 s = string; |
| 172 } else { |
| 173 s = ToString(string); |
| 174 } |
| 176 var lastIndex = this.lastIndex; | 175 var lastIndex = this.lastIndex; |
| 177 | 176 |
| 178 // Conversion is required by the ES5 specification (RegExp.prototype.exec | 177 // Conversion is required by the ES5 specification (RegExp.prototype.exec |
| 179 // algorithm, step 5) even if the value is discarded for non-global RegExps. | 178 // algorithm, step 5) even if the value is discarded for non-global RegExps. |
| 180 var i = TO_INTEGER(lastIndex); | 179 var i = TO_INTEGER(lastIndex); |
| 181 | 180 |
| 182 var global = this.global; | 181 var global = this.global; |
| 183 if (global) { | 182 if (global) { |
| 184 if (i < 0 || i > string.length) { | 183 if (i < 0 || i > s.length) { |
| 185 this.lastIndex = 0; | 184 this.lastIndex = 0; |
| 186 return null; | 185 return null; |
| 187 } | 186 } |
| 188 } else { | 187 } else { |
| 189 i = 0; | 188 i = 0; |
| 190 } | 189 } |
| 191 | 190 |
| 192 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]); | 191 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
| 193 // matchIndices is either null or the lastMatchInfo array. | 192 // matchIndices is either null or the lastMatchInfo array. |
| 194 var matchIndices = %_RegExpExec(this, string, i, lastMatchInfo); | 193 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); |
| 195 | 194 |
| 196 if (matchIndices === null) { | 195 if (matchIndices === null) { |
| 197 if (global) this.lastIndex = 0; | 196 if (global) this.lastIndex = 0; |
| 198 return null; | 197 return null; |
| 199 } | 198 } |
| 200 | 199 |
| 201 // Successful match. | 200 // Successful match. |
| 202 lastMatchInfoOverride = null; | 201 lastMatchInfoOverride = null; |
| 203 if (global) { | 202 if (global) { |
| 204 this.lastIndex = lastMatchInfo[CAPTURE1]; | 203 this.lastIndex = lastMatchInfo[CAPTURE1]; |
| 205 } | 204 } |
| 206 return BuildResultFromMatchInfo(matchIndices, string); | 205 return BuildResultFromMatchInfo(matchIndices, s); |
| 207 } | 206 } |
| 208 | 207 |
| 209 | 208 |
| 210 // One-element cache for the simplified test regexp. | 209 // One-element cache for the simplified test regexp. |
| 211 var regexp_key; | 210 var regexp_key; |
| 212 var regexp_val; | 211 var regexp_val; |
| 213 | 212 |
| 214 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be | 213 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be |
| 215 // that test is defined in terms of String.prototype.exec. However, it probably | 214 // that test is defined in terms of String.prototype.exec. However, it probably |
| 216 // means the original value of String.prototype.exec, which is what everybody | 215 // means the original value of String.prototype.exec, which is what everybody |
| 217 // else implements. | 216 // else implements. |
| 218 function RegExpTest(string) { | 217 function RegExpTest(string) { |
| 219 if (!IS_REGEXP(this)) { | 218 if (!IS_REGEXP(this)) { |
| 220 throw MakeTypeError('incompatible_method_receiver', | 219 throw MakeTypeError('incompatible_method_receiver', |
| 221 ['RegExp.prototype.test', this]); | 220 ['RegExp.prototype.test', this]); |
| 222 } | 221 } |
| 223 if (%_ArgumentsLength() == 0) { | 222 if (%_ArgumentsLength() == 0) { |
| 224 var regExpInput = LAST_INPUT(lastMatchInfo); | 223 var regExpInput = LAST_INPUT(lastMatchInfo); |
| 225 if (IS_UNDEFINED(regExpInput)) { | 224 if (IS_UNDEFINED(regExpInput)) { |
| 226 throw MakeError('no_input_to_regexp', [this]); | 225 throw MakeError('no_input_to_regexp', [this]); |
| 227 } | 226 } |
| 228 string = regExpInput; | 227 string = regExpInput; |
| 229 } | 228 } |
| 230 | 229 |
| 231 string = TO_STRING_INLINE(string); | 230 var s; |
| 231 if (IS_STRING(string)) { |
| 232 s = string; |
| 233 } else { |
| 234 s = ToString(string); |
| 235 } |
| 232 | 236 |
| 233 var lastIndex = this.lastIndex; | 237 var lastIndex = this.lastIndex; |
| 234 | 238 |
| 235 // Conversion is required by the ES5 specification (RegExp.prototype.exec | 239 // Conversion is required by the ES5 specification (RegExp.prototype.exec |
| 236 // algorithm, step 5) even if the value is discarded for non-global RegExps. | 240 // algorithm, step 5) even if the value is discarded for non-global RegExps. |
| 237 var i = TO_INTEGER(lastIndex); | 241 var i = TO_INTEGER(lastIndex); |
| 238 | 242 |
| 239 if (this.global) { | 243 if (this.global) { |
| 240 if (i < 0 || i > string.length) { | 244 if (i < 0 || i > s.length) { |
| 241 this.lastIndex = 0; | 245 this.lastIndex = 0; |
| 242 return false; | 246 return false; |
| 243 } | 247 } |
| 244 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]); | 248 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
| 245 // matchIndices is either null or the lastMatchInfo array. | 249 // matchIndices is either null or the lastMatchInfo array. |
| 246 var matchIndices = %_RegExpExec(this, string, i, lastMatchInfo); | 250 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); |
| 247 if (matchIndices === null) { | 251 if (matchIndices === null) { |
| 248 this.lastIndex = 0; | 252 this.lastIndex = 0; |
| 249 return false; | 253 return false; |
| 250 } | 254 } |
| 251 lastMatchInfoOverride = null; | 255 lastMatchInfoOverride = null; |
| 252 this.lastIndex = lastMatchInfo[CAPTURE1]; | 256 this.lastIndex = lastMatchInfo[CAPTURE1]; |
| 253 return true; | 257 return true; |
| 254 } else { | 258 } else { |
| 255 // Non-global regexp. | 259 // Non-global regexp. |
| 256 // Remove irrelevant preceeding '.*' in a non-global test regexp. | 260 // Remove irrelevant preceeding '.*' in a non-global test regexp. |
| 257 // The expression checks whether this.source starts with '.*' and | 261 // The expression checks whether this.source starts with '.*' and |
| 258 // that the third char is not a '?'. | 262 // that the third char is not a '?'. |
| 259 if (%_StringCharCodeAt(this.source, 0) == 46 && // '.' | 263 if (%_StringCharCodeAt(this.source, 0) == 46 && // '.' |
| 260 %_StringCharCodeAt(this.source, 1) == 42 && // '*' | 264 %_StringCharCodeAt(this.source, 1) == 42 && // '*' |
| 261 %_StringCharCodeAt(this.source, 2) != 63) { // '?' | 265 %_StringCharCodeAt(this.source, 2) != 63) { // '?' |
| 262 if (!%_ObjectEquals(regexp_key, this)) { | 266 if (!%_ObjectEquals(regexp_key, this)) { |
| 263 regexp_key = this; | 267 regexp_key = this; |
| 264 regexp_val = new $RegExp(this.source.substring(2, this.source.length), | 268 regexp_val = new $RegExp(this.source.substring(2, this.source.length), |
| 265 (this.ignoreCase ? 'i' : '') | 269 (this.ignoreCase ? 'i' : '') |
| 266 + (this.multiline ? 'm' : '')); | 270 + (this.multiline ? 'm' : '')); |
| 267 } | 271 } |
| 268 if (!regexp_val.test(string)) return false; | 272 if (!regexp_val.test(s)) return false; |
| 269 } | 273 } |
| 270 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]); | 274 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
| 271 // matchIndices is either null or the lastMatchInfo array. | 275 // matchIndices is either null or the lastMatchInfo array. |
| 272 var matchIndices = %_RegExpExec(this, string, 0, lastMatchInfo); | 276 var matchIndices = %_RegExpExec(this, s, 0, lastMatchInfo); |
| 273 if (matchIndices === null) return false; | 277 if (matchIndices === null) return false; |
| 274 lastMatchInfoOverride = null; | 278 lastMatchInfoOverride = null; |
| 275 return true; | 279 return true; |
| 276 } | 280 } |
| 277 } | 281 } |
| 278 | 282 |
| 279 | 283 |
| 280 function RegExpToString() { | 284 function RegExpToString() { |
| 281 // If this.source is an empty string, output /(?:)/. | 285 // If this.source is an empty string, output /(?:)/. |
| 282 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 | 286 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); | 475 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
| 472 | 476 |
| 473 for (var i = 1; i < 10; ++i) { | 477 for (var i = 1; i < 10; ++i) { |
| 474 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D
ELETE); | 478 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D
ELETE); |
| 475 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); | 479 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); |
| 476 } | 480 } |
| 477 } | 481 } |
| 478 | 482 |
| 479 | 483 |
| 480 SetupRegExp(); | 484 SetupRegExp(); |
| OLD | NEW |