| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 } | 80 } |
| 81 | 81 |
| 82 Handle<Object> ApplyToJsonFunction(Handle<Object> object, | 82 Handle<Object> ApplyToJsonFunction(Handle<Object> object, |
| 83 Handle<Object> key); | 83 Handle<Object> key); |
| 84 | 84 |
| 85 Result SerializeGeneric(Handle<Object> object, | 85 Result SerializeGeneric(Handle<Object> object, |
| 86 Handle<Object> key, | 86 Handle<Object> key, |
| 87 bool deferred_comma, | 87 bool deferred_comma, |
| 88 bool deferred_key); | 88 bool deferred_key); |
| 89 | 89 |
| 90 template <typename StringType> | 90 template <typename ResultType, typename Char> |
| 91 INLINE(static MaybeObject* StringifyString_(Isolate* isolate, | 91 INLINE(static MaybeObject* StringifyString_(Isolate* isolate, |
| 92 Handle<String> string, | 92 Vector<Char> vector, |
| 93 Handle<String> result)); | 93 Handle<String> result)); |
| 94 | 94 |
| 95 // Entry point to serialize the object. | 95 // Entry point to serialize the object. |
| 96 INLINE(Result SerializeObject(Handle<Object> obj)) { | 96 INLINE(Result SerializeObject(Handle<Object> obj)) { |
| 97 return Serialize_<false>(obj, false, factory_->empty_string()); | 97 return Serialize_<false>(obj, false, factory_->empty_string()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Serialize an array element. | 100 // Serialize an array element. |
| 101 // The index may serve as argument for the toJSON function. | 101 // The index may serve as argument for the toJSON function. |
| 102 INLINE(Result SerializeElement(Isolate* isolate, | 102 INLINE(Result SerializeElement(Isolate* isolate, |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 static const int kJsonQuoteWorstCaseBlowup = 6; | 288 static const int kJsonQuoteWorstCaseBlowup = 6; |
| 289 static const int kSpaceForQuotes = 2; | 289 static const int kSpaceForQuotes = 2; |
| 290 int worst_case_length = | 290 int worst_case_length = |
| 291 object->length() * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; | 291 object->length() * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; |
| 292 | 292 |
| 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 object = FlattenGetString(object); | 298 FlattenString(object); |
| 299 if (object->IsSeqOneByteString()) { | 299 String::FlatContent flat = object->GetFlatContent(); |
| 300 if (flat.IsAscii()) { |
| 300 return StringifyString_<SeqOneByteString>( | 301 return StringifyString_<SeqOneByteString>( |
| 301 isolate, | 302 isolate, |
| 302 object, | 303 flat.ToOneByteVector(), |
| 303 isolate->factory()->NewRawOneByteString(worst_case_length)); | 304 isolate->factory()->NewRawOneByteString(worst_case_length)); |
| 304 } else { | 305 } else { |
| 305 return StringifyString_<SeqTwoByteString>( | 306 return StringifyString_<SeqTwoByteString>( |
| 306 isolate, | 307 isolate, |
| 307 object, | 308 flat.ToUC16Vector(), |
| 308 isolate->factory()->NewRawTwoByteString(worst_case_length)); | 309 isolate->factory()->NewRawTwoByteString(worst_case_length)); |
| 309 } | 310 } |
| 310 } | 311 } |
| 311 | 312 |
| 312 | 313 |
| 313 template <typename StringType> | 314 template <typename ResultType, typename Char> |
| 314 MaybeObject* BasicJsonStringifier::StringifyString_(Isolate* isolate, | 315 MaybeObject* BasicJsonStringifier::StringifyString_(Isolate* isolate, |
| 315 Handle<String> string, | 316 Vector<Char> vector, |
| 316 Handle<String> result) { | 317 Handle<String> result) { |
| 317 AssertNoAllocation no_allocation; | 318 AssertNoAllocation no_allocation; |
| 318 int final_size = 0; | 319 int final_size = 0; |
| 319 StringType* dest = StringType::cast(*result); | 320 ResultType* dest = ResultType::cast(*result); |
| 320 dest->Set(final_size++, '\"'); | 321 dest->Set(final_size++, '\"'); |
| 321 final_size += SerializeStringUnchecked_(StringType::cast(*string)->GetChars(), | 322 final_size += SerializeStringUnchecked_(vector.start(), |
| 322 dest->GetChars() + 1, | 323 dest->GetChars() + 1, |
| 323 string->length()); | 324 vector.length()); |
| 324 dest->Set(final_size++, '\"'); | 325 dest->Set(final_size++, '\"'); |
| 325 if (isolate->heap()->InNewSpace(*result)) { | 326 if (isolate->heap()->InNewSpace(*result)) { |
| 326 // In new space, simply lower the allocation top to fit the actual size. | 327 // In new space, simply lower the allocation top to fit the actual size. |
| 327 isolate->heap()->new_space()->ShrinkStringAtAllocationBoundary<StringType>( | 328 isolate->heap()->new_space()->ShrinkStringAtAllocationBoundary<ResultType>( |
| 328 *result, final_size); | 329 *result, final_size); |
| 329 return *result; | 330 return *result; |
| 330 } else { | 331 } else { |
| 331 // Not in new space, need to fill the wasted space with filler objects. | 332 // Not in new space, need to fill the wasted space with filler objects. |
| 332 return SeqString::cast(*result)->Truncate(final_size); | 333 return SeqString::cast(*result)->Truncate(final_size); |
| 333 } | 334 } |
| 334 } | 335 } |
| 335 | 336 |
| 336 | 337 |
| 337 template <bool is_ascii, typename Char> | 338 template <bool is_ascii, typename Char> |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 SerializeString_<false, uint8_t>(object); | 841 SerializeString_<false, uint8_t>(object); |
| 841 } else { | 842 } else { |
| 842 SerializeString_<false, uc16>(object); | 843 SerializeString_<false, uc16>(object); |
| 843 } | 844 } |
| 844 } | 845 } |
| 845 } | 846 } |
| 846 | 847 |
| 847 } } // namespace v8::internal | 848 } } // namespace v8::internal |
| 848 | 849 |
| 849 #endif // V8_JSON_STRINGIFIER_H_ | 850 #endif // V8_JSON_STRINGIFIER_H_ |
| OLD | NEW |