| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function(global, utils) { | 5 (function(global, utils) { |
| 6 | 6 |
| 7 %CheckIsBootstrapping(); | 7 %CheckIsBootstrapping(); |
| 8 | 8 |
| 9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
| 10 // Imports | 10 // Imports |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 if (REGEXP_STICKY(regexp)) regexp.lastIndex = matchInfo[CAPTURE1]; | 171 if (REGEXP_STICKY(regexp)) regexp.lastIndex = matchInfo[CAPTURE1]; |
| 172 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string); | 172 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string); |
| 173 } | 173 } |
| 174 regexp.lastIndex = 0; | 174 regexp.lastIndex = 0; |
| 175 return null; | 175 return null; |
| 176 } | 176 } |
| 177 | 177 |
| 178 | 178 |
| 179 // ES#sec-regexp.prototype.exec | 179 // ES#sec-regexp.prototype.exec |
| 180 // RegExp.prototype.exec ( string ) | 180 // RegExp.prototype.exec ( string ) |
| 181 function RegExpSubclassExecJS(string) { | 181 function RegExpExecJS(string) { |
| 182 if (!IS_REGEXP(this)) { | 182 if (!IS_REGEXP(this)) { |
| 183 throw %make_type_error(kIncompatibleMethodReceiver, | 183 throw %make_type_error(kIncompatibleMethodReceiver, |
| 184 'RegExp.prototype.exec', this); | 184 'RegExp.prototype.exec', this); |
| 185 } | 185 } |
| 186 | 186 |
| 187 string = TO_STRING(string); | 187 string = TO_STRING(string); |
| 188 var lastIndex = this.lastIndex; | 188 var lastIndex = this.lastIndex; |
| 189 | 189 |
| 190 // Conversion is required by the ES2015 specification (RegExpBuiltinExec | 190 // Conversion is required by the ES2015 specification (RegExpBuiltinExec |
| 191 // algorithm, step 4) even if the value is discarded for non-global RegExps. | 191 // algorithm, step 4) even if the value is discarded for non-global RegExps. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 210 this.lastIndex = 0; | 210 this.lastIndex = 0; |
| 211 return null; | 211 return null; |
| 212 } | 212 } |
| 213 | 213 |
| 214 // Successful match. | 214 // Successful match. |
| 215 if (updateLastIndex) { | 215 if (updateLastIndex) { |
| 216 this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; | 216 this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; |
| 217 } | 217 } |
| 218 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); | 218 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); |
| 219 } | 219 } |
| 220 %FunctionRemovePrototype(RegExpSubclassExecJS); | 220 %FunctionRemovePrototype(RegExpExecJS); |
| 221 | |
| 222 | |
| 223 // Legacy implementation of RegExp.prototype.exec | |
| 224 function RegExpExecJS(string) { | |
| 225 if (!IS_REGEXP(this)) { | |
| 226 throw %make_type_error(kIncompatibleMethodReceiver, | |
| 227 'RegExp.prototype.exec', this); | |
| 228 } | |
| 229 | |
| 230 string = TO_STRING(string); | |
| 231 var lastIndex = this.lastIndex; | |
| 232 | |
| 233 // Conversion is required by the ES2015 specification (RegExpBuiltinExec | |
| 234 // algorithm, step 4) even if the value is discarded for non-global RegExps. | |
| 235 var i = TO_LENGTH(lastIndex); | |
| 236 | |
| 237 var updateLastIndex = REGEXP_GLOBAL(this) || REGEXP_STICKY(this); | |
| 238 if (updateLastIndex) { | |
| 239 if (i < 0 || i > string.length) { | |
| 240 this.lastIndex = 0; | |
| 241 return null; | |
| 242 } | |
| 243 } else { | |
| 244 i = 0; | |
| 245 } | |
| 246 | |
| 247 // matchIndices is either null or the RegExpLastMatchInfo array. | |
| 248 var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); | |
| 249 | |
| 250 if (IS_NULL(matchIndices)) { | |
| 251 this.lastIndex = 0; | |
| 252 return null; | |
| 253 } | |
| 254 | |
| 255 // Successful match. | |
| 256 if (updateLastIndex) { | |
| 257 this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; | |
| 258 } | |
| 259 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); | |
| 260 } | |
| 261 | 221 |
| 262 | 222 |
| 263 // ES#sec-regexpexec Runtime Semantics: RegExpExec ( R, S ) | 223 // ES#sec-regexpexec Runtime Semantics: RegExpExec ( R, S ) |
| 264 // Also takes an optional exec method in case our caller | 224 // Also takes an optional exec method in case our caller |
| 265 // has already fetched exec. | 225 // has already fetched exec. |
| 266 function RegExpSubclassExec(regexp, string, exec) { | 226 function RegExpSubclassExec(regexp, string, exec) { |
| 267 if (IS_UNDEFINED(exec)) { | 227 if (IS_UNDEFINED(exec)) { |
| 268 exec = regexp.exec; | 228 exec = regexp.exec; |
| 269 } | 229 } |
| 270 if (IS_CALLABLE(exec)) { | 230 if (IS_CALLABLE(exec)) { |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 } | 427 } |
| 468 string = TO_STRING(string); | 428 string = TO_STRING(string); |
| 469 var constructor = SpeciesConstructor(this, GlobalRegExp); | 429 var constructor = SpeciesConstructor(this, GlobalRegExp); |
| 470 var flags = TO_STRING(this.flags); | 430 var flags = TO_STRING(this.flags); |
| 471 | 431 |
| 472 // TODO(adamk): this fast path is wrong as we doesn't ensure that 'exec' | 432 // TODO(adamk): this fast path is wrong as we doesn't ensure that 'exec' |
| 473 // is actually a data property on RegExp.prototype. | 433 // is actually a data property on RegExp.prototype. |
| 474 var exec; | 434 var exec; |
| 475 if (IS_REGEXP(this) && constructor === GlobalRegExp) { | 435 if (IS_REGEXP(this) && constructor === GlobalRegExp) { |
| 476 exec = this.exec; | 436 exec = this.exec; |
| 477 if (exec === RegExpSubclassExecJS) { | 437 if (exec === RegExpExecJS) { |
| 478 return %_Call(RegExpSplit, this, string, limit); | 438 return %_Call(RegExpSplit, this, string, limit); |
| 479 } | 439 } |
| 480 } | 440 } |
| 481 | 441 |
| 482 var unicode = %StringIndexOf(flags, 'u', 0) >= 0; | 442 var unicode = %StringIndexOf(flags, 'u', 0) >= 0; |
| 483 var sticky = %StringIndexOf(flags, 'y', 0) >= 0; | 443 var sticky = %StringIndexOf(flags, 'y', 0) >= 0; |
| 484 var newFlags = sticky ? flags : flags + "y"; | 444 var newFlags = sticky ? flags : flags + "y"; |
| 485 var splitter = new constructor(this, newFlags); | 445 var splitter = new constructor(this, newFlags); |
| 486 var array = new GlobalArray(); | 446 var array = new GlobalArray(); |
| 487 var arrayIndex = 0; | 447 var arrayIndex = 0; |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 860 if (global) { | 820 if (global) { |
| 861 var unicode = TO_BOOLEAN(this.unicode); | 821 var unicode = TO_BOOLEAN(this.unicode); |
| 862 this.lastIndex = 0; | 822 this.lastIndex = 0; |
| 863 } | 823 } |
| 864 | 824 |
| 865 // TODO(adamk): this fast path is wrong as we doesn't ensure that 'exec' | 825 // TODO(adamk): this fast path is wrong as we doesn't ensure that 'exec' |
| 866 // is actually a data property on RegExp.prototype. | 826 // is actually a data property on RegExp.prototype. |
| 867 var exec; | 827 var exec; |
| 868 if (IS_REGEXP(this)) { | 828 if (IS_REGEXP(this)) { |
| 869 exec = this.exec; | 829 exec = this.exec; |
| 870 if (exec === RegExpSubclassExecJS) { | 830 if (exec === RegExpExecJS) { |
| 871 return %_Call(RegExpReplace, this, string, replace); | 831 return %_Call(RegExpReplace, this, string, replace); |
| 872 } | 832 } |
| 873 } | 833 } |
| 874 | 834 |
| 875 var results = new InternalArray(); | 835 var results = new InternalArray(); |
| 876 var result, replacement; | 836 var result, replacement; |
| 877 while (true) { | 837 while (true) { |
| 878 result = RegExpSubclassExec(this, string, exec); | 838 result = RegExpSubclassExec(this, string, exec); |
| 879 // Ensure exec will be read again on the next loop through. | 839 // Ensure exec will be read again on the next loop through. |
| 880 exec = UNDEFINED; | 840 exec = UNDEFINED; |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1112 %FunctionSetInstanceClassName(GlobalRegExp, 'RegExp'); | 1072 %FunctionSetInstanceClassName(GlobalRegExp, 'RegExp'); |
| 1113 GlobalRegExpPrototype = new GlobalObject(); | 1073 GlobalRegExpPrototype = new GlobalObject(); |
| 1114 %FunctionSetPrototype(GlobalRegExp, GlobalRegExpPrototype); | 1074 %FunctionSetPrototype(GlobalRegExp, GlobalRegExpPrototype); |
| 1115 %AddNamedProperty( | 1075 %AddNamedProperty( |
| 1116 GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM); | 1076 GlobalRegExp.prototype, 'constructor', GlobalRegExp, DONT_ENUM); |
| 1117 %SetCode(GlobalRegExp, RegExpConstructor); | 1077 %SetCode(GlobalRegExp, RegExpConstructor); |
| 1118 | 1078 |
| 1119 utils.InstallGetter(GlobalRegExp, speciesSymbol, RegExpSpecies); | 1079 utils.InstallGetter(GlobalRegExp, speciesSymbol, RegExpSpecies); |
| 1120 | 1080 |
| 1121 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ | 1081 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ |
| 1122 "exec", RegExpSubclassExecJS, | 1082 "exec", RegExpExecJS, |
| 1123 "test", RegExpSubclassTest, | 1083 "test", RegExpSubclassTest, |
| 1124 "toString", RegExpToString, | 1084 "toString", RegExpToString, |
| 1125 "compile", RegExpCompileJS, | 1085 "compile", RegExpCompileJS, |
| 1126 matchSymbol, RegExpSubclassMatch, | 1086 matchSymbol, RegExpSubclassMatch, |
| 1127 replaceSymbol, RegExpSubclassReplace, | 1087 replaceSymbol, RegExpSubclassReplace, |
| 1128 searchSymbol, RegExpSubclassSearch, | 1088 searchSymbol, RegExpSubclassSearch, |
| 1129 splitSymbol, RegExpSubclassSplit, | 1089 splitSymbol, RegExpSubclassSplit, |
| 1130 ]); | 1090 ]); |
| 1131 | 1091 |
| 1132 utils.InstallGetter(GlobalRegExp.prototype, 'flags', RegExpGetFlags); | 1092 utils.InstallGetter(GlobalRegExp.prototype, 'flags', RegExpGetFlags); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1222 to.InternalRegExpMatch = InternalRegExpMatch; | 1182 to.InternalRegExpMatch = InternalRegExpMatch; |
| 1223 to.InternalRegExpReplace = InternalRegExpReplace; | 1183 to.InternalRegExpReplace = InternalRegExpReplace; |
| 1224 to.IsRegExp = IsRegExp; | 1184 to.IsRegExp = IsRegExp; |
| 1225 to.RegExpExec = DoRegExpExec; | 1185 to.RegExpExec = DoRegExpExec; |
| 1226 to.RegExpInitialize = RegExpInitialize; | 1186 to.RegExpInitialize = RegExpInitialize; |
| 1227 to.RegExpLastMatchInfo = RegExpLastMatchInfo; | 1187 to.RegExpLastMatchInfo = RegExpLastMatchInfo; |
| 1228 to.RegExpTest = RegExpTest; | 1188 to.RegExpTest = RegExpTest; |
| 1229 }); | 1189 }); |
| 1230 | 1190 |
| 1231 }) | 1191 }) |
| OLD | NEW |