OLD | NEW |
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 var $stringCharAt; | 5 var $stringCharAt; |
6 var $stringIndexOf; | 6 var $stringIndexOf; |
7 var $stringSubstring; | 7 var $stringSubstring; |
8 | 8 |
9 (function() { | 9 (function() { |
10 | 10 |
(...skipping 913 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
924 return "<sub>" + this + "</sub>"; | 924 return "<sub>" + this + "</sub>"; |
925 } | 925 } |
926 | 926 |
927 | 927 |
928 // ES6 draft, revision 26 (2014-07-18), section B.2.3.14 | 928 // ES6 draft, revision 26 (2014-07-18), section B.2.3.14 |
929 function StringSup() { | 929 function StringSup() { |
930 CHECK_OBJECT_COERCIBLE(this, "String.prototype.sup"); | 930 CHECK_OBJECT_COERCIBLE(this, "String.prototype.sup"); |
931 return "<sup>" + this + "</sup>"; | 931 return "<sup>" + this + "</sup>"; |
932 } | 932 } |
933 | 933 |
| 934 // ES6 draft 01-20-14, section 21.1.3.13 |
| 935 function StringRepeat(count) { |
| 936 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat"); |
| 937 |
| 938 var s = TO_STRING_INLINE(this); |
| 939 var n = ToInteger(count); |
| 940 // The maximum string length is stored in a smi, so a longer repeat |
| 941 // must result in a range error. |
| 942 if (n < 0 || n > %_MaxSmi()) { |
| 943 throw MakeRangeError("invalid_count_value", []); |
| 944 } |
| 945 |
| 946 var r = ""; |
| 947 while (true) { |
| 948 if (n & 1) r += s; |
| 949 n >>= 1; |
| 950 if (n === 0) return r; |
| 951 s += s; |
| 952 } |
| 953 } |
| 954 |
| 955 |
| 956 // ES6 draft 04-05-14, section 21.1.3.18 |
| 957 function StringStartsWith(searchString /* position */) { // length == 1 |
| 958 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith"); |
| 959 |
| 960 var s = TO_STRING_INLINE(this); |
| 961 |
| 962 if (IS_REGEXP(searchString)) { |
| 963 throw MakeTypeError("first_argument_not_regexp", |
| 964 ["String.prototype.startsWith"]); |
| 965 } |
| 966 |
| 967 var ss = TO_STRING_INLINE(searchString); |
| 968 var pos = 0; |
| 969 if (%_ArgumentsLength() > 1) { |
| 970 pos = %_Arguments(1); // position |
| 971 pos = ToInteger(pos); |
| 972 } |
| 973 |
| 974 var s_len = s.length; |
| 975 var start = $min($max(pos, 0), s_len); |
| 976 var ss_len = ss.length; |
| 977 if (ss_len + start > s_len) { |
| 978 return false; |
| 979 } |
| 980 |
| 981 return %StringIndexOf(s, ss, start) === start; |
| 982 } |
| 983 |
| 984 |
| 985 // ES6 draft 04-05-14, section 21.1.3.7 |
| 986 function StringEndsWith(searchString /* position */) { // length == 1 |
| 987 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); |
| 988 |
| 989 var s = TO_STRING_INLINE(this); |
| 990 |
| 991 if (IS_REGEXP(searchString)) { |
| 992 throw MakeTypeError("first_argument_not_regexp", |
| 993 ["String.prototype.endsWith"]); |
| 994 } |
| 995 |
| 996 var ss = TO_STRING_INLINE(searchString); |
| 997 var s_len = s.length; |
| 998 var pos = s_len; |
| 999 if (%_ArgumentsLength() > 1) { |
| 1000 var arg = %_Arguments(1); // position |
| 1001 if (!IS_UNDEFINED(arg)) { |
| 1002 pos = ToInteger(arg); |
| 1003 } |
| 1004 } |
| 1005 |
| 1006 var end = $min($max(pos, 0), s_len); |
| 1007 var ss_len = ss.length; |
| 1008 var start = end - ss_len; |
| 1009 if (start < 0) { |
| 1010 return false; |
| 1011 } |
| 1012 |
| 1013 return %StringLastIndexOf(s, ss, start) === start; |
| 1014 } |
| 1015 |
| 1016 |
| 1017 // ES6 draft 04-05-14, section 21.1.3.6 |
| 1018 function StringIncludes(searchString /* position */) { // length == 1 |
| 1019 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes"); |
| 1020 |
| 1021 var s = TO_STRING_INLINE(this); |
| 1022 |
| 1023 if (IS_REGEXP(searchString)) { |
| 1024 throw MakeTypeError("first_argument_not_regexp", |
| 1025 ["String.prototype.includes"]); |
| 1026 } |
| 1027 |
| 1028 var ss = TO_STRING_INLINE(searchString); |
| 1029 var pos = 0; |
| 1030 if (%_ArgumentsLength() > 1) { |
| 1031 pos = %_Arguments(1); // position |
| 1032 pos = ToInteger(pos); |
| 1033 } |
| 1034 |
| 1035 var s_len = s.length; |
| 1036 var start = $min($max(pos, 0), s_len); |
| 1037 var ss_len = ss.length; |
| 1038 if (ss_len + start > s_len) { |
| 1039 return false; |
| 1040 } |
| 1041 |
| 1042 return %StringIndexOf(s, ss, start) !== -1; |
| 1043 } |
| 1044 |
| 1045 |
| 1046 // ES6 Draft 05-22-2014, section 21.1.3.3 |
| 1047 function StringCodePointAt(pos) { |
| 1048 CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt"); |
| 1049 |
| 1050 var string = TO_STRING_INLINE(this); |
| 1051 var size = string.length; |
| 1052 pos = TO_INTEGER(pos); |
| 1053 if (pos < 0 || pos >= size) { |
| 1054 return UNDEFINED; |
| 1055 } |
| 1056 var first = %_StringCharCodeAt(string, pos); |
| 1057 if (first < 0xD800 || first > 0xDBFF || pos + 1 == size) { |
| 1058 return first; |
| 1059 } |
| 1060 var second = %_StringCharCodeAt(string, pos + 1); |
| 1061 if (second < 0xDC00 || second > 0xDFFF) { |
| 1062 return first; |
| 1063 } |
| 1064 return (first - 0xD800) * 0x400 + second + 0x2400; |
| 1065 } |
| 1066 |
| 1067 |
| 1068 // ES6 Draft 05-22-2014, section 21.1.2.2 |
| 1069 function StringFromCodePoint(_) { // length = 1 |
| 1070 var code; |
| 1071 var length = %_ArgumentsLength(); |
| 1072 var index; |
| 1073 var result = ""; |
| 1074 for (index = 0; index < length; index++) { |
| 1075 code = %_Arguments(index); |
| 1076 if (!%_IsSmi(code)) { |
| 1077 code = ToNumber(code); |
| 1078 } |
| 1079 if (code < 0 || code > 0x10FFFF || code !== TO_INTEGER(code)) { |
| 1080 throw MakeRangeError("invalid_code_point", [code]); |
| 1081 } |
| 1082 if (code <= 0xFFFF) { |
| 1083 result += %_StringCharFromCode(code); |
| 1084 } else { |
| 1085 code -= 0x10000; |
| 1086 result += %_StringCharFromCode((code >>> 10) & 0x3FF | 0xD800); |
| 1087 result += %_StringCharFromCode(code & 0x3FF | 0xDC00); |
| 1088 } |
| 1089 } |
| 1090 return result; |
| 1091 } |
| 1092 |
| 1093 |
| 1094 |
934 // ------------------------------------------------------------------- | 1095 // ------------------------------------------------------------------- |
935 | 1096 |
936 // Set the String function and constructor. | 1097 // Set the String function and constructor. |
937 %SetCode(GlobalString, StringConstructor); | 1098 %SetCode(GlobalString, StringConstructor); |
938 %FunctionSetPrototype(GlobalString, new GlobalString()); | 1099 %FunctionSetPrototype(GlobalString, new GlobalString()); |
939 | 1100 |
940 // Set up the constructor property on the String prototype object. | 1101 // Set up the constructor property on the String prototype object. |
941 %AddNamedProperty( | 1102 %AddNamedProperty( |
942 GlobalString.prototype, "constructor", GlobalString, DONT_ENUM); | 1103 GlobalString.prototype, "constructor", GlobalString, DONT_ENUM); |
943 | 1104 |
944 // Set up the non-enumerable functions on the String object. | 1105 // Set up the non-enumerable functions on the String object. |
945 InstallFunctions(GlobalString, DONT_ENUM, GlobalArray( | 1106 InstallFunctions(GlobalString, DONT_ENUM, GlobalArray( |
946 "fromCharCode", StringFromCharCode | 1107 "fromCharCode", StringFromCharCode, |
| 1108 "fromCodePoint", StringFromCodePoint |
947 )); | 1109 )); |
948 | 1110 |
949 // Set up the non-enumerable functions on the String prototype object. | 1111 // Set up the non-enumerable functions on the String prototype object. |
950 InstallFunctions(GlobalString.prototype, DONT_ENUM, GlobalArray( | 1112 InstallFunctions(GlobalString.prototype, DONT_ENUM, GlobalArray( |
951 "valueOf", StringValueOf, | 1113 "valueOf", StringValueOf, |
952 "toString", StringToString, | 1114 "toString", StringToString, |
953 "charAt", StringCharAtJS, | 1115 "charAt", StringCharAtJS, |
954 "charCodeAt", StringCharCodeAtJS, | 1116 "charCodeAt", StringCharCodeAtJS, |
| 1117 "codePointAt", StringCodePointAt, |
955 "concat", StringConcat, | 1118 "concat", StringConcat, |
| 1119 "endsWith", StringEndsWith, |
| 1120 "includes", StringIncludes, |
956 "indexOf", StringIndexOfJS, | 1121 "indexOf", StringIndexOfJS, |
957 "lastIndexOf", StringLastIndexOfJS, | 1122 "lastIndexOf", StringLastIndexOfJS, |
958 "localeCompare", StringLocaleCompareJS, | 1123 "localeCompare", StringLocaleCompareJS, |
959 "match", StringMatchJS, | 1124 "match", StringMatchJS, |
960 "normalize", StringNormalizeJS, | 1125 "normalize", StringNormalizeJS, |
| 1126 "repeat", StringRepeat, |
961 "replace", StringReplace, | 1127 "replace", StringReplace, |
962 "search", StringSearch, | 1128 "search", StringSearch, |
963 "slice", StringSlice, | 1129 "slice", StringSlice, |
964 "split", StringSplitJS, | 1130 "split", StringSplitJS, |
965 "substring", StringSubstring, | 1131 "substring", StringSubstring, |
966 "substr", StringSubstr, | 1132 "substr", StringSubstr, |
| 1133 "startsWith", StringStartsWith, |
967 "toLowerCase", StringToLowerCaseJS, | 1134 "toLowerCase", StringToLowerCaseJS, |
968 "toLocaleLowerCase", StringToLocaleLowerCase, | 1135 "toLocaleLowerCase", StringToLocaleLowerCase, |
969 "toUpperCase", StringToUpperCaseJS, | 1136 "toUpperCase", StringToUpperCaseJS, |
970 "toLocaleUpperCase", StringToLocaleUpperCase, | 1137 "toLocaleUpperCase", StringToLocaleUpperCase, |
971 "trim", StringTrimJS, | 1138 "trim", StringTrimJS, |
972 "trimLeft", StringTrimLeft, | 1139 "trimLeft", StringTrimLeft, |
973 "trimRight", StringTrimRight, | 1140 "trimRight", StringTrimRight, |
| 1141 |
974 "link", StringLink, | 1142 "link", StringLink, |
975 "anchor", StringAnchor, | 1143 "anchor", StringAnchor, |
976 "fontcolor", StringFontcolor, | 1144 "fontcolor", StringFontcolor, |
977 "fontsize", StringFontsize, | 1145 "fontsize", StringFontsize, |
978 "big", StringBig, | 1146 "big", StringBig, |
979 "blink", StringBlink, | 1147 "blink", StringBlink, |
980 "bold", StringBold, | 1148 "bold", StringBold, |
981 "fixed", StringFixed, | 1149 "fixed", StringFixed, |
982 "italics", StringItalics, | 1150 "italics", StringItalics, |
983 "small", StringSmall, | 1151 "small", StringSmall, |
984 "strike", StringStrike, | 1152 "strike", StringStrike, |
985 "sub", StringSub, | 1153 "sub", StringSub, |
986 "sup", StringSup | 1154 "sup", StringSup |
987 )); | 1155 )); |
988 | 1156 |
989 $stringCharAt = StringCharAtJS; | 1157 $stringCharAt = StringCharAtJS; |
990 $stringIndexOf = StringIndexOfJS; | 1158 $stringIndexOf = StringIndexOfJS; |
991 $stringSubstring = StringSubstring; | 1159 $stringSubstring = StringSubstring; |
992 | 1160 |
993 })(); | 1161 })(); |
OLD | NEW |