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

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

Issue 40290: Experimental: Merge 1395:1441 from bleeding_edge branch to the... (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/global/
Patch Set: Created 11 years, 9 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
« no previous file with comments | « src/platform-win32.cc ('k') | src/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 = StringCharAt.call(flags, i); 55 var c = flags.charAt(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 JSC, it returns undefined. For compatibility with JSC, we match their 120 // In KJS, it returns undefined. For compatibility with KJS, we match their
121 // behavior. 121 // behavior.
122 function CompileRegExp(pattern, flags) { 122 function CompileRegExp(pattern, flags) {
123 // Both JSC and SpiderMonkey treat a missing pattern argument as the 123 // Both KJS 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 // pattern as the string 'undefined'. Note that JSC is inconsistent 125 // patter as the string 'undefined'. Note that KJS 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 JSC, we match their 128 // the empty string. For compatibility with KJS, 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.
138 function DoRegExpExec(regexp, string, index) { 141 function DoRegExpExec(regexp, string, index) {
139 return %RegExpExec(regexp, string, index, lastMatchInfo); 142 var matchIndices = %RegExpExec(regexp, string, index);
143 if (!IS_NULL(matchIndices)) {
144 regExpCaptures = matchIndices;
145 regExpSubject = regExpInput = string;
146 }
147 return matchIndices;
140 } 148 }
141 149
142 150
143 function DoRegExpExecGlobal(regexp, string) { 151 function DoRegExpExecGlobal(regexp, string) {
144 // Returns an array of arrays of substring indices. 152 // Here, matchIndices is an array of arrays of substring indices.
145 return %RegExpExecGlobal(regexp, string, lastMatchInfo); 153 var matchIndices = %RegExpExecGlobal(regexp, string);
154 if (matchIndices.length != 0) {
155 regExpCaptures = matchIndices[matchIndices.length - 1];
156 regExpSubject = regExpInput = string;
157 }
158 return matchIndices;
146 } 159 }
147 160
148 161
149 function RegExpExec(string) { 162 function RegExpExec(string) {
150 if (%_ArgumentsLength() == 0) { 163 if (%_ArgumentsLength() == 0) {
151 var regExpInput = LAST_INPUT(lastMatchInfo);
152 if (IS_UNDEFINED(regExpInput)) { 164 if (IS_UNDEFINED(regExpInput)) {
153 throw MakeError('no_input_to_regexp', [this]); 165 throw MakeError('no_input_to_regexp', [this]);
154 } 166 }
155 string = regExpInput; 167 string = regExpInput;
156 } 168 }
157 var s = ToString(string); 169 var s = ToString(string);
158 var length = s.length; 170 var length = s.length;
159 var lastIndex = this.lastIndex; 171 var lastIndex = this.lastIndex;
160 var i = this.global ? TO_INTEGER(lastIndex) : 0; 172 var i = this.global ? TO_INTEGER(lastIndex) : 0;
161 173
162 if (i < 0 || i > s.length) { 174 if (i < 0 || i > s.length) {
163 this.lastIndex = 0; 175 this.lastIndex = 0;
164 return null; 176 return null;
165 } 177 }
166 178
167 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); 179 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]);
168 // matchIndices is either null or the lastMatchInfo array. 180 // matchIndices is an array of integers with length of captures*2,
169 var matchIndices = %RegExpExec(this, s, i, lastMatchInfo); 181 // each pair of integers specified the start and the end of index
182 // in the string.
183 var matchIndices = DoRegExpExec(this, s, i);
170 184
171 if (matchIndices == null) { 185 if (matchIndices == null) {
172 if (this.global) this.lastIndex = 0; 186 if (this.global) this.lastIndex = 0;
173 return matchIndices; // no match 187 return matchIndices; // no match
174 } 188 }
175 189
176 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; 190 var numResults = matchIndices.length >> 1;
177 var result = new $Array(numResults); 191 var result = new $Array(numResults);
178 for (var i = 0; i < numResults; i++) { 192 for (var i = 0; i < numResults; i++) {
179 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; 193 var matchStart = matchIndices[2*i];
180 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; 194 var matchEnd = matchIndices[2*i + 1];
181 if (matchStart != -1 && matchEnd != -1) { 195 if (matchStart != -1 && matchEnd != -1) {
182 result[i] = SubString(s, matchStart, matchEnd); 196 result[i] = s.slice(matchStart, matchEnd);
183 } else { 197 } else {
184 // Make sure the element is present. Avoid reading the undefined 198 // Make sure the element is present. Avoid reading the undefined
185 // property from the global object since this may change. 199 // property from the global object since this may change.
186 result[i] = void 0; 200 result[i] = void 0;
187 } 201 }
188 } 202 }
189 203
190 if (this.global) 204 if (this.global)
191 this.lastIndex = lastMatchInfo[CAPTURE1]; 205 this.lastIndex = matchIndices[1];
192 result.index = lastMatchInfo[CAPTURE0]; 206 result.index = matchIndices[0];
193 result.input = s; 207 result.input = s;
194 return result; 208 return result;
195 } 209 }
196 210
197 211
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.
200 function RegExpTest(string) { 212 function RegExpTest(string) {
201 var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string); 213 var result = (%_ArgumentsLength() == 0) ? this.exec() : this.exec(string);
202 return result != null; 214 return result != null;
203 } 215 }
204 216
205 217
206 function RegExpToString() { 218 function RegExpToString() {
207 // If this.source is an empty string, output /(?:)/. 219 // If this.source is an empty string, output /(?:)/.
208 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550 220 // http://bugzilla.mozilla.org/show_bug.cgi?id=225550
209 // ecma_2/RegExp/properties-001.js. 221 // ecma_2/RegExp/properties-001.js.
210 var src = this.source ? this.source : '(?:)'; 222 var src = this.source ? this.source : '(?:)';
211 var result = '/' + src + '/'; 223 var result = '/' + src + '/';
212 if (this.global) 224 if (this.global)
213 result += 'g'; 225 result += 'g';
214 if (this.ignoreCase) 226 if (this.ignoreCase)
215 result += 'i'; 227 result += 'i';
216 if (this.multiline) 228 if (this.multiline)
217 result += 'm'; 229 result += 'm';
218 return result; 230 return result;
219 } 231 }
220 232
221 233
222 // Getters for the static properties lastMatch, lastParen, leftContext, and 234 // Getters for the static properties lastMatch, lastParen, leftContext, and
223 // rightContext of the RegExp constructor. The properties are computed based 235 // rightContext of the RegExp constructor. The properties are computed based
224 // on the captures array of the last successful match and the subject string 236 // on the captures array of the last successful match and the subject string
225 // of the last successful match. 237 // of the last successful match.
226 function RegExpGetLastMatch() { 238 function RegExpGetLastMatch() {
227 var regExpSubject = LAST_SUBJECT(lastMatchInfo); 239 return regExpSubject.slice(regExpCaptures[0], regExpCaptures[1]);
228 return SubString(regExpSubject,
229 lastMatchInfo[CAPTURE0],
230 lastMatchInfo[CAPTURE1]);
231 } 240 }
232 241
233 242
234 function RegExpGetLastParen() { 243 function RegExpGetLastParen() {
235 var length = NUMBER_OF_CAPTURES(lastMatchInfo); 244 var length = regExpCaptures.length;
236 if (length <= 2) return ''; // There were no captures. 245 if (length <= 2) return ''; // There were no captures.
237 // We match the SpiderMonkey behavior: return the substring defined by the 246 // We match the SpiderMonkey behavior: return the substring defined by the
238 // last pair (after the first pair) of elements of the capture array even if 247 // last pair (after the first pair) of elements of the capture array even if
239 // it is empty. 248 // it is empty.
240 var regExpSubject = LAST_SUBJECT(lastMatchInfo); 249 return regExpSubject.slice(regExpCaptures[length - 2],
241 return SubString(regExpSubject, 250 regExpCaptures[length - 1]);
242 lastMatchInfo[CAPTURE(length - 2)],
243 lastMatchInfo[CAPTURE(length - 1)]);
244 } 251 }
245 252
246 253
247 function RegExpGetLeftContext() { 254 function RegExpGetLeftContext() {
248 return SubString(LAST_SUBJECT(lastMatchInfo), 255 return regExpSubject.slice(0, regExpCaptures[0]);
249 0,
250 lastMatchInfo[CAPTURE0]);
251 } 256 }
252 257
253 258
254 function RegExpGetRightContext() { 259 function RegExpGetRightContext() {
255 var subject = LAST_SUBJECT(lastMatchInfo); 260 return regExpSubject.slice(regExpCaptures[1], regExpSubject.length);
256 return SubString(subject,
257 lastMatchInfo[CAPTURE1],
258 subject.length);
259 } 261 }
260 262
261 263
262 // The properties $1..$9 are the first nine capturing substrings of the last 264 // The properties $1..$9 are the first nine capturing substrings of the last
263 // successful match, or ''. The function RegExpMakeCaptureGetter will be 265 // successful match, or ''. The function RegExpMakeCaptureGetter will be
264 // called with indeces from 1 to 9. 266 // called with an index greater than or equal to 1 but it actually works for
267 // any non-negative index.
265 function RegExpMakeCaptureGetter(n) { 268 function RegExpMakeCaptureGetter(n) {
266 return function() { 269 return function() {
267 var index = n * 2; 270 var index = n * 2;
268 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; 271 if (index >= regExpCaptures.length) return '';
269 var matchStart = lastMatchInfo[CAPTURE(index)]; 272 var matchStart = regExpCaptures[index];
270 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; 273 var matchEnd = regExpCaptures[index + 1];
271 if (matchStart == -1 || matchEnd == -1) return ''; 274 if (matchStart == -1 || matchEnd == -1) return '';
272 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); 275 return regExpSubject.slice(matchStart, matchEnd);
273 }; 276 };
274 } 277 }
275 278
276 279
277 // Property of the builtins object for recording the result of the last 280 // Properties of the builtins object for recording the result of the last
278 // regexp match. The property lastMatchInfo includes the matchIndices 281 // regexp match. The property regExpCaptures is the matchIndices array of the
279 // array of the last successful regexp match (an array of start/end index 282 // last successful regexp match (an array of start/end index pairs for the
280 // pairs for the match and all the captured substrings), the invariant is 283 // match and all the captured substrings), the invariant is that there is at
281 // that there are at least two capture indeces. The array also contains 284 // least two elements. The property regExpSubject is the subject string for
282 // the subject string for the last successful match. 285 // the last successful match.
283 var lastMatchInfo = [ 286 var regExpCaptures = [0, 0];
284 2, // REGEXP_NUMBER_OF_CAPTURES 287 var regExpSubject = '';
285 0, // REGEXP_FIRST_CAPTURE + 0 288 var regExpInput;
286 0, // REGEXP_FIRST_CAPTURE + 1
287 "", // Last subject.
288 void 0, // Last input - settable with RegExpSetInput.
289 ];
290 289
291 // ------------------------------------------------------------------- 290 // -------------------------------------------------------------------
292 291
293 function SetupRegExp() { 292 function SetupRegExp() {
294 %FunctionSetInstanceClassName($RegExp, 'RegExp'); 293 %FunctionSetInstanceClassName($RegExp, 'RegExp');
295 %FunctionSetPrototype($RegExp, new $Object()); 294 %FunctionSetPrototype($RegExp, new $Object());
296 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); 295 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM);
297 %SetCode($RegExp, RegExpConstructor); 296 %SetCode($RegExp, RegExpConstructor);
298 297
299 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( 298 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array(
300 "exec", RegExpExec, 299 "exec", RegExpExec,
301 "test", RegExpTest, 300 "test", RegExpTest,
302 "toString", RegExpToString, 301 "toString", RegExpToString,
303 "compile", CompileRegExp 302 "compile", CompileRegExp
304 )); 303 ));
305 304
306 // The spec says nothing about the length of exec and test, but 305 // The spec says nothing about the length of exec and test, but
307 // SpiderMonkey and JSC have length equal to 0. 306 // SpiderMonkey and KJS have length equal to 0.
308 %FunctionSetLength($RegExp.prototype.exec, 0); 307 %FunctionSetLength($RegExp.prototype.exec, 0);
309 %FunctionSetLength($RegExp.prototype.test, 0); 308 %FunctionSetLength($RegExp.prototype.test, 0);
310 // The length of compile is 1 in SpiderMonkey. 309 // The length of compile is 1 in SpiderMonkey.
311 %FunctionSetLength($RegExp.prototype.compile, 1); 310 %FunctionSetLength($RegExp.prototype.compile, 1);
312 311
313 // The properties input, $input, and $_ are aliases for each other. When this 312 // The properties input, $input, and $_ are aliases for each other. When this
314 // value is set the value it is set to is coerced to a string. 313 // value is set the value it is set to is coerced to a string.
315 // Getter and setter for the input. 314 // Getter and setter for the input.
316 function RegExpGetInput() { 315 function RegExpGetInput() {
317 var regExpInput = LAST_INPUT(lastMatchInfo);
318 return IS_UNDEFINED(regExpInput) ? "" : regExpInput; 316 return IS_UNDEFINED(regExpInput) ? "" : regExpInput;
319 } 317 }
320 function RegExpSetInput(string) { 318 function RegExpSetInput(string) { regExpInput = ToString(string); }
321 lastMatchInfo[lastMatchInfo[REGEXP_NUMBER_OF_CAPTURES] + 2] =
322 ToString(string);
323 };
324 319
325 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); 320 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE);
326 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE); 321 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE);
327 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE ); 322 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE );
328 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE ); 323 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE );
329 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE); 324 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE);
330 %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DE LETE); 325 %DefineAccessor($RegExp, '$input', SETTER, RegExpSetInput, DONT_ENUM | DONT_DE LETE);
331 326
332 // The properties multiline and $* are aliases for each other. When this 327 // The properties multiline and $* are aliases for each other. When this
333 // value is set in SpiderMonkey, the value it is set to is coerced to a 328 // 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
369 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); 364 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE);
370 365
371 for (var i = 1; i < 10; ++i) { 366 for (var i = 1; i < 10; ++i) {
372 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); 367 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE);
373 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); 368 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE);
374 } 369 }
375 } 370 }
376 371
377 372
378 SetupRegExp(); 373 SetupRegExp();
OLDNEW
« no previous file with comments | « src/platform-win32.cc ('k') | src/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698