OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 4218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4229 } | 4229 } |
4230 } | 4230 } |
4231 | 4231 |
4232 if (!replacement->IsFlat()) replacement = FlattenGetString(replacement); | 4232 if (!replacement->IsFlat()) replacement = FlattenGetString(replacement); |
4233 | 4233 |
4234 return StringReplaceGlobalRegExpWithString( | 4234 return StringReplaceGlobalRegExpWithString( |
4235 isolate, subject, regexp, replacement, last_match_info); | 4235 isolate, subject, regexp, replacement, last_match_info); |
4236 } | 4236 } |
4237 | 4237 |
4238 | 4238 |
4239 Handle<String> StringReplaceOneCharWithString(Isolate* isolate, | 4239 // This may return an empty MaybeHandle if an exception is thrown or |
4240 Handle<String> subject, | 4240 // we abort due to reaching the recursion limit. |
4241 Handle<String> search, | 4241 MaybeHandle<String> StringReplaceOneCharWithString(Isolate* isolate, |
4242 Handle<String> replace, | 4242 Handle<String> subject, |
4243 bool* found, | 4243 Handle<String> search, |
4244 int recursion_limit) { | 4244 Handle<String> replace, |
4245 if (recursion_limit == 0) return Handle<String>::null(); | 4245 bool* found, |
| 4246 int recursion_limit) { |
| 4247 if (recursion_limit == 0) return MaybeHandle<String>(); |
| 4248 recursion_limit--; |
4246 if (subject->IsConsString()) { | 4249 if (subject->IsConsString()) { |
4247 ConsString* cons = ConsString::cast(*subject); | 4250 ConsString* cons = ConsString::cast(*subject); |
4248 Handle<String> first = Handle<String>(cons->first()); | 4251 Handle<String> first = Handle<String>(cons->first()); |
4249 Handle<String> second = Handle<String>(cons->second()); | 4252 Handle<String> second = Handle<String>(cons->second()); |
4250 Handle<String> new_first = | 4253 Handle<String> new_first; |
4251 StringReplaceOneCharWithString(isolate, | 4254 if (!StringReplaceOneCharWithString( |
4252 first, | 4255 isolate, first, search, replace, found, recursion_limit) |
4253 search, | 4256 .ToHandle(&new_first)) { |
4254 replace, | 4257 return MaybeHandle<String>(); |
4255 found, | 4258 } |
4256 recursion_limit - 1); | |
4257 if (new_first.is_null()) return new_first; | |
4258 if (*found) return isolate->factory()->NewConsString(new_first, second); | 4259 if (*found) return isolate->factory()->NewConsString(new_first, second); |
4259 | 4260 |
4260 Handle<String> new_second = | 4261 Handle<String> new_second; |
4261 StringReplaceOneCharWithString(isolate, | 4262 if (!StringReplaceOneCharWithString( |
4262 second, | 4263 isolate, second, search, replace, found, recursion_limit) |
4263 search, | 4264 .ToHandle(&new_second)) { |
4264 replace, | 4265 return MaybeHandle<String>(); |
4265 found, | 4266 } |
4266 recursion_limit - 1); | |
4267 if (new_second.is_null()) return new_second; | |
4268 if (*found) return isolate->factory()->NewConsString(first, new_second); | 4267 if (*found) return isolate->factory()->NewConsString(first, new_second); |
4269 | 4268 |
4270 return subject; | 4269 return subject; |
4271 } else { | 4270 } else { |
4272 int index = Runtime::StringMatch(isolate, subject, search, 0); | 4271 int index = Runtime::StringMatch(isolate, subject, search, 0); |
4273 if (index == -1) return subject; | 4272 if (index == -1) return subject; |
4274 *found = true; | 4273 *found = true; |
4275 Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); | 4274 Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); |
4276 Handle<String> cons1 = isolate->factory()->NewConsString(first, replace); | 4275 Handle<String> cons1; |
4277 RETURN_IF_EMPTY_HANDLE_VALUE(isolate, cons1, Handle<String>()); | 4276 ASSIGN_RETURN_ON_EXCEPTION( |
| 4277 isolate, cons1, |
| 4278 isolate->factory()->NewConsString(first, replace), |
| 4279 String); |
4278 Handle<String> second = | 4280 Handle<String> second = |
4279 isolate->factory()->NewSubString(subject, index + 1, subject->length()); | 4281 isolate->factory()->NewSubString(subject, index + 1, subject->length()); |
4280 return isolate->factory()->NewConsString(cons1, second); | 4282 return isolate->factory()->NewConsString(cons1, second); |
4281 } | 4283 } |
4282 } | 4284 } |
4283 | 4285 |
4284 | 4286 |
4285 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) { | 4287 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) { |
4286 HandleScope scope(isolate); | 4288 HandleScope scope(isolate); |
4287 ASSERT(args.length() == 3); | 4289 ASSERT(args.length() == 3); |
4288 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); | 4290 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
4289 CONVERT_ARG_HANDLE_CHECKED(String, search, 1); | 4291 CONVERT_ARG_HANDLE_CHECKED(String, search, 1); |
4290 CONVERT_ARG_HANDLE_CHECKED(String, replace, 2); | 4292 CONVERT_ARG_HANDLE_CHECKED(String, replace, 2); |
4291 | 4293 |
4292 // If the cons string tree is too deep, we simply abort the recursion and | 4294 // If the cons string tree is too deep, we simply abort the recursion and |
4293 // retry with a flattened subject string. | 4295 // retry with a flattened subject string. |
4294 const int kRecursionLimit = 0x1000; | 4296 const int kRecursionLimit = 0x1000; |
4295 bool found = false; | 4297 bool found = false; |
4296 Handle<String> result = StringReplaceOneCharWithString(isolate, | 4298 Handle<String> result; |
4297 subject, | 4299 if (StringReplaceOneCharWithString( |
4298 search, | 4300 isolate, subject, search, replace, &found, kRecursionLimit) |
4299 replace, | 4301 .ToHandle(&result)) { |
4300 &found, | 4302 return *result; |
4301 kRecursionLimit); | 4303 } |
4302 if (!result.is_null()) return *result; | |
4303 if (isolate->has_pending_exception()) return Failure::Exception(); | 4304 if (isolate->has_pending_exception()) return Failure::Exception(); |
4304 return *StringReplaceOneCharWithString(isolate, | 4305 |
4305 FlattenGetString(subject), | 4306 subject = FlattenGetString(subject); |
4306 search, | 4307 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
4307 replace, | 4308 isolate, result, |
4308 &found, | 4309 StringReplaceOneCharWithString( |
4309 kRecursionLimit); | 4310 isolate, subject, search, replace, &found, kRecursionLimit)); |
| 4311 return *result; |
4310 } | 4312 } |
4311 | 4313 |
4312 | 4314 |
4313 // Perform string match of pattern on subject, starting at start index. | 4315 // Perform string match of pattern on subject, starting at start index. |
4314 // Caller must ensure that 0 <= start_index <= sub->length(), | 4316 // Caller must ensure that 0 <= start_index <= sub->length(), |
4315 // and should check that pat->length() + start_index <= sub->length(). | 4317 // and should check that pat->length() + start_index <= sub->length(). |
4316 int Runtime::StringMatch(Isolate* isolate, | 4318 int Runtime::StringMatch(Isolate* isolate, |
4317 Handle<String> sub, | 4319 Handle<String> sub, |
4318 Handle<String> pat, | 4320 Handle<String> pat, |
4319 int start_index) { | 4321 int start_index) { |
(...skipping 1968 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6288 return *result; | 6290 return *result; |
6289 } | 6291 } |
6290 | 6292 |
6291 | 6293 |
6292 RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { | 6294 RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { |
6293 HandleScope scope(isolate); | 6295 HandleScope scope(isolate); |
6294 ASSERT(args.length() == 1); | 6296 ASSERT(args.length() == 1); |
6295 CONVERT_ARG_HANDLE_CHECKED(String, source, 0); | 6297 CONVERT_ARG_HANDLE_CHECKED(String, source, 0); |
6296 Handle<String> string = FlattenGetString(source); | 6298 Handle<String> string = FlattenGetString(source); |
6297 ASSERT(string->IsFlat()); | 6299 ASSERT(string->IsFlat()); |
6298 return string->IsOneByteRepresentationUnderneath() | 6300 Handle<String> result; |
6299 ? *URIUnescape::Unescape<uint8_t>(isolate, source) | 6301 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
6300 : *URIUnescape::Unescape<uc16>(isolate, source); | 6302 isolate, result, |
| 6303 string->IsOneByteRepresentationUnderneath() |
| 6304 ? URIUnescape::Unescape<uint8_t>(isolate, source) |
| 6305 : URIUnescape::Unescape<uc16>(isolate, source)); |
| 6306 return *result; |
6301 } | 6307 } |
6302 | 6308 |
6303 | 6309 |
6304 RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { | 6310 RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { |
6305 HandleScope scope(isolate); | 6311 HandleScope scope(isolate); |
6306 CONVERT_ARG_HANDLE_CHECKED(String, string, 0); | 6312 CONVERT_ARG_HANDLE_CHECKED(String, string, 0); |
6307 ASSERT(args.length() == 1); | 6313 ASSERT(args.length() == 1); |
6308 return BasicJsonStringifier::StringifyString(isolate, string); | 6314 return BasicJsonStringifier::StringifyString(isolate, string); |
6309 } | 6315 } |
6310 | 6316 |
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7060 return isolate->heap()->NumberFromInt32(x * y); | 7066 return isolate->heap()->NumberFromInt32(x * y); |
7061 } | 7067 } |
7062 | 7068 |
7063 | 7069 |
7064 RUNTIME_FUNCTION(MaybeObject*, RuntimeHidden_StringAdd) { | 7070 RUNTIME_FUNCTION(MaybeObject*, RuntimeHidden_StringAdd) { |
7065 HandleScope scope(isolate); | 7071 HandleScope scope(isolate); |
7066 ASSERT(args.length() == 2); | 7072 ASSERT(args.length() == 2); |
7067 CONVERT_ARG_HANDLE_CHECKED(String, str1, 0); | 7073 CONVERT_ARG_HANDLE_CHECKED(String, str1, 0); |
7068 CONVERT_ARG_HANDLE_CHECKED(String, str2, 1); | 7074 CONVERT_ARG_HANDLE_CHECKED(String, str2, 1); |
7069 isolate->counters()->string_add_runtime()->Increment(); | 7075 isolate->counters()->string_add_runtime()->Increment(); |
7070 Handle<String> result = isolate->factory()->NewConsString(str1, str2); | 7076 Handle<String> result; |
7071 RETURN_IF_EMPTY_HANDLE(isolate, result); | 7077 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 7078 isolate, result, isolate->factory()->NewConsString(str1, str2)); |
7072 return *result; | 7079 return *result; |
7073 } | 7080 } |
7074 | 7081 |
7075 | 7082 |
7076 template <typename sinkchar> | 7083 template <typename sinkchar> |
7077 static inline void StringBuilderConcatHelper(String* special, | 7084 static inline void StringBuilderConcatHelper(String* special, |
7078 sinkchar* sink, | 7085 sinkchar* sink, |
7079 FixedArray* fixed_array, | 7086 FixedArray* fixed_array, |
7080 int array_length) { | 7087 int array_length) { |
7081 int position = 0; | 7088 int position = 0; |
(...skipping 8140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15222 } | 15229 } |
15223 } | 15230 } |
15224 | 15231 |
15225 | 15232 |
15226 void Runtime::OutOfMemory() { | 15233 void Runtime::OutOfMemory() { |
15227 Heap::FatalProcessOutOfMemory("CALL_AND_RETRY_LAST", true); | 15234 Heap::FatalProcessOutOfMemory("CALL_AND_RETRY_LAST", true); |
15228 UNREACHABLE(); | 15235 UNREACHABLE(); |
15229 } | 15236 } |
15230 | 15237 |
15231 } } // namespace v8::internal | 15238 } } // namespace v8::internal |
OLD | NEW |