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

Side by Side Diff: src/js/string.js

Issue 1692003003: [builtins] Remove almost all remaining uses of %_Arguments and %_ArgumentsLength. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « no previous file | 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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 6
7 %CheckIsBootstrapping(); 7 %CheckIsBootstrapping();
8 8
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 // Imports 10 // Imports
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 var result = %_StringCharCodeAt(this, pos); 74 var result = %_StringCharCodeAt(this, pos);
75 if (!%_IsSmi(result)) { 75 if (!%_IsSmi(result)) {
76 result = %_StringCharCodeAt(TO_STRING(this), TO_INTEGER(pos)); 76 result = %_StringCharCodeAt(TO_STRING(this), TO_INTEGER(pos));
77 } 77 }
78 return result; 78 return result;
79 } 79 }
80 80
81 81
82 // ECMA-262, section 15.5.4.6 82 // ECMA-262, section 15.5.4.6
83 function StringConcat(other /* and more */) { // length == 1 83 function StringConcat(other /* and more */) { // length == 1
84 "use strict";
84 CHECK_OBJECT_COERCIBLE(this, "String.prototype.concat"); 85 CHECK_OBJECT_COERCIBLE(this, "String.prototype.concat");
85 var len = %_ArgumentsLength(); 86 var s = TO_STRING(this);
86 var this_as_string = TO_STRING(this); 87 var len = arguments.length;
87 if (len === 1) { 88 for (var i = 0; i < len; ++i) {
88 return this_as_string + TO_STRING(other); 89 s = s + TO_STRING(arguments[i]);
89 } 90 }
90 var parts = new InternalArray(len + 1); 91 return s;
91 parts[0] = this_as_string;
92 for (var i = 0; i < len; i++) {
93 var part = %_Arguments(i);
94 parts[i + 1] = TO_STRING(part);
95 }
96 return %StringBuilderConcat(parts, len + 1, "");
97 } 92 }
98 93
99 94
100 // ECMA-262 section 15.5.4.7 95 // ECMA-262 section 15.5.4.7
101 function StringIndexOfJS(pattern /* position */) { // length == 1 96 function StringIndexOf(pattern, position) { // length == 1
102 CHECK_OBJECT_COERCIBLE(this, "String.prototype.indexOf"); 97 CHECK_OBJECT_COERCIBLE(this, "String.prototype.indexOf");
103 98
104 var subject = TO_STRING(this); 99 var subject = TO_STRING(this);
105 pattern = TO_STRING(pattern); 100 pattern = TO_STRING(pattern);
106 var index = 0; 101 var index = TO_INTEGER(position);
107 if (%_ArgumentsLength() > 1) { 102 if (index < 0) index = 0;
108 index = %_Arguments(1); // position 103 if (index > subject.length) index = subject.length;
109 index = TO_INTEGER(index);
110 if (index < 0) index = 0;
111 if (index > subject.length) index = subject.length;
112 }
113 return %StringIndexOf(subject, pattern, index); 104 return %StringIndexOf(subject, pattern, index);
114 } 105 }
115 106
107 %FunctionSetLength(StringIndexOf, 1);
108
116 109
117 // ECMA-262 section 15.5.4.8 110 // ECMA-262 section 15.5.4.8
118 function StringLastIndexOfJS(pat /* position */) { // length == 1 111 function StringLastIndexOfJS(pat /* position */) { // length == 1
112 "use strict";
119 CHECK_OBJECT_COERCIBLE(this, "String.prototype.lastIndexOf"); 113 CHECK_OBJECT_COERCIBLE(this, "String.prototype.lastIndexOf");
120 114
121 var sub = TO_STRING(this); 115 var sub = TO_STRING(this);
122 var subLength = sub.length; 116 var subLength = sub.length;
123 var pat = TO_STRING(pat); 117 var pat = TO_STRING(pat);
124 var patLength = pat.length; 118 var patLength = pat.length;
125 var index = subLength - patLength; 119 var index = subLength - patLength;
126 if (%_ArgumentsLength() > 1) { 120 if (arguments.length > 1) {
Yang 2016/02/12 09:25:39 why not do the same thing as for StringIndexOf?
Benedikt Meurer 2016/02/12 09:30:14 Done.
127 var position = TO_NUMBER(%_Arguments(1)); 121 var position = TO_NUMBER(arguments[1]);
128 if (!NUMBER_IS_NAN(position)) { 122 if (!NUMBER_IS_NAN(position)) {
129 position = TO_INTEGER(position); 123 position = TO_INTEGER(position);
130 if (position < 0) { 124 if (position < 0) {
131 position = 0; 125 position = 0;
132 } 126 }
133 if (position + patLength < subLength) { 127 if (position + patLength < subLength) {
134 index = position; 128 index = position;
135 } 129 }
136 } 130 }
137 } 131 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 return RegExpExecNoTests(regexp, subject, 0); 165 return RegExpExecNoTests(regexp, subject, 0);
172 } 166 }
173 167
174 168
175 // ECMA-262 v6, section 21.1.3.12 169 // ECMA-262 v6, section 21.1.3.12
176 // 170 //
177 // For now we do nothing, as proper normalization requires big tables. 171 // For now we do nothing, as proper normalization requires big tables.
178 // If Intl is enabled, then i18n.js will override it and provide the the 172 // If Intl is enabled, then i18n.js will override it and provide the the
179 // proper functionality. 173 // proper functionality.
180 function StringNormalizeJS() { 174 function StringNormalizeJS() {
175 "use strict";
181 CHECK_OBJECT_COERCIBLE(this, "String.prototype.normalize"); 176 CHECK_OBJECT_COERCIBLE(this, "String.prototype.normalize");
182 var s = TO_STRING(this); 177 var s = TO_STRING(this);
183 178
184 var formArg = %_Arguments(0); 179 var formArg = arguments[0];
Yang 2016/02/12 09:25:39 I also prefer SetFunctionLength for this as well.
Benedikt Meurer 2016/02/12 09:30:14 Done.
185 var form = IS_UNDEFINED(formArg) ? 'NFC' : TO_STRING(formArg); 180 var form = IS_UNDEFINED(formArg) ? 'NFC' : TO_STRING(formArg);
186 181
187 var NORMALIZATION_FORMS = ['NFC', 'NFD', 'NFKC', 'NFKD']; 182 var NORMALIZATION_FORMS = ['NFC', 'NFD', 'NFKC', 'NFKD'];
188 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form); 183 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form);
189 if (normalizationForm === -1) { 184 if (normalizationForm === -1) {
190 throw MakeRangeError(kNormalizationForm, 185 throw MakeRangeError(kNormalizationForm,
191 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', ')); 186 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', '));
192 } 187 }
193 188
194 return s; 189 return s;
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 function StringTrimRight() { 555 function StringTrimRight() {
561 CHECK_OBJECT_COERCIBLE(this, "String.prototype.trimRight"); 556 CHECK_OBJECT_COERCIBLE(this, "String.prototype.trimRight");
562 557
563 return %StringTrim(TO_STRING(this), false, true); 558 return %StringTrim(TO_STRING(this), false, true);
564 } 559 }
565 560
566 561
567 // ECMA-262, section 15.5.3.2 562 // ECMA-262, section 15.5.3.2
568 function StringFromCharCode(code) { 563 function StringFromCharCode(code) {
569 var n = %_ArgumentsLength(); 564 var n = %_ArgumentsLength();
570 if (n == 1) return %_StringCharFromCode(code & 0xffff); 565 if (n === 1) return %_StringCharFromCode(code & 0xffff);
571 566
572 var one_byte = %NewString(n, NEW_ONE_BYTE_STRING); 567 var one_byte = %NewString(n, NEW_ONE_BYTE_STRING);
573 var i; 568 var i;
574 for (i = 0; i < n; i++) { 569 for (i = 0; i < n; i++) {
575 code = %_Arguments(i) & 0xffff; 570 code = %_Arguments(i) & 0xffff;
576 if (code > 0xff) break; 571 if (code > 0xff) break;
577 %_OneByteSeqStringSetChar(i, code, one_byte); 572 %_OneByteSeqStringSetChar(i, code, one_byte);
578 } 573 }
579 if (i == n) return one_byte; 574 if (i == n) return one_byte;
580 one_byte = %TruncateString(one_byte, i); 575 one_byte = %TruncateString(one_byte, i);
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 while (true) { 704 while (true) {
710 if (n & 1) r += s; 705 if (n & 1) r += s;
711 n >>= 1; 706 n >>= 1;
712 if (n === 0) return r; 707 if (n === 0) return r;
713 s += s; 708 s += s;
714 } 709 }
715 } 710 }
716 711
717 712
718 // ES6 draft 04-05-14, section 21.1.3.18 713 // ES6 draft 04-05-14, section 21.1.3.18
719 function StringStartsWith(searchString /* position */) { // length == 1 714 function StringStartsWith(searchString, position) { // length == 1
720 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith"); 715 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
721 716
722 var s = TO_STRING(this); 717 var s = TO_STRING(this);
723 718
724 if (IS_REGEXP(searchString)) { 719 if (IS_REGEXP(searchString)) {
725 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.startsWith"); 720 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.startsWith");
726 } 721 }
727 722
728 var ss = TO_STRING(searchString); 723 var ss = TO_STRING(searchString);
729 var pos = 0; 724 var pos = TO_INTEGER(position);
730 if (%_ArgumentsLength() > 1) {
731 var arg = %_Arguments(1); // position
732 if (!IS_UNDEFINED(arg)) {
733 pos = TO_INTEGER(arg);
734 }
735 }
736 725
737 var s_len = s.length; 726 var s_len = s.length;
738 var start = MinSimple(MaxSimple(pos, 0), s_len); 727 var start = MinSimple(MaxSimple(pos, 0), s_len);
739 var ss_len = ss.length; 728 var ss_len = ss.length;
740 if (ss_len + start > s_len) { 729 if (ss_len + start > s_len) {
741 return false; 730 return false;
742 } 731 }
743 732
744 return %_SubString(s, start, start + ss_len) === ss; 733 return %_SubString(s, start, start + ss_len) === ss;
745 } 734 }
746 735
736 %FunctionSetLength(StringStartsWith, 1);
737
747 738
748 // ES6 draft 04-05-14, section 21.1.3.7 739 // ES6 draft 04-05-14, section 21.1.3.7
749 function StringEndsWith(searchString /* position */) { // length == 1 740 function StringEndsWith(searchString, position) { // length == 1
750 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); 741 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
751 742
752 var s = TO_STRING(this); 743 var s = TO_STRING(this);
753 744
754 if (IS_REGEXP(searchString)) { 745 if (IS_REGEXP(searchString)) {
755 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith"); 746 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith");
756 } 747 }
757 748
758 var ss = TO_STRING(searchString); 749 var ss = TO_STRING(searchString);
759 var s_len = s.length; 750 var s_len = s.length;
760 var pos = s_len; 751 var pos = !IS_UNDEFINED(position) ? TO_INTEGER(position) : s_len
761 if (%_ArgumentsLength() > 1) {
762 var arg = %_Arguments(1); // position
763 if (!IS_UNDEFINED(arg)) {
764 pos = TO_INTEGER(arg);
765 }
766 }
767 752
768 var end = MinSimple(MaxSimple(pos, 0), s_len); 753 var end = MinSimple(MaxSimple(pos, 0), s_len);
769 var ss_len = ss.length; 754 var ss_len = ss.length;
770 var start = end - ss_len; 755 var start = end - ss_len;
771 if (start < 0) { 756 if (start < 0) {
772 return false; 757 return false;
773 } 758 }
774 759
775 return %_SubString(s, start, start + ss_len) === ss; 760 return %_SubString(s, start, start + ss_len) === ss;
776 } 761 }
777 762
763 %FunctionSetLength(StringEndsWith, 1);
764
778 765
779 // ES6 draft 04-05-14, section 21.1.3.6 766 // ES6 draft 04-05-14, section 21.1.3.6
780 function StringIncludes(searchString /* position */) { // length == 1 767 function StringIncludes(searchString, position) { // length == 1
781 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes"); 768 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes");
782 769
783 var string = TO_STRING(this); 770 var string = TO_STRING(this);
784 771
785 if (IS_REGEXP(searchString)) { 772 if (IS_REGEXP(searchString)) {
786 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.includes"); 773 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.includes");
787 } 774 }
788 775
789 searchString = TO_STRING(searchString); 776 searchString = TO_STRING(searchString);
790 var pos = 0; 777 var pos = TO_INTEGER(position);
791 if (%_ArgumentsLength() > 1) {
792 pos = %_Arguments(1); // position
793 pos = TO_INTEGER(pos);
794 }
795 778
796 var stringLength = string.length; 779 var stringLength = string.length;
797 if (pos < 0) pos = 0; 780 if (pos < 0) pos = 0;
798 if (pos > stringLength) pos = stringLength; 781 if (pos > stringLength) pos = stringLength;
799 var searchStringLength = searchString.length; 782 var searchStringLength = searchString.length;
800 783
801 if (searchStringLength + pos > stringLength) { 784 if (searchStringLength + pos > stringLength) {
802 return false; 785 return false;
803 } 786 }
804 787
805 return %StringIndexOf(string, searchString, pos) !== -1; 788 return %StringIndexOf(string, searchString, pos) !== -1;
806 } 789 }
807 790
791 %FunctionSetLength(StringIncludes, 1);
792
808 793
809 // ES6 Draft 05-22-2014, section 21.1.3.3 794 // ES6 Draft 05-22-2014, section 21.1.3.3
810 function StringCodePointAt(pos) { 795 function StringCodePointAt(pos) {
811 CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt"); 796 CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt");
812 797
813 var string = TO_STRING(this); 798 var string = TO_STRING(this);
814 var size = string.length; 799 var size = string.length;
815 pos = TO_INTEGER(pos); 800 pos = TO_INTEGER(pos);
816 if (pos < 0 || pos >= size) { 801 if (pos < 0 || pos >= size) {
817 return UNDEFINED; 802 return UNDEFINED;
818 } 803 }
819 var first = %_StringCharCodeAt(string, pos); 804 var first = %_StringCharCodeAt(string, pos);
820 if (first < 0xD800 || first > 0xDBFF || pos + 1 == size) { 805 if (first < 0xD800 || first > 0xDBFF || pos + 1 == size) {
821 return first; 806 return first;
822 } 807 }
823 var second = %_StringCharCodeAt(string, pos + 1); 808 var second = %_StringCharCodeAt(string, pos + 1);
824 if (second < 0xDC00 || second > 0xDFFF) { 809 if (second < 0xDC00 || second > 0xDFFF) {
825 return first; 810 return first;
826 } 811 }
827 return (first - 0xD800) * 0x400 + second + 0x2400; 812 return (first - 0xD800) * 0x400 + second + 0x2400;
828 } 813 }
829 814
830 815
831 // ES6 Draft 05-22-2014, section 21.1.2.2 816 // ES6 Draft 05-22-2014, section 21.1.2.2
832 function StringFromCodePoint(_) { // length = 1 817 function StringFromCodePoint(_) { // length = 1
818 "use strict";
833 var code; 819 var code;
834 var length = %_ArgumentsLength(); 820 var length = arguments.length;
835 var index; 821 var index;
836 var result = ""; 822 var result = "";
837 for (index = 0; index < length; index++) { 823 for (index = 0; index < length; index++) {
838 code = %_Arguments(index); 824 code = arguments[index];
839 if (!%_IsSmi(code)) { 825 if (!%_IsSmi(code)) {
840 code = TO_NUMBER(code); 826 code = TO_NUMBER(code);
841 } 827 }
842 if (code < 0 || code > 0x10FFFF || code !== TO_INTEGER(code)) { 828 if (code < 0 || code > 0x10FFFF || code !== TO_INTEGER(code)) {
843 throw MakeRangeError(kInvalidCodePoint, code); 829 throw MakeRangeError(kInvalidCodePoint, code);
844 } 830 }
845 if (code <= 0xFFFF) { 831 if (code <= 0xFFFF) {
846 result += %_StringCharFromCode(code); 832 result += %_StringCharFromCode(code);
847 } else { 833 } else {
848 code -= 0x10000; 834 code -= 0x10000;
849 result += %_StringCharFromCode((code >>> 10) & 0x3FF | 0xD800); 835 result += %_StringCharFromCode((code >>> 10) & 0x3FF | 0xD800);
850 result += %_StringCharFromCode(code & 0x3FF | 0xDC00); 836 result += %_StringCharFromCode(code & 0x3FF | 0xDC00);
851 } 837 }
852 } 838 }
853 return result; 839 return result;
854 } 840 }
855 841
856 842
857 // ------------------------------------------------------------------- 843 // -------------------------------------------------------------------
858 // String methods related to templates 844 // String methods related to templates
859 845
860 // ES6 Draft 03-17-2015, section 21.1.2.4 846 // ES6 Draft 03-17-2015, section 21.1.2.4
861 function StringRaw(callSite) { 847 function StringRaw(callSite) {
862 // TODO(caitp): Use rest parameters when implemented 848 "use strict";
863 var numberOfSubstitutions = %_ArgumentsLength(); 849 var numberOfSubstitutions = arguments.length;
864 var cooked = TO_OBJECT(callSite); 850 var cooked = TO_OBJECT(callSite);
865 var raw = TO_OBJECT(cooked.raw); 851 var raw = TO_OBJECT(cooked.raw);
866 var literalSegments = TO_LENGTH(raw.length); 852 var literalSegments = TO_LENGTH(raw.length);
867 if (literalSegments <= 0) return ""; 853 if (literalSegments <= 0) return "";
868 854
869 var result = TO_STRING(raw[0]); 855 var result = TO_STRING(raw[0]);
870 856
871 for (var i = 1; i < literalSegments; ++i) { 857 for (var i = 1; i < literalSegments; ++i) {
872 if (i < numberOfSubstitutions) { 858 if (i < numberOfSubstitutions) {
873 result += TO_STRING(%_Arguments(i)); 859 result += TO_STRING(arguments[i]);
874 } 860 }
875 result += TO_STRING(raw[i]); 861 result += TO_STRING(raw[i]);
876 } 862 }
877 863
878 return result; 864 return result;
879 } 865 }
880 866
881 // ------------------------------------------------------------------- 867 // -------------------------------------------------------------------
882 868
883 // Set the String function and constructor. 869 // Set the String function and constructor.
(...skipping 13 matching lines...) Expand all
897 // Set up the non-enumerable functions on the String prototype object. 883 // Set up the non-enumerable functions on the String prototype object.
898 utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [ 884 utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
899 "valueOf", StringValueOf, 885 "valueOf", StringValueOf,
900 "toString", StringToString, 886 "toString", StringToString,
901 "charAt", StringCharAtJS, 887 "charAt", StringCharAtJS,
902 "charCodeAt", StringCharCodeAtJS, 888 "charCodeAt", StringCharCodeAtJS,
903 "codePointAt", StringCodePointAt, 889 "codePointAt", StringCodePointAt,
904 "concat", StringConcat, 890 "concat", StringConcat,
905 "endsWith", StringEndsWith, 891 "endsWith", StringEndsWith,
906 "includes", StringIncludes, 892 "includes", StringIncludes,
907 "indexOf", StringIndexOfJS, 893 "indexOf", StringIndexOf,
908 "lastIndexOf", StringLastIndexOfJS, 894 "lastIndexOf", StringLastIndexOfJS,
909 "localeCompare", StringLocaleCompareJS, 895 "localeCompare", StringLocaleCompareJS,
910 "match", StringMatchJS, 896 "match", StringMatchJS,
911 "normalize", StringNormalizeJS, 897 "normalize", StringNormalizeJS,
912 "repeat", StringRepeat, 898 "repeat", StringRepeat,
913 "replace", StringReplace, 899 "replace", StringReplace,
914 "search", StringSearch, 900 "search", StringSearch,
915 "slice", StringSlice, 901 "slice", StringSlice,
916 "split", StringSplitJS, 902 "split", StringSplitJS,
917 "substring", StringSubstring, 903 "substring", StringSubstring,
(...skipping 21 matching lines...) Expand all
939 "sub", StringSub, 925 "sub", StringSub,
940 "sup", StringSup 926 "sup", StringSup
941 ]); 927 ]);
942 928
943 // ------------------------------------------------------------------- 929 // -------------------------------------------------------------------
944 // Exports 930 // Exports
945 931
946 utils.Export(function(to) { 932 utils.Export(function(to) {
947 to.ExpandReplacement = ExpandReplacement; 933 to.ExpandReplacement = ExpandReplacement;
948 to.StringCharAt = StringCharAtJS; 934 to.StringCharAt = StringCharAtJS;
949 to.StringIndexOf = StringIndexOfJS; 935 to.StringIndexOf = StringIndexOf;
950 to.StringLastIndexOf = StringLastIndexOfJS; 936 to.StringLastIndexOf = StringLastIndexOfJS;
951 to.StringMatch = StringMatchJS; 937 to.StringMatch = StringMatchJS;
952 to.StringReplace = StringReplace; 938 to.StringReplace = StringReplace;
953 to.StringSlice = StringSlice; 939 to.StringSlice = StringSlice;
954 to.StringSplit = StringSplitJS; 940 to.StringSplit = StringSplitJS;
955 to.StringSubstr = StringSubstr; 941 to.StringSubstr = StringSubstr;
956 to.StringSubstring = StringSubstring; 942 to.StringSubstring = StringSubstring;
957 }); 943 });
958 944
959 }) 945 })
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698