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

Side by Side Diff: src/string.js

Issue 7887031: Make built-in functions not call .apply on functions. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 3 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/runtime.js ('k') | no next file » | 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-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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 override[1] = match_start; 433 override[1] = match_start;
434 lastMatchInfoOverride = override; 434 lastMatchInfoOverride = override;
435 var func_result = 435 var func_result =
436 %_CallFunction(receiver, elem, match_start, subject, replace); 436 %_CallFunction(receiver, elem, match_start, subject, replace);
437 res[i] = TO_STRING_INLINE(func_result); 437 res[i] = TO_STRING_INLINE(func_result);
438 match_start += elem.length; 438 match_start += elem.length;
439 } 439 }
440 i++; 440 i++;
441 } 441 }
442 } else { 442 } else {
443 var receiver = %GetDefaultReceiver(replace);
443 while (i < len) { 444 while (i < len) {
444 var elem = res[i]; 445 var elem = res[i];
445 if (!%_IsSmi(elem)) { 446 if (!%_IsSmi(elem)) {
446 // elem must be an Array. 447 // elem must be an Array.
447 // Use the apply argument as backing for global RegExp properties. 448 // Use the apply argument as backing for global RegExp properties.
448 lastMatchInfoOverride = elem; 449 lastMatchInfoOverride = elem;
449 var func_result = replace.apply(null, elem); 450 var func_result = %Apply(replace, receiver, elem, 0, elem.length);
450 res[i] = TO_STRING_INLINE(func_result); 451 res[i] = TO_STRING_INLINE(func_result);
451 } 452 }
452 i++; 453 i++;
453 } 454 }
454 } 455 }
455 var resultBuilder = new ReplaceResultBuilder(subject, res); 456 var resultBuilder = new ReplaceResultBuilder(subject, res);
456 var result = resultBuilder.generate(); 457 var result = resultBuilder.generate();
457 resultArray.length = 0; 458 resultArray.length = 0;
458 reusableReplaceArray = resultArray; 459 reusableReplaceArray = resultArray;
459 return result; 460 return result;
460 } 461 }
461 462
462 463
463 function StringReplaceNonGlobalRegExpWithFunction(subject, regexp, replace) { 464 function StringReplaceNonGlobalRegExpWithFunction(subject, regexp, replace) {
464 var matchInfo = DoRegExpExec(regexp, subject, 0); 465 var matchInfo = DoRegExpExec(regexp, subject, 0);
465 if (IS_NULL(matchInfo)) return subject; 466 if (IS_NULL(matchInfo)) return subject;
466 var result = new ReplaceResultBuilder(subject); 467 var result = new ReplaceResultBuilder(subject);
467 var index = matchInfo[CAPTURE0]; 468 var index = matchInfo[CAPTURE0];
468 result.addSpecialSlice(0, index); 469 result.addSpecialSlice(0, index);
469 var endOfMatch = matchInfo[CAPTURE1]; 470 var endOfMatch = matchInfo[CAPTURE1];
470 // Compute the parameter list consisting of the match, captures, index, 471 // Compute the parameter list consisting of the match, captures, index,
471 // and subject for the replace function invocation. 472 // and subject for the replace function invocation.
472 // The number of captures plus one for the match. 473 // The number of captures plus one for the match.
473 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; 474 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1;
474 var replacement; 475 var replacement;
476 var receiver = %GetDefaultReceiver(replace);
475 if (m == 1) { 477 if (m == 1) {
476 // No captures, only the match, which is always valid. 478 // No captures, only the match, which is always valid.
477 var s = SubString(subject, index, endOfMatch); 479 var s = SubString(subject, index, endOfMatch);
478 // Don't call directly to avoid exposing the built-in global object. 480 // Don't call directly to avoid exposing the built-in global object.
479 var receiver = %GetDefaultReceiver(replace);
480 replacement = 481 replacement =
481 %_CallFunction(receiver, s, index, subject, replace); 482 %_CallFunction(receiver, s, index, subject, replace);
482 } else { 483 } else {
483 var parameters = new InternalArray(m + 2); 484 var parameters = new InternalArray(m + 2);
484 for (var j = 0; j < m; j++) { 485 for (var j = 0; j < m; j++) {
485 parameters[j] = CaptureString(subject, matchInfo, j); 486 parameters[j] = CaptureString(subject, matchInfo, j);
486 } 487 }
487 parameters[j] = index; 488 parameters[j] = index;
488 parameters[j + 1] = subject; 489 parameters[j + 1] = subject;
489 490
490 replacement = replace.apply(null, parameters); 491 replacement = %Apply(replace, receiver, parameters, 0, j + 2);
491 } 492 }
492 493
493 result.add(replacement); // The add method converts to string if necessary. 494 result.add(replacement); // The add method converts to string if necessary.
494 // Can't use matchInfo any more from here, since the function could 495 // Can't use matchInfo any more from here, since the function could
495 // overwrite it. 496 // overwrite it.
496 result.addSpecialSlice(endOfMatch, subject.length); 497 result.addSpecialSlice(endOfMatch, subject.length);
497 return result.generate(); 498 return result.generate();
498 } 499 }
499 500
500 501
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after
985 "fixed", StringFixed, 986 "fixed", StringFixed,
986 "italics", StringItalics, 987 "italics", StringItalics,
987 "small", StringSmall, 988 "small", StringSmall,
988 "strike", StringStrike, 989 "strike", StringStrike,
989 "sub", StringSub, 990 "sub", StringSub,
990 "sup", StringSup 991 "sup", StringSup
991 )); 992 ));
992 } 993 }
993 994
994 SetUpString(); 995 SetUpString();
OLDNEW
« no previous file with comments | « src/runtime.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698