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 |
11 | 11 |
12 var ExpandReplacement; | 12 var ExpandReplacement; |
13 var GlobalArray = global.Array; | 13 var GlobalArray = global.Array; |
14 var GlobalObject = global.Object; | 14 var GlobalObject = global.Object; |
15 var GlobalRegExp = global.RegExp; | 15 var GlobalRegExp = global.RegExp; |
16 var InternalArray = utils.InternalArray; | 16 var InternalArray = utils.InternalArray; |
17 var InternalPackedArray = utils.InternalPackedArray; | 17 var InternalPackedArray = utils.InternalPackedArray; |
18 var MaxSimple; | 18 var MaxSimple; |
19 var MinSimple; | 19 var MinSimple; |
20 var lastMatchInfoSymbol = utils.ImportNow("regexp_last_match_info_symbol"); | 20 var lastMatchInfoSymbol = utils.ImportNow("regexp_last_match_info_symbol"); |
21 var matchSymbol = utils.ImportNow("match_symbol"); | 21 var matchSymbol = utils.ImportNow("match_symbol"); |
22 var replaceSymbol = utils.ImportNow("replace_symbol"); | 22 var replaceSymbol = utils.ImportNow("replace_symbol"); |
23 var searchSymbol = utils.ImportNow("search_symbol"); | 23 var searchSymbol = utils.ImportNow("search_symbol"); |
24 var speciesSymbol = utils.ImportNow("species_symbol"); | 24 var speciesSymbol = utils.ImportNow("species_symbol"); |
25 var splitSymbol = utils.ImportNow("split_symbol"); | 25 var splitSymbol = utils.ImportNow("split_symbol"); |
26 var SpeciesConstructor; | 26 var SpeciesConstructor; |
27 var RegExpSubclassExecJS; | |
28 | 27 |
29 utils.Import(function(from) { | 28 utils.Import(function(from) { |
30 ExpandReplacement = from.ExpandReplacement; | 29 ExpandReplacement = from.ExpandReplacement; |
31 MaxSimple = from.MaxSimple; | 30 MaxSimple = from.MaxSimple; |
32 MinSimple = from.MinSimple; | 31 MinSimple = from.MinSimple; |
33 SpeciesConstructor = from.SpeciesConstructor; | 32 SpeciesConstructor = from.SpeciesConstructor; |
34 }); | 33 }); |
35 | 34 |
36 // ------------------------------------------------------------------- | 35 // ------------------------------------------------------------------- |
37 | 36 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 if (matchInfo !== null) { | 108 if (matchInfo !== null) { |
110 // ES6 21.2.5.2.2 step 18. | 109 // ES6 21.2.5.2.2 step 18. |
111 if (REGEXP_STICKY(regexp)) regexp.lastIndex = matchInfo[CAPTURE1]; | 110 if (REGEXP_STICKY(regexp)) regexp.lastIndex = matchInfo[CAPTURE1]; |
112 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string); | 111 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchInfo, string); |
113 } | 112 } |
114 regexp.lastIndex = 0; | 113 regexp.lastIndex = 0; |
115 return null; | 114 return null; |
116 } | 115 } |
117 | 116 |
118 | 117 |
| 118 // ES#sec-regexp.prototype.exec |
| 119 // RegExp.prototype.exec ( string ) |
| 120 function RegExpSubclassExecJS(string) { |
| 121 if (!IS_REGEXP(this)) { |
| 122 throw %make_type_error(kIncompatibleMethodReceiver, |
| 123 'RegExp.prototype.exec', this); |
| 124 } |
| 125 |
| 126 string = TO_STRING(string); |
| 127 var lastIndex = this.lastIndex; |
| 128 |
| 129 // Conversion is required by the ES2015 specification (RegExpBuiltinExec |
| 130 // algorithm, step 4) even if the value is discarded for non-global RegExps. |
| 131 var i = TO_LENGTH(lastIndex); |
| 132 |
| 133 var global = TO_BOOLEAN(REGEXP_GLOBAL(this)); |
| 134 var sticky = TO_BOOLEAN(REGEXP_STICKY(this)); |
| 135 var updateLastIndex = global || sticky; |
| 136 if (updateLastIndex) { |
| 137 if (i > string.length) { |
| 138 this.lastIndex = 0; |
| 139 return null; |
| 140 } |
| 141 } else { |
| 142 i = 0; |
| 143 } |
| 144 |
| 145 // matchIndices is either null or the RegExpLastMatchInfo array. |
| 146 // TODO(littledan): Whether a RegExp is sticky is compiled into the RegExp |
| 147 // itself, but ES2015 allows monkey-patching this property to differ from |
| 148 // the internal flags. If it differs, recompile a different RegExp? |
| 149 var matchIndices = %_RegExpExec(this, string, i, RegExpLastMatchInfo); |
| 150 |
| 151 if (IS_NULL(matchIndices)) { |
| 152 this.lastIndex = 0; |
| 153 return null; |
| 154 } |
| 155 |
| 156 // Successful match. |
| 157 if (updateLastIndex) { |
| 158 this.lastIndex = RegExpLastMatchInfo[CAPTURE1]; |
| 159 } |
| 160 RETURN_NEW_RESULT_FROM_MATCH_INFO(matchIndices, string); |
| 161 } |
| 162 %FunctionRemovePrototype(RegExpSubclassExecJS); |
| 163 |
| 164 |
119 // ES#sec-regexpexec Runtime Semantics: RegExpExec ( R, S ) | 165 // ES#sec-regexpexec Runtime Semantics: RegExpExec ( R, S ) |
120 // Also takes an optional exec method in case our caller | 166 // Also takes an optional exec method in case our caller |
121 // has already fetched exec. | 167 // has already fetched exec. |
122 function RegExpSubclassExec(regexp, string, exec) { | 168 function RegExpSubclassExec(regexp, string, exec) { |
123 if (IS_UNDEFINED(exec)) { | 169 if (IS_UNDEFINED(exec)) { |
124 exec = regexp.exec; | 170 exec = regexp.exec; |
125 } | 171 } |
126 if (IS_CALLABLE(exec)) { | 172 if (IS_CALLABLE(exec)) { |
127 var result = %_Call(exec, regexp, string); | 173 var result = %_Call(exec, regexp, string); |
128 if (!IS_RECEIVER(result) && !IS_NULL(result)) { | 174 if (!IS_RECEIVER(result) && !IS_NULL(result)) { |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 this.lastIndex = previousLastIndex; | 767 this.lastIndex = previousLastIndex; |
722 if (IS_NULL(result)) return -1; | 768 if (IS_NULL(result)) return -1; |
723 return result.index; | 769 return result.index; |
724 } | 770 } |
725 %FunctionRemovePrototype(RegExpSubclassSearch); | 771 %FunctionRemovePrototype(RegExpSubclassSearch); |
726 | 772 |
727 | 773 |
728 // ------------------------------------------------------------------- | 774 // ------------------------------------------------------------------- |
729 | 775 |
730 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ | 776 utils.InstallFunctions(GlobalRegExp.prototype, DONT_ENUM, [ |
| 777 "exec", RegExpSubclassExecJS, |
731 "test", RegExpSubclassTest, | 778 "test", RegExpSubclassTest, |
732 matchSymbol, RegExpSubclassMatch, | 779 matchSymbol, RegExpSubclassMatch, |
733 replaceSymbol, RegExpSubclassReplace, | 780 replaceSymbol, RegExpSubclassReplace, |
734 searchSymbol, RegExpSubclassSearch, | 781 searchSymbol, RegExpSubclassSearch, |
735 splitSymbol, RegExpSubclassSplit, | 782 splitSymbol, RegExpSubclassSplit, |
736 ]); | 783 ]); |
737 | 784 |
738 // Temporary until all RegExpLastMatchInfo accesses are ported to C++. | 785 // Temporary until all RegExpLastMatchInfo accesses are ported to C++. |
739 SET_PRIVATE(GlobalRegExp, lastMatchInfoSymbol, RegExpLastMatchInfo); | 786 SET_PRIVATE(GlobalRegExp, lastMatchInfoSymbol, RegExpLastMatchInfo); |
740 | 787 |
741 var RegExpSubclassExecJS = GlobalRegExp.prototype.exec; | |
742 | |
743 // ------------------------------------------------------------------- | 788 // ------------------------------------------------------------------- |
744 // Internal | 789 // Internal |
745 | 790 |
746 var InternalRegExpMatchInfo = { | 791 var InternalRegExpMatchInfo = { |
747 REGEXP_NUMBER_OF_CAPTURES: 2, | 792 REGEXP_NUMBER_OF_CAPTURES: 2, |
748 REGEXP_LAST_SUBJECT: "", | 793 REGEXP_LAST_SUBJECT: "", |
749 REGEXP_LAST_INPUT: UNDEFINED, | 794 REGEXP_LAST_INPUT: UNDEFINED, |
750 CAPTURE0: 0, | 795 CAPTURE0: 0, |
751 CAPTURE1: 0 | 796 CAPTURE1: 0 |
752 }; | 797 }; |
(...skipping 15 matching lines...) Expand all Loading... |
768 // Exports | 813 // Exports |
769 | 814 |
770 utils.Export(function(to) { | 815 utils.Export(function(to) { |
771 to.InternalRegExpMatch = InternalRegExpMatch; | 816 to.InternalRegExpMatch = InternalRegExpMatch; |
772 to.InternalRegExpReplace = InternalRegExpReplace; | 817 to.InternalRegExpReplace = InternalRegExpReplace; |
773 to.IsRegExp = IsRegExp; | 818 to.IsRegExp = IsRegExp; |
774 to.RegExpInitialize = RegExpInitialize; | 819 to.RegExpInitialize = RegExpInitialize; |
775 }); | 820 }); |
776 | 821 |
777 }) | 822 }) |
OLD | NEW |