OLD | NEW |
1 // Copyright 2006-2009 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 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 case 'm': | 64 case 'm': |
65 multiline = true; | 65 multiline = true; |
66 break; | 66 break; |
67 default: | 67 default: |
68 // Ignore flags that have no meaning to be consistent with | 68 // Ignore flags that have no meaning to be consistent with |
69 // JSC. | 69 // JSC. |
70 break; | 70 break; |
71 } | 71 } |
72 } | 72 } |
73 | 73 |
74 if (!isConstructorCall) { | 74 if (isConstructorCall) { |
| 75 // ECMA-262, section 15.10.7.1. |
| 76 %SetProperty(object, 'source', pattern, |
| 77 DONT_DELETE | READ_ONLY | DONT_ENUM); |
| 78 |
| 79 // ECMA-262, section 15.10.7.2. |
| 80 %SetProperty(object, 'global', global, DONT_DELETE | READ_ONLY | DONT_ENUM); |
| 81 |
| 82 // ECMA-262, section 15.10.7.3. |
| 83 %SetProperty(object, 'ignoreCase', ignoreCase, |
| 84 DONT_DELETE | READ_ONLY | DONT_ENUM); |
| 85 |
| 86 // ECMA-262, section 15.10.7.4. |
| 87 %SetProperty(object, 'multiline', multiline, |
| 88 DONT_DELETE | READ_ONLY | DONT_ENUM); |
| 89 |
| 90 // ECMA-262, section 15.10.7.5. |
| 91 %SetProperty(object, 'lastIndex', 0, DONT_DELETE | DONT_ENUM); |
| 92 } else { // RegExp is being recompiled via RegExp.prototype.compile. |
| 93 %IgnoreAttributesAndSetProperty(object, 'source', pattern); |
| 94 %IgnoreAttributesAndSetProperty(object, 'global', global); |
| 95 %IgnoreAttributesAndSetProperty(object, 'ignoreCase', ignoreCase); |
| 96 %IgnoreAttributesAndSetProperty(object, 'multiline', multiline); |
| 97 %IgnoreAttributesAndSetProperty(object, 'lastIndex', 0); |
75 regExpCache.type = 'none'; | 98 regExpCache.type = 'none'; |
76 } | 99 } |
77 %RegExpInitializeObject(object, pattern, global, ignoreCase, multiline); | |
78 | 100 |
79 // Call internal function to compile the pattern. | 101 // Call internal function to compile the pattern. |
80 %RegExpCompile(object, pattern, flags); | 102 %RegExpCompile(object, pattern, flags); |
81 } | 103 } |
82 | 104 |
83 | 105 |
84 function RegExpConstructor(pattern, flags) { | 106 function RegExpConstructor(pattern, flags) { |
85 if (%_IsConstructCall()) { | 107 if (%_IsConstructCall()) { |
86 DoConstructRegExp(this, pattern, flags, true); | 108 DoConstructRegExp(this, pattern, flags, true); |
87 } else { | 109 } else { |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 result += 'm'; | 337 result += 'm'; |
316 return result; | 338 return result; |
317 } | 339 } |
318 | 340 |
319 | 341 |
320 // Getters for the static properties lastMatch, lastParen, leftContext, and | 342 // Getters for the static properties lastMatch, lastParen, leftContext, and |
321 // rightContext of the RegExp constructor. The properties are computed based | 343 // rightContext of the RegExp constructor. The properties are computed based |
322 // on the captures array of the last successful match and the subject string | 344 // on the captures array of the last successful match and the subject string |
323 // of the last successful match. | 345 // of the last successful match. |
324 function RegExpGetLastMatch() { | 346 function RegExpGetLastMatch() { |
325 if (lastMatchInfoOverride) { return lastMatchInfoOverride[0]; } | |
326 var regExpSubject = LAST_SUBJECT(lastMatchInfo); | 347 var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
327 return SubString(regExpSubject, | 348 return SubString(regExpSubject, |
328 lastMatchInfo[CAPTURE0], | 349 lastMatchInfo[CAPTURE0], |
329 lastMatchInfo[CAPTURE1]); | 350 lastMatchInfo[CAPTURE1]); |
330 } | 351 } |
331 | 352 |
332 | 353 |
333 function RegExpGetLastParen() { | 354 function RegExpGetLastParen() { |
334 if (lastMatchInfoOverride) { | |
335 var override = lastMatchInfoOverride; | |
336 if (override.length <= 3) return ''; | |
337 return override[override.length - 3]; | |
338 } | |
339 var length = NUMBER_OF_CAPTURES(lastMatchInfo); | 355 var length = NUMBER_OF_CAPTURES(lastMatchInfo); |
340 if (length <= 2) return ''; // There were no captures. | 356 if (length <= 2) return ''; // There were no captures. |
341 // We match the SpiderMonkey behavior: return the substring defined by the | 357 // We match the SpiderMonkey behavior: return the substring defined by the |
342 // last pair (after the first pair) of elements of the capture array even if | 358 // last pair (after the first pair) of elements of the capture array even if |
343 // it is empty. | 359 // it is empty. |
344 var regExpSubject = LAST_SUBJECT(lastMatchInfo); | 360 var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
345 var start = lastMatchInfo[CAPTURE(length - 2)]; | 361 var start = lastMatchInfo[CAPTURE(length - 2)]; |
346 var end = lastMatchInfo[CAPTURE(length - 1)]; | 362 var end = lastMatchInfo[CAPTURE(length - 1)]; |
347 if (start != -1 && end != -1) { | 363 if (start != -1 && end != -1) { |
348 return SubString(regExpSubject, start, end); | 364 return SubString(regExpSubject, start, end); |
349 } | 365 } |
350 return ""; | 366 return ""; |
351 } | 367 } |
352 | 368 |
353 | 369 |
354 function RegExpGetLeftContext() { | 370 function RegExpGetLeftContext() { |
355 var start_index; | 371 return SubString(LAST_SUBJECT(lastMatchInfo), |
356 var subject; | 372 0, |
357 if (!lastMatchInfoOverride) { | 373 lastMatchInfo[CAPTURE0]); |
358 start_index = lastMatchInfo[CAPTURE0]; | |
359 subject = LAST_SUBJECT(lastMatchInfo); | |
360 } else { | |
361 var override = lastMatchInfoOverride; | |
362 start_index = override[override.length - 2]; | |
363 subject = override[override.length - 1]; | |
364 } | |
365 return SubString(subject, 0, start_index); | |
366 } | 374 } |
367 | 375 |
368 | 376 |
369 function RegExpGetRightContext() { | 377 function RegExpGetRightContext() { |
370 var start_index; | 378 var subject = LAST_SUBJECT(lastMatchInfo); |
371 var subject; | 379 return SubString(subject, |
372 if (!lastMatchInfoOverride) { | 380 lastMatchInfo[CAPTURE1], |
373 start_index = lastMatchInfo[CAPTURE1]; | 381 subject.length); |
374 subject = LAST_SUBJECT(lastMatchInfo); | |
375 } else { | |
376 var override = lastMatchInfoOverride; | |
377 subject = override[override.length - 1]; | |
378 start_index = override[override.length - 2] + subject.length; | |
379 } | |
380 return SubString(subject, start_index, subject.length); | |
381 } | 382 } |
382 | 383 |
383 | 384 |
384 // The properties $1..$9 are the first nine capturing substrings of the last | 385 // The properties $1..$9 are the first nine capturing substrings of the last |
385 // successful match, or ''. The function RegExpMakeCaptureGetter will be | 386 // successful match, or ''. The function RegExpMakeCaptureGetter will be |
386 // called with indices from 1 to 9. | 387 // called with indices from 1 to 9. |
387 function RegExpMakeCaptureGetter(n) { | 388 function RegExpMakeCaptureGetter(n) { |
388 return function() { | 389 return function() { |
389 if (lastMatchInfoOverride) { | |
390 if (n < lastMatchInfoOverride.length - 2) return lastMatchInfoOverride[n]; | |
391 return ''; | |
392 } | |
393 var index = n * 2; | 390 var index = n * 2; |
394 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; | 391 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; |
395 var matchStart = lastMatchInfo[CAPTURE(index)]; | 392 var matchStart = lastMatchInfo[CAPTURE(index)]; |
396 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; | 393 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; |
397 if (matchStart == -1 || matchEnd == -1) return ''; | 394 if (matchStart == -1 || matchEnd == -1) return ''; |
398 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); | 395 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); |
399 }; | 396 }; |
400 } | 397 } |
401 | 398 |
402 | 399 |
403 // Property of the builtins object for recording the result of the last | 400 // Property of the builtins object for recording the result of the last |
404 // regexp match. The property lastMatchInfo includes the matchIndices | 401 // regexp match. The property lastMatchInfo includes the matchIndices |
405 // array of the last successful regexp match (an array of start/end index | 402 // array of the last successful regexp match (an array of start/end index |
406 // pairs for the match and all the captured substrings), the invariant is | 403 // pairs for the match and all the captured substrings), the invariant is |
407 // that there are at least two capture indeces. The array also contains | 404 // that there are at least two capture indeces. The array also contains |
408 // the subject string for the last successful match. | 405 // the subject string for the last successful match. |
409 var lastMatchInfo = [ | 406 var lastMatchInfo = [ |
410 2, // REGEXP_NUMBER_OF_CAPTURES | 407 2, // REGEXP_NUMBER_OF_CAPTURES |
411 "", // Last subject. | 408 "", // Last subject. |
412 void 0, // Last input - settable with RegExpSetInput. | 409 void 0, // Last input - settable with RegExpSetInput. |
413 0, // REGEXP_FIRST_CAPTURE + 0 | 410 0, // REGEXP_FIRST_CAPTURE + 0 |
414 0, // REGEXP_FIRST_CAPTURE + 1 | 411 0, // REGEXP_FIRST_CAPTURE + 1 |
415 ]; | 412 ]; |
416 | 413 |
417 // Override last match info with an array of actual substrings. | |
418 // Used internally by replace regexp with function. | |
419 // The array has the format of an "apply" argument for a replacement | |
420 // function. | |
421 var lastMatchInfoOverride = null; | |
422 | |
423 // ------------------------------------------------------------------- | 414 // ------------------------------------------------------------------- |
424 | 415 |
425 function SetupRegExp() { | 416 function SetupRegExp() { |
426 %FunctionSetInstanceClassName($RegExp, 'RegExp'); | 417 %FunctionSetInstanceClassName($RegExp, 'RegExp'); |
427 %FunctionSetPrototype($RegExp, new $Object()); | 418 %FunctionSetPrototype($RegExp, new $Object()); |
428 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); | 419 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); |
429 %SetCode($RegExp, RegExpConstructor); | 420 %SetCode($RegExp, RegExpConstructor); |
430 | 421 |
431 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( | 422 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( |
432 "exec", RegExpExec, | 423 "exec", RegExpExec, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); | 488 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); |
498 | 489 |
499 for (var i = 1; i < 10; ++i) { | 490 for (var i = 1; i < 10; ++i) { |
500 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D
ELETE); | 491 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D
ELETE); |
501 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); | 492 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); |
502 } | 493 } |
503 } | 494 } |
504 | 495 |
505 | 496 |
506 SetupRegExp(); | 497 SetupRegExp(); |
OLD | NEW |