| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 static const int kInitialPartLength = 32; | 48 static const int kInitialPartLength = 32; |
| 49 static const int kMaxPartLength = 16 * 1024; | 49 static const int kMaxPartLength = 16 * 1024; |
| 50 static const int kPartLengthGrowthFactor = 2; | 50 static const int kPartLengthGrowthFactor = 2; |
| 51 | 51 |
| 52 enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW }; | 52 enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW }; |
| 53 | 53 |
| 54 void Extend(); | 54 void Extend(); |
| 55 | 55 |
| 56 void ChangeEncoding(); | 56 void ChangeEncoding(); |
| 57 | 57 |
| 58 void ShrinkCurrentPart(); | 58 INLINE(void ShrinkCurrentPart()); |
| 59 | 59 |
| 60 template <bool is_ascii, typename Char> | 60 template <bool is_ascii, typename Char> |
| 61 INLINE(void Append_(Char c)); | 61 INLINE(void Append_(Char c)); |
| 62 | 62 |
| 63 template <bool is_ascii, typename Char> | 63 template <bool is_ascii, typename Char> |
| 64 INLINE(void Append_(const Char* chars)); | 64 INLINE(void Append_(const Char* chars)); |
| 65 | 65 |
| 66 INLINE(void Append(uint8_t c)) { | 66 INLINE(void Append(uint8_t c)) { |
| 67 if (is_ascii_) { | 67 if (is_ascii_) { |
| 68 Append_<true>(c); | 68 Append_<true>(c); |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 Handle<String> string, | 315 Handle<String> string, |
| 316 Handle<String> result) { | 316 Handle<String> result) { |
| 317 AssertNoAllocation no_allocation; | 317 AssertNoAllocation no_allocation; |
| 318 int final_size = 0; | 318 int final_size = 0; |
| 319 StringType* dest = StringType::cast(*result); | 319 StringType* dest = StringType::cast(*result); |
| 320 dest->Set(final_size++, '\"'); | 320 dest->Set(final_size++, '\"'); |
| 321 final_size += SerializeStringUnchecked_(StringType::cast(*string)->GetChars(), | 321 final_size += SerializeStringUnchecked_(StringType::cast(*string)->GetChars(), |
| 322 dest->GetChars() + 1, | 322 dest->GetChars() + 1, |
| 323 string->length()); | 323 string->length()); |
| 324 dest->Set(final_size++, '\"'); | 324 dest->Set(final_size++, '\"'); |
| 325 if (isolate->heap()->InNewSpace(*result)) { | 325 return *SeqString::Truncate(Handle<SeqString>::cast(result), final_size); |
| 326 // In new space, simply lower the allocation top to fit the actual size. | |
| 327 isolate->heap()->new_space()->ShrinkStringAtAllocationBoundary<StringType>( | |
| 328 *result, final_size); | |
| 329 return *result; | |
| 330 } else { | |
| 331 // Not in new space, need to fill the wasted space with filler objects. | |
| 332 return SeqString::cast(*result)->Truncate(final_size); | |
| 333 } | |
| 334 } | 326 } |
| 335 | 327 |
| 336 | 328 |
| 337 template <bool is_ascii, typename Char> | 329 template <bool is_ascii, typename Char> |
| 338 void BasicJsonStringifier::Append_(Char c) { | 330 void BasicJsonStringifier::Append_(Char c) { |
| 339 if (is_ascii) { | 331 if (is_ascii) { |
| 340 SeqOneByteString::cast(*current_part_)->SeqOneByteStringSet( | 332 SeqOneByteString::cast(*current_part_)->SeqOneByteStringSet( |
| 341 current_index_++, c); | 333 current_index_++, c); |
| 342 } else { | 334 } else { |
| 343 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( | 335 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 | 682 |
| 691 Append('}'); | 683 Append('}'); |
| 692 StackPop(); | 684 StackPop(); |
| 693 current_part_ = handle_scope.CloseAndEscape(current_part_); | 685 current_part_ = handle_scope.CloseAndEscape(current_part_); |
| 694 return SUCCESS; | 686 return SUCCESS; |
| 695 } | 687 } |
| 696 | 688 |
| 697 | 689 |
| 698 void BasicJsonStringifier::ShrinkCurrentPart() { | 690 void BasicJsonStringifier::ShrinkCurrentPart() { |
| 699 ASSERT(current_index_ < part_length_); | 691 ASSERT(current_index_ < part_length_); |
| 700 current_part_ = Handle<String>( | 692 current_part_ = SeqString::Truncate(Handle<SeqString>::cast(current_part_), |
| 701 SeqString::cast(*current_part_)->Truncate(current_index_), isolate_); | 693 current_index_); |
| 702 } | 694 } |
| 703 | 695 |
| 704 | 696 |
| 705 void BasicJsonStringifier::Extend() { | 697 void BasicJsonStringifier::Extend() { |
| 706 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); | 698 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
| 707 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { | 699 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { |
| 708 part_length_ *= kPartLengthGrowthFactor; | 700 part_length_ *= kPartLengthGrowthFactor; |
| 709 } | 701 } |
| 710 if (is_ascii_) { | 702 if (is_ascii_) { |
| 711 current_part_ = factory_->NewRawOneByteString(part_length_); | 703 current_part_ = factory_->NewRawOneByteString(part_length_); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 SerializeString_<false, uint8_t>(object); | 832 SerializeString_<false, uint8_t>(object); |
| 841 } else { | 833 } else { |
| 842 SerializeString_<false, uc16>(object); | 834 SerializeString_<false, uc16>(object); |
| 843 } | 835 } |
| 844 } | 836 } |
| 845 } | 837 } |
| 846 | 838 |
| 847 } } // namespace v8::internal | 839 } } // namespace v8::internal |
| 848 | 840 |
| 849 #endif // V8_JSON_STRINGIFIER_H_ | 841 #endif // V8_JSON_STRINGIFIER_H_ |
| OLD | NEW |