Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | (function(global, utils) { |
| 6 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 7 | 'use strict'; |
| 8 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | %CheckIsBootstrapping(); |
| 10 | |
| 11 | // ------------------------------------------------------------------- |
| 12 | // Imports |
| 13 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 14 | var AddIndexedProperty; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15 | var ExpandReplacement; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 16 | var GlobalArray = global.Array; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | var GlobalObject = global.Object; |
| 18 | var GlobalRegExp = global.RegExp; |
| 19 | var GlobalRegExpPrototype; |
| 20 | var InternalArray = utils.InternalArray; |
| 21 | var InternalPackedArray = utils.InternalPackedArray; |
| 22 | var MakeTypeError; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 23 | var MaxSimple; |
| 24 | var MinSimple; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 25 | var matchSymbol = utils.ImportNow("match_symbol"); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 26 | var replaceSymbol = utils.ImportNow("replace_symbol"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 27 | var searchSymbol = utils.ImportNow("search_symbol"); |
| 28 | var splitSymbol = utils.ImportNow("split_symbol"); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 29 | var SpeciesConstructor; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | utils.Import(function(from) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 32 | AddIndexedProperty = from.AddIndexedProperty; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 33 | ExpandReplacement = from.ExpandReplacement; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | MakeTypeError = from.MakeTypeError; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 35 | MaxSimple = from.MaxSimple; |
| 36 | MinSimple = from.MinSimple; |
| 37 | SpeciesConstructor = from.SpeciesConstructor; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 38 | }); |
| 39 | |
| 40 | // ------------------------------------------------------------------- |
| 41 | |
| 42 | // Property of the builtins object for recording the result of the last |
| 43 | // regexp match. The property RegExpLastMatchInfo includes the matchIndices |
| 44 | // array of the last successful regexp match (an array of start/end index |
| 45 | // pairs for the match and all the captured substrings), the invariant is |
| 46 | // that there are at least two capture indeces. The array also contains |
| 47 | // the subject string for the last successful match. |
| 48 | var RegExpLastMatchInfo = new InternalPackedArray( |
| 49 | 2, // REGEXP_NUMBER_OF_CAPTURES |
| 50 | "", // Last subject. |
| 51 | UNDEFINED, // Last input - settable with RegExpSetInput. |
| 52 | 0, // REGEXP_FIRST_CAPTURE + 0 |
| 53 | 0 // REGEXP_FIRST_CAPTURE + 1 |
| 54 | ); |
| 55 | |
| 56 | // ------------------------------------------------------------------- |
| 57 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 58 | // ES#sec-isregexp IsRegExp ( argument ) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 59 | function IsRegExp(o) { |
| 60 | if (!IS_RECEIVER(o)) return false; |
| 61 | var is_regexp = o[matchSymbol]; |
| 62 | if (!IS_UNDEFINED(is_regexp)) return TO_BOOLEAN(is_regexp); |
| 63 | return IS_REGEXP(o); |
| 64 | } |
| 65 | |
| 66 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 67 | // ES#sec-regexpinitialize |
| 68 | // Runtime Semantics: RegExpInitialize ( obj, pattern, flags ) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | function RegExpInitialize(object, pattern, flags) { |
| 70 | pattern = IS_UNDEFINED(pattern) ? '' : TO_STRING(pattern); |
| 71 | flags = IS_UNDEFINED(flags) ? '' : TO_STRING(flags); |
| 72 | %RegExpInitializeAndCompile(object, pattern, flags); |
| 73 | return object; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | function PatternFlags(pattern) { |
| 78 | return (REGEXP_GLOBAL(pattern) ? 'g' : '') + |
| 79 | (REGEXP_IGNORE_CASE(pattern) ? 'i' : '') + |
| 80 | (REGEXP_MULTILINE(pattern) ? 'm' : '') + |
| 81 | (REGEXP_UNICODE(pattern) ? 'u' : '') + |
| 82 | (REGEXP_STICKY(pattern) ? 'y' : ''); |
| 83 | } |
| 84 | |
| 85 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 86 | // ES#sec-regexp-pattern-flags |
| 87 | // RegExp ( pattern, flags ) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | function RegExpConstructor(pattern, flags) { |
| 89 | var newtarget = new.target; |
| 90 | var pattern_is_regexp = IsRegExp(pattern); |
| 91 | |
| 92 | if (IS_UNDEFINED(newtarget)) { |
| 93 | newtarget = GlobalRegExp; |
| 94 | |
| 95 | // ES6 section 21.2.3.1 step 3.b |
| 96 | if (pattern_is_regexp && IS_UNDEFINED(flags) && |
| 97 | pattern.constructor === newtarget) { |
| 98 | return pattern; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (IS_REGEXP(pattern)) { |
| 103 | if (IS_UNDEFINED(flags)) flags = PatternFlags(pattern); |
| 104 | pattern = REGEXP_SOURCE(pattern); |
| 105 | |
| 106 | } else if (pattern_is_regexp) { |
| 107 | var input_pattern = pattern; |
| 108 | pattern = pattern.source; |
| 109 | if (IS_UNDEFINED(flags)) flags = input_pattern.flags; |
| 110 | } |
| 111 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 112 | var object = %_NewObject(GlobalRegExp, newtarget); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | return RegExpInitialize(object, pattern, flags); |
| 114 | } |
| 115 | |
| 116 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 117 | // ES#sec-regexp.prototype.compile RegExp.prototype.compile (pattern, flags) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | function RegExpCompileJS(pattern, flags) { |
| 119 | if (!IS_REGEXP(this)) { |
| 120 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 121 | "RegExp.prototype.compile", this); |
| 122 | } |
| 123 | |
| 124 | if (IS_REGEXP(pattern)) { |
| 125 | if (!IS_UNDEFINED(flags)) throw MakeTypeError(kRegExpFlags); |
| 126 | |
| 127 | flags = PatternFlags(pattern); |
| 128 | pattern = REGEXP_SOURCE(pattern); |
| 129 | } |
| 130 | |
| 131 | RegExpInitialize(this, pattern, flags); |
| 132 | |
| 133 | // Return undefined for compatibility with JSC. |
| 134 | // See http://crbug.com/585775 for web compat details. |
| 135 | } |
| 136 | |
| 137 | |
| 138 | function DoRegExpExec(regexp, string, index) { |
| 139 | return %_RegExpExec(regexp, string, index, RegExpLastMatchInfo); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | // This is kind of performance sensitive, so we want to avoid unnecessary |
| 144 | // type checks on inputs. But we also don't want to inline it several times |
| 145 | // manually, so we use a macro :-) |
| 146 | macro RETURN_NEW_RESULT_FROM_MATCH_INFO(MATCHINFO, STRING) |
| 147 | var numResults = NUMBER_OF_CAPTURES(MATCHINFO) >> 1; |
| 148 | var start = MATCHINFO[CAPTURE0]; |
| 149 | var end = MATCHINFO[CAPTURE1]; |
| 150 | // Calculate the substring of the first match before creating the result array |
| 151 | // to avoid an unnecessary write barrier storing the first result. |
| 152 | var first = %_SubString(STRING, start, end); |
| 153 | var result = %_RegExpConstructResult(numResults, start, STRING); |
| 154 | result[0] = first; |
| 155 | if (numResults == 1) return result; |
| 156 | var j = REGEXP_FIRST_CAPTURE + 2; |
| 157 | for (var i = 1; i < numResults; i++) { |
| 158 | start = MATCHINFO[j++]; |
| 159 | if (start != -1) { |
| 160 | end = MATCHINFO[j]; |
| 161 | result[i] = %_SubString(STRING, start, end); |
| 162 | } |
| 163 | j++; |
| 164 | } |
| 165 | return result; |
| 166 | endmacro |
| 167 | |
| 168 | |
| 169 | function RegExpExecNoTests(regexp, string, start) { |
| 170 | // Must be called with RegExp, string and positive integer as arguments. |
| 171 | var matchInfo = %_RegExpExec(regexp, string, start, RegExpLastMatchInfo); |
| 172 | if (matchInfo !== null) { |
| 173 | // ES6 21.2.5.2.2 step 18. |
| 174 | if (REGEXP_STICKY(regexp)) regexp.lastIndex = matchInfo[CAPTURE1]; |
| 175 | RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string); |
| 176 | } |
| 177 | regexp.lastIndex = 0; |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 182 | // ES#sec-regexp.prototype.exec |
| 183 | // RegExp.prototype.exec ( string ) |
| 184 | function RegExpSubclassExecJS(string) { |
| 185 | if (!IS_REGEXP(this)) { |
| 186 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 187 | 'RegExp.prototype.exec', this); |
| 188 | } |
| 189 | |
| 190 | string = TO_STRING(string); |
| 191 | var lastIndex = this.lastIndex; |
| 192 | |
| 193 | // Conversion is required by the ES2015 specification (RegExpBuiltinExec |
| 194 | // algorithm, step 4) even if the value is discarded for non-global RegExps. |
| 195 | var i = TO_LENGTH(lastIndex); |
| 196 | |
| 197 | var global = TO_BOOLEAN(REGEXP_GLOBAL(this)); |
| 198 | var sticky = TO_BOOLEAN(REGEXP_STICKY(this)); |
| 199 | var updateLastIndex = global || sticky; |
| 200 | if (updateLastIndex) { |
| 201 | if (i > string.length) { |
| 202 | this.lastIndex = 0; |
| 203 | return null; |
| 204 | } |
| 205 | } else { |
| 206 | i = 0; |
| 207 | } |
| 208 | |
| 209 | // matchIndices is either null or the RegExpLastMatchInfo array. |
| 210 | // TODO(littledan): Whether a RegExp is sticky is compiled into the RegExp |
| 211 | // itself, but ES2015 allows monkey-patching this property to differ from |
| 212 | // the internal flags. If it differs, recompile a different RegExp? |
| 213 | var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); |
| 214 | |
| 215 | if (IS_NULL(matchIndices)) { |
| 216 | this.lastIndex = 0; |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | // Successful match. |
| 221 | if (updateLastIndex) { |
| 222 | this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; |
| 223 | } |
| 224 | RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); |
| 225 | } |
| 226 | %FunctionRemovePrototype(RegExpSubclassExecJS); |
| 227 | |
| 228 | |
| 229 | // Legacy implementation of RegExp.prototype.exec |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 230 | function RegExpExecJS(string) { |
| 231 | if (!IS_REGEXP(this)) { |
| 232 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 233 | 'RegExp.prototype.exec', this); |
| 234 | } |
| 235 | |
| 236 | string = TO_STRING(string); |
| 237 | var lastIndex = this.lastIndex; |
| 238 | |
| 239 | // Conversion is required by the ES2015 specification (RegExpBuiltinExec |
| 240 | // algorithm, step 4) even if the value is discarded for non-global RegExps. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 241 | var i = TO_LENGTH(lastIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 242 | |
| 243 | var updateLastIndex = REGEXP_GLOBAL(this) || REGEXP_STICKY(this); |
| 244 | if (updateLastIndex) { |
| 245 | if (i < 0 || i > string.length) { |
| 246 | this.lastIndex = 0; |
| 247 | return null; |
| 248 | } |
| 249 | } else { |
| 250 | i = 0; |
| 251 | } |
| 252 | |
| 253 | // matchIndices is either null or the RegExpLastMatchInfo array. |
| 254 | var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); |
| 255 | |
| 256 | if (IS_NULL(matchIndices)) { |
| 257 | this.lastIndex = 0; |
| 258 | return null; |
| 259 | } |
| 260 | |
| 261 | // Successful match. |
| 262 | if (updateLastIndex) { |
| 263 | this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; |
| 264 | } |
| 265 | RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); |
| 266 | } |
| 267 | |
| 268 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 269 | // ES#sec-regexpexec Runtime Semantics: RegExpExec ( R, S ) |
| 270 | // Also takes an optional exec method in case our caller |
| 271 | // has already fetched exec. |
| 272 | function RegExpSubclassExec(regexp, string, exec) { |
| 273 | if (IS_UNDEFINED(exec)) { |
| 274 | exec = regexp.exec; |
| 275 | } |
| 276 | if (IS_CALLABLE(exec)) { |
| 277 | var result = %_Call(exec, regexp, string); |
| 278 | if (!IS_RECEIVER(result) && !IS_NULL(result)) { |
| 279 | throw MakeTypeError(kInvalidRegExpExecResult); |
| 280 | } |
| 281 | return result; |
| 282 | } |
| 283 | return %_Call(RegExpExecJS, regexp, string); |
| 284 | } |
| 285 | %SetForceInlineFlag(RegExpSubclassExec); |
| 286 | |
| 287 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 288 | // One-element cache for the simplified test regexp. |
| 289 | var regexp_key; |
| 290 | var regexp_val; |
| 291 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 292 | // Legacy implementation of RegExp.prototype.test |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 293 | // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be |
| 294 | // that test is defined in terms of String.prototype.exec. However, it probably |
| 295 | // means the original value of String.prototype.exec, which is what everybody |
| 296 | // else implements. |
| 297 | function RegExpTest(string) { |
| 298 | if (!IS_REGEXP(this)) { |
| 299 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 300 | 'RegExp.prototype.test', this); |
| 301 | } |
| 302 | string = TO_STRING(string); |
| 303 | |
| 304 | var lastIndex = this.lastIndex; |
| 305 | |
| 306 | // Conversion is required by the ES2015 specification (RegExpBuiltinExec |
| 307 | // algorithm, step 4) even if the value is discarded for non-global RegExps. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 308 | var i = TO_LENGTH(lastIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | |
| 310 | if (REGEXP_GLOBAL(this) || REGEXP_STICKY(this)) { |
| 311 | if (i < 0 || i > string.length) { |
| 312 | this.lastIndex = 0; |
| 313 | return false; |
| 314 | } |
| 315 | // matchIndices is either null or the RegExpLastMatchInfo array. |
| 316 | var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); |
| 317 | if (IS_NULL(matchIndices)) { |
| 318 | this.lastIndex = 0; |
| 319 | return false; |
| 320 | } |
| 321 | this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; |
| 322 | return true; |
| 323 | } else { |
| 324 | // Non-global, non-sticky regexp. |
| 325 | // Remove irrelevant preceeding '.*' in a test regexp. The expression |
| 326 | // checks whether this.source starts with '.*' and that the third char is |
| 327 | // not a '?'. But see https://code.google.com/p/v8/issues/detail?id=3560 |
| 328 | var regexp = this; |
| 329 | var source = REGEXP_SOURCE(regexp); |
| 330 | if (regexp.length >= 3 && |
| 331 | %_StringCharCodeAt(regexp, 0) == 46 && // '.' |
| 332 | %_StringCharCodeAt(regexp, 1) == 42 && // '*' |
| 333 | %_StringCharCodeAt(regexp, 2) != 63) { // '?' |
| 334 | regexp = TrimRegExp(regexp); |
| 335 | } |
| 336 | // matchIndices is either null or the RegExpLastMatchInfo array. |
| 337 | var matchIndices = %_RegExpExec(regexp, string, 0, RegExpLastMatchInfo); |
| 338 | if (IS_NULL(matchIndices)) { |
| 339 | this.lastIndex = 0; |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | } |
| 345 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 346 | |
| 347 | // ES#sec-regexp.prototype.test RegExp.prototype.test ( S ) |
| 348 | function RegExpSubclassTest(string) { |
| 349 | if (!IS_RECEIVER(this)) { |
| 350 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 351 | 'RegExp.prototype.test', this); |
| 352 | } |
| 353 | string = TO_STRING(string); |
| 354 | var match = RegExpSubclassExec(this, string); |
| 355 | return !IS_NULL(match); |
| 356 | } |
| 357 | %FunctionRemovePrototype(RegExpSubclassTest); |
| 358 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 359 | function TrimRegExp(regexp) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 360 | if (regexp_key !== regexp) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 361 | regexp_key = regexp; |
| 362 | regexp_val = |
| 363 | new GlobalRegExp( |
| 364 | %_SubString(REGEXP_SOURCE(regexp), 2, REGEXP_SOURCE(regexp).length), |
| 365 | (REGEXP_IGNORE_CASE(regexp) ? REGEXP_MULTILINE(regexp) ? "im" : "i" |
| 366 | : REGEXP_MULTILINE(regexp) ? "m" : "")); |
| 367 | } |
| 368 | return regexp_val; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | function RegExpToString() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 373 | if (!IS_RECEIVER(this)) { |
| 374 | throw MakeTypeError( |
| 375 | kIncompatibleMethodReceiver, 'RegExp.prototype.toString', this); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 376 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 377 | if (this === GlobalRegExpPrototype) { |
| 378 | %IncrementUseCounter(kRegExpPrototypeToString); |
| 379 | } |
| 380 | return '/' + TO_STRING(this.source) + '/' + TO_STRING(this.flags); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 384 | function AtSurrogatePair(subject, index) { |
| 385 | if (index + 1 >= subject.length) return false; |
| 386 | var first = %_StringCharCodeAt(subject, index); |
| 387 | if (first < 0xD800 || first > 0xDBFF) return false; |
| 388 | var second = %_StringCharCodeAt(subject, index + 1); |
| 389 | return second >= 0xDC00 || second <= 0xDFFF; |
| 390 | } |
| 391 | |
| 392 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 393 | // Legacy implementation of RegExp.prototype[Symbol.split] which |
| 394 | // doesn't properly call the underlying exec, @@species methods |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 395 | function RegExpSplit(string, limit) { |
| 396 | // TODO(yangguo): allow non-regexp receivers. |
| 397 | if (!IS_REGEXP(this)) { |
| 398 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 399 | "RegExp.prototype.@@split", this); |
| 400 | } |
| 401 | var separator = this; |
| 402 | var subject = TO_STRING(string); |
| 403 | |
| 404 | limit = (IS_UNDEFINED(limit)) ? kMaxUint32 : TO_UINT32(limit); |
| 405 | var length = subject.length; |
| 406 | |
| 407 | if (limit === 0) return []; |
| 408 | |
| 409 | if (length === 0) { |
| 410 | if (DoRegExpExec(separator, subject, 0, 0) !== null) return []; |
| 411 | return [subject]; |
| 412 | } |
| 413 | |
| 414 | var currentIndex = 0; |
| 415 | var startIndex = 0; |
| 416 | var startMatch = 0; |
| 417 | var result = new InternalArray(); |
| 418 | |
| 419 | outer_loop: |
| 420 | while (true) { |
| 421 | if (startIndex === length) { |
| 422 | result[result.length] = %_SubString(subject, currentIndex, length); |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | var matchInfo = DoRegExpExec(separator, subject, startIndex); |
| 427 | if (matchInfo === null || length === (startMatch = matchInfo[CAPTURE0])) { |
| 428 | result[result.length] = %_SubString(subject, currentIndex, length); |
| 429 | break; |
| 430 | } |
| 431 | var endIndex = matchInfo[CAPTURE1]; |
| 432 | |
| 433 | // We ignore a zero-length match at the currentIndex. |
| 434 | if (startIndex === endIndex && endIndex === currentIndex) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 435 | if (REGEXP_UNICODE(this) && AtSurrogatePair(subject, startIndex)) { |
| 436 | startIndex += 2; |
| 437 | } else { |
| 438 | startIndex++; |
| 439 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 440 | continue; |
| 441 | } |
| 442 | |
| 443 | result[result.length] = %_SubString(subject, currentIndex, startMatch); |
| 444 | |
| 445 | if (result.length === limit) break; |
| 446 | |
| 447 | var matchinfo_len = NUMBER_OF_CAPTURES(matchInfo) + REGEXP_FIRST_CAPTURE; |
| 448 | for (var i = REGEXP_FIRST_CAPTURE + 2; i < matchinfo_len; ) { |
| 449 | var start = matchInfo[i++]; |
| 450 | var end = matchInfo[i++]; |
| 451 | if (end != -1) { |
| 452 | result[result.length] = %_SubString(subject, start, end); |
| 453 | } else { |
| 454 | result[result.length] = UNDEFINED; |
| 455 | } |
| 456 | if (result.length === limit) break outer_loop; |
| 457 | } |
| 458 | |
| 459 | startIndex = currentIndex = endIndex; |
| 460 | } |
| 461 | |
| 462 | var array_result = []; |
| 463 | %MoveArrayContents(result, array_result); |
| 464 | return array_result; |
| 465 | } |
| 466 | |
| 467 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 468 | // ES#sec-regexp.prototype-@@split |
| 469 | // RegExp.prototype [ @@split ] ( string, limit ) |
| 470 | function RegExpSubclassSplit(string, limit) { |
| 471 | if (!IS_RECEIVER(this)) { |
| 472 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 473 | "RegExp.prototype.@@split", this); |
| 474 | } |
| 475 | string = TO_STRING(string); |
| 476 | var constructor = SpeciesConstructor(this, GlobalRegExp); |
| 477 | var flags = TO_STRING(this.flags); |
| 478 | |
| 479 | // TODO(adamk): this fast path is wrong with respect to this.global |
| 480 | // and this.sticky, but hopefully the spec will remove those gets |
| 481 | // and thus make the assumption of 'exec' having no side-effects |
| 482 | // more correct. Also, we doesn't ensure that 'exec' is actually |
| 483 | // a data property on RegExp.prototype. |
| 484 | var exec; |
| 485 | if (IS_REGEXP(this) && constructor === GlobalRegExp) { |
| 486 | exec = this.exec; |
| 487 | if (exec === RegExpSubclassExecJS) { |
| 488 | return %_Call(RegExpSplit, this, string, limit); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | var unicode = %StringIndexOf(flags, 'u', 0) >= 0; |
| 493 | var sticky = %StringIndexOf(flags, 'y', 0) >= 0; |
| 494 | var newFlags = sticky ? flags : flags + "y"; |
| 495 | var splitter = new constructor(this, newFlags); |
| 496 | var array = new GlobalArray(); |
| 497 | var arrayIndex = 0; |
| 498 | var lim = (IS_UNDEFINED(limit)) ? kMaxUint32 : TO_UINT32(limit); |
| 499 | var size = string.length; |
| 500 | var prevStringIndex = 0; |
| 501 | if (lim === 0) return array; |
| 502 | var result; |
| 503 | if (size === 0) { |
| 504 | result = RegExpSubclassExec(splitter, string); |
| 505 | if (IS_NULL(result)) AddIndexedProperty(array, 0, string); |
| 506 | return array; |
| 507 | } |
| 508 | var stringIndex = prevStringIndex; |
| 509 | while (stringIndex < size) { |
| 510 | splitter.lastIndex = stringIndex; |
| 511 | result = RegExpSubclassExec(splitter, string, exec); |
| 512 | // Ensure exec will be read again on the next loop through. |
| 513 | exec = UNDEFINED; |
| 514 | if (IS_NULL(result)) { |
| 515 | stringIndex += AdvanceStringIndex(string, stringIndex, unicode); |
| 516 | } else { |
| 517 | var end = MinSimple(TO_LENGTH(splitter.lastIndex), size); |
| 518 | if (end === stringIndex) { |
| 519 | stringIndex += AdvanceStringIndex(string, stringIndex, unicode); |
| 520 | } else { |
| 521 | AddIndexedProperty( |
| 522 | array, arrayIndex, |
| 523 | %_SubString(string, prevStringIndex, stringIndex)); |
| 524 | arrayIndex++; |
| 525 | if (arrayIndex === lim) return array; |
| 526 | prevStringIndex = end; |
| 527 | var numberOfCaptures = MaxSimple(TO_LENGTH(result.length), 0); |
| 528 | for (var i = 1; i < numberOfCaptures; i++) { |
| 529 | AddIndexedProperty(array, arrayIndex, result[i]); |
| 530 | arrayIndex++; |
| 531 | if (arrayIndex === lim) return array; |
| 532 | } |
| 533 | stringIndex = prevStringIndex; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | AddIndexedProperty(array, arrayIndex, |
| 538 | %_SubString(string, prevStringIndex, size)); |
| 539 | return array; |
| 540 | } |
| 541 | %FunctionRemovePrototype(RegExpSubclassSplit); |
| 542 | |
| 543 | |
| 544 | // Legacy implementation of RegExp.prototype[Symbol.match] which |
| 545 | // doesn't properly call the underlying exec method |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 546 | function RegExpMatch(string) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 547 | if (!IS_REGEXP(this)) { |
| 548 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 549 | "RegExp.prototype.@@match", this); |
| 550 | } |
| 551 | var subject = TO_STRING(string); |
| 552 | |
| 553 | if (!REGEXP_GLOBAL(this)) return RegExpExecNoTests(this, subject, 0); |
| 554 | this.lastIndex = 0; |
| 555 | var result = %StringMatch(subject, this, RegExpLastMatchInfo); |
| 556 | return result; |
| 557 | } |
| 558 | |
| 559 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 560 | // ES#sec-regexp.prototype-@@match |
| 561 | // RegExp.prototype [ @@match ] ( string ) |
| 562 | function RegExpSubclassMatch(string) { |
| 563 | if (!IS_RECEIVER(this)) { |
| 564 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 565 | "RegExp.prototype.@@match", this); |
| 566 | } |
| 567 | string = TO_STRING(string); |
| 568 | var global = this.global; |
| 569 | if (!global) return RegExpSubclassExec(this, string); |
| 570 | var unicode = this.unicode; |
| 571 | this.lastIndex = 0; |
| 572 | var array = new InternalArray(); |
| 573 | var n = 0; |
| 574 | var result; |
| 575 | while (true) { |
| 576 | result = RegExpSubclassExec(this, string); |
| 577 | if (IS_NULL(result)) { |
| 578 | if (n === 0) return null; |
| 579 | break; |
| 580 | } |
| 581 | var matchStr = TO_STRING(result[0]); |
| 582 | array[n] = matchStr; |
| 583 | if (matchStr === "") SetAdvancedStringIndex(this, string, unicode); |
| 584 | n++; |
| 585 | } |
| 586 | var resultArray = []; |
| 587 | %MoveArrayContents(array, resultArray); |
| 588 | return resultArray; |
| 589 | } |
| 590 | %FunctionRemovePrototype(RegExpSubclassMatch); |
| 591 | |
| 592 | |
| 593 | // Legacy implementation of RegExp.prototype[Symbol.replace] which |
| 594 | // doesn't properly call the underlying exec method. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 595 | |
| 596 | // TODO(lrn): This array will survive indefinitely if replace is never |
| 597 | // called again. However, it will be empty, since the contents are cleared |
| 598 | // in the finally block. |
| 599 | var reusableReplaceArray = new InternalArray(4); |
| 600 | |
| 601 | // Helper function for replacing regular expressions with the result of a |
| 602 | // function application in String.prototype.replace. |
| 603 | function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) { |
| 604 | var resultArray = reusableReplaceArray; |
| 605 | if (resultArray) { |
| 606 | reusableReplaceArray = null; |
| 607 | } else { |
| 608 | // Inside a nested replace (replace called from the replacement function |
| 609 | // of another replace) or we have failed to set the reusable array |
| 610 | // back due to an exception in a replacement function. Create a new |
| 611 | // array to use in the future, or until the original is written back. |
| 612 | resultArray = new InternalArray(16); |
| 613 | } |
| 614 | var res = %RegExpExecMultiple(regexp, |
| 615 | subject, |
| 616 | RegExpLastMatchInfo, |
| 617 | resultArray); |
| 618 | regexp.lastIndex = 0; |
| 619 | if (IS_NULL(res)) { |
| 620 | // No matches at all. |
| 621 | reusableReplaceArray = resultArray; |
| 622 | return subject; |
| 623 | } |
| 624 | var len = res.length; |
| 625 | if (NUMBER_OF_CAPTURES(RegExpLastMatchInfo) == 2) { |
| 626 | // If the number of captures is two then there are no explicit captures in |
| 627 | // the regexp, just the implicit capture that captures the whole match. In |
| 628 | // this case we can simplify quite a bit and end up with something faster. |
| 629 | // The builder will consist of some integers that indicate slices of the |
| 630 | // input string and some replacements that were returned from the replace |
| 631 | // function. |
| 632 | var match_start = 0; |
| 633 | for (var i = 0; i < len; i++) { |
| 634 | var elem = res[i]; |
| 635 | if (%_IsSmi(elem)) { |
| 636 | // Integers represent slices of the original string. |
| 637 | if (elem > 0) { |
| 638 | match_start = (elem >> 11) + (elem & 0x7ff); |
| 639 | } else { |
| 640 | match_start = res[++i] - elem; |
| 641 | } |
| 642 | } else { |
| 643 | var func_result = replace(elem, match_start, subject); |
| 644 | // Overwrite the i'th element in the results with the string we got |
| 645 | // back from the callback function. |
| 646 | res[i] = TO_STRING(func_result); |
| 647 | match_start += elem.length; |
| 648 | } |
| 649 | } |
| 650 | } else { |
| 651 | for (var i = 0; i < len; i++) { |
| 652 | var elem = res[i]; |
| 653 | if (!%_IsSmi(elem)) { |
| 654 | // elem must be an Array. |
| 655 | // Use the apply argument as backing for global RegExp properties. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 656 | var func_result = %reflect_apply(replace, UNDEFINED, elem); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 657 | // Overwrite the i'th element in the results with the string we got |
| 658 | // back from the callback function. |
| 659 | res[i] = TO_STRING(func_result); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | var result = %StringBuilderConcat(res, len, subject); |
| 664 | resultArray.length = 0; |
| 665 | reusableReplaceArray = resultArray; |
| 666 | return result; |
| 667 | } |
| 668 | |
| 669 | |
| 670 | // Compute the string of a given regular expression capture. |
| 671 | function CaptureString(string, lastCaptureInfo, index) { |
| 672 | // Scale the index. |
| 673 | var scaled = index << 1; |
| 674 | // Compute start and end. |
| 675 | var start = lastCaptureInfo[CAPTURE(scaled)]; |
| 676 | // If start isn't valid, return undefined. |
| 677 | if (start < 0) return; |
| 678 | var end = lastCaptureInfo[CAPTURE(scaled + 1)]; |
| 679 | return %_SubString(string, start, end); |
| 680 | } |
| 681 | |
| 682 | |
| 683 | function StringReplaceNonGlobalRegExpWithFunction(subject, regexp, replace) { |
| 684 | var matchInfo = DoRegExpExec(regexp, subject, 0); |
| 685 | if (IS_NULL(matchInfo)) { |
| 686 | regexp.lastIndex = 0; |
| 687 | return subject; |
| 688 | } |
| 689 | var index = matchInfo[CAPTURE0]; |
| 690 | var result = %_SubString(subject, 0, index); |
| 691 | var endOfMatch = matchInfo[CAPTURE1]; |
| 692 | // Compute the parameter list consisting of the match, captures, index, |
| 693 | // and subject for the replace function invocation. |
| 694 | // The number of captures plus one for the match. |
| 695 | var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; |
| 696 | var replacement; |
| 697 | if (m == 1) { |
| 698 | // No captures, only the match, which is always valid. |
| 699 | var s = %_SubString(subject, index, endOfMatch); |
| 700 | // Don't call directly to avoid exposing the built-in global object. |
| 701 | replacement = replace(s, index, subject); |
| 702 | } else { |
| 703 | var parameters = new InternalArray(m + 2); |
| 704 | for (var j = 0; j < m; j++) { |
| 705 | parameters[j] = CaptureString(subject, matchInfo, j); |
| 706 | } |
| 707 | parameters[j] = index; |
| 708 | parameters[j + 1] = subject; |
| 709 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 710 | replacement = %reflect_apply(replace, UNDEFINED, parameters); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | result += replacement; // The add method converts to string if necessary. |
| 714 | // Can't use matchInfo any more from here, since the function could |
| 715 | // overwrite it. |
| 716 | return result + %_SubString(subject, endOfMatch, subject.length); |
| 717 | } |
| 718 | |
| 719 | |
| 720 | function RegExpReplace(string, replace) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 721 | if (!IS_REGEXP(this)) { |
| 722 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 723 | "RegExp.prototype.@@replace", this); |
| 724 | } |
| 725 | var subject = TO_STRING(string); |
| 726 | var search = this; |
| 727 | |
| 728 | if (!IS_CALLABLE(replace)) { |
| 729 | replace = TO_STRING(replace); |
| 730 | |
| 731 | if (!REGEXP_GLOBAL(search)) { |
| 732 | // Non-global regexp search, string replace. |
| 733 | var match = DoRegExpExec(search, subject, 0); |
| 734 | if (match == null) { |
| 735 | search.lastIndex = 0 |
| 736 | return subject; |
| 737 | } |
| 738 | if (replace.length == 0) { |
| 739 | return %_SubString(subject, 0, match[CAPTURE0]) + |
| 740 | %_SubString(subject, match[CAPTURE1], subject.length) |
| 741 | } |
| 742 | return ExpandReplacement(replace, subject, RegExpLastMatchInfo, |
| 743 | %_SubString(subject, 0, match[CAPTURE0])) + |
| 744 | %_SubString(subject, match[CAPTURE1], subject.length); |
| 745 | } |
| 746 | |
| 747 | // Global regexp search, string replace. |
| 748 | search.lastIndex = 0; |
| 749 | return %StringReplaceGlobalRegExpWithString( |
| 750 | subject, search, replace, RegExpLastMatchInfo); |
| 751 | } |
| 752 | |
| 753 | if (REGEXP_GLOBAL(search)) { |
| 754 | // Global regexp search, function replace. |
| 755 | return StringReplaceGlobalRegExpWithFunction(subject, search, replace); |
| 756 | } |
| 757 | // Non-global regexp search, function replace. |
| 758 | return StringReplaceNonGlobalRegExpWithFunction(subject, search, replace); |
| 759 | } |
| 760 | |
| 761 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 762 | // ES#sec-getsubstitution |
| 763 | // GetSubstitution(matched, str, position, captures, replacement) |
| 764 | // Expand the $-expressions in the string and return a new string with |
| 765 | // the result. |
| 766 | // TODO(littledan): Call this function from String.prototype.replace instead |
| 767 | // of the very similar ExpandReplacement in src/js/string.js |
| 768 | function GetSubstitution(matched, string, position, captures, replacement) { |
| 769 | var matchLength = matched.length; |
| 770 | var stringLength = string.length; |
| 771 | var capturesLength = captures.length; |
| 772 | var tailPos = position + matchLength; |
| 773 | var result = ""; |
| 774 | var pos, expansion, peek, next, scaledIndex, advance, newScaledIndex; |
| 775 | |
| 776 | var next = %StringIndexOf(replacement, '$', 0); |
| 777 | if (next < 0) { |
| 778 | result += replacement; |
| 779 | return result; |
| 780 | } |
| 781 | |
| 782 | if (next > 0) result += %_SubString(replacement, 0, next); |
| 783 | |
| 784 | while (true) { |
| 785 | expansion = '$'; |
| 786 | pos = next + 1; |
| 787 | if (pos < replacement.length) { |
| 788 | peek = %_StringCharCodeAt(replacement, pos); |
| 789 | if (peek == 36) { // $$ |
| 790 | ++pos; |
| 791 | result += '$'; |
| 792 | } else if (peek == 38) { // $& - match |
| 793 | ++pos; |
| 794 | result += matched; |
| 795 | } else if (peek == 96) { // $` - prefix |
| 796 | ++pos; |
| 797 | result += %_SubString(string, 0, position); |
| 798 | } else if (peek == 39) { // $' - suffix |
| 799 | ++pos; |
| 800 | result += %_SubString(string, tailPos, stringLength); |
| 801 | } else if (peek >= 48 && peek <= 57) { |
| 802 | // Valid indices are $1 .. $9, $01 .. $09 and $10 .. $99 |
| 803 | scaledIndex = (peek - 48); |
| 804 | advance = 1; |
| 805 | if (pos + 1 < replacement.length) { |
| 806 | next = %_StringCharCodeAt(replacement, pos + 1); |
| 807 | if (next >= 48 && next <= 57) { |
| 808 | newScaledIndex = scaledIndex * 10 + ((next - 48)); |
| 809 | if (newScaledIndex < capturesLength) { |
| 810 | scaledIndex = newScaledIndex; |
| 811 | advance = 2; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | if (scaledIndex != 0 && scaledIndex < capturesLength) { |
| 816 | var capture = captures[scaledIndex]; |
| 817 | if (!IS_UNDEFINED(capture)) result += capture; |
| 818 | pos += advance; |
| 819 | } else { |
| 820 | result += '$'; |
| 821 | } |
| 822 | } else { |
| 823 | result += '$'; |
| 824 | } |
| 825 | } else { |
| 826 | result += '$'; |
| 827 | } |
| 828 | |
| 829 | // Go the the next $ in the replacement. |
| 830 | next = %StringIndexOf(replacement, '$', pos); |
| 831 | |
| 832 | // Return if there are no more $ characters in the replacement. If we |
| 833 | // haven't reached the end, we need to append the suffix. |
| 834 | if (next < 0) { |
| 835 | if (pos < replacement.length) { |
| 836 | result += %_SubString(replacement, pos, replacement.length); |
| 837 | } |
| 838 | return result; |
| 839 | } |
| 840 | |
| 841 | // Append substring between the previous and the next $ character. |
| 842 | if (next > pos) { |
| 843 | result += %_SubString(replacement, pos, next); |
| 844 | } |
| 845 | } |
| 846 | return result; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | // ES#sec-advancestringindex |
| 851 | // AdvanceStringIndex ( S, index, unicode ) |
| 852 | function AdvanceStringIndex(string, index, unicode) { |
| 853 | var increment = 1; |
| 854 | if (unicode) { |
| 855 | var first = %_StringCharCodeAt(string, index); |
| 856 | if (first >= 0xD800 && first <= 0xDBFF && string.length > index + 1) { |
| 857 | var second = %_StringCharCodeAt(string, index + 1); |
| 858 | if (second >= 0xDC00 && second <= 0xDFFF) { |
| 859 | increment = 2; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | return increment; |
| 864 | } |
| 865 | |
| 866 | |
| 867 | function SetAdvancedStringIndex(regexp, string, unicode) { |
| 868 | var lastIndex = regexp.lastIndex; |
| 869 | regexp.lastIndex = lastIndex + |
| 870 | AdvanceStringIndex(string, lastIndex, unicode); |
| 871 | } |
| 872 | |
| 873 | |
| 874 | // ES#sec-regexp.prototype-@@replace |
| 875 | // RegExp.prototype [ @@replace ] ( string, replaceValue ) |
| 876 | function RegExpSubclassReplace(string, replace) { |
| 877 | if (!IS_RECEIVER(this)) { |
| 878 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 879 | "RegExp.prototype.@@replace", this); |
| 880 | } |
| 881 | string = TO_STRING(string); |
| 882 | var length = string.length; |
| 883 | var functionalReplace = IS_CALLABLE(replace); |
| 884 | if (!functionalReplace) replace = TO_STRING(replace); |
| 885 | var global = TO_BOOLEAN(this.global); |
| 886 | if (global) { |
| 887 | var unicode = TO_BOOLEAN(this.unicode); |
| 888 | this.lastIndex = 0; |
| 889 | } |
| 890 | |
| 891 | // TODO(adamk): this fast path is wrong with respect to this.global |
| 892 | // and this.sticky, but hopefully the spec will remove those gets |
| 893 | // and thus make the assumption of 'exec' having no side-effects |
| 894 | // more correct. Also, we doesn't ensure that 'exec' is actually |
| 895 | // a data property on RegExp.prototype, nor does the fast path |
| 896 | // correctly handle lastIndex setting. |
| 897 | var exec; |
| 898 | if (IS_REGEXP(this)) { |
| 899 | exec = this.exec; |
| 900 | if (exec === RegExpSubclassExecJS) { |
| 901 | return %_Call(RegExpReplace, this, string, replace); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | var results = new InternalArray(); |
| 906 | var result, replacement; |
| 907 | while (true) { |
| 908 | result = RegExpSubclassExec(this, string, exec); |
| 909 | // Ensure exec will be read again on the next loop through. |
| 910 | exec = UNDEFINED; |
| 911 | if (IS_NULL(result)) { |
| 912 | break; |
| 913 | } else { |
| 914 | results.push(result); |
| 915 | if (!global) break; |
| 916 | var matchStr = TO_STRING(result[0]); |
| 917 | if (matchStr === "") SetAdvancedStringIndex(this, string, unicode); |
| 918 | } |
| 919 | } |
| 920 | var accumulatedResult = ""; |
| 921 | var nextSourcePosition = 0; |
| 922 | for (var i = 0; i < results.length; i++) { |
| 923 | result = results[i]; |
| 924 | var capturesLength = MaxSimple(TO_LENGTH(result.length), 0); |
| 925 | var matched = TO_STRING(result[0]); |
| 926 | var matchedLength = matched.length; |
| 927 | var position = MaxSimple(MinSimple(TO_INTEGER(result.index), length), 0); |
| 928 | var captures = new InternalArray(); |
| 929 | for (var n = 0; n < capturesLength; n++) { |
| 930 | var capture = result[n]; |
| 931 | if (!IS_UNDEFINED(capture)) capture = TO_STRING(capture); |
| 932 | captures[n] = capture; |
| 933 | } |
| 934 | if (functionalReplace) { |
| 935 | var parameters = new InternalArray(capturesLength + 2); |
| 936 | for (var j = 0; j < capturesLength; j++) { |
| 937 | parameters[j] = captures[j]; |
| 938 | } |
| 939 | parameters[j] = position; |
| 940 | parameters[j + 1] = string; |
| 941 | replacement = %reflect_apply(replace, UNDEFINED, parameters, 0, |
| 942 | parameters.length); |
| 943 | } else { |
| 944 | replacement = GetSubstitution(matched, string, position, captures, |
| 945 | replace); |
| 946 | } |
| 947 | if (position >= nextSourcePosition) { |
| 948 | accumulatedResult += |
| 949 | %_SubString(string, nextSourcePosition, position) + replacement; |
| 950 | nextSourcePosition = position + matchedLength; |
| 951 | } |
| 952 | } |
| 953 | if (nextSourcePosition >= length) return accumulatedResult; |
| 954 | return accumulatedResult + %_SubString(string, nextSourcePosition, length); |
| 955 | } |
| 956 | %FunctionRemovePrototype(RegExpSubclassReplace); |
| 957 | |
| 958 | |
| 959 | // Legacy implementation of RegExp.prototype[Symbol.search] which |
| 960 | // doesn't properly use the overridden exec method |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 961 | function RegExpSearch(string) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 962 | if (!IS_REGEXP(this)) { |
| 963 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 964 | "RegExp.prototype.@@search", this); |
| 965 | } |
| 966 | var match = DoRegExpExec(this, TO_STRING(string), 0); |
| 967 | if (match) return match[CAPTURE0]; |
| 968 | return -1; |
| 969 | } |
| 970 | |
| 971 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 972 | // ES#sec-regexp.prototype-@@search |
| 973 | // RegExp.prototype [ @@search ] ( string ) |
| 974 | function RegExpSubclassSearch(string) { |
| 975 | if (!IS_RECEIVER(this)) { |
| 976 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 977 | "RegExp.prototype.@@search", this); |
| 978 | } |
| 979 | string = TO_STRING(string); |
| 980 | var previousLastIndex = this.lastIndex; |
| 981 | this.lastIndex = 0; |
| 982 | var result = RegExpSubclassExec(this, string); |
| 983 | this.lastIndex = previousLastIndex; |
| 984 | if (IS_NULL(result)) return -1; |
| 985 | return result.index; |
| 986 | } |
| 987 | %FunctionRemovePrototype(RegExpSubclassSearch); |
| 988 | |
| 989 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 990 | // Getters for the static properties lastMatch, lastParen, leftContext, and |
| 991 | // rightContext of the RegExp constructor. The properties are computed based |
| 992 | // on the captures array of the last successful match and the subject string |
| 993 | // of the last successful match. |
| 994 | function RegExpGetLastMatch() { |
| 995 | var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo); |
| 996 | return %_SubString(regExpSubject, |
| 997 | RegExpLastMatchInfo[CAPTURE0], |
| 998 | RegExpLastMatchInfo[CAPTURE1]); |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | function RegExpGetLastParen() { |
| 1003 | var length = NUMBER_OF_CAPTURES(RegExpLastMatchInfo); |
| 1004 | if (length <= 2) return ''; // There were no captures. |
| 1005 | // We match the SpiderMonkey behavior: return the substring defined by the |
| 1006 | // last pair (after the first pair) of elements of the capture array even if |
| 1007 | // it is empty. |
| 1008 | var regExpSubject = LAST_SUBJECT(RegExpLastMatchInfo); |
| 1009 | var start = RegExpLastMatchInfo[CAPTURE(length - 2)]; |
| 1010 | var end = RegExpLastMatchInfo[CAPTURE(length - 1)]; |
| 1011 | if (start != -1 && end != -1) { |
| 1012 | return %_SubString(regExpSubject, start, end); |
| 1013 | } |
| 1014 | return ""; |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | function RegExpGetLeftContext() { |
| 1019 | var start_index; |
| 1020 | var subject; |
| 1021 | start_index = RegExpLastMatchInfo[CAPTURE0]; |
| 1022 | subject = LAST_SUBJECT(RegExpLastMatchInfo); |
| 1023 | return %_SubString(subject, 0, start_index); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | function RegExpGetRightContext() { |
| 1028 | var start_index; |
| 1029 | var subject; |
| 1030 | start_index = RegExpLastMatchInfo[CAPTURE1]; |
| 1031 | subject = LAST_SUBJECT(RegExpLastMatchInfo); |
| 1032 | return %_SubString(subject, start_index, subject.length); |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | // The properties $1..$9 are the first nine capturing substrings of the last |
| 1037 | // successful match, or ''. The function RegExpMakeCaptureGetter will be |
| 1038 | // called with indices from 1 to 9. |
| 1039 | function RegExpMakeCaptureGetter(n) { |
| 1040 | return function foo() { |
| 1041 | var index = n * 2; |
| 1042 | if (index >= NUMBER_OF_CAPTURES(RegExpLastMatchInfo)) return ''; |
| 1043 | var matchStart = RegExpLastMatchInfo[CAPTURE(index)]; |
| 1044 | var matchEnd = RegExpLastMatchInfo[CAPTURE(index + 1)]; |
| 1045 | if (matchStart == -1 || matchEnd == -1) return ''; |
| 1046 | return %_SubString(LAST_SUBJECT(RegExpLastMatchInfo), matchStart, matchEnd); |
| 1047 | }; |
| 1048 | } |
| 1049 | |
| 1050 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1051 | // ES6 21.2.5.3. |
| 1052 | function RegExpGetFlags() { |
| 1053 | if (!IS_RECEIVER(this)) { |
| 1054 | throw MakeTypeError( |
| 1055 | kRegExpNonObject, "RegExp.prototype.flags", TO_STRING(this)); |
| 1056 | } |
| 1057 | var result = ''; |
| 1058 | if (this.global) result += 'g'; |
| 1059 | if (this.ignoreCase) result += 'i'; |
| 1060 | if (this.multiline) result += 'm'; |
| 1061 | if (this.unicode) result += 'u'; |
| 1062 | if (this.sticky) result += 'y'; |
| 1063 | return result; |
| 1064 | } |
| 1065 | |
| 1066 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1067 | // ES6 21.2.5.4. |
| 1068 | function RegExpGetGlobal() { |
| 1069 | if (!IS_REGEXP(this)) { |
| 1070 | // TODO(littledan): Remove this RegExp compat workaround |
| 1071 | if (this === GlobalRegExpPrototype) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1072 | %IncrementUseCounter(kRegExpPrototypeOldFlagGetter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1073 | return UNDEFINED; |
| 1074 | } |
| 1075 | throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.global"); |
| 1076 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1077 | return TO_BOOLEAN(REGEXP_GLOBAL(this)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1078 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1079 | %SetForceInlineFlag(RegExpGetGlobal); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1080 | |
| 1081 | |
| 1082 | // ES6 21.2.5.5. |
| 1083 | function RegExpGetIgnoreCase() { |
| 1084 | if (!IS_REGEXP(this)) { |
| 1085 | // TODO(littledan): Remove this RegExp compat workaround |
| 1086 | if (this === GlobalRegExpPrototype) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1087 | %IncrementUseCounter(kRegExpPrototypeOldFlagGetter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1088 | return UNDEFINED; |
| 1089 | } |
| 1090 | throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.ignoreCase"); |
| 1091 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1092 | return TO_BOOLEAN(REGEXP_IGNORE_CASE(this)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1093 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1094 | |
| 1095 | |
| 1096 | // ES6 21.2.5.7. |
| 1097 | function RegExpGetMultiline() { |
| 1098 | if (!IS_REGEXP(this)) { |
| 1099 | // TODO(littledan): Remove this RegExp compat workaround |
| 1100 | if (this === GlobalRegExpPrototype) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1101 | %IncrementUseCounter(kRegExpPrototypeOldFlagGetter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1102 | return UNDEFINED; |
| 1103 | } |
| 1104 | throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.multiline"); |
| 1105 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1106 | return TO_BOOLEAN(REGEXP_MULTILINE(this)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1107 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1108 | |
| 1109 | |
| 1110 | // ES6 21.2.5.10. |
| 1111 | function RegExpGetSource() { |
| 1112 | if (!IS_REGEXP(this)) { |
| 1113 | // TODO(littledan): Remove this RegExp compat workaround |
| 1114 | if (this === GlobalRegExpPrototype) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1115 | %IncrementUseCounter(kRegExpPrototypeSourceGetter); |
| 1116 | return "(?:)"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1117 | } |
| 1118 | throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.source"); |
| 1119 | } |
| 1120 | return REGEXP_SOURCE(this); |
| 1121 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1122 | |
| 1123 | |
| 1124 | // ES6 21.2.5.12. |
| 1125 | function RegExpGetSticky() { |
| 1126 | if (!IS_REGEXP(this)) { |
| 1127 | // Compat fix: RegExp.prototype.sticky == undefined; UseCounter tracks it |
| 1128 | // TODO(littledan): Remove this workaround or standardize it |
| 1129 | if (this === GlobalRegExpPrototype) { |
| 1130 | %IncrementUseCounter(kRegExpPrototypeStickyGetter); |
| 1131 | return UNDEFINED; |
| 1132 | } |
| 1133 | throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.sticky"); |
| 1134 | } |
| 1135 | return TO_BOOLEAN(REGEXP_STICKY(this)); |
| 1136 | } |
| 1137 | %SetForceInlineFlag(RegExpGetSticky); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1138 | |
| 1139 | // ------------------------------------------------------------------- |
| 1140 | |
| 1141 | %FunctionSetInstanceClassName(GlobalRegExp, 'RegExp'); |
| 1142 | GlobalRegExpPrototype = new GlobalObject(); |
| 1143 | %FunctionSetPrototype(GlobalRegExp, GlobalRegExpPrototype); |
| 1144 | %AddNamedProperty( |
| 1145 | GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM); |
| 1146 | %SetCode(GlobalRegExp, RegExpConstructor); |
| 1147 | |
| 1148 | utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ |
| 1149 | "exec", RegExpExecJS, |
| 1150 | "test", RegExpTest, |
| 1151 | "toString", RegExpToString, |
| 1152 | "compile", RegExpCompileJS, |
| 1153 | matchSymbol, RegExpMatch, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1154 | replaceSymbol, RegExpReplace, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1155 | searchSymbol, RegExpSearch, |
| 1156 | splitSymbol, RegExpSplit, |
| 1157 | ]); |
| 1158 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1159 | utils.InstallGetter(GlobalRegExp.prototype, 'flags', RegExpGetFlags); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1160 | utils.InstallGetter(GlobalRegExp.prototype, 'global', RegExpGetGlobal); |
| 1161 | utils.InstallGetter(GlobalRegExp.prototype, 'ignoreCase', RegExpGetIgnoreCase); |
| 1162 | utils.InstallGetter(GlobalRegExp.prototype, 'multiline', RegExpGetMultiline); |
| 1163 | utils.InstallGetter(GlobalRegExp.prototype, 'source', RegExpGetSource); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1164 | utils.InstallGetter(GlobalRegExp.prototype, 'sticky', RegExpGetSticky); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1165 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1166 | // The properties `input` and `$_` are aliases for each other. When this |
| 1167 | // value is set the value it is set to is coerced to a string. |
| 1168 | // Getter and setter for the input. |
| 1169 | var RegExpGetInput = function() { |
| 1170 | var regExpInput = LAST_INPUT(RegExpLastMatchInfo); |
| 1171 | return IS_UNDEFINED(regExpInput) ? "" : regExpInput; |
| 1172 | }; |
| 1173 | var RegExpSetInput = function(string) { |
| 1174 | LAST_INPUT(RegExpLastMatchInfo) = TO_STRING(string); |
| 1175 | }; |
| 1176 | |
| 1177 | %OptimizeObjectForAddingMultipleProperties(GlobalRegExp, 22); |
| 1178 | utils.InstallGetterSetter(GlobalRegExp, 'input', RegExpGetInput, RegExpSetInput, |
| 1179 | DONT_DELETE); |
| 1180 | utils.InstallGetterSetter(GlobalRegExp, '$_', RegExpGetInput, RegExpSetInput, |
| 1181 | DONT_ENUM | DONT_DELETE); |
| 1182 | |
| 1183 | |
| 1184 | var NoOpSetter = function(ignored) {}; |
| 1185 | |
| 1186 | |
| 1187 | // Static properties set by a successful match. |
| 1188 | utils.InstallGetterSetter(GlobalRegExp, 'lastMatch', RegExpGetLastMatch, |
| 1189 | NoOpSetter, DONT_DELETE); |
| 1190 | utils.InstallGetterSetter(GlobalRegExp, '$&', RegExpGetLastMatch, NoOpSetter, |
| 1191 | DONT_ENUM | DONT_DELETE); |
| 1192 | utils.InstallGetterSetter(GlobalRegExp, 'lastParen', RegExpGetLastParen, |
| 1193 | NoOpSetter, DONT_DELETE); |
| 1194 | utils.InstallGetterSetter(GlobalRegExp, '$+', RegExpGetLastParen, NoOpSetter, |
| 1195 | DONT_ENUM | DONT_DELETE); |
| 1196 | utils.InstallGetterSetter(GlobalRegExp, 'leftContext', RegExpGetLeftContext, |
| 1197 | NoOpSetter, DONT_DELETE); |
| 1198 | utils.InstallGetterSetter(GlobalRegExp, '$`', RegExpGetLeftContext, NoOpSetter, |
| 1199 | DONT_ENUM | DONT_DELETE); |
| 1200 | utils.InstallGetterSetter(GlobalRegExp, 'rightContext', RegExpGetRightContext, |
| 1201 | NoOpSetter, DONT_DELETE); |
| 1202 | utils.InstallGetterSetter(GlobalRegExp, "$'", RegExpGetRightContext, NoOpSetter, |
| 1203 | DONT_ENUM | DONT_DELETE); |
| 1204 | |
| 1205 | for (var i = 1; i < 10; ++i) { |
| 1206 | utils.InstallGetterSetter(GlobalRegExp, '$' + i, RegExpMakeCaptureGetter(i), |
| 1207 | NoOpSetter, DONT_DELETE); |
| 1208 | } |
| 1209 | %ToFastProperties(GlobalRegExp); |
| 1210 | |
| 1211 | // ------------------------------------------------------------------- |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1212 | // Internal |
| 1213 | |
| 1214 | var InternalRegExpMatchInfo = new InternalPackedArray(2, "", UNDEFINED, 0, 0); |
| 1215 | |
| 1216 | function InternalRegExpMatch(regexp, subject) { |
| 1217 | var matchInfo = %_RegExpExec(regexp, subject, 0, InternalRegExpMatchInfo); |
| 1218 | if (!IS_NULL(matchInfo)) { |
| 1219 | RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, subject); |
| 1220 | } |
| 1221 | return null; |
| 1222 | } |
| 1223 | |
| 1224 | function InternalRegExpReplace(regexp, subject, replacement) { |
| 1225 | return %StringReplaceGlobalRegExpWithString( |
| 1226 | subject, regexp, replacement, InternalRegExpMatchInfo); |
| 1227 | } |
| 1228 | |
| 1229 | // ------------------------------------------------------------------- |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1230 | // Exports |
| 1231 | |
| 1232 | utils.Export(function(to) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1233 | to.InternalRegExpMatch = InternalRegExpMatch; |
| 1234 | to.InternalRegExpReplace = InternalRegExpReplace; |
| 1235 | to.IsRegExp = IsRegExp; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1236 | to.RegExpExec = DoRegExpExec; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1237 | to.RegExpInitialize = RegExpInitialize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1238 | to.RegExpLastMatchInfo = RegExpLastMatchInfo; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1239 | to.RegExpSubclassExecJS = RegExpSubclassExecJS; |
| 1240 | to.RegExpSubclassMatch = RegExpSubclassMatch; |
| 1241 | to.RegExpSubclassReplace = RegExpSubclassReplace; |
| 1242 | to.RegExpSubclassSearch = RegExpSubclassSearch; |
| 1243 | to.RegExpSubclassSplit = RegExpSubclassSplit; |
| 1244 | to.RegExpSubclassTest = RegExpSubclassTest; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1245 | to.RegExpTest = RegExpTest; |
| 1246 | }); |
| 1247 | |
| 1248 | }) |