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), throw_exception(this, Label::kDeferred); | |
1076 Branch(IsNullOrUndefined(value), &throw_exception, &out); | |
1077 | |
1078 Bind(&throw_exception); | |
1079 TailCallRuntime( | |
1080 Runtime::kThrowCalledOnNullOrUndefined, context, | |
1081 HeapConstant(factory()->NewStringFromAsciiChecked(method_name, TENURED))); | |
1082 | |
1083 Bind(&out); | |
1084 } | |
1085 | |
1086 void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol( | |
1087 Node* const context, Node* const object, Handle<Symbol> symbol, | |
1088 const NodeFunction0& regexp_call, const NodeFunction1& generic_call) { | |
1089 Label out(this); | |
1090 | |
1091 // Smis definitely don't have an attached symbol. | |
1092 GotoIf(TaggedIsSmi(object), &out); | |
1093 | |
1094 Node* const object_map = LoadMap(object); | |
1095 | |
1096 // Skip the slow lookup for Strings. | |
1097 { | |
1098 Label next(this); | |
1099 | |
1100 GotoUnless(IsStringInstanceType(LoadMapInstanceType(object_map)), &next); | |
1101 | |
1102 Node* const native_context = LoadNativeContext(context); | |
1103 Node* const initial_proto_initial_map = LoadContextElement( | |
1104 native_context, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX); | |
1105 | |
1106 Node* const string_fun = | |
1107 LoadContextElement(native_context, Context::STRING_FUNCTION_INDEX); | |
1108 Node* const initial_map = | |
1109 LoadObjectField(string_fun, JSFunction::kPrototypeOrInitialMapOffset); | |
1110 Node* const proto_map = LoadMap(LoadMapPrototype(initial_map)); | |
1111 | |
1112 Branch(WordEqual(proto_map, initial_proto_initial_map), &out, &next); | |
1113 | |
1114 Bind(&next); | |
1115 } | |
1116 | |
1117 // Take the fast path for RegExps. | |
1118 if (regexp_call != nullptr) { | |
1119 Label stub_call(this), slow_lookup(this); | |
1120 | |
1121 RegExpBuiltinsAssembler regexp_asm(state()); | |
1122 regexp_asm.BranchIfFastRegExp(context, object_map, &stub_call, | |
1123 &slow_lookup); | |
1124 | |
1125 Bind(&stub_call); | |
1126 Return(regexp_call()); | |
1127 | |
1128 Bind(&slow_lookup); | |
1129 } | |
1130 | |
1131 GotoIf(IsNullOrUndefined(object), &out); | |
1132 | |
1133 // Fall back to a slow lookup of {object[symbol]}. | |
1134 | |
1135 Callable getproperty_callable = CodeFactory::GetProperty(isolate()); | |
1136 Node* const key = HeapConstant(symbol); | |
1137 Node* const maybe_func = CallStub(getproperty_callable, context, object, key); | |
1138 | |
1139 GotoIf(IsUndefined(maybe_func), &out); | |
1140 | |
1141 // Attempt to call the function. | |
1142 | |
1143 Node* const result = generic_call(maybe_func); | |
1144 Return(result); | |
1145 | |
1146 Bind(&out); | |
1147 } | |
1148 | |
1149 // ES6 section 21.1.3.16 String.prototype.replace ( search, replace ) | |
1150 TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) { | |
1151 Label out(this); | |
1152 | |
1153 Node* const receiver = Parameter(0); | |
1154 Node* const search = Parameter(1); | |
1155 Node* const replace = Parameter(2); | |
1156 Node* const context = Parameter(5); | |
1157 | |
1158 Node* const smi_zero = SmiConstant(0); | |
1159 | |
1160 RequireObjectCoercible(context, receiver, "String.prototype.replace"); | |
1161 | |
1162 // Redirect to replacer method if {search[@@replace]} is not undefined. | |
1163 // TODO(jgruber): Call RegExp.p.replace stub for fast path. | |
1164 | |
1165 MaybeCallFunctionAtSymbol( | |
1166 context, search, isolate()->factory()->replace_symbol(), nullptr, | |
1167 [=](Node* fn) { | |
1168 Callable call_callable = CodeFactory::Call(isolate()); | |
1169 return CallJS(call_callable, context, fn, search, receiver, replace); | |
1170 }); | |
1171 | |
1172 // Convert {receiver} and {search} to strings. | |
1173 | |
1174 Callable tostring_callable = CodeFactory::ToString(isolate()); | |
1175 Node* const subject_string = CallStub(tostring_callable, context, receiver); | |
1176 Node* const search_string = CallStub(tostring_callable, context, search); | |
1177 | |
1178 Node* const subject_length = LoadStringLength(subject_string); | |
1179 Node* const search_length = LoadStringLength(search_string); | |
1180 | |
1181 // Fast-path single-char {search}, long {receiver}, and simple string | |
1182 // {replace}. | |
1183 { | |
1184 Label next(this); | |
1185 | |
1186 GotoUnless(SmiEqual(search_length, SmiConstant(1)), &next); | |
1187 GotoUnless(SmiGreaterThan(subject_length, SmiConstant(0xFF)), &next); | |
1188 GotoIf(TaggedIsSmi(replace), &next); | |
1189 GotoUnless(IsString(replace), &next); | |
1190 | |
1191 Node* const dollar_char = Int32Constant('$'); | |
1192 Node* const index_of_dollar = | |
1193 StringIndexOfChar(context, replace, dollar_char, smi_zero); | |
1194 GotoUnless(SmiIsNegative(index_of_dollar), &next); | |
1195 | |
1196 // Searching by traversing a cons string tree and replace with cons of | |
1197 // slices works only when the replaced string is a single character, being | |
1198 // replaced by a simple string and only pays off for long strings. | |
1199 // TODO(jgruber): Reevaluate if this is still beneficial. | |
1200 TailCallRuntime(Runtime::kStringReplaceOneCharWithString, context, | |
1201 subject_string, search_string, replace); | |
1202 | |
1203 Bind(&next); | |
1204 } | |
1205 | |
1206 // TODO(jgruber): Extend StringIndexOfChar to handle two-byte strings and | |
1207 // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars | |
1208 // (2-byte). | |
1209 | |
1210 Callable indexof_stub = CodeFactory::StringIndexOf(isolate()); | |
1211 Node* const match_start_index = | |
1212 CallStub(indexof_stub, context, subject_string, search_string, smi_zero); | |
1213 CSA_ASSERT(this, TaggedIsSmi(match_start_index)); | |
1214 | |
1215 // Early exit if no match found. | |
1216 { | |
1217 Label next(this), return_subject(this); | |
1218 | |
1219 GotoUnless(SmiIsNegative(match_start_index), &next); | |
Igor Sheludko
2017/02/01 15:31:22
// The spec requires to perform ToString(replace)
jgruber
2017/02/02 07:49:40
Done.
| |
1220 GotoIf(IsCallableMap(LoadMap(replace)), &return_subject); | |
Igor Sheludko
2017/02/01 15:31:23
GotoIf(TaggedIsSmi(replace), &call_tostring);
jgruber
2017/02/02 07:49:40
Leftover comment?
Igor Sheludko
2017/02/02 07:57:40
Yep. Decided to add a comment above, but forgot to
| |
1221 | |
1222 // Call ToString(replace) and throw away the result. The call is required | |
Igor Sheludko
2017/02/01 15:31:22
I suggested another comment above.
jgruber
2017/02/02 07:49:40
Done.
| |
1223 // by the spec. | |
1224 CallStub(tostring_callable, context, replace); | |
Igor Sheludko
2017/02/01 15:31:22
// TODO(jgruber): consider introducing ToStringSid
jgruber
2017/02/02 07:49:40
Done.
| |
1225 Goto(&return_subject); | |
1226 | |
1227 Bind(&return_subject); | |
1228 Return(subject_string); | |
1229 | |
1230 Bind(&next); | |
1231 } | |
1232 | |
1233 Node* const match_end_index = SmiAdd(match_start_index, search_length); | |
1234 | |
1235 Callable substring_callable = CodeFactory::SubString(isolate()); | |
1236 Callable stringadd_callable = | |
1237 CodeFactory::StringAdd(isolate(), STRING_ADD_CHECK_NONE, NOT_TENURED); | |
1238 | |
1239 Variable var_result(this, MachineRepresentation::kTagged, | |
1240 EmptyStringConstant()); | |
1241 | |
1242 // Compute the prefix. | |
1243 { | |
1244 Label next(this); | |
1245 | |
1246 GotoIf(SmiEqual(match_start_index, smi_zero), &next); | |
1247 Node* const prefix = CallStub(substring_callable, context, subject_string, | |
1248 smi_zero, match_start_index); | |
1249 var_result.Bind(prefix); | |
1250 | |
1251 Goto(&next); | |
1252 Bind(&next); | |
1253 } | |
1254 | |
1255 // Compute the string to replace with. | |
1256 | |
1257 Label if_iscallablereplace(this), if_notcallablereplace(this); | |
1258 GotoIf(TaggedIsSmi(replace), &if_notcallablereplace); | |
1259 Branch(IsCallableMap(LoadMap(replace)), &if_iscallablereplace, | |
1260 &if_notcallablereplace); | |
1261 | |
1262 Bind(&if_iscallablereplace); | |
1263 { | |
1264 Callable call_callable = CodeFactory::Call(isolate()); | |
1265 Node* const replacement = | |
1266 CallJS(call_callable, context, replace, UndefinedConstant(), | |
1267 search_string, match_start_index, subject_string); | |
1268 Node* const replacement_string = | |
1269 CallStub(tostring_callable, context, replacement); | |
1270 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(), | |
1271 replacement_string)); | |
1272 Goto(&out); | |
1273 } | |
1274 | |
1275 Bind(&if_notcallablereplace); | |
1276 { | |
1277 Node* const replace_string = CallStub(tostring_callable, context, replace); | |
1278 | |
1279 // TODO(jgruber): Simplified GetSubstitution implementation in CSA. | |
1280 Node* const matched = CallStub(substring_callable, context, subject_string, | |
1281 match_start_index, match_end_index); | |
1282 Node* const replacement_string = | |
1283 CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string, | |
1284 match_start_index, replace_string); | |
1285 var_result.Bind(CallStub(stringadd_callable, context, var_result.value(), | |
1286 replacement_string)); | |
1287 Goto(&out); | |
1288 } | |
1289 | |
1290 Bind(&out); | |
1291 { | |
1292 Node* const suffix = CallStub(substring_callable, context, subject_string, | |
1293 match_end_index, subject_length); | |
1294 Node* const result = | |
1295 CallStub(stringadd_callable, context, var_result.value(), suffix); | |
1296 Return(result); | |
1297 } | |
1298 } | |
1299 | |
1300 // ES6 section 21.1.3.19 String.prototype.split ( separator, limit ) | |
1301 TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) { | |
1302 Label out(this); | |
1303 | |
1304 Node* const receiver = Parameter(0); | |
1305 Node* const separator = Parameter(1); | |
1306 Node* const limit = Parameter(2); | |
1307 Node* const context = Parameter(5); | |
1308 | |
1309 Node* const smi_zero = SmiConstant(0); | |
1310 | |
1311 RequireObjectCoercible(context, receiver, "String.prototype.split"); | |
1312 | |
1313 // Redirect to splitter method if {separator[@@split]} is not undefined. | |
1314 // TODO(jgruber): Call RegExp.p.split stub for fast path. | |
1315 | |
1316 MaybeCallFunctionAtSymbol( | |
1317 context, separator, isolate()->factory()->split_symbol(), nullptr, | |
1318 [=](Node* fn) { | |
1319 Callable call_callable = CodeFactory::Call(isolate()); | |
1320 return CallJS(call_callable, context, fn, separator, receiver, limit); | |
1321 }); | |
1322 | |
1323 // String and integer conversions. | |
1324 // TODO(jgruber): The old implementation used Uint32Max instead of SmiMax - | |
1325 // but AFAIK there should not be a difference since arrays are capped at Smi | |
1326 // lengths. | |
1327 | |
1328 Callable tostring_callable = CodeFactory::ToString(isolate()); | |
1329 Node* const subject_string = CallStub(tostring_callable, context, receiver); | |
1330 Node* const limit_number = | |
1331 Select(IsUndefined(limit), [=]() { return SmiConstant(Smi::kMaxValue); }, | |
1332 [=]() { return ToUint32(context, limit); }, | |
1333 MachineRepresentation::kTagged); | |
1334 Node* const separator_string = | |
1335 CallStub(tostring_callable, context, separator); | |
1336 | |
1337 // Shortcut for {limit} == 0. | |
1338 { | |
1339 Label next(this); | |
1340 GotoUnless(SmiEqual(limit_number, smi_zero), &next); | |
1341 | |
1342 const ElementsKind kind = FAST_ELEMENTS; | |
1343 Node* const native_context = LoadNativeContext(context); | |
1344 Node* const array_map = LoadJSArrayElementsMap(kind, native_context); | |
1345 | |
1346 Node* const length = smi_zero; | |
1347 Node* const capacity = IntPtrConstant(0); | |
1348 Node* const result = AllocateJSArray(kind, array_map, capacity, length); | |
1349 | |
1350 Return(result); | |
1351 | |
1352 Bind(&next); | |
1353 } | |
1354 | |
1355 // ECMA-262 says that if {separator} is undefined, the result should | |
1356 // be an array of size 1 containing the entire string. | |
1357 { | |
1358 Label next(this); | |
1359 GotoUnless(IsUndefined(separator), &next); | |
1360 | |
1361 const ElementsKind kind = FAST_ELEMENTS; | |
1362 Node* const native_context = LoadNativeContext(context); | |
1363 Node* const array_map = LoadJSArrayElementsMap(kind, native_context); | |
1364 | |
1365 Node* const length = SmiConstant(1); | |
1366 Node* const capacity = IntPtrConstant(1); | |
1367 Node* const result = AllocateJSArray(kind, array_map, capacity, length); | |
1368 | |
1369 Node* const fixed_array = LoadElements(result); | |
1370 StoreFixedArrayElement(fixed_array, 0, subject_string); | |
1371 | |
1372 Return(result); | |
1373 | |
1374 Bind(&next); | |
1375 } | |
1376 | |
1377 // If the separator string is empty then return the elements in the subject. | |
1378 { | |
1379 Label next(this); | |
1380 GotoUnless(SmiEqual(LoadStringLength(separator_string), smi_zero), &next); | |
1381 | |
1382 Node* const result = CallRuntime(Runtime::kStringToArray, context, | |
1383 subject_string, limit_number); | |
1384 Return(result); | |
1385 | |
1386 Bind(&next); | |
1387 } | |
1388 | |
1389 Node* const result = | |
1390 CallRuntime(Runtime::kStringSplit, context, subject_string, | |
1391 separator_string, limit_number); | |
1392 Return(result); | |
1393 } | |
1394 | |
1041 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) | 1395 // ES6 section B.2.3.1 String.prototype.substr ( start, length ) |
1042 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { | 1396 TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) { |
1043 Label out(this), handle_length(this); | 1397 Label out(this), handle_length(this); |
1044 | 1398 |
1045 Variable var_start(this, MachineRepresentation::kTagged); | 1399 Variable var_start(this, MachineRepresentation::kTagged); |
1046 Variable var_length(this, MachineRepresentation::kTagged); | 1400 Variable var_length(this, MachineRepresentation::kTagged); |
1047 | 1401 |
1048 Node* const receiver = Parameter(0); | 1402 Node* const receiver = Parameter(0); |
1049 Node* const start = Parameter(1); | 1403 Node* const start = Parameter(1); |
1050 Node* const length = Parameter(2); | 1404 Node* const length = Parameter(2); |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1495 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, | 1849 CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context, |
1496 HeapConstant(factory()->NewStringFromAsciiChecked( | 1850 HeapConstant(factory()->NewStringFromAsciiChecked( |
1497 "String Iterator.prototype.next", TENURED)), | 1851 "String Iterator.prototype.next", TENURED)), |
1498 iterator); | 1852 iterator); |
1499 Return(result); // Never reached. | 1853 Return(result); // Never reached. |
1500 } | 1854 } |
1501 } | 1855 } |
1502 | 1856 |
1503 } // namespace internal | 1857 } // namespace internal |
1504 } // namespace v8 | 1858 } // namespace v8 |
OLD | NEW |