Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(897)

Side by Side Diff: src/json-stringifier.h

Issue 11421200: Fix JSON stringify. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 Result SerializeJSArraySlow(Handle<JSArray> object, int length); 129 Result SerializeJSArraySlow(Handle<JSArray> object, int length);
130 130
131 void SerializeString(Handle<String> object); 131 void SerializeString(Handle<String> object);
132 132
133 template <typename SrcChar, typename DestChar> 133 template <typename SrcChar, typename DestChar>
134 INLINE(void SerializeStringUnchecked_(const SrcChar* src, 134 INLINE(void SerializeStringUnchecked_(const SrcChar* src,
135 DestChar* dest, 135 DestChar* dest,
136 int length)); 136 int length));
137 137
138 template <bool is_ascii, typename Char> 138 template <bool is_ascii, typename Char>
139 INLINE(void SerializeString_(Vector<const Char> vector, 139 INLINE(void SerializeString_(Handle<String> string));
140 Handle<String> string));
141 140
142 template <typename Char> 141 template <typename Char>
143 INLINE(bool DoNotEscape(Char c)); 142 INLINE(bool DoNotEscape(Char c));
144 143
145 template <typename Char> 144 template <typename Char>
146 INLINE(Vector<const Char> GetCharVector(Handle<String> string)); 145 INLINE(Vector<const Char> GetCharVector(Handle<String> string));
147 146
148 Result StackPush(Handle<Object> object); 147 Result StackPush(Handle<Object> object);
149 void StackPop(); 148 void StackPop();
150 149
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 const char* chars = &JsonEscapeTable[c * kJsonEscapeTableEntrySize]; 667 const char* chars = &JsonEscapeTable[c * kJsonEscapeTableEntrySize];
669 while (*chars != '\0') *(dest++) = *(chars++); 668 while (*chars != '\0') *(dest++) = *(chars++);
670 } 669 }
671 } 670 }
672 671
673 current_index_ += static_cast<int>(dest - dest_start); 672 current_index_ += static_cast<int>(dest - dest_start);
674 } 673 }
675 674
676 675
677 template <bool is_ascii, typename Char> 676 template <bool is_ascii, typename Char>
678 void BasicJsonStringifier::SerializeString_(Vector<const Char> vector, 677 void BasicJsonStringifier::SerializeString_(Handle<String> string) {
679 Handle<String> string) { 678 int length = string->length();
680 int length = vector.length();
681 Append_<is_ascii, char>('"'); 679 Append_<is_ascii, char>('"');
682 // We make a rough estimate to find out if the current string can be 680 // We make a rough estimate to find out if the current string can be
683 // serialized without allocating a new string part. The worst case length of 681 // serialized without allocating a new string part. The worst case length of
684 // an escaped character is 6. Shifting the remainin string length right by 3 682 // an escaped character is 6. Shifting the remainin string length right by 3
685 // is a more pessimistic estimate, but faster to calculate. 683 // is a more pessimistic estimate, but faster to calculate.
686 684
687 if (((part_length_ - current_index_) >> 3) > length) { 685 if (((part_length_ - current_index_) >> 3) > length) {
686 AssertNoAllocation no_allocation;
687 Vector<const Char> vector = GetCharVector<Char>(string);
688 if (is_ascii) { 688 if (is_ascii) {
689 SerializeStringUnchecked_( 689 SerializeStringUnchecked_(
690 vector.start(), 690 vector.start(),
691 SeqOneByteString::cast(*current_part_)->GetChars(), 691 SeqOneByteString::cast(*current_part_)->GetChars(),
692 length); 692 length);
693 } else { 693 } else {
694 SerializeStringUnchecked_( 694 SerializeStringUnchecked_(
695 vector.start(), 695 vector.start(),
696 SeqTwoByteString::cast(*current_part_)->GetChars(), 696 SeqTwoByteString::cast(*current_part_)->GetChars(),
697 length); 697 length);
698 } 698 }
699 } else { 699 } else {
700 String* string_location = *string; 700 String* string_location = *string;
701 Vector<const Char> vector = GetCharVector<Char>(string);
701 for (int i = 0; i < length; i++) { 702 for (int i = 0; i < length; i++) {
702 Char c = vector[i]; 703 Char c = vector[i];
703 if (DoNotEscape(c)) { 704 if (DoNotEscape(c)) {
704 Append_<is_ascii, Char>(c); 705 Append_<is_ascii, Char>(c);
705 } else { 706 } else {
706 Append_<is_ascii, char>( 707 Append_<is_ascii, char>(
707 &JsonEscapeTable[c * kJsonEscapeTableEntrySize]); 708 &JsonEscapeTable[c * kJsonEscapeTableEntrySize]);
708 } 709 }
709 // If GC moved the string, we need to refresh the vector. 710 // If GC moved the string, we need to refresh the vector.
710 if (*string != string_location) { 711 if (*string != string_location) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 ASSERT(flat.IsTwoByte()); 745 ASSERT(flat.IsTwoByte());
745 return flat.ToUC16Vector(); 746 return flat.ToUC16Vector();
746 } 747 }
747 748
748 749
749 void BasicJsonStringifier::SerializeString(Handle<String> object) { 750 void BasicJsonStringifier::SerializeString(Handle<String> object) {
750 FlattenString(object); 751 FlattenString(object);
751 String::FlatContent flat = object->GetFlatContent(); 752 String::FlatContent flat = object->GetFlatContent();
752 if (is_ascii_) { 753 if (is_ascii_) {
753 if (flat.IsAscii()) { 754 if (flat.IsAscii()) {
754 SerializeString_<true, char>(flat.ToAsciiVector(), object); 755 SerializeString_<true, char>(object);
755 } else { 756 } else {
756 ChangeEncoding(); 757 ChangeEncoding();
757 SerializeString(object); 758 SerializeString(object);
758 } 759 }
759 } else { 760 } else {
760 if (flat.IsAscii()) { 761 if (flat.IsAscii()) {
761 SerializeString_<false, char>(flat.ToAsciiVector(), object); 762 SerializeString_<false, char>(object);
762 } else { 763 } else {
763 SerializeString_<false, uc16>(flat.ToUC16Vector(), object); 764 SerializeString_<false, uc16>(object);
764 } 765 }
765 } 766 }
766 } 767 }
767 768
768 } } // namespace v8::internal 769 } } // namespace v8::internal
769 770
770 #endif // V8_JSON_STRINGIFIER_H_ 771 #endif // V8_JSON_STRINGIFIER_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698