Chromium Code Reviews| 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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 function RegExpGetRightContext() { | 293 function RegExpGetRightContext() { |
| 294 var subject = LAST_SUBJECT(lastMatchInfo); | 294 var subject = LAST_SUBJECT(lastMatchInfo); |
| 295 return SubString(subject, | 295 return SubString(subject, |
| 296 lastMatchInfo[CAPTURE1], | 296 lastMatchInfo[CAPTURE1], |
| 297 subject.length); | 297 subject.length); |
| 298 } | 298 } |
| 299 | 299 |
| 300 | 300 |
| 301 // The properties $1..$9 are the first nine capturing substrings of the last | 301 // The properties $1..$9 are the first nine capturing substrings of the last |
| 302 // successful match, or ''. The function RegExpMakeCaptureGetter will be | 302 // successful match, or ''. The function RegExpMakeCaptureGetter will be |
| 303 // called with indeces from 1 to 9. | 303 // called with indices from 1 to 9. |
| 304 function RegExpMakeCaptureGetter(n) { | 304 function RegExpMakeCaptureGetter(n) { |
| 305 return function() { | 305 return function() { |
| 306 var index = n * 2; | 306 var index = n * 2; |
| 307 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; | 307 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; |
| 308 var matchStart = lastMatchInfo[CAPTURE(index)]; | 308 var matchStart = lastMatchInfo[CAPTURE(index)]; |
| 309 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; | 309 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; |
| 310 if (matchStart == -1 || matchEnd == -1) return ''; | 310 if (matchStart == -1 || matchEnd == -1) return ''; |
| 311 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); | 311 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); |
| 312 }; | 312 }; |
| 313 } | 313 } |
| 314 | 314 |
| 315 | 315 |
| 316 // Property of the builtins object for recording the result of the last | 316 // Property of the builtins object for recording the result of the last |
| 317 // regexp match. The property lastMatchInfo includes the matchIndices | 317 // regexp match. The property lastMatchInfo includes the matchIndices |
| 318 // array of the last successful regexp match (an array of start/end index | 318 // array of the last successful regexp match (an array of start/end index |
| 319 // pairs for the match and all the captured substrings), the invariant is | 319 // pairs for the match and all the captured substrings), the invariant is |
| 320 // that there are at least two capture indeces. The array also contains | 320 // that there are at least two capture indeces. The array also contains |
| 321 // the subject string for the last successful match. | 321 // the subject string for the last successful match. |
| 322 var lastMatchInfo = [ | 322 var lastMatchInfo = [ |
| 323 2, // REGEXP_NUMBER_OF_CAPTURES | 323 2, // REGEXP_NUMBER_OF_CAPTURES |
| 324 "", // Last subject. | |
| 325 void 0, // Last input - settable with RegExpSetInput. | |
| 324 0, // REGEXP_FIRST_CAPTURE + 0 | 326 0, // REGEXP_FIRST_CAPTURE + 0 |
| 325 0, // REGEXP_FIRST_CAPTURE + 1 | 327 0, // REGEXP_FIRST_CAPTURE + 1 |
| 326 "", // Last subject. | |
| 327 void 0, // Last input - settable with RegExpSetInput. | |
| 328 ]; | 328 ]; |
| 329 | 329 |
| 330 // ------------------------------------------------------------------- | 330 // ------------------------------------------------------------------- |
| 331 | 331 |
| 332 function SetupRegExp() { | 332 function SetupRegExp() { |
| 333 %FunctionSetInstanceClassName($RegExp, 'RegExp'); | 333 %FunctionSetInstanceClassName($RegExp, 'RegExp'); |
| 334 %FunctionSetPrototype($RegExp, new $Object()); | 334 %FunctionSetPrototype($RegExp, new $Object()); |
| 335 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); | 335 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); |
| 336 %SetCode($RegExp, RegExpConstructor); | 336 %SetCode($RegExp, RegExpConstructor); |
| 337 | 337 |
| 338 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( | 338 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( |
| 339 "exec", RegExpExec, | 339 "exec", RegExpExec, |
| 340 "test", RegExpTest, | 340 "test", RegExpTest, |
| 341 "toString", RegExpToString, | 341 "toString", RegExpToString, |
| 342 "compile", CompileRegExp | 342 "compile", CompileRegExp |
| 343 )); | 343 )); |
| 344 | 344 |
| 345 // The length of compile is 1 in SpiderMonkey. | 345 // The length of compile is 1 in SpiderMonkey. |
| 346 %FunctionSetLength($RegExp.prototype.compile, 1); | 346 %FunctionSetLength($RegExp.prototype.compile, 1); |
| 347 | 347 |
| 348 // The properties input, $input, and $_ are aliases for each other. When this | 348 // The properties input, $input, and $_ are aliases for each other. When this |
| 349 // value is set the value it is set to is coerced to a string. | 349 // value is set the value it is set to is coerced to a string. |
| 350 // Getter and setter for the input. | 350 // Getter and setter for the input. |
| 351 function RegExpGetInput() { | 351 function RegExpGetInput() { |
| 352 var regExpInput = LAST_INPUT(lastMatchInfo); | 352 var regExpInput = LAST_INPUT(lastMatchInfo); |
| 353 return IS_UNDEFINED(regExpInput) ? "" : regExpInput; | 353 return IS_UNDEFINED(regExpInput) ? "" : regExpInput; |
| 354 } | 354 } |
| 355 function RegExpSetInput(string) { | 355 function RegExpSetInput(string) { |
| 356 lastMatchInfo[lastMatchInfo[REGEXP_NUMBER_OF_CAPTURES] + 2] = | 356 LAST_INPUT(lastMatchInfo) = ToString(string); |
|
Erik Corry
2009/03/17 12:39:32
This is much prettier, thanks!
| |
| 357 ToString(string); | |
| 358 }; | 357 }; |
| 359 | 358 |
| 360 // All these accessors are set with the 'never_used' flag set to true. | 359 // All these accessors are set with the 'never_used' flag set to true. |
| 361 // This is because at this point there can be no existing ics for setting | 360 // This is because at this point there can be no existing ics for setting |
| 362 // these properties and so we don't have to bother clearing them. | 361 // these properties and so we don't have to bother clearing them. |
| 363 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); | 362 %DefineAccessor($RegExp, 'input', GETTER, RegExpGetInput, DONT_DELETE); |
| 364 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE, true); | 363 %DefineAccessor($RegExp, 'input', SETTER, RegExpSetInput, DONT_DELETE, true); |
| 365 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE ); | 364 %DefineAccessor($RegExp, '$_', GETTER, RegExpGetInput, DONT_ENUM | DONT_DELETE ); |
| 366 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE , true); | 365 %DefineAccessor($RegExp, '$_', SETTER, RegExpSetInput, DONT_ENUM | DONT_DELETE , true); |
| 367 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE); | 366 %DefineAccessor($RegExp, '$input', GETTER, RegExpGetInput, DONT_ENUM | DONT_DE LETE); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE, tr ue); | 406 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE, tr ue); |
| 408 | 407 |
| 409 for (var i = 1; i < 10; ++i) { | 408 for (var i = 1; i < 10; ++i) { |
| 410 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); | 409 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); |
| 411 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE, true); | 410 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE, true); |
| 412 } | 411 } |
| 413 } | 412 } |
| 414 | 413 |
| 415 | 414 |
| 416 SetupRegExp(); | 415 SetupRegExp(); |
| OLD | NEW |