kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // Expect $Object = global.Object; |
| 29 | // Expect $Array = global.Array; |
| 30 | |
| 31 | const $RegExp = global.RegExp; |
| 32 | |
| 33 | // A recursive descent parser for Patterns according to the grammar of |
| 34 | // ECMA-262 15.10.1, with deviations noted below. |
| 35 | function DoConstructRegExp(object, pattern, flags, isConstructorCall) { |
| 36 | // RegExp : Called as constructor; see ECMA-262, section 15.10.4. |
| 37 | if (IS_REGEXP(pattern)) { |
| 38 | if (!IS_UNDEFINED(flags)) { |
| 39 | throw MakeTypeError('regexp_flags', []); |
| 40 | } |
| 41 | flags = (pattern.global ? 'g' : '') |
| 42 | + (pattern.ignoreCase ? 'i' : '') |
| 43 | + (pattern.multiline ? 'm' : ''); |
| 44 | pattern = pattern.source; |
| 45 | } |
| 46 | |
| 47 | pattern = IS_UNDEFINED(pattern) ? '' : ToString(pattern); |
| 48 | flags = IS_UNDEFINED(flags) ? '' : ToString(flags); |
| 49 | |
| 50 | var global = false; |
| 51 | var ignoreCase = false; |
| 52 | var multiline = false; |
| 53 | |
| 54 | for (var i = 0; i < flags.length; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 55 | var c = StringCharAt.call(flags, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | switch (c) { |
| 57 | case 'g': |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 58 | // Allow duplicate flags to be consistent with JSC and others. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | global = true; |
| 60 | break; |
| 61 | case 'i': |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | ignoreCase = true; |
| 63 | break; |
| 64 | case 'm': |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | multiline = true; |
| 66 | break; |
| 67 | default: |
| 68 | // Ignore flags that have no meaning to be consistent with |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 69 | // JSC. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 74 | if (!isConstructorCall) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 75 | regExpCache.type = 'none'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 77 | %RegExpInitializeObject(object, pattern, global, ignoreCase, multiline); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | |
| 79 | // Call internal function to compile the pattern. |
| 80 | %RegExpCompile(object, pattern, flags); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 81 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | function RegExpConstructor(pattern, flags) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 85 | if (%_IsConstructCall()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 86 | DoConstructRegExp(this, pattern, flags, true); |
| 87 | } else { |
| 88 | // RegExp : Called as function; see ECMA-262, section 15.10.3.1. |
| 89 | if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) { |
| 90 | return pattern; |
| 91 | } |
| 92 | return new $RegExp(pattern, flags); |
| 93 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 94 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | // Deprecated RegExp.prototype.compile method. We behave like the constructor |
| 98 | // were called again. In SpiderMonkey, this method returns the regexp object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 99 | // In JSC, it returns undefined. For compatibility with JSC, we match their |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | // behavior. |
| 101 | function CompileRegExp(pattern, flags) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 102 | // Both JSC and SpiderMonkey treat a missing pattern argument as the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | // empty subject string, and an actual undefined value passed as the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 104 | // pattern as the string 'undefined'. Note that JSC is inconsistent |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | // here, treating undefined values differently in |
| 106 | // RegExp.prototype.compile and in the constructor, where they are |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 107 | // the empty string. For compatibility with JSC, we match their |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | // behavior. |
| 109 | if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) { |
| 110 | DoConstructRegExp(this, 'undefined', flags, false); |
| 111 | } else { |
| 112 | DoConstructRegExp(this, pattern, flags, false); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | function DoRegExpExec(regexp, string, index) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 118 | return %_RegExpExec(regexp, string, index, lastMatchInfo); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 119 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | |
| 121 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 122 | function RegExpCache() { |
| 123 | this.type = 'none'; |
| 124 | this.regExp = 0; |
| 125 | this.subject = 0; |
| 126 | this.replaceString = 0; |
| 127 | this.lastIndex = 0; |
| 128 | this.answer = 0; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 129 | // answerSaved marks whether the contents of answer is valid for a cache |
| 130 | // hit in RegExpExec, StringMatch and StringSplit. |
| 131 | this.answerSaved = false; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | var regExpCache = new RegExpCache(); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | function CloneRegexpAnswer(array) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 139 | if (array == null) return null; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 140 | var len = array.length; |
| 141 | var answer = new $Array(len); |
| 142 | for (var i = 0; i < len; i++) { |
| 143 | answer[i] = array[i]; |
| 144 | } |
| 145 | answer.index = array.index; |
| 146 | answer.input = array.input; |
| 147 | return answer; |
| 148 | } |
| 149 | |
| 150 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | function RegExpExec(string) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 152 | if (!IS_REGEXP(this)) { |
| 153 | throw MakeTypeError('incompatible_method_receiver', |
| 154 | ['RegExp.prototype.exec', this]); |
| 155 | } |
| 156 | |
| 157 | var cache = regExpCache; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 158 | var saveAnswer = false; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 159 | |
| 160 | if (%_ObjectEquals(cache.type, 'exec') && |
| 161 | %_ObjectEquals(cache.lastIndex, this.lastIndex) && |
| 162 | %_ObjectEquals(cache.regExp, this) && |
| 163 | %_ObjectEquals(cache.subject, string)) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 164 | if (cache.answerSaved) { |
| 165 | return CloneRegexpAnswer(cache.answer); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 166 | } else { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 167 | saveAnswer = true; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 171 | if (%_ArgumentsLength() == 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 172 | var regExpInput = LAST_INPUT(lastMatchInfo); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 173 | if (IS_UNDEFINED(regExpInput)) { |
| 174 | throw MakeError('no_input_to_regexp', [this]); |
| 175 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 176 | string = regExpInput; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 178 | var s; |
| 179 | if (IS_STRING(string)) { |
| 180 | s = string; |
| 181 | } else { |
| 182 | s = ToString(string); |
| 183 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | var lastIndex = this.lastIndex; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 185 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | var i = this.global ? TO_INTEGER(lastIndex) : 0; |
| 187 | |
| 188 | if (i < 0 || i > s.length) { |
| 189 | this.lastIndex = 0; |
| 190 | return null; |
| 191 | } |
| 192 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 193 | %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 194 | // matchIndices is either null or the lastMatchInfo array. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 195 | var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | |
| 197 | if (matchIndices == null) { |
| 198 | if (this.global) this.lastIndex = 0; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 199 | cache.lastIndex = lastIndex; |
| 200 | cache.regExp = this; |
| 201 | cache.subject = s; |
| 202 | cache.answer = matchIndices; // Null. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 203 | cache.answerSaved = true; // Safe since no cloning is needed. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 204 | cache.type = 'exec'; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 205 | return matchIndices; // No match. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | } |
| 207 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 208 | var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 209 | var result; |
| 210 | if (numResults === 1) { |
| 211 | var matchStart = lastMatchInfo[CAPTURE(0)]; |
| 212 | var matchEnd = lastMatchInfo[CAPTURE(1)]; |
| 213 | result = [SubString(s, matchStart, matchEnd)]; |
| 214 | } else { |
| 215 | result = new $Array(numResults); |
| 216 | for (var i = 0; i < numResults; i++) { |
| 217 | var matchStart = lastMatchInfo[CAPTURE(i << 1)]; |
| 218 | var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; |
| 219 | if (matchStart != -1 && matchEnd != -1) { |
| 220 | result[i] = SubString(s, matchStart, matchEnd); |
| 221 | } else { |
| 222 | // Make sure the element is present. Avoid reading the undefined |
| 223 | // property from the global object since this may change. |
| 224 | result[i] = void 0; |
| 225 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 229 | result.index = lastMatchInfo[CAPTURE0]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 230 | result.input = s; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 231 | if (this.global) { |
| 232 | this.lastIndex = lastMatchInfo[CAPTURE1]; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 233 | } else { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 234 | cache.regExp = this; |
| 235 | cache.subject = s; |
| 236 | cache.lastIndex = lastIndex; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 237 | if (saveAnswer) cache.answer = CloneRegexpAnswer(result); |
| 238 | cache.answerSaved = saveAnswer; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 239 | cache.type = 'exec'; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 240 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame^] | 241 | return result; |
| 242 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 243 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | |
| 245 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 246 | // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 247 | // that test is defined in terms of String.prototype.exec. However, it probably |
| 248 | // means the original value of String.prototype.exec, which is what everybody |
| 249 | // else implements. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | function RegExpTest(string) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 251 | if (!IS_REGEXP(this)) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 252 | throw MakeTypeError('incompatible_method_receiver', |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 253 | ['RegExp.prototype.test', this]); |
| 254 | } |
| 255 | if (%_ArgumentsLength() == 0) { |
| 256 | var regExpInput = LAST_INPUT(lastMatchInfo); |
| 257 | if (IS_UNDEFINED(regExpInput)) { |
| 258 | throw MakeError('no_input_to_regexp', [this]); |
| 259 | } |
| 260 | string = regExpInput; |
| 261 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 262 | var s; |
| 263 | if (IS_STRING(string)) { |
| 264 | s = string; |
| 265 | } else { |
| 266 | s = ToString(string); |
| 267 | } |
| 268 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 269 | var lastIndex = this.lastIndex; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 270 | |
| 271 | var cache = regExpCache; |
| 272 | |
| 273 | if (%_ObjectEquals(cache.type, 'test') && |
| 274 | %_ObjectEquals(cache.regExp, this) && |
| 275 | %_ObjectEquals(cache.subject, string) && |
| 276 | %_ObjectEquals(cache.lastIndex, lastIndex)) { |
| 277 | return cache.answer; |
| 278 | } |
| 279 | |
| 280 | var length = s.length; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 281 | var i = this.global ? TO_INTEGER(lastIndex) : 0; |
| 282 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 283 | cache.type = 'test'; |
| 284 | cache.regExp = this; |
| 285 | cache.subject = s; |
| 286 | cache.lastIndex = i; |
| 287 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 288 | if (i < 0 || i > s.length) { |
| 289 | this.lastIndex = 0; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 290 | cache.answer = false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | |
| 294 | %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); |
| 295 | // matchIndices is either null or the lastMatchInfo array. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 296 | var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 297 | |
| 298 | if (matchIndices == null) { |
| 299 | if (this.global) this.lastIndex = 0; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 300 | cache.answer = false; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | |
| 304 | if (this.global) this.lastIndex = lastMatchInfo[CAPTURE1]; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 305 | cache.answer = true; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 306 | return true; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 307 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | function RegExpToString() { |
| 311 | // If this.source is an empty string, output /(?:)/. |
| 312 | // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 |
| 313 | // ecma_2/RegExp/properties-001.js. |
| 314 | var src = this.source ? this.source : '(?:)'; |
| 315 | var result = '/' + src + '/'; |
| 316 | if (this.global) |
| 317 | result += 'g'; |
| 318 | if (this.ignoreCase) |
| 319 | result += 'i'; |
| 320 | if (this.multiline) |
| 321 | result += 'm'; |
| 322 | return result; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 323 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | |
| 325 | |
| 326 | // Getters for the static properties lastMatch, lastParen, leftContext, and |
| 327 | // rightContext of the RegExp constructor. The properties are computed based |
| 328 | // on the captures array of the last successful match and the subject string |
| 329 | // of the last successful match. |
| 330 | function RegExpGetLastMatch() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 331 | if (lastMatchInfoOverride) { return lastMatchInfoOverride[0]; } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 332 | var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
| 333 | return SubString(regExpSubject, |
| 334 | lastMatchInfo[CAPTURE0], |
| 335 | lastMatchInfo[CAPTURE1]); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 336 | } |
| 337 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | |
| 339 | function RegExpGetLastParen() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 340 | if (lastMatchInfoOverride) { |
| 341 | var override = lastMatchInfoOverride; |
| 342 | if (override.length <= 3) return ''; |
| 343 | return override[override.length - 3]; |
| 344 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 345 | var length = NUMBER_OF_CAPTURES(lastMatchInfo); |
| 346 | if (length <= 2) return ''; // There were no captures. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 347 | // We match the SpiderMonkey behavior: return the substring defined by the |
| 348 | // last pair (after the first pair) of elements of the capture array even if |
| 349 | // it is empty. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 350 | var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
| 351 | var start = lastMatchInfo[CAPTURE(length - 2)]; |
| 352 | var end = lastMatchInfo[CAPTURE(length - 1)]; |
| 353 | if (start != -1 && end != -1) { |
| 354 | return SubString(regExpSubject, start, end); |
| 355 | } |
| 356 | return ""; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 357 | } |
| 358 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 359 | |
| 360 | function RegExpGetLeftContext() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 361 | var start_index; |
| 362 | var subject; |
| 363 | if (!lastMatchInfoOverride) { |
| 364 | start_index = lastMatchInfo[CAPTURE0]; |
| 365 | subject = LAST_SUBJECT(lastMatchInfo); |
| 366 | } else { |
| 367 | var override = lastMatchInfoOverride; |
| 368 | start_index = override[override.length - 2]; |
| 369 | subject = override[override.length - 1]; |
| 370 | } |
| 371 | return SubString(subject, 0, start_index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 372 | } |
| 373 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 374 | |
| 375 | function RegExpGetRightContext() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 376 | var start_index; |
| 377 | var subject; |
| 378 | if (!lastMatchInfoOverride) { |
| 379 | start_index = lastMatchInfo[CAPTURE1]; |
| 380 | subject = LAST_SUBJECT(lastMatchInfo); |
| 381 | } else { |
| 382 | var override = lastMatchInfoOverride; |
| 383 | subject = override[override.length - 1]; |
| 384 | start_index = override[override.length - 2] + subject.length; |
| 385 | } |
| 386 | return SubString(subject, start_index, subject.length); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 387 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 388 | |
| 389 | |
| 390 | // The properties $1..$9 are the first nine capturing substrings of the last |
| 391 | // successful match, or ''. The function RegExpMakeCaptureGetter will be |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 392 | // called with indices from 1 to 9. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | function RegExpMakeCaptureGetter(n) { |
| 394 | return function() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 395 | if (lastMatchInfoOverride) { |
| 396 | if (n < lastMatchInfoOverride.length - 2) return lastMatchInfoOverride[n]; |
| 397 | return ''; |
| 398 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | var index = n * 2; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 400 | if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; |
| 401 | var matchStart = lastMatchInfo[CAPTURE(index)]; |
| 402 | var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 403 | if (matchStart == -1 || matchEnd == -1) return ''; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 404 | return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 405 | }; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 406 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 407 | |
| 408 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 409 | // Property of the builtins object for recording the result of the last |
| 410 | // regexp match. The property lastMatchInfo includes the matchIndices |
| 411 | // array of the last successful regexp match (an array of start/end index |
| 412 | // pairs for the match and all the captured substrings), the invariant is |
| 413 | // that there are at least two capture indeces. The array also contains |
| 414 | // the subject string for the last successful match. |
| 415 | var lastMatchInfo = [ |
| 416 | 2, // REGEXP_NUMBER_OF_CAPTURES |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 417 | "", // Last subject. |
| 418 | void 0, // Last input - settable with RegExpSetInput. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 419 | 0, // REGEXP_FIRST_CAPTURE + 0 |
| 420 | 0, // REGEXP_FIRST_CAPTURE + 1 |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 421 | ]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 422 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 423 | // Override last match info with an array of actual substrings. |
| 424 | // Used internally by replace regexp with function. |
| 425 | // The array has the format of an "apply" argument for a replacement |
| 426 | // function. |
| 427 | var lastMatchInfoOverride = null; |
| 428 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 429 | // ------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 430 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 431 | function SetupRegExp() { |
| 432 | %FunctionSetInstanceClassName($RegExp, 'RegExp'); |
| 433 | %FunctionSetPrototype($RegExp, new $Object()); |
| 434 | %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); |
| 435 | %SetCode($RegExp, RegExpConstructor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 436 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 437 | InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( |
| 438 | "exec", RegExpExec, |
| 439 | "test", RegExpTest, |
| 440 | "toString", RegExpToString, |
| 441 | "compile", CompileRegExp |
| 442 | )); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 444 | // The length of compile is 1 in SpiderMonkey. |
| 445 | %FunctionSetLength($RegExp.prototype.compile, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 446 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 447 | // The properties input, $input, and $_ are aliases for each other. When this |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 448 | // value is set the value it is set to is coerced to a string. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 449 | // Getter and setter for the input. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 450 | function RegExpGetInput() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 451 | var regExpInput = LAST_INPUT(lastMatchInfo); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 452 | return IS_UNDEFINED(regExpInput) ? "" : regExpInput; |
| 453 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | function RegExpSetInput(string) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 455 | regExpCache.type = 'none'; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 456 | LAST_INPUT(lastMatchInfo) = ToString(string); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 457 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 458 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 459 | %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); |
| 460 | %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE); |
| 461 | %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE); |
| 462 | %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE); |
| 463 | %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE); |
| 464 | %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 465 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 466 | // The properties multiline and $* are aliases for each other. When this |
| 467 | // value is set in SpiderMonkey, the value it is set to is coerced to a |
| 468 | // boolean. We mimic that behavior with a slight difference: in SpiderMonkey |
| 469 | // the value of the expression 'RegExp.multiline = null' (for instance) is the |
| 470 | // boolean false (ie, the value after coercion), while in V8 it is the value |
| 471 | // null (ie, the value before coercion). |
| 472 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 473 | // Getter and setter for multiline. |
| 474 | var multiline = false; |
| 475 | function RegExpGetMultiline() { return multiline; }; |
| 476 | function RegExpSetMultiline(flag) { multiline = flag ? true : false; }; |
| 477 | |
| 478 | %DefineAccessor($RegExp, 'multiline', GETTER, RegExpGetMultiline, DONT_DELETE); |
| 479 | %DefineAccessor($RegExp, 'multiline', SETTER, RegExpSetMultiline, DONT_DELETE); |
| 480 | %DefineAccessor($RegExp, '$*', GETTER, RegExpGetMultiline, DONT_ENUM | DONT_DELETE); |
| 481 | %DefineAccessor($RegExp, '$*', SETTER, RegExpSetMultiline, DONT_ENUM | DONT_DELETE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 482 | |
| 483 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 484 | function NoOpSetter(ignored) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | |
| 486 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 487 | // Static properties set by a successful match. |
| 488 | %DefineAccessor($RegExp, 'lastMatch', GETTER, RegExpGetLastMatch, DONT_DELETE); |
| 489 | %DefineAccessor($RegExp, 'lastMatch', SETTER, NoOpSetter, DONT_DELETE); |
| 490 | %DefineAccessor($RegExp, '$&', GETTER, RegExpGetLastMatch, DONT_ENUM | DONT_DELETE); |
| 491 | %DefineAccessor($RegExp, '$&', SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
| 492 | %DefineAccessor($RegExp, 'lastParen', GETTER, RegExpGetLastParen, DONT_DELETE); |
| 493 | %DefineAccessor($RegExp, 'lastParen', SETTER, NoOpSetter, DONT_DELETE); |
| 494 | %DefineAccessor($RegExp, '$+', GETTER, RegExpGetLastParen, DONT_ENUM | DONT_DELETE); |
| 495 | %DefineAccessor($RegExp, '$+', SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
| 496 | %DefineAccessor($RegExp, 'leftContext', GETTER, RegExpGetLeftContext, DONT_DELETE); |
| 497 | %DefineAccessor($RegExp, 'leftContext', SETTER, NoOpSetter, DONT_DELETE); |
| 498 | %DefineAccessor($RegExp, '$`', GETTER, RegExpGetLeftContext, DONT_ENUM | DONT_DELETE); |
| 499 | %DefineAccessor($RegExp, '$`', SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
| 500 | %DefineAccessor($RegExp, 'rightContext', GETTER, RegExpGetRightContext, DONT_DELETE); |
| 501 | %DefineAccessor($RegExp, 'rightContext', SETTER, NoOpSetter, DONT_DELETE); |
| 502 | %DefineAccessor($RegExp, "$'", GETTER, RegExpGetRightContext, DONT_ENUM | DONT_DELETE); |
| 503 | %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 505 | for (var i = 1; i < 10; ++i) { |
| 506 | %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_DELETE); |
| 507 | %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); |
| 508 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
| 512 | SetupRegExp(); |