OLD | NEW |
---|---|
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #include "src/builtins/builtins-regexp.h" | |
5 #include "src/builtins/builtins-utils.h" | 6 #include "src/builtins/builtins-utils.h" |
6 #include "src/builtins/builtins.h" | 7 #include "src/builtins/builtins.h" |
7 #include "src/code-factory.h" | 8 #include "src/code-factory.h" |
8 #include "src/code-stub-assembler.h" | 9 #include "src/code-stub-assembler.h" |
9 #include "src/regexp/regexp-utils.h" | 10 #include "src/regexp/regexp-utils.h" |
10 | 11 |
11 namespace v8 { | 12 namespace v8 { |
12 namespace internal { | 13 namespace internal { |
13 | 14 |
14 typedef CodeStubAssembler::ResultMode ResultMode; | 15 typedef CodeStubAssembler::ResultMode ResultMode; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 void GenerateStringRelationalComparison(RelationalComparisonMode mode); | 58 void GenerateStringRelationalComparison(RelationalComparisonMode mode); |
58 | 59 |
59 Node* ToSmiBetweenZeroAnd(Node* context, Node* value, Node* limit); | 60 Node* ToSmiBetweenZeroAnd(Node* context, Node* value, Node* limit); |
60 | 61 |
61 Node* LoadSurrogatePairAt(Node* string, Node* length, Node* index, | 62 Node* LoadSurrogatePairAt(Node* string, Node* length, Node* index, |
62 UnicodeEncoding encoding); | 63 UnicodeEncoding encoding); |
63 | 64 |
64 void StringIndexOf(Node* receiver, Node* instance_type, Node* search_string, | 65 void StringIndexOf(Node* receiver, Node* instance_type, Node* search_string, |
65 Node* search_string_instance_type, Node* position, | 66 Node* search_string_instance_type, Node* position, |
66 std::function<void(Node*)> f_return); | 67 std::function<void(Node*)> f_return); |
68 | |
69 Node* IsNullOrUndefined(Node* const value); | |
70 void RequireObjectCoercible(Node* const context, Node* const value, | |
71 const char* method_name); | |
72 | |
73 Node* SmiIsNegative(Node* const value) { | |
74 return SmiLessThan(value, SmiConstant(0)); | |
75 } | |
76 | |
77 // Implements boilerplate logic for {match, split, replace, search} of the | |
78 // form: | |
79 // | |
80 // if (!IS_NULL_OR_UNDEFINED(object)) { | |
81 // var maybe_function = object[symbol]; | |
82 // if (!IS_UNDEFINED(maybe_function)) { | |
83 // return %_Call(maybe_function, ...); | |
84 // } | |
85 // } | |
86 // | |
87 // Contains fast paths for Smi and RegExp objects. | |
88 typedef std::function<Node*()> NodeFunction0; | |
89 typedef std::function<Node*(Node* fn)> NodeFunction1; | |
90 void MaybeCallFunctionAtSymbol(Node* const context, Node* const object, | |
91 Handle<Symbol> symbol, | |
92 const NodeFunction0& regexp_call, | |
93 const NodeFunction1& generic_call); | |
67 }; | 94 }; |
68 | 95 |
69 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) { | 96 void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) { |
70 // Here's pseudo-code for the algorithm below in case of kDontNegateResult | 97 // Here's pseudo-code for the algorithm below in case of kDontNegateResult |
71 // mode; for kNegateResult mode we properly negate the result. | 98 // mode; for kNegateResult mode we properly negate the result. |
72 // | 99 // |
73 // if (lhs == rhs) return true; | 100 // if (lhs == rhs) return true; |
74 // if (lhs->length() != rhs->length()) return false; | 101 // if (lhs->length() != rhs->length()) return false; |
75 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) { | 102 // if (lhs->IsInternalizedString() && rhs->IsInternalizedString()) { |
76 // return false; | 103 // return false; |
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1031 Handle<String> valid_forms = | 1058 Handle<String> valid_forms = |
1032 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); | 1059 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); |
1033 THROW_NEW_ERROR_RETURN_FAILURE( | 1060 THROW_NEW_ERROR_RETURN_FAILURE( |
1034 isolate, | 1061 isolate, |
1035 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); | 1062 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); |
1036 } | 1063 } |
1037 | 1064 |
1038 return *string; | 1065 return *string; |
1039 } | 1066 } |
1040 | 1067 |
1068 compiler::Node* StringBuiltinsAssembler::IsNullOrUndefined(Node* const value) { | |
1069 return Word32Or(IsUndefined(value), IsNull(value)); | |
1070 } | |
1071 | |
1072 void StringBuiltinsAssembler::RequireObjectCoercible(Node* const context, | |
1073 Node* const value, | |
1074 const char* method_name) { | |
1075 Label out(this); | |
1076 GotoUnless(IsNullOrUndefined(value), &out); | |
1077 | |
1078 // {value} is null or undefined, throw exception. | |
1079 | |
1080 TailCallRuntime( | |
1081 Runtime::kThrowCalledOnNullOrUndefined, context, | |
1082 HeapConstant(factory()->NewStringFromAsciiChecked(method_name, TENURED))); | |
1083 | |
1084 Bind(&out); | |
1085 } | |
1086 | |
1087 void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol( | |
1088 Node* const context, Node* const object, Handle<Symbol> symbol, | |
1089 const NodeFunction0& regexp_call, const NodeFunction1& generic_call) { | |
1090 Label out(this); | |
1091 | |
1092 // Smis definitely don't have an attached symbol. | |
1093 GotoIf(TaggedIsSmi(object), &out); | |
1094 | |
1095 Node* const object_map = LoadMap(object); | |
1096 | |
1097 // Skip the slow lookup for Strings. | |
1098 { | |
1099 Label next(this); | |
1100 | |
1101 GotoUnless(IsStringInstanceType(LoadMapInstanceType(object_map)), &next); | |
1102 | |
1103 Node* const native_context = LoadNativeContext(context); | |
1104 Node* const initial_proto_initial_map = LoadContextElement( | |
1105 native_context, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX); | |
1106 | |
1107 Node* const string_function = | |
1108 LoadContextElement(native_context, Context::STRING_FUNCTION_INDEX); | |
1109 Node* const proto_map = LoadMap(LoadMapPrototype(LoadMap(string_function))); | |
Benedikt Meurer
2017/01/31 15:02:36
This needs to check the String.prototype map rathe
jgruber
2017/02/01 09:01:13
You're right. I was confused because the stored St
| |
1110 | |
1111 Branch(WordEqual(proto_map, initial_proto_initial_map), &out, &next); | |
1112 | |
1113 Bind(&next); | |
1114 } | |
1115 | |
1116 // Take the fast path for RegExps. | |
1117 if (regexp_call != nullptr) { | |
1118 Label stub_call(this), slow_lookup(this); | |
1119 | |
1120 RegExpBuiltinsAssembler regexp_asm(state()); | |
1121 regexp_asm.BranchIfFastRegExp(context, object_map, &stub_call, | |
1122 &slow_lookup); | |
1123 | |
1124 Bind(&stub_call); | |
1125 Return(regexp_call()); | |
1126 | |
1127 Bind(&slow_lookup); | |
1128 } | |
1129 | |
1130 GotoIf(IsNullOrUndefined(object), &out); | |
1131 | |
1132 // Fall back to a slow lookup of {object[symbol]}. | |
1133 | |
1134 Callable getproperty_callable = CodeFactory::GetProperty(isolate()); | |
1135 Node* const key = HeapConstant(symbol); | |
1136 Node* const maybe_func = CallStub(getproperty_callable, context, object, key); | |
1137 | |
1138 GotoIf(IsUndefined(maybe_func), &out); | |
1139 | |
1140 // Attempt to call the function. | |
1141 | |
1142 Node* const result = generic_call(maybe_func); | |
1143 Return(result); | |
1144 | |
1145 Bind(&out); | |
1146 } | |
1147 | |
1148 // ES6 section 21.1.3.16 String.prototype.replace ( search, replace ) | |
1149 TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) { | |
1150 Label out(this); | |
1151 | |
1152 Node* const receiver = Parameter(0); | |
1153 Node* const search = Parameter(1); | |
1154 Node* const replace = Parameter(2); | |
1155 Node* const context = Parameter(5); | |
1156 | |
1157 Node* const smi_zero = SmiConstant(0); | |
1158 | |
1159 RequireObjectCoercible(context, receiver, "String.prototype.replace"); | |
1160 | |
1161 // Redirect to replacer method if {search[@@replace]} is not undefined. | |
1162 // TODO(jgruber): Call RegExp.p.replace stub for fast path. | |
1163 | |
1164 MaybeCallFunctionAtSymbol( | |
1165 context, search, isolate()->factory()->replace_symbol(), nullptr, | |
1166 [this, context, search, receiver, replace](Node* fn) { | |
1167 Callable call_callable = CodeFactory::Call(isolate()); | |
1168 return CallJS(call_callable, context, fn, search, receiver, replace); | |
1169 }); | |
1170 | |
1171 // Convert {receiver} and {search} to strings. | |
1172 | |
1173 Callable tostring_callable = CodeFactory::ToString(isolate()); | |
1174 Node* const subject_string = CallStub(tostring_callable, context, receiver); | |
1175 Node* const search_string = CallStub(tostring_callable, context, search); | |
1176 | |
1177 Node* const subject_length = LoadStringLength(subject_string); | |
1178 Node* const search_length = LoadStringLength(search_string); | |
1179 | |
1180 // Fast-path single-char {search}, long {receiver}, and simple string | |
1181 // {replace}. | |
1182 { | |
1183 Label next(this); | |
1184 | |
1185 GotoUnless(SmiEqual(search_length, SmiConstant(1)), &next); | |
1186 GotoUnless(SmiGreaterThan(subject_length, SmiConstant(0xFF)), &next); | |
1187 GotoIf(TaggedIsSmi(replace), &next); | |
1188 GotoUnless(IsString(replace), &next); | |
1189 | |
1190 Node* const dollar_char = Int32Constant('$'); | |
1191 Node* const index_of_dollar = | |
1192 StringIndexOfChar(context, replace, dollar_char, smi_zero); | |
1193 GotoUnless(SmiIsNegative(index_of_dollar), &next); | |
1194 | |
1195 // Searching by traversing a cons string tree and replace with cons of | |
1196 // slices works only when the replaced string is a single character, being | |
1197 // replaced by a simple string and only pays off for long strings. | |
1198 // TODO(jgruber): Reevaluate if this is still beneficial. | |
1199 TailCallRuntime(Runtime::kStringReplaceOneCharWithString, context, | |
1200 subject_string, search_string, replace); | |
1201 | |
1202 Bind(&next); | |
1203 } | |
1204 | |
1205 // TODO(jgruber): Extend StringIndexOfChar to handle two-byte strings and | |
1206 // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars | |
1207 // (2-byte). | |
1208 | |
1209 Node* const match_start_index = | |
1210 CallRuntime(Runtime::kStringIndexOfUnchecked, context, subject_string, | |
1211 search_string, smi_zero); | |
1212 CSA_ASSERT(this, TaggedIsSmi(match_start_index)); | |
1213 | |
1214 // Early exit if no match found. | |
1215 { | |
1216 Label next(this); | |
1217 | |
1218 GotoUnless(SmiIsNegative(match_start_index), &next); | |
1219 Return(subject_string); | |
1220 | |
1221 Bind(&next); | |
1222 } | |
1223 | |
1224 Node* const match_end_index = SmiAdd(match_start_index, search_length); | |
1225 | |
1226 Callable substring_callable = CodeFactory::SubString(isolate()); | |
1227 Callable stringadd_callable = | |
1228 CodeFactory::StringAdd(isolate(), STRING_ADD_CHECK_NONE, NOT_TENURED); | |
1229 | |
1230 Variable var_result(this, MachineRepresentation::kTagged, | |
1231 EmptyStringConstant()); | |
1232 | |
1233 // Compute the prefix. | |
1234 { | |
1235 Label next(this); | |
1236 | |
1237 GotoIf(SmiEqual(match_start_index, smi_zero), &next); | |
1238 Node* const prefix = CallStub(substring_callable, context, subject_string, | |
1239 smi_zero, match_start_index); | |
1240 var_result.Bind(prefix); | |
1241 | |
1242 Goto(&next); | |
1243 Bind(&next); | |
1244 } | |
1245 | |
1246 // Compute the string to replace with. | |
1247 | |
1248 Label if_iscallablereplace(this), if_notcallablereplace(this); | |
1249 GotoIf(TaggedIsSmi(replace), &if_notcallablereplace); | |
1250 Branch(IsCallableMap(LoadMap(replace)), &if_iscallablereplace, | |
1251 &if_notcallablereplace); | |
1252 | |
1253 Bind(&if_iscallablereplace); | |
1254 { | |
1255 Callable call_callable = CodeFactory::Call(isolate()); | |
1256 Node* const replacement = | |
1257 CallJS(call_callable, context, replace, UndefinedConstant(), | |
1258 search_string, match_start_index, subject_string); | |
1259 Node* const replacement_string = | |
1260 CallStub(tostring_callable, context, replacement); | |
1261 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(), | |
1262 replacement_string)); | |
1263 Goto(&out); | |
1264 } | |
1265 | |
1266 Bind(&if_notcallablereplace); | |
1267 { | |
1268 Node* const replace_string = CallStub(tostring_callable, context, replace); | |
1269 | |
1270 // TODO(jgruber): Simplified GetSubstitution implementation in CSA. | |
1271 Node* const matched = CallStub(substring_callable, context, subject_string, | |
1272 match_start_index, match_end_index); | |
1273 Node* const replacement_string = | |
1274 CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string, | |
1275 match_start_index, replace_string); | |
1276 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(), | |
1277 replacement_string)); | |
1278 Goto(&out); | |
1279 } | |
1280 | |
1281 Bind(&out); | |
1282 { | |
1283 Node* const suffix = CallStub(substring_callable, context, subject_string, | |
1284 match_end_index, subject_length); | |
1285 Node* const result = | |
1286 CallStub(stringadd_callable, context, var_result.value(), suffix); | |
1287 Return(result); | |
1288 } | |
1289 } | |
1290 | |
1291 // ES6 section 21.1.3.19 String.prototype.split ( separator, limit ) | |
1292 TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) { | |
1293 Label out(this); | |
1294 | |
1295 Node* const receiver = Parameter(0); | |
1296 Node* const separator = Parameter(1); | |
1297 Node* const limit = Parameter(2); | |
1298 Node* const context = Parameter(5); | |
1299 | |
1300 Node* const smi_zero = SmiConstant(0); | |
1301 | |
1302 RequireObjectCoercible(context, receiver, "String.prototype.split"); | |
1303 | |
1304 // Redirect to splitter method if {separator[@@split]} is not undefined. | |
1305 // TODO(jgruber): Call RegExp.p.split stub for fast path. | |
1306 | |
1307 MaybeCallFunctionAtSymbol( | |
1308 context, separator, isolate()->factory()->split_symbol(), nullptr, | |
1309 [this, context, separator, receiver, limit](Node* fn) { | |
1310 Callable call_callable = CodeFactory::Call(isolate()); | |
1311 return CallJS(call_callable, context, fn, separator, receiver, limit); | |
1312 }); | |
1313 | |
1314 // String and integer conversions. | |
1315 // TODO(jgruber): The old implementation used Uint32Max instead of SmiMax - | |
1316 // but AFAIK there should not be a difference since arrays are capped at Smi | |
1317 // lengths. | |
1318 | |
1319 Callable tostring_callable = CodeFactory::ToString(isolate()); | |
1320 Node* const subject_string = CallStub(tostring_callable, context, receiver); | |
1321 Node* const limit_number = Select( | |
1322 IsUndefined(limit), [this]() { return SmiConstant(Smi::kMaxValue); }, | |
1323 [this, context, limit]() { return ToUint32(context, limit); }, | |
1324 MachineRepresentation::kTagged); | |
1325 Node* const separator_string = | |
1326 CallStub(tostring_callable, context, separator); | |
1327 | |
1328 // Shortcut for {limit} == 0. | |
1329 { | |
1330 Label next(this); | |
1331 GotoUnless(SmiEqual(limit_number, smi_zero), &next); | |
1332 | |
1333 const ElementsKind kind = FAST_ELEMENTS; | |
1334 const ParameterMode mode = CodeStubAssembler::INTPTR_PARAMETERS; | |
1335 | |
1336 Node* const allocation_site = nullptr; | |
1337 Node* const native_context = LoadNativeContext(context); | |
1338 Node* const array_map = LoadJSArrayElementsMap(kind, native_context); | |
1339 | |
1340 Node* const length = smi_zero; | |
1341 Node* const capacity = IntPtrConstant(0); | |
1342 Node* const result = AllocateJSArray(kind, array_map, capacity, length, | |
1343 allocation_site, mode); | |
1344 | |
1345 Return(result); | |
1346 | |
1347 Bind(&next); | |
1348 } | |
1349 | |
1350 // ECMA-262 says that if {separator} is undefined, the result should | |
1351 // be an array of size 1 containing the entire string. | |
1352 { | |
1353 Label next(this); | |
1354 GotoUnless(IsUndefined(separator), &next); | |
1355 | |
1356 const ElementsKind kind = FAST_ELEMENTS; | |
1357 const ParameterMode mode = CodeStubAssembler::INTPTR_PARAMETERS; | |
1358 | |
1359 Node* const allocation_site = nullptr; | |
1360 Node* const native_context = LoadNativeContext(context); | |
1361 Node* const array_map = LoadJSArrayElementsMap(kind, native_context); | |
1362 | |
1363 Node* const length = SmiConstant(1); | |
1364 Node* const capacity = IntPtrConstant(1); | |
1365 Node* const result = AllocateJSArray(kind, array_map, capacity, length, | |
1366 allocation_site, mode); | |
1367 | |
1368 Node* const fixed_array = LoadElements(result); | |
1369 StoreFixedArrayElement(fixed_array, 0, subject_string); | |
1370 | |
1371 Return(result); | |
1372 | |
1373 Bind(&next); | |
1374 } | |
1375 | |
1376 // If the separator string is empty then return the elements in the subject. | |
1377 { | |
1378 Label next(this); | |
1379 GotoUnless(SmiEqual(LoadStringLength(separator_string), smi_zero), &next); | |
1380 | |
1381 Node* const result = CallRuntime(Runtime::kStringToArray, context, | |
1382 subject_string, limit_number); | |
1383 Return(result); | |
1384 | |
1385 Bind(&next); | |
1386 } | |
1387 | |
1388 Node* const result = | |
1389 CallRuntime(Runtime::kStringSplit, context, subject_string, | |
1390 separator_string, limit_number); | |
1391 Return(result); | |
1392 } | |
1393 | |
1041 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) | 1394 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) |
1042 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { | 1395 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { |
1043 Label out(this), handle_length(this); | 1396 Label out(this), handle_length(this); |
1044 | 1397 |
1045 Variable var_start(this, MachineRepresentation::kTagged); | 1398 Variable var_start(this, MachineRepresentation::kTagged); |
1046 Variable var_length(this, MachineRepresentation::kTagged); | 1399 Variable var_length(this, MachineRepresentation::kTagged); |
1047 | 1400 |
1048 Node* const receiver = Parameter(0); | 1401 Node* const receiver = Parameter(0); |
1049 Node* const start = Parameter(1); | 1402 Node* const start = Parameter(1); |
1050 Node* const length = Parameter(2); | 1403 Node* const length = Parameter(2); |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1495 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, | 1848 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, |
1496 HeapConstant(factory()->NewStringFromAsciiChecked( | 1849 HeapConstant(factory()->NewStringFromAsciiChecked( |
1497 "String Iterator.prototype.next", TENURED)), | 1850 "String Iterator.prototype.next", TENURED)), |
1498 iterator); | 1851 iterator); |
1499 Return(result); // Never reached. | 1852 Return(result); // Never reached. |
1500 } | 1853 } |
1501 } | 1854 } |
1502 | 1855 |
1503 } // namespace internal | 1856 } // namespace internal |
1504 } // namespace v8 | 1857 } // namespace v8 |
OLD | NEW |