| 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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 if (worst_case_length > 32 * KB) { // Slow path if too large. | 293 if (worst_case_length > 32 * KB) { // Slow path if too large. |
| 294 BasicJsonStringifier stringifier(isolate); | 294 BasicJsonStringifier stringifier(isolate); |
| 295 return stringifier.Stringify(object); | 295 return stringifier.Stringify(object); |
| 296 } | 296 } |
| 297 | 297 |
| 298 FlattenString(object); | 298 FlattenString(object); |
| 299 ASSERT(object->IsFlat()); | 299 ASSERT(object->IsFlat()); |
| 300 if (object->IsOneByteRepresentationUnderneath()) { | 300 if (object->IsOneByteRepresentationUnderneath()) { |
| 301 Handle<String> result = | 301 Handle<String> result = |
| 302 isolate->factory()->NewRawOneByteString(worst_case_length); | 302 isolate->factory()->NewRawOneByteString(worst_case_length); |
| 303 AssertNoAllocation no_alloc; | 303 DisallowHeapAllocation no_gc; |
| 304 return StringifyString_<SeqOneByteString>( | 304 return StringifyString_<SeqOneByteString>( |
| 305 isolate, | 305 isolate, |
| 306 object->GetFlatContent().ToOneByteVector(), | 306 object->GetFlatContent().ToOneByteVector(), |
| 307 result); | 307 result); |
| 308 } else { | 308 } else { |
| 309 Handle<String> result = | 309 Handle<String> result = |
| 310 isolate->factory()->NewRawTwoByteString(worst_case_length); | 310 isolate->factory()->NewRawTwoByteString(worst_case_length); |
| 311 AssertNoAllocation no_alloc; | 311 DisallowHeapAllocation no_gc; |
| 312 return StringifyString_<SeqTwoByteString>( | 312 return StringifyString_<SeqTwoByteString>( |
| 313 isolate, | 313 isolate, |
| 314 object->GetFlatContent().ToUC16Vector(), | 314 object->GetFlatContent().ToUC16Vector(), |
| 315 result); | 315 result); |
| 316 } | 316 } |
| 317 } | 317 } |
| 318 | 318 |
| 319 | 319 |
| 320 template <typename ResultType, typename Char> | 320 template <typename ResultType, typename Char> |
| 321 MaybeObject* BasicJsonStringifier::StringifyString_(Isolate* isolate, | 321 MaybeObject* BasicJsonStringifier::StringifyString_(Isolate* isolate, |
| 322 Vector<Char> vector, | 322 Vector<Char> vector, |
| 323 Handle<String> result) { | 323 Handle<String> result) { |
| 324 AssertNoAllocation no_allocation; | 324 DisallowHeapAllocation no_gc; |
| 325 int final_size = 0; | 325 int final_size = 0; |
| 326 ResultType* dest = ResultType::cast(*result); | 326 ResultType* dest = ResultType::cast(*result); |
| 327 dest->Set(final_size++, '\"'); | 327 dest->Set(final_size++, '\"'); |
| 328 final_size += SerializeStringUnchecked_(vector.start(), | 328 final_size += SerializeStringUnchecked_(vector.start(), |
| 329 dest->GetChars() + 1, | 329 dest->GetChars() + 1, |
| 330 vector.length()); | 330 vector.length()); |
| 331 dest->Set(final_size++, '\"'); | 331 dest->Set(final_size++, '\"'); |
| 332 return *SeqString::Truncate(Handle<SeqString>::cast(result), final_size); | 332 return *SeqString::Truncate(Handle<SeqString>::cast(result), final_size); |
| 333 } | 333 } |
| 334 | 334 |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 template <bool is_ascii, typename Char> | 752 template <bool is_ascii, typename Char> |
| 753 void BasicJsonStringifier::SerializeString_(Handle<String> string) { | 753 void BasicJsonStringifier::SerializeString_(Handle<String> string) { |
| 754 int length = string->length(); | 754 int length = string->length(); |
| 755 Append_<is_ascii, char>('"'); | 755 Append_<is_ascii, char>('"'); |
| 756 // We make a rough estimate to find out if the current string can be | 756 // We make a rough estimate to find out if the current string can be |
| 757 // serialized without allocating a new string part. The worst case length of | 757 // serialized without allocating a new string part. The worst case length of |
| 758 // an escaped character is 6. Shifting the remainin string length right by 3 | 758 // an escaped character is 6. Shifting the remainin string length right by 3 |
| 759 // is a more pessimistic estimate, but faster to calculate. | 759 // is a more pessimistic estimate, but faster to calculate. |
| 760 | 760 |
| 761 if (((part_length_ - current_index_) >> 3) > length) { | 761 if (((part_length_ - current_index_) >> 3) > length) { |
| 762 AssertNoAllocation no_allocation; | 762 DisallowHeapAllocation no_gc; |
| 763 Vector<const Char> vector = GetCharVector<Char>(string); | 763 Vector<const Char> vector = GetCharVector<Char>(string); |
| 764 if (is_ascii) { | 764 if (is_ascii) { |
| 765 current_index_ += SerializeStringUnchecked_( | 765 current_index_ += SerializeStringUnchecked_( |
| 766 vector.start(), | 766 vector.start(), |
| 767 SeqOneByteString::cast(*current_part_)->GetChars() + current_index_, | 767 SeqOneByteString::cast(*current_part_)->GetChars() + current_index_, |
| 768 length); | 768 length); |
| 769 } else { | 769 } else { |
| 770 current_index_ += SerializeStringUnchecked_( | 770 current_index_ += SerializeStringUnchecked_( |
| 771 vector.start(), | 771 vector.start(), |
| 772 SeqTwoByteString::cast(*current_part_)->GetChars() + current_index_, | 772 SeqTwoByteString::cast(*current_part_)->GetChars() + current_index_, |
| 773 length); | 773 length); |
| 774 } | 774 } |
| 775 } else { | 775 } else { |
| 776 String* string_location = NULL; | 776 String* string_location = NULL; |
| 777 Vector<const Char> vector(NULL, 0); | 777 Vector<const Char> vector(NULL, 0); |
| 778 for (int i = 0; i < length; i++) { | 778 for (int i = 0; i < length; i++) { |
| 779 // If GC moved the string, we need to refresh the vector. | 779 // If GC moved the string, we need to refresh the vector. |
| 780 if (*string != string_location) { | 780 if (*string != string_location) { |
| 781 AssertNoAllocation no_gc; | 781 DisallowHeapAllocation no_gc; |
| 782 // This does not actually prevent the string from being relocated later. | 782 // This does not actually prevent the string from being relocated later. |
| 783 vector = GetCharVector<Char>(string); | 783 vector = GetCharVector<Char>(string); |
| 784 string_location = *string; | 784 string_location = *string; |
| 785 } | 785 } |
| 786 Char c = vector[i]; | 786 Char c = vector[i]; |
| 787 if (DoNotEscape(c)) { | 787 if (DoNotEscape(c)) { |
| 788 Append_<is_ascii, Char>(c); | 788 Append_<is_ascii, Char>(c); |
| 789 } else { | 789 } else { |
| 790 Append_<is_ascii, uint8_t>(reinterpret_cast<const uint8_t*>( | 790 Append_<is_ascii, uint8_t>(reinterpret_cast<const uint8_t*>( |
| 791 &JsonEscapeTable[c * kJsonEscapeTableEntrySize])); | 791 &JsonEscapeTable[c * kJsonEscapeTableEntrySize])); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 SerializeString_<false, uint8_t>(object); | 840 SerializeString_<false, uint8_t>(object); |
| 841 } else { | 841 } else { |
| 842 SerializeString_<false, uc16>(object); | 842 SerializeString_<false, uc16>(object); |
| 843 } | 843 } |
| 844 } | 844 } |
| 845 } | 845 } |
| 846 | 846 |
| 847 } } // namespace v8::internal | 847 } } // namespace v8::internal |
| 848 | 848 |
| 849 #endif // V8_JSON_STRINGIFIER_H_ | 849 #endif // V8_JSON_STRINGIFIER_H_ |
| OLD | NEW |