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 28 matching lines...) Expand all Loading... |
39 %_SetValueOf(this, value); | 39 %_SetValueOf(this, value); |
40 } else { | 40 } else { |
41 return value; | 41 return value; |
42 } | 42 } |
43 }); | 43 }); |
44 | 44 |
45 %FunctionSetPrototype($String, new $String()); | 45 %FunctionSetPrototype($String, new $String()); |
46 | 46 |
47 // ECMA-262 section 15.5.4.2 | 47 // ECMA-262 section 15.5.4.2 |
48 function StringToString() { | 48 function StringToString() { |
49 if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) | 49 if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) { |
50 throw new $TypeError('String.prototype.toString is not generic'); | 50 throw new $TypeError('String.prototype.toString is not generic'); |
| 51 } |
51 return %_ValueOf(this); | 52 return %_ValueOf(this); |
52 } | 53 } |
53 | 54 |
54 | 55 |
55 // ECMA-262 section 15.5.4.3 | 56 // ECMA-262 section 15.5.4.3 |
56 function StringValueOf() { | 57 function StringValueOf() { |
57 if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) | 58 if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) { |
58 throw new $TypeError('String.prototype.valueOf is not generic'); | 59 throw new $TypeError('String.prototype.valueOf is not generic'); |
| 60 } |
59 return %_ValueOf(this); | 61 return %_ValueOf(this); |
60 } | 62 } |
61 | 63 |
62 | 64 |
63 // ECMA-262, section 15.5.4.4 | 65 // ECMA-262, section 15.5.4.4 |
64 function StringCharAt(pos) { | 66 function StringCharAt(pos) { |
65 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 67 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
66 throw MakeTypeError("called_on_null_or_undefined", | 68 throw MakeTypeError("called_on_null_or_undefined", |
67 ["String.prototype.charAt"]); | 69 ["String.prototype.charAt"]); |
68 } | 70 } |
(...skipping 15 matching lines...) Expand all Loading... |
84 if (!%_IsSmi(result)) { | 86 if (!%_IsSmi(result)) { |
85 result = %_StringCharCodeAt(TO_STRING_INLINE(this), TO_INTEGER(pos)); | 87 result = %_StringCharCodeAt(TO_STRING_INLINE(this), TO_INTEGER(pos)); |
86 } | 88 } |
87 return result; | 89 return result; |
88 } | 90 } |
89 | 91 |
90 | 92 |
91 // ECMA-262, section 15.5.4.6 | 93 // ECMA-262, section 15.5.4.6 |
92 function StringConcat() { | 94 function StringConcat() { |
93 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 95 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
94 throw MakeTypeError("called_on_null_or_undefined", ["String.prototype.concat
"]); | 96 throw MakeTypeError("called_on_null_or_undefined", |
| 97 ["String.prototype.concat"]); |
95 } | 98 } |
96 var len = %_ArgumentsLength(); | 99 var len = %_ArgumentsLength(); |
97 var this_as_string = TO_STRING_INLINE(this); | 100 var this_as_string = TO_STRING_INLINE(this); |
98 if (len === 1) { | 101 if (len === 1) { |
99 return this_as_string + %_Arguments(0); | 102 return this_as_string + %_Arguments(0); |
100 } | 103 } |
101 var parts = new InternalArray(len + 1); | 104 var parts = new InternalArray(len + 1); |
102 parts[0] = this_as_string; | 105 parts[0] = this_as_string; |
103 for (var i = 0; i < len; i++) { | 106 for (var i = 0; i < len; i++) { |
104 var part = %_Arguments(i); | 107 var part = %_Arguments(i); |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 builder_elements.push(SubString(string, position, length)); | 354 builder_elements.push(SubString(string, position, length)); |
352 } | 355 } |
353 return; | 356 return; |
354 } | 357 } |
355 | 358 |
356 // Append substring between the previous and the next $ character. | 359 // Append substring between the previous and the next $ character. |
357 if (next > position) { | 360 if (next > position) { |
358 builder_elements.push(SubString(string, position, next)); | 361 builder_elements.push(SubString(string, position, next)); |
359 } | 362 } |
360 } | 363 } |
361 }; | 364 } |
362 | 365 |
363 | 366 |
364 // Compute the string of a given regular expression capture. | 367 // Compute the string of a given regular expression capture. |
365 function CaptureString(string, lastCaptureInfo, index) { | 368 function CaptureString(string, lastCaptureInfo, index) { |
366 // Scale the index. | 369 // Scale the index. |
367 var scaled = index << 1; | 370 var scaled = index << 1; |
368 // Compute start and end. | 371 // Compute start and end. |
369 var start = lastCaptureInfo[CAPTURE(scaled)]; | 372 var start = lastCaptureInfo[CAPTURE(scaled)]; |
370 // If start isn't valid, return undefined. | 373 // If start isn't valid, return undefined. |
371 if (start < 0) return; | 374 if (start < 0) return; |
372 var end = lastCaptureInfo[CAPTURE(scaled + 1)]; | 375 var end = lastCaptureInfo[CAPTURE(scaled + 1)]; |
373 return SubString(string, start, end); | 376 return SubString(string, start, end); |
374 }; | 377 } |
375 | 378 |
376 | 379 |
377 // Add the string of a given regular expression capture to the | 380 // Add the string of a given regular expression capture to the |
378 // ReplaceResultBuilder | 381 // ReplaceResultBuilder |
379 function addCaptureString(builder, matchInfo, index) { | 382 function addCaptureString(builder, matchInfo, index) { |
380 // Scale the index. | 383 // Scale the index. |
381 var scaled = index << 1; | 384 var scaled = index << 1; |
382 // Compute start and end. | 385 // Compute start and end. |
383 var start = matchInfo[CAPTURE(scaled)]; | 386 var start = matchInfo[CAPTURE(scaled)]; |
384 if (start < 0) return; | 387 if (start < 0) return; |
385 var end = matchInfo[CAPTURE(scaled + 1)]; | 388 var end = matchInfo[CAPTURE(scaled + 1)]; |
386 builder.addSpecialSlice(start, end); | 389 builder.addSpecialSlice(start, end); |
387 }; | 390 } |
388 | 391 |
389 // TODO(lrn): This array will survive indefinitely if replace is never | 392 // TODO(lrn): This array will survive indefinitely if replace is never |
390 // called again. However, it will be empty, since the contents are cleared | 393 // called again. However, it will be empty, since the contents are cleared |
391 // in the finally block. | 394 // in the finally block. |
392 var reusableReplaceArray = new InternalArray(16); | 395 var reusableReplaceArray = new InternalArray(16); |
393 | 396 |
394 // Helper function for replacing regular expressions with the result of a | 397 // Helper function for replacing regular expressions with the result of a |
395 // function application in String.prototype.replace. | 398 // function application in String.prototype.replace. |
396 function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) { | 399 function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) { |
397 var resultArray = reusableReplaceArray; | 400 var resultArray = reusableReplaceArray; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 // ECMA-262 section 15.5.4.13 | 527 // ECMA-262 section 15.5.4.13 |
525 function StringSlice(start, end) { | 528 function StringSlice(start, end) { |
526 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 529 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
527 throw MakeTypeError("called_on_null_or_undefined", | 530 throw MakeTypeError("called_on_null_or_undefined", |
528 ["String.prototype.slice"]); | 531 ["String.prototype.slice"]); |
529 } | 532 } |
530 var s = TO_STRING_INLINE(this); | 533 var s = TO_STRING_INLINE(this); |
531 var s_len = s.length; | 534 var s_len = s.length; |
532 var start_i = TO_INTEGER(start); | 535 var start_i = TO_INTEGER(start); |
533 var end_i = s_len; | 536 var end_i = s_len; |
534 if (end !== void 0) | 537 if (end !== void 0) { |
535 end_i = TO_INTEGER(end); | 538 end_i = TO_INTEGER(end); |
| 539 } |
536 | 540 |
537 if (start_i < 0) { | 541 if (start_i < 0) { |
538 start_i += s_len; | 542 start_i += s_len; |
539 if (start_i < 0) | 543 if (start_i < 0) { |
540 start_i = 0; | 544 start_i = 0; |
| 545 } |
541 } else { | 546 } else { |
542 if (start_i > s_len) | 547 if (start_i > s_len) { |
543 start_i = s_len; | 548 start_i = s_len; |
| 549 } |
544 } | 550 } |
545 | 551 |
546 if (end_i < 0) { | 552 if (end_i < 0) { |
547 end_i += s_len; | 553 end_i += s_len; |
548 if (end_i < 0) | 554 if (end_i < 0) { |
549 end_i = 0; | 555 end_i = 0; |
| 556 } |
550 } else { | 557 } else { |
551 if (end_i > s_len) | 558 if (end_i > s_len) { |
552 end_i = s_len; | 559 end_i = s_len; |
| 560 } |
553 } | 561 } |
554 | 562 |
555 var num_c = end_i - start_i; | 563 var num_c = end_i - start_i; |
556 if (num_c < 0) | 564 if (num_c < 0) { |
557 num_c = 0; | 565 num_c = 0; |
| 566 } |
558 | 567 |
559 return SubString(s, start_i, start_i + num_c); | 568 return SubString(s, start_i, start_i + num_c); |
560 } | 569 } |
561 | 570 |
562 | 571 |
563 // ECMA-262 section 15.5.4.14 | 572 // ECMA-262 section 15.5.4.14 |
564 function StringSplit(separator, limit) { | 573 function StringSplit(separator, limit) { |
565 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 574 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
566 throw MakeTypeError("called_on_null_or_undefined", | 575 throw MakeTypeError("called_on_null_or_undefined", |
567 ["String.prototype.split"]); | 576 ["String.prototype.split"]); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 } else { | 694 } else { |
686 if (end_i < 0) end_i = 0; | 695 if (end_i < 0) end_i = 0; |
687 if (start_i > end_i) { | 696 if (start_i > end_i) { |
688 var tmp = end_i; | 697 var tmp = end_i; |
689 end_i = start_i; | 698 end_i = start_i; |
690 start_i = tmp; | 699 start_i = tmp; |
691 } | 700 } |
692 } | 701 } |
693 } | 702 } |
694 | 703 |
695 return (start_i + 1 == end_i | 704 return ((start_i + 1 == end_i) |
696 ? %_StringCharAt(s, start_i) | 705 ? %_StringCharAt(s, start_i) |
697 : %_SubString(s, start_i, end_i)); | 706 : %_SubString(s, start_i, end_i)); |
698 } | 707 } |
699 | 708 |
700 | 709 |
701 // This is not a part of ECMA-262. | 710 // This is not a part of ECMA-262. |
702 function StringSubstr(start, n) { | 711 function StringSubstr(start, n) { |
703 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 712 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
704 throw MakeTypeError("called_on_null_or_undefined", | 713 throw MakeTypeError("called_on_null_or_undefined", |
705 ["String.prototype.substr"]); | 714 ["String.prototype.substr"]); |
(...skipping 23 matching lines...) Expand all Loading... |
729 // use zero. | 738 // use zero. |
730 if (start < 0) { | 739 if (start < 0) { |
731 start += s.length; | 740 start += s.length; |
732 if (start < 0) start = 0; | 741 if (start < 0) start = 0; |
733 } | 742 } |
734 } | 743 } |
735 | 744 |
736 var end = start + len; | 745 var end = start + len; |
737 if (end > s.length) end = s.length; | 746 if (end > s.length) end = s.length; |
738 | 747 |
739 return (start + 1 == end | 748 return ((start + 1 == end) |
740 ? %_StringCharAt(s, start) | 749 ? %_StringCharAt(s, start) |
741 : %_SubString(s, start, end)); | 750 : %_SubString(s, start, end)); |
742 } | 751 } |
743 | 752 |
744 | 753 |
745 // ECMA-262, 15.5.4.16 | 754 // ECMA-262, 15.5.4.16 |
746 function StringToLowerCase() { | 755 function StringToLowerCase() { |
747 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 756 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
748 throw MakeTypeError("called_on_null_or_undefined", | 757 throw MakeTypeError("called_on_null_or_undefined", |
749 ["String.prototype.toLowerCase"]); | 758 ["String.prototype.toLowerCase"]); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 return %StringFromCharCodeArray(codes); | 838 return %StringFromCharCodeArray(codes); |
830 } | 839 } |
831 | 840 |
832 | 841 |
833 // Helper function for very basic XSS protection. | 842 // Helper function for very basic XSS protection. |
834 function HtmlEscape(str) { | 843 function HtmlEscape(str) { |
835 return TO_STRING_INLINE(str).replace(/</g, "<") | 844 return TO_STRING_INLINE(str).replace(/</g, "<") |
836 .replace(/>/g, ">") | 845 .replace(/>/g, ">") |
837 .replace(/"/g, """) | 846 .replace(/"/g, """) |
838 .replace(/'/g, "'"); | 847 .replace(/'/g, "'"); |
839 }; | 848 } |
840 | 849 |
841 | 850 |
842 // Compatibility support for KJS. | 851 // Compatibility support for KJS. |
843 // Tested by mozilla/js/tests/js1_5/Regress/regress-276103.js. | 852 // Tested by mozilla/js/tests/js1_5/Regress/regress-276103.js. |
844 function StringLink(s) { | 853 function StringLink(s) { |
845 return "<a href=\"" + HtmlEscape(s) + "\">" + this + "</a>"; | 854 return "<a href=\"" + HtmlEscape(s) + "\">" + this + "</a>"; |
846 } | 855 } |
847 | 856 |
848 | 857 |
849 function StringAnchor(name) { | 858 function StringAnchor(name) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 "fixed", StringFixed, | 999 "fixed", StringFixed, |
991 "italics", StringItalics, | 1000 "italics", StringItalics, |
992 "small", StringSmall, | 1001 "small", StringSmall, |
993 "strike", StringStrike, | 1002 "strike", StringStrike, |
994 "sub", StringSub, | 1003 "sub", StringSub, |
995 "sup", StringSup | 1004 "sup", StringSup |
996 )); | 1005 )); |
997 } | 1006 } |
998 | 1007 |
999 SetUpString(); | 1008 SetUpString(); |
OLD | NEW |