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