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. |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 35 | function DoConstructRegExp(object, pattern, flags) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | for (var i = 0; i < flags.length; i++) { |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 54 | var c = %_CallFunction(flags, i, StringCharAt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | switch (c) { |
| 56 | case 'g': |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 57 | if (global) { |
| 58 | throw MakeSyntaxError("invalid_regexp_flags", [flags]); |
| 59 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | global = true; |
| 61 | break; |
| 62 | case 'i': |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 63 | if (ignoreCase) { |
| 64 | throw MakeSyntaxError("invalid_regexp_flags", [flags]); |
| 65 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | ignoreCase = true; |
| 67 | break; |
| 68 | case 'm': |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 69 | if (multiline) { |
| 70 | throw MakeSyntaxError("invalid_regexp_flags", [flags]); |
| 71 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | multiline = true; |
| 73 | break; |
| 74 | default: |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 75 | throw MakeSyntaxError("invalid_regexp_flags", [flags]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 79 | %RegExpInitializeObject(object, pattern, global, ignoreCase, multiline); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | |
| 81 | // Call internal function to compile the pattern. |
| 82 | %RegExpCompile(object, pattern, flags); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 83 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | function RegExpConstructor(pattern, flags) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 87 | if (%_IsConstructCall()) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 88 | DoConstructRegExp(this, pattern, flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | } else { |
| 90 | // RegExp : Called as function; see ECMA-262, section 15.10.3.1. |
| 91 | if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) { |
| 92 | return pattern; |
| 93 | } |
| 94 | return new $RegExp(pattern, flags); |
| 95 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 96 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | // Deprecated RegExp.prototype.compile method. We behave like the constructor |
| 100 | // were called again. In SpiderMonkey, this method returns the regexp object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 101 | // 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] | 102 | // behavior. |
| 103 | function CompileRegExp(pattern, flags) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 104 | // Both JSC and SpiderMonkey treat a missing pattern argument as the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | // empty subject string, and an actual undefined value passed as the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 106 | // pattern as the string 'undefined'. Note that JSC is inconsistent |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | // here, treating undefined values differently in |
| 108 | // RegExp.prototype.compile and in the constructor, where they are |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 109 | // the empty string. For compatibility with JSC, we match their |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | // behavior. |
| 111 | if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 112 | DoConstructRegExp(this, 'undefined', flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | } else { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 114 | DoConstructRegExp(this, pattern, flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | function DoRegExpExec(regexp, string, index) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 120 | var result = %_RegExpExec(regexp, string, index, lastMatchInfo); |
| 121 | if (result !== null) lastMatchInfoOverride = null; |
| 122 | return result; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 123 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | |
| 125 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 126 | function BuildResultFromMatchInfo(lastMatchInfo, s) { |
| 127 | var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 128 | var start = lastMatchInfo[CAPTURE0]; |
| 129 | var end = lastMatchInfo[CAPTURE1]; |
| 130 | var result = %_RegExpConstructResult(numResults, start, s); |
| 131 | if (start + 1 == end) { |
| 132 | result[0] = %_StringCharAt(s, start); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 133 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 134 | result[0] = %_SubString(s, start, end); |
| 135 | } |
| 136 | var j = REGEXP_FIRST_CAPTURE + 2; |
| 137 | for (var i = 1; i < numResults; i++) { |
| 138 | start = lastMatchInfo[j++]; |
| 139 | end = lastMatchInfo[j++]; |
| 140 | if (end != -1) { |
| 141 | if (start + 1 == end) { |
| 142 | result[i] = %_StringCharAt(s, start); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 143 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 144 | result[i] = %_SubString(s, start, end); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 145 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 146 | } else { |
| 147 | // Make sure the element is present. Avoid reading the undefined |
| 148 | // property from the global object since this may change. |
| 149 | result[i] = void 0; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | function RegExpExecNoTests(regexp, string, start) { |
| 157 | // Must be called with RegExp, string and positive integer as arguments. |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 158 | var matchInfo = %_RegExpExec(regexp, string, start, lastMatchInfo); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 159 | if (matchInfo !== null) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 160 | lastMatchInfoOverride = null; |
| 161 | return BuildResultFromMatchInfo(matchInfo, string); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 162 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 163 | return null; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | function RegExpExec(string) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 168 | if (!IS_REGEXP(this)) { |
| 169 | throw MakeTypeError('incompatible_method_receiver', |
| 170 | ['RegExp.prototype.exec', this]); |
| 171 | } |
| 172 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 173 | if (%_ArgumentsLength() === 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 174 | var regExpInput = LAST_INPUT(lastMatchInfo); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 175 | if (IS_UNDEFINED(regExpInput)) { |
| 176 | throw MakeError('no_input_to_regexp', [this]); |
| 177 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 178 | string = regExpInput; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 180 | string = TO_STRING_INLINE(string); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 181 | var lastIndex = this.lastIndex; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 182 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 183 | // Conversion is required by the ES5 specification (RegExp.prototype.exec |
| 184 | // algorithm, step 5) even if the value is discarded for non-global RegExps. |
| 185 | var i = TO_INTEGER(lastIndex); |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 186 | |
| 187 | var global = this.global; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 188 | if (global) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 189 | if (i < 0 || i > string.length) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 190 | this.lastIndex = 0; |
| 191 | return null; |
| 192 | } |
| 193 | } else { |
| 194 | i = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 197 | %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 198 | // matchIndices is either null or the lastMatchInfo array. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 199 | var matchIndices = %_RegExpExec(this, string, i, lastMatchInfo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 200 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 201 | if (matchIndices === null) { |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 202 | if (global) this.lastIndex = 0; |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 203 | return null; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 205 | |
| 206 | // Successful match. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 207 | lastMatchInfoOverride = null; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 208 | if (global) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 209 | this.lastIndex = lastMatchInfo[CAPTURE1]; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 210 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 211 | return BuildResultFromMatchInfo(matchIndices, string); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 212 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | |
| 214 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 215 | // One-element cache for the simplified test regexp. |
| 216 | var regexp_key; |
| 217 | var regexp_val; |
| 218 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 219 | // 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] | 220 | // that test is defined in terms of String.prototype.exec. However, it probably |
| 221 | // means the original value of String.prototype.exec, which is what everybody |
| 222 | // else implements. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | function RegExpTest(string) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 224 | if (!IS_REGEXP(this)) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 225 | throw MakeTypeError('incompatible_method_receiver', |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 226 | ['RegExp.prototype.test', this]); |
| 227 | } |
| 228 | if (%_ArgumentsLength() == 0) { |
| 229 | var regExpInput = LAST_INPUT(lastMatchInfo); |
| 230 | if (IS_UNDEFINED(regExpInput)) { |
| 231 | throw MakeError('no_input_to_regexp', [this]); |
| 232 | } |
| 233 | string = regExpInput; |
| 234 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 235 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 236 | string = TO_STRING_INLINE(string); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 237 | |
lrn@chromium.org | fa943b7 | 2010-11-03 08:14:36 +0000 | [diff] [blame] | 238 | var lastIndex = this.lastIndex; |
| 239 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 240 | // Conversion is required by the ES5 specification (RegExp.prototype.exec |
| 241 | // algorithm, step 5) even if the value is discarded for non-global RegExps. |
| 242 | var i = TO_INTEGER(lastIndex); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 243 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 244 | if (this.global) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 245 | if (i < 0 || i > string.length) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 246 | this.lastIndex = 0; |
| 247 | return false; |
| 248 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 249 | %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 250 | // matchIndices is either null or the lastMatchInfo array. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 251 | var matchIndices = %_RegExpExec(this, string, i, lastMatchInfo); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 252 | if (matchIndices === null) { |
| 253 | this.lastIndex = 0; |
| 254 | return false; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 255 | } |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 256 | lastMatchInfoOverride = null; |
| 257 | this.lastIndex = lastMatchInfo[CAPTURE1]; |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 258 | return true; |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 259 | } else { |
| 260 | // Non-global regexp. |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 261 | // Remove irrelevant preceeding '.*' in a non-global test regexp. |
| 262 | // The expression checks whether this.source starts with '.*' and |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 263 | // that the third char is not a '?'. |
| 264 | if (%_StringCharCodeAt(this.source, 0) == 46 && // '.' |
| 265 | %_StringCharCodeAt(this.source, 1) == 42 && // '*' |
| 266 | %_StringCharCodeAt(this.source, 2) != 63) { // '?' |
| 267 | if (!%_ObjectEquals(regexp_key, this)) { |
| 268 | regexp_key = this; |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 269 | regexp_val = new $RegExp(SubString(this.source, 2, this.source.length), |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 270 | (!this.ignoreCase |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 271 | ? !this.multiline ? "" : "m" |
| 272 | : !this.multiline ? "i" : "im")); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 273 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 274 | if (%_RegExpExec(regexp_val, string, 0, lastMatchInfo) === null) { |
| 275 | return false; |
| 276 | } |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 277 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 278 | %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 279 | // matchIndices is either null or the lastMatchInfo array. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 280 | var matchIndices = %_RegExpExec(this, string, 0, lastMatchInfo); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 281 | if (matchIndices === null) return false; |
| 282 | lastMatchInfoOverride = null; |
| 283 | return true; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 284 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 285 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | function RegExpToString() { |
| 289 | // If this.source is an empty string, output /(?:)/. |
| 290 | // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 |
| 291 | // ecma_2/RegExp/properties-001.js. |
| 292 | var src = this.source ? this.source : '(?:)'; |
| 293 | var result = '/' + src + '/'; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 294 | if (this.global) result += 'g'; |
| 295 | if (this.ignoreCase) result += 'i'; |
| 296 | if (this.multiline) result += 'm'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | return result; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 298 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | // Getters for the static properties lastMatch, lastParen, leftContext, and |
| 302 | // rightContext of the RegExp constructor. The properties are computed based |
| 303 | // on the captures array of the last successful match and the subject string |
| 304 | // of the last successful match. |
| 305 | function RegExpGetLastMatch() { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 306 | if (lastMatchInfoOverride !== null) { |
| 307 | return lastMatchInfoOverride[0]; |
| 308 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 309 | var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
| 310 | return SubString(regExpSubject, |
| 311 | lastMatchInfo[CAPTURE0], |
| 312 | lastMatchInfo[CAPTURE1]); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 313 | } |
| 314 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 315 | |
| 316 | function RegExpGetLastParen() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 317 | if (lastMatchInfoOverride) { |
| 318 | var override = lastMatchInfoOverride; |
| 319 | if (override.length <= 3) return ''; |
| 320 | return override[override.length - 3]; |
| 321 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 322 | var length = NUMBER_OF_CAPTURES(lastMatchInfo); |
| 323 | if (length <= 2) return ''; // There were no captures. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | // We match the SpiderMonkey behavior: return the substring defined by the |
| 325 | // last pair (after the first pair) of elements of the capture array even if |
| 326 | // it is empty. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 327 | var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
| 328 | var start = lastMatchInfo[CAPTURE(length - 2)]; |
| 329 | var end = lastMatchInfo[CAPTURE(length - 1)]; |
| 330 | if (start != -1 && end != -1) { |
| 331 | return SubString(regExpSubject, start, end); |
| 332 | } |
| 333 | return ""; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 334 | } |
| 335 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 336 | |
| 337 | function RegExpGetLeftContext() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 338 | var start_index; |
| 339 | var subject; |
| 340 | if (!lastMatchInfoOverride) { |
| 341 | start_index = lastMatchInfo[CAPTURE0]; |
| 342 | subject = LAST_SUBJECT(lastMatchInfo); |
| 343 | } else { |
| 344 | var override = lastMatchInfoOverride; |
| 345 | start_index = override[override.length - 2]; |
| 346 | subject = override[override.length - 1]; |
| 347 | } |
| 348 | return SubString(subject, 0, start_index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 351 | |
| 352 | function RegExpGetRightContext() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 353 | var start_index; |
| 354 | var subject; |
| 355 | if (!lastMatchInfoOverride) { |
| 356 | start_index = lastMatchInfo[CAPTURE1]; |
| 357 | subject = LAST_SUBJECT(lastMatchInfo); |
| 358 | } else { |
| 359 | var override = lastMatchInfoOverride; |
| 360 | subject = override[override.length - 1]; |
| 361 | start_index = override[override.length - 2] + subject.length; |
| 362 | } |
| 363 | return SubString(subject, start_index, subject.length); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 364 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 365 | |
| 366 | |
| 367 | // The properties $1..$9 are the first nine capturing substrings of the last |
| 368 | // successful match, or ''. The function RegExpMakeCaptureGetter will be |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 369 | // called with indices from 1 to 9. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | function RegExpMakeCaptureGetter(n) { |
| 371 | return function() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 372 | if (lastMatchInfoOverride) { |
| 373 | if (n < lastMatchInfoOverride.length - 2) return lastMatchInfoOverride[n]; |
| 374 | return ''; |
| 375 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | var index = n * 2; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 377 | if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; |
| 378 | var matchStart = lastMatchInfo[CAPTURE(index)]; |
| 379 | var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 380 | if (matchStart == -1 || matchEnd == -1) return ''; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 381 | return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 382 | }; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 383 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | |
| 385 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 386 | // Property of the builtins object for recording the result of the last |
| 387 | // regexp match. The property lastMatchInfo includes the matchIndices |
| 388 | // array of the last successful regexp match (an array of start/end index |
| 389 | // pairs for the match and all the captured substrings), the invariant is |
| 390 | // that there are at least two capture indeces. The array also contains |
| 391 | // the subject string for the last successful match. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 392 | var lastMatchInfo = new InternalArray( |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 393 | 2, // REGEXP_NUMBER_OF_CAPTURES |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 394 | "", // Last subject. |
| 395 | void 0, // Last input - settable with RegExpSetInput. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 396 | 0, // REGEXP_FIRST_CAPTURE + 0 |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 397 | 0 // REGEXP_FIRST_CAPTURE + 1 |
| 398 | ); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 400 | // Override last match info with an array of actual substrings. |
| 401 | // Used internally by replace regexp with function. |
| 402 | // The array has the format of an "apply" argument for a replacement |
| 403 | // function. |
| 404 | var lastMatchInfoOverride = null; |
| 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 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 408 | function SetUpRegExp() { |
| 409 | %CheckIsBootstrapping(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 410 | %FunctionSetInstanceClassName($RegExp, 'RegExp'); |
| 411 | %FunctionSetPrototype($RegExp, new $Object()); |
| 412 | %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); |
| 413 | %SetCode($RegExp, RegExpConstructor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 414 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 415 | InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( |
| 416 | "exec", RegExpExec, |
| 417 | "test", RegExpTest, |
| 418 | "toString", RegExpToString, |
| 419 | "compile", CompileRegExp |
| 420 | )); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 422 | // The length of compile is 1 in SpiderMonkey. |
| 423 | %FunctionSetLength($RegExp.prototype.compile, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 424 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 425 | // 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] | 426 | // 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] | 427 | // Getter and setter for the input. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 428 | function RegExpGetInput() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 429 | var regExpInput = LAST_INPUT(lastMatchInfo); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 430 | return IS_UNDEFINED(regExpInput) ? "" : regExpInput; |
| 431 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 432 | function RegExpSetInput(string) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 433 | LAST_INPUT(lastMatchInfo) = ToString(string); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 434 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 435 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 436 | %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); |
| 437 | %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE); |
| 438 | %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE); |
| 439 | %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE); |
| 440 | %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE); |
| 441 | %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 442 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 443 | // The properties multiline and $* are aliases for each other. When this |
| 444 | // value is set in SpiderMonkey, the value it is set to is coerced to a |
| 445 | // boolean. We mimic that behavior with a slight difference: in SpiderMonkey |
| 446 | // the value of the expression 'RegExp.multiline = null' (for instance) is the |
| 447 | // boolean false (ie, the value after coercion), while in V8 it is the value |
| 448 | // null (ie, the value before coercion). |
| 449 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 450 | // Getter and setter for multiline. |
| 451 | var multiline = false; |
| 452 | function RegExpGetMultiline() { return multiline; }; |
| 453 | function RegExpSetMultiline(flag) { multiline = flag ? true : false; }; |
| 454 | |
| 455 | %DefineAccessor($RegExp, 'multiline', GETTER, RegExpGetMultiline, DONT_DELETE); |
| 456 | %DefineAccessor($RegExp, 'multiline', SETTER, RegExpSetMultiline, DONT_DELETE); |
| 457 | %DefineAccessor($RegExp, '$*', GETTER, RegExpGetMultiline, DONT_ENUM | DONT_DELETE); |
| 458 | %DefineAccessor($RegExp, '$*', SETTER, RegExpSetMultiline, DONT_ENUM | DONT_DELETE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 459 | |
| 460 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 461 | function NoOpSetter(ignored) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 462 | |
| 463 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 464 | // Static properties set by a successful match. |
| 465 | %DefineAccessor($RegExp, 'lastMatch', GETTER, RegExpGetLastMatch, DONT_DELETE); |
| 466 | %DefineAccessor($RegExp, 'lastMatch', SETTER, NoOpSetter, DONT_DELETE); |
| 467 | %DefineAccessor($RegExp, '$&', GETTER, RegExpGetLastMatch, DONT_ENUM | DONT_DELETE); |
| 468 | %DefineAccessor($RegExp, '$&', SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
| 469 | %DefineAccessor($RegExp, 'lastParen', GETTER, RegExpGetLastParen, DONT_DELETE); |
| 470 | %DefineAccessor($RegExp, 'lastParen', SETTER, NoOpSetter, DONT_DELETE); |
| 471 | %DefineAccessor($RegExp, '$+', GETTER, RegExpGetLastParen, DONT_ENUM | DONT_DELETE); |
| 472 | %DefineAccessor($RegExp, '$+', SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
| 473 | %DefineAccessor($RegExp, 'leftContext', GETTER, RegExpGetLeftContext, DONT_DELETE); |
| 474 | %DefineAccessor($RegExp, 'leftContext', SETTER, NoOpSetter, DONT_DELETE); |
| 475 | %DefineAccessor($RegExp, '$`', GETTER, RegExpGetLeftContext, DONT_ENUM | DONT_DELETE); |
| 476 | %DefineAccessor($RegExp, '$`', SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
| 477 | %DefineAccessor($RegExp, 'rightContext', GETTER, RegExpGetRightContext, DONT_DELETE); |
| 478 | %DefineAccessor($RegExp, 'rightContext', SETTER, NoOpSetter, DONT_DELETE); |
| 479 | %DefineAccessor($RegExp, "$'", GETTER, RegExpGetRightContext, DONT_ENUM | DONT_DELETE); |
| 480 | %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 481 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 482 | for (var i = 1; i < 10; ++i) { |
| 483 | %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_DELETE); |
| 484 | %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); |
| 485 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 486 | } |
| 487 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 488 | SetUpRegExp(); |