Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1130)

Side by Side Diff: src/regexp-delay.js

Issue 28184: Avoids allocating a JSArray of capture information on each non-global... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 } 45 }
46 46
47 pattern = IS_UNDEFINED(pattern) ? '' : ToString(pattern); 47 pattern = IS_UNDEFINED(pattern) ? '' : ToString(pattern);
48 flags = IS_UNDEFINED(flags) ? '' : ToString(flags); 48 flags = IS_UNDEFINED(flags) ? '' : ToString(flags);
49 49
50 var global = false; 50 var global = false;
51 var ignoreCase = false; 51 var ignoreCase = false;
52 var multiline = false; 52 var multiline = false;
53 53
54 for (var i = 0; i < flags.length; i++) { 54 for (var i = 0; i < flags.length; i++) {
55 var c = flags.charAt(i); 55 var c = StringCharAt.call(flags, i);
56 switch (c) { 56 switch (c) {
57 case 'g': 57 case 'g':
58 // Allow duplicate flags to be consistent with JSC and others. 58 // Allow duplicate flags to be consistent with JSC and others.
59 global = true; 59 global = true;
60 break; 60 break;
61 case 'i': 61 case 'i':
62 ignoreCase = true; 62 ignoreCase = true;
63 break; 63 break;
64 case 'm': 64 case 'm':
65 multiline = true; 65 multiline = true;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) { 110 if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) {
111 return pattern; 111 return pattern;
112 } 112 }
113 return new $RegExp(pattern, flags); 113 return new $RegExp(pattern, flags);
114 } 114 }
115 } 115 }
116 116
117 117
118 // Deprecated RegExp.prototype.compile method. We behave like the constructor 118 // Deprecated RegExp.prototype.compile method. We behave like the constructor
119 // were called again. In SpiderMonkey, this method returns the regexp object. 119 // were called again. In SpiderMonkey, this method returns the regexp object.
120 // In KJS, it returns undefined. For compatibility with KJS, we match their 120 // In JSC, it returns undefined. For compatibility with JSC, we match their
121 // behavior. 121 // behavior.
122 function CompileRegExp(pattern, flags) { 122 function CompileRegExp(pattern, flags) {
123 // Both KJS and SpiderMonkey treat a missing pattern argument as the 123 // Both JSC and SpiderMonkey treat a missing pattern argument as the
124 // empty subject string, and an actual undefined value passed as the 124 // empty subject string, and an actual undefined value passed as the
125 // patter as the string 'undefined'. Note that KJS is inconsistent 125 // pattern as the string 'undefined'. Note that JSC is inconsistent
126 // here, treating undefined values differently in 126 // here, treating undefined values differently in
127 // RegExp.prototype.compile and in the constructor, where they are 127 // RegExp.prototype.compile and in the constructor, where they are
128 // the empty string. For compatibility with KJS, we match their 128 // the empty string. For compatibility with JSC, we match their
129 // behavior. 129 // behavior.
130 if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) { 130 if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) {
131 DoConstructRegExp(this, 'undefined', flags, false); 131 DoConstructRegExp(this, 'undefined', flags, false);
132 } else { 132 } else {
133 DoConstructRegExp(this, pattern, flags, false); 133 DoConstructRegExp(this, pattern, flags, false);
134 } 134 }
135 } 135 }
136 136
137 137
138 // DoRegExpExec and DoRegExpExecGlobal are wrappers around the runtime
139 // %RegExp and %RegExpGlobal functions that ensure that the static
140 // properties of the RegExp constructor are set.
141 function DoRegExpExec(regexp, string, index) { 138 function DoRegExpExec(regexp, string, index) {
142 var matchIndices = %RegExpExec(regexp, string, index); 139 return %RegExpExec(regexp, string, index, lastMatchInfo);
143 if (!IS_NULL(matchIndices)) {
144 regExpCaptures = matchIndices;
145 regExpSubject = regExpInput = string;
146 }
147 return matchIndices;
148 } 140 }
149 141
150 142
151 function DoRegExpExecGlobal(regexp, string) { 143 function DoRegExpExecGlobal(regexp, string) {
152 // Here, matchIndices is an array of arrays of substring indices. 144 // Returns an array of arrays of substring indices.
153 var matchIndices = %RegExpExecGlobal(regexp, string); 145 return %RegExpExecGlobal(regexp, string, lastMatchInfo);
154 if (matchIndices.length != 0) {
155 regExpCaptures = matchIndices[matchIndices.length - 1];
156 regExpSubject = regExpInput = string;
157 }
158 return matchIndices;
159 } 146 }
160 147
161 148
162 function RegExpExec(string) { 149 function RegExpExec(string) {
163 if (%_ArgumentsLength() == 0) { 150 if (%_ArgumentsLength() == 0) {
151 var regExpInput = LAST_INPUT(lastMatchInfo);
164 if (IS_UNDEFINED(regExpInput)) { 152 if (IS_UNDEFINED(regExpInput)) {
165 throw MakeError('no_input_to_regexp', [this]); 153 throw MakeError('no_input_to_regexp', [this]);
166 } 154 }
167 string = regExpInput; 155 string = regExpInput;
168 } 156 }
169 var s = ToString(string); 157 var s = ToString(string);
170 var length = s.length; 158 var length = s.length;
171 var lastIndex = this.lastIndex; 159 var lastIndex = this.lastIndex;
172 var i = this.global ? TO_INTEGER(lastIndex) : 0; 160 var i = this.global ? TO_INTEGER(lastIndex) : 0;
173 161
174 if (i < 0 || i > s.length) { 162 if (i < 0 || i > s.length) {
175 this.lastIndex = 0; 163 this.lastIndex = 0;
176 return null; 164 return null;
177 } 165 }
178 166
179 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); 167 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]);
180 // matchIndices is an array of integers with length of captures*2, 168 // matchIndices is either null or the lastMatchInfo array.
181 // each pair of integers specified the start and the end of index 169 var matchIndices = %RegExpExec(this, s, i, lastMatchInfo);
182 // in the string.
183 var matchIndices = DoRegExpExec(this, s, i);
184 170
185 if (matchIndices == null) { 171 if (matchIndices == null) {
186 if (this.global) this.lastIndex = 0; 172 if (this.global) this.lastIndex = 0;
187 return matchIndices; // no match 173 return matchIndices; // no match
188 } 174 }
189 175
190 var numResults = matchIndices.length >> 1; 176 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1;
191 var result = new $Array(numResults); 177 var result = new $Array(numResults);
192 for (var i = 0; i < numResults; i++) { 178 for (var i = 0; i < numResults; i++) {
193 var matchStart = matchIndices[2*i]; 179 var matchStart = lastMatchInfo[CAPTURE(i << 1)];
194 var matchEnd = matchIndices[2*i + 1]; 180 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)];
195 if (matchStart != -1 && matchEnd != -1) { 181 if (matchStart != -1 && matchEnd != -1) {
196 result[i] = s.slice(matchStart, matchEnd); 182 result[i] = SubString(s, matchStart, matchEnd);
197 } else { 183 } else {
198 // Make sure the element is present. Avoid reading the undefined 184 // Make sure the element is present. Avoid reading the undefined
199 // property from the global object since this may change. 185 // property from the global object since this may change.
200 result[i] = void 0; 186 result[i] = void 0;
201 } 187 }
202 } 188 }
203 189
204 if (this.global) 190 if (this.global)
205 this.lastIndex = matchIndices[1]; 191 this.lastIndex = lastMatchInfo[CAPTURE1];
206 result.index = matchIndices[0]; 192 result.index = lastMatchInfo[CAPTURE0];
207 result.input = s; 193 result.input = s;
208 return result; 194 return result;
209 } 195 }
210 196
211 197
198 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be
199 // that test is defined in terms of String.prototype.exec even if it changes.
212 function RegExpTest(string) { 200 function RegExpTest(string) {
213 var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string); 201 var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string);
214 return result != null; 202 return result != null;
215 } 203 }
216 204
217 205
218 function RegExpToString() { 206 function RegExpToString() {
219 // If this.source is an empty string, output /(?:)/. 207 // If this.source is an empty string, output /(?:)/.
220 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 208 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550
221 // ecma_2/RegExp/properties-001.js. 209 // ecma_2/RegExp/properties-001.js.
222 var src = this.source ? this.source : '(?:)'; 210 var src = this.source ? this.source : '(?:)';
223 var result = '/' + src + '/'; 211 var result = '/' + src + '/';
224 if (this.global) 212 if (this.global)
225 result += 'g'; 213 result += 'g';
226 if (this.ignoreCase) 214 if (this.ignoreCase)
227 result += 'i'; 215 result += 'i';
228 if (this.multiline) 216 if (this.multiline)
229 result += 'm'; 217 result += 'm';
230 return result; 218 return result;
231 } 219 }
232 220
233 221
234 // Getters for the static properties lastMatch, lastParen, leftContext, and 222 // Getters for the static properties lastMatch, lastParen, leftContext, and
235 // rightContext of the RegExp constructor. The properties are computed based 223 // rightContext of the RegExp constructor. The properties are computed based
236 // on the captures array of the last successful match and the subject string 224 // on the captures array of the last successful match and the subject string
237 // of the last successful match. 225 // of the last successful match.
238 function RegExpGetLastMatch() { 226 function RegExpGetLastMatch() {
239 return regExpSubject.slice(regExpCaptures[0], regExpCaptures[1]); 227 var regExpSubject = LAST_SUBJECT(lastMatchInfo);
228 return SubString(regExpSubject,
229 lastMatchInfo[CAPTURE0],
230 lastMatchInfo[CAPTURE1]);
240 } 231 }
241 232
242 233
243 function RegExpGetLastParen() { 234 function RegExpGetLastParen() {
244 var length = regExpCaptures.length; 235 var length = NUMBER_OF_CAPTURES(lastMatchInfo);
245 if (length <= 2) return ''; // There were no captures. 236 if (length <= 2) return ''; // There were no captures.
246 // We match the SpiderMonkey behavior: return the substring defined by the 237 // We match the SpiderMonkey behavior: return the substring defined by the
247 // last pair (after the first pair) of elements of the capture array even if 238 // last pair (after the first pair) of elements of the capture array even if
248 // it is empty. 239 // it is empty.
249 return regExpSubject.slice(regExpCaptures[length - 2], 240 var regExpSubject = LAST_SUBJECT(lastMatchInfo);
250 regExpCaptures[length - 1]); 241 return SubString(regExpSubject,
242 lastMatchInfo[CAPTURE(length - 2)],
243 lastMatchInfo[CAPTURE(length - 1)]);
251 } 244 }
252 245
253 246
254 function RegExpGetLeftContext() { 247 function RegExpGetLeftContext() {
255 return regExpSubject.slice(0, regExpCaptures[0]); 248 return SubString(LAST_SUBJECT(lastMatchInfo),
249 0,
250 lastMatchInfo[CAPTURE0]);
256 } 251 }
257 252
258 253
259 function RegExpGetRightContext() { 254 function RegExpGetRightContext() {
260 return regExpSubject.slice(regExpCaptures[1], regExpSubject.length); 255 var subject = LAST_SUBJECT(lastMatchInfo);
256 return SubString(subject,
257 lastMatchInfo[CAPTURE1],
258 subject.length);
261 } 259 }
262 260
263 261
264 // The properties $1..$9 are the first nine capturing substrings of the last 262 // The properties $1..$9 are the first nine capturing substrings of the last
265 // successful match, or ''. The function RegExpMakeCaptureGetter will be 263 // successful match, or ''. The function RegExpMakeCaptureGetter will be
266 // called with an index greater than or equal to 1 but it actually works for 264 // called with indeces from 1 to 9.
267 // any non-negative index.
268 function RegExpMakeCaptureGetter(n) { 265 function RegExpMakeCaptureGetter(n) {
269 return function() { 266 return function() {
270 var index = n * 2; 267 var index = n * 2;
271 if (index >= regExpCaptures.length) return ''; 268 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return '';
272 var matchStart = regExpCaptures[index]; 269 var matchStart = lastMatchInfo[CAPTURE(index)];
273 var matchEnd = regExpCaptures[index + 1]; 270 var matchEnd = lastMatchInfo[CAPTURE(index + 1)];
274 if (matchStart == -1 || matchEnd == -1) return ''; 271 if (matchStart == -1 || matchEnd == -1) return '';
275 return regExpSubject.slice(matchStart, matchEnd); 272 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd);
276 }; 273 };
277 } 274 }
278 275
279 276
280 // Properties of the builtins object for recording the result of the last 277 // Property of the builtins object for recording the result of the last
281 // regexp match. The property regExpCaptures is the matchIndices array of the 278 // regexp match. The property lastMatchInfo includes the matchIndices
282 // last successful regexp match (an array of start/end index pairs for the 279 // array of the last successful regexp match (an array of start/end index
283 // match and all the captured substrings), the invariant is that there is at 280 // pairs for the match and all the captured substrings), the invariant is
284 // least two elements. The property regExpSubject is the subject string for 281 // that there are at least two capture indeces. The array also contains
285 // the last successful match. 282 // the subject string for the last successful match.
286 var regExpCaptures = [0, 0]; 283 var lastMatchInfo = [
287 var regExpSubject = ''; 284 2, // REGEXP_NUMBER_OF_CAPTURES
288 var regExpInput; 285 0, // REGEXP_FIRST_CAPTURE + 0
286 0, // REGEXP_FIRST_CAPTURE + 1
287 "", // Last subject.
288 void 0, // Last input - settable with RegExpSetInput.
289 ];
289 290
290 // ------------------------------------------------------------------- 291 // -------------------------------------------------------------------
291 292
292 function SetupRegExp() { 293 function SetupRegExp() {
293 %FunctionSetInstanceClassName($RegExp, 'RegExp'); 294 %FunctionSetInstanceClassName($RegExp, 'RegExp');
294 %FunctionSetPrototype($RegExp, new $Object()); 295 %FunctionSetPrototype($RegExp, new $Object());
295 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); 296 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM);
296 %SetCode($RegExp, RegExpConstructor); 297 %SetCode($RegExp, RegExpConstructor);
297 298
298 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( 299 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array(
299 "exec", RegExpExec, 300 "exec", RegExpExec,
300 "test", RegExpTest, 301 "test", RegExpTest,
301 "toString", RegExpToString, 302 "toString", RegExpToString,
302 "compile", CompileRegExp 303 "compile", CompileRegExp
303 )); 304 ));
304 305
305 // The spec says nothing about the length of exec and test, but 306 // The spec says nothing about the length of exec and test, but
306 // SpiderMonkey and KJS have length equal to 0. 307 // SpiderMonkey and JSC have length equal to 0.
307 %FunctionSetLength($RegExp.prototype.exec, 0); 308 %FunctionSetLength($RegExp.prototype.exec, 0);
308 %FunctionSetLength($RegExp.prototype.test, 0); 309 %FunctionSetLength($RegExp.prototype.test, 0);
309 // The length of compile is 1 in SpiderMonkey. 310 // The length of compile is 1 in SpiderMonkey.
310 %FunctionSetLength($RegExp.prototype.compile, 1); 311 %FunctionSetLength($RegExp.prototype.compile, 1);
311 312
312 // The properties input, $input, and $_ are aliases for each other. When this 313 // The properties input, $input, and $_ are aliases for each other. When this
313 // value is set the value it is set to is coerced to a string. 314 // value is set the value it is set to is coerced to a string.
314 // Getter and setter for the input. 315 // Getter and setter for the input.
315 function RegExpGetInput() { 316 function RegExpGetInput() {
317 var regExpInput = LAST_INPUT(lastMatchInfo);
316 return IS_UNDEFINED(regExpInput) ? "" : regExpInput; 318 return IS_UNDEFINED(regExpInput) ? "" : regExpInput;
317 } 319 }
318 function RegExpSetInput(string) { regExpInput = ToString(string); } 320 function RegExpSetInput(string) {
321 lastMatchInfo[lastMatchInfo[REGEXP_NUMBER_OF_CAPTURES] + 2] = string;
322 };
319 323
320 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); 324 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE);
321 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE); 325 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE);
322 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE ); 326 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE );
323 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE ); 327 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE );
324 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE); 328 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE);
325 %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DE LETE); 329 %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DE LETE);
326 330
327 // The properties multiline and $* are aliases for each other. When this 331 // The properties multiline and $* are aliases for each other. When this
328 // value is set in SpiderMonkey, the value it is set to is coerced to a 332 // value is set in SpiderMonkey, the value it is set to is coerced to a
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); 368 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE);
365 369
366 for (var i = 1; i < 10; ++i) { 370 for (var i = 1; i < 10; ++i) {
367 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); 371 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE);
368 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); 372 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE);
369 } 373 }
370 } 374 }
371 375
372 376
373 SetupRegExp(); 377 SetupRegExp();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698