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

Side by Side Diff: src/string.js

Issue 1378533002: [es6] Introduce %ToInteger and %ToLength. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 2 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 | « src/runtime/runtime-object.cc ('k') | src/typedarray.js » ('j') | 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 148
149 149
150 // ECMA-262 section 15.5.4.10 150 // ECMA-262 section 15.5.4.10
151 function StringMatchJS(regexp) { 151 function StringMatchJS(regexp) {
152 CHECK_OBJECT_COERCIBLE(this, "String.prototype.match"); 152 CHECK_OBJECT_COERCIBLE(this, "String.prototype.match");
153 153
154 var subject = TO_STRING(this); 154 var subject = TO_STRING(this);
155 if (IS_REGEXP(regexp)) { 155 if (IS_REGEXP(regexp)) {
156 // Emulate RegExp.prototype.exec's side effect in step 5, even though 156 // Emulate RegExp.prototype.exec's side effect in step 5, even though
157 // value is discarded. 157 // value is discarded.
158 var lastIndex = regexp.lastIndex; 158 var lastIndex = TO_INTEGER(regexp.lastIndex);
159 TO_INTEGER_FOR_SIDE_EFFECT(lastIndex);
160 if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0); 159 if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0);
161 var result = %StringMatch(subject, regexp, RegExpLastMatchInfo); 160 var result = %StringMatch(subject, regexp, RegExpLastMatchInfo);
162 if (result !== null) $regexpLastMatchInfoOverride = null; 161 if (result !== null) $regexpLastMatchInfoOverride = null;
163 regexp.lastIndex = 0; 162 regexp.lastIndex = 0;
164 return result; 163 return result;
165 } 164 }
166 // Non-regexp argument. 165 // Non-regexp argument.
167 regexp = new GlobalRegExp(regexp); 166 regexp = new GlobalRegExp(regexp);
168 return RegExpExecNoTests(regexp, subject, 0); 167 return RegExpExecNoTests(regexp, subject, 0);
169 } 168 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // ...... global search 219 // ...... global search
221 // ...... non-global search 220 // ...... non-global search
222 // .. string search 221 // .. string search
223 // .... special case that replaces with one single character 222 // .... special case that replaces with one single character
224 // ...... function replace 223 // ...... function replace
225 // ...... string replace (with $-expansion) 224 // ...... string replace (with $-expansion)
226 225
227 if (IS_REGEXP(search)) { 226 if (IS_REGEXP(search)) {
228 // Emulate RegExp.prototype.exec's side effect in step 5, even if 227 // Emulate RegExp.prototype.exec's side effect in step 5, even if
229 // value is discarded. 228 // value is discarded.
230 var lastIndex = search.lastIndex; 229 var lastIndex = TO_INTEGER(search.lastIndex);
231 TO_INTEGER_FOR_SIDE_EFFECT(lastIndex);
232 230
233 if (!IS_CALLABLE(replace)) { 231 if (!IS_CALLABLE(replace)) {
234 replace = TO_STRING(replace); 232 replace = TO_STRING(replace);
235 233
236 if (!search.global) { 234 if (!search.global) {
237 // Non-global regexp search, string replace. 235 // Non-global regexp search, string replace.
238 var match = RegExpExec(search, subject, 0); 236 var match = RegExpExec(search, subject, 0);
239 if (match == null) { 237 if (match == null) {
240 search.lastIndex = 0 238 search.lastIndex = 0
241 return subject; 239 return subject;
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 function StringSup() { 927 function StringSup() {
930 CHECK_OBJECT_COERCIBLE(this, "String.prototype.sup"); 928 CHECK_OBJECT_COERCIBLE(this, "String.prototype.sup");
931 return "<sup>" + TO_STRING(this) + "</sup>"; 929 return "<sup>" + TO_STRING(this) + "</sup>";
932 } 930 }
933 931
934 // ES6 draft 01-20-14, section 21.1.3.13 932 // ES6 draft 01-20-14, section 21.1.3.13
935 function StringRepeat(count) { 933 function StringRepeat(count) {
936 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat"); 934 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
937 935
938 var s = TO_STRING(this); 936 var s = TO_STRING(this);
939 var n = $toInteger(count); 937 var n = TO_INTEGER(count);
940 // The maximum string length is stored in a smi, so a longer repeat 938 // The maximum string length is stored in a smi, so a longer repeat
941 // must result in a range error. 939 // must result in a range error.
942 if (n < 0 || n > %_MaxSmi()) throw MakeRangeError(kInvalidCountValue); 940 if (n < 0 || n > %_MaxSmi()) throw MakeRangeError(kInvalidCountValue);
943 941
944 var r = ""; 942 var r = "";
945 while (true) { 943 while (true) {
946 if (n & 1) r += s; 944 if (n & 1) r += s;
947 n >>= 1; 945 n >>= 1;
948 if (n === 0) return r; 946 if (n === 0) return r;
949 s += s; 947 s += s;
950 } 948 }
951 } 949 }
952 950
953 951
954 // ES6 draft 04-05-14, section 21.1.3.18 952 // ES6 draft 04-05-14, section 21.1.3.18
955 function StringStartsWith(searchString /* position */) { // length == 1 953 function StringStartsWith(searchString /* position */) { // length == 1
956 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith"); 954 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
957 955
958 var s = TO_STRING(this); 956 var s = TO_STRING(this);
959 957
960 if (IS_REGEXP(searchString)) { 958 if (IS_REGEXP(searchString)) {
961 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.startsWith"); 959 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.startsWith");
962 } 960 }
963 961
964 var ss = TO_STRING(searchString); 962 var ss = TO_STRING(searchString);
965 var pos = 0; 963 var pos = 0;
966 if (%_ArgumentsLength() > 1) { 964 if (%_ArgumentsLength() > 1) {
967 var arg = %_Arguments(1); // position 965 var arg = %_Arguments(1); // position
968 if (!IS_UNDEFINED(arg)) { 966 if (!IS_UNDEFINED(arg)) {
969 pos = $toInteger(arg); 967 pos = TO_INTEGER(arg);
970 } 968 }
971 } 969 }
972 970
973 var s_len = s.length; 971 var s_len = s.length;
974 if (pos < 0) pos = 0; 972 if (pos < 0) pos = 0;
975 if (pos > s_len) pos = s_len; 973 if (pos > s_len) pos = s_len;
976 var ss_len = ss.length; 974 var ss_len = ss.length;
977 975
978 if (ss_len + pos > s_len) { 976 if (ss_len + pos > s_len) {
979 return false; 977 return false;
(...skipping 18 matching lines...) Expand all
998 if (IS_REGEXP(searchString)) { 996 if (IS_REGEXP(searchString)) {
999 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith"); 997 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith");
1000 } 998 }
1001 999
1002 var ss = TO_STRING(searchString); 1000 var ss = TO_STRING(searchString);
1003 var s_len = s.length; 1001 var s_len = s.length;
1004 var pos = s_len; 1002 var pos = s_len;
1005 if (%_ArgumentsLength() > 1) { 1003 if (%_ArgumentsLength() > 1) {
1006 var arg = %_Arguments(1); // position 1004 var arg = %_Arguments(1); // position
1007 if (!IS_UNDEFINED(arg)) { 1005 if (!IS_UNDEFINED(arg)) {
1008 pos = $toInteger(arg); 1006 pos = TO_INTEGER(arg);
1009 } 1007 }
1010 } 1008 }
1011 1009
1012 if (pos < 0) pos = 0; 1010 if (pos < 0) pos = 0;
1013 if (pos > s_len) pos = s_len; 1011 if (pos > s_len) pos = s_len;
1014 var ss_len = ss.length; 1012 var ss_len = ss.length;
1015 pos = pos - ss_len; 1013 pos = pos - ss_len;
1016 1014
1017 if (pos < 0) { 1015 if (pos < 0) {
1018 return false; 1016 return false;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 1106
1109 // ------------------------------------------------------------------- 1107 // -------------------------------------------------------------------
1110 // String methods related to templates 1108 // String methods related to templates
1111 1109
1112 // ES6 Draft 03-17-2015, section 21.1.2.4 1110 // ES6 Draft 03-17-2015, section 21.1.2.4
1113 function StringRaw(callSite) { 1111 function StringRaw(callSite) {
1114 // TODO(caitp): Use rest parameters when implemented 1112 // TODO(caitp): Use rest parameters when implemented
1115 var numberOfSubstitutions = %_ArgumentsLength(); 1113 var numberOfSubstitutions = %_ArgumentsLength();
1116 var cooked = TO_OBJECT(callSite); 1114 var cooked = TO_OBJECT(callSite);
1117 var raw = TO_OBJECT(cooked.raw); 1115 var raw = TO_OBJECT(cooked.raw);
1118 var literalSegments = $toLength(raw.length); 1116 var literalSegments = TO_LENGTH(raw.length);
1119 if (literalSegments <= 0) return ""; 1117 if (literalSegments <= 0) return "";
1120 1118
1121 var result = TO_STRING(raw[0]); 1119 var result = TO_STRING(raw[0]);
1122 1120
1123 for (var i = 1; i < literalSegments; ++i) { 1121 for (var i = 1; i < literalSegments; ++i) {
1124 if (i < numberOfSubstitutions) { 1122 if (i < numberOfSubstitutions) {
1125 result += TO_STRING(%_Arguments(i)); 1123 result += TO_STRING(%_Arguments(i));
1126 } 1124 }
1127 result += TO_STRING(raw[i]); 1125 result += TO_STRING(raw[i]);
1128 } 1126 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1201 to.StringLastIndexOf = StringLastIndexOfJS; 1199 to.StringLastIndexOf = StringLastIndexOfJS;
1202 to.StringMatch = StringMatchJS; 1200 to.StringMatch = StringMatchJS;
1203 to.StringReplace = StringReplace; 1201 to.StringReplace = StringReplace;
1204 to.StringSlice = StringSlice; 1202 to.StringSlice = StringSlice;
1205 to.StringSplit = StringSplitJS; 1203 to.StringSplit = StringSplitJS;
1206 to.StringSubstr = StringSubstr; 1204 to.StringSubstr = StringSubstr;
1207 to.StringSubstring = StringSubstring; 1205 to.StringSubstring = StringSubstring;
1208 }); 1206 });
1209 1207
1210 }) 1208 })
OLDNEW
« no previous file with comments | « src/runtime/runtime-object.cc ('k') | src/typedarray.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698