Chromium Code Reviews| 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 21 matching lines...) Expand all Loading... | |
| 32 #include "v8utils.h" | 32 #include "v8utils.h" |
| 33 #include "v8conversions.h" | 33 #include "v8conversions.h" |
| 34 | 34 |
| 35 namespace v8 { | 35 namespace v8 { |
| 36 namespace internal { | 36 namespace internal { |
| 37 | 37 |
| 38 class BasicJsonStringifier BASE_EMBEDDED { | 38 class BasicJsonStringifier BASE_EMBEDDED { |
| 39 public: | 39 public: |
| 40 explicit BasicJsonStringifier(Isolate* isolate); | 40 explicit BasicJsonStringifier(Isolate* isolate); |
| 41 | 41 |
| 42 MaybeObject* Stringify(Handle<Object> object); | 42 MUST_USE_RESULT MaybeHandle<Object> Stringify(Handle<Object> object); |
| 43 | 43 |
| 44 INLINE(static MaybeObject* StringifyString(Isolate* isolate, | 44 MUST_USE_RESULT INLINE(static MaybeHandle<Object> StringifyString( |
| 45 Handle<String> object)); | 45 Isolate* isolate, |
| 46 Handle<String> object)); | |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 static const int kInitialPartLength = 32; | 49 static const int kInitialPartLength = 32; |
| 49 static const int kMaxPartLength = 16 * 1024; | 50 static const int kMaxPartLength = 16 * 1024; |
| 50 static const int kPartLengthGrowthFactor = 2; | 51 static const int kPartLengthGrowthFactor = 2; |
| 51 | 52 |
| 52 enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW }; | 53 enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW }; |
| 53 | 54 |
| 54 void Accumulate(); | 55 void Accumulate(); |
| 55 | 56 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 84 MUST_USE_RESULT MaybeHandle<Object> ApplyToJsonFunction( | 85 MUST_USE_RESULT MaybeHandle<Object> ApplyToJsonFunction( |
| 85 Handle<Object> object, | 86 Handle<Object> object, |
| 86 Handle<Object> key); | 87 Handle<Object> key); |
| 87 | 88 |
| 88 Result SerializeGeneric(Handle<Object> object, | 89 Result SerializeGeneric(Handle<Object> object, |
| 89 Handle<Object> key, | 90 Handle<Object> key, |
| 90 bool deferred_comma, | 91 bool deferred_comma, |
| 91 bool deferred_key); | 92 bool deferred_key); |
| 92 | 93 |
| 93 template <typename ResultType, typename Char> | 94 template <typename ResultType, typename Char> |
| 94 INLINE(static MaybeObject* StringifyString_(Isolate* isolate, | 95 INLINE(static Handle<String> StringifyString_(Isolate* isolate, |
| 95 Vector<Char> vector, | 96 Vector<Char> vector, |
| 96 Handle<String> result)); | 97 Handle<String> result)); |
| 97 | 98 |
| 98 // Entry point to serialize the object. | 99 // Entry point to serialize the object. |
| 99 INLINE(Result SerializeObject(Handle<Object> obj)) { | 100 INLINE(Result SerializeObject(Handle<Object> obj)) { |
| 100 return Serialize_<false>(obj, false, factory_->empty_string()); | 101 return Serialize_<false>(obj, false, factory_->empty_string()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 // Serialize an array element. | 104 // Serialize an array element. |
| 104 // The index may serve as argument for the toJSON function. | 105 // The index may serve as argument for the toJSON function. |
| 105 INLINE(Result SerializeElement(Isolate* isolate, | 106 INLINE(Result SerializeElement(Isolate* isolate, |
| 106 Handle<Object> object, | 107 Handle<Object> object, |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 factory_ = isolate_->factory(); | 266 factory_ = isolate_->factory(); |
| 266 accumulator_store_ = Handle<JSValue>::cast( | 267 accumulator_store_ = Handle<JSValue>::cast( |
| 267 Object::ToObject(isolate, factory_->empty_string()).ToHandleChecked()); | 268 Object::ToObject(isolate, factory_->empty_string()).ToHandleChecked()); |
| 268 part_length_ = kInitialPartLength; | 269 part_length_ = kInitialPartLength; |
| 269 current_part_ = factory_->NewRawOneByteString(part_length_).ToHandleChecked(); | 270 current_part_ = factory_->NewRawOneByteString(part_length_).ToHandleChecked(); |
| 270 tojson_string_ = factory_->toJSON_string(); | 271 tojson_string_ = factory_->toJSON_string(); |
| 271 stack_ = factory_->NewJSArray(8); | 272 stack_ = factory_->NewJSArray(8); |
| 272 } | 273 } |
| 273 | 274 |
| 274 | 275 |
| 275 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { | 276 MaybeHandle<Object> BasicJsonStringifier::Stringify(Handle<Object> object) { |
| 276 switch (SerializeObject(object)) { | 277 switch (SerializeObject(object)) { |
| 277 case UNCHANGED: | 278 case UNCHANGED: |
| 278 return isolate_->heap()->undefined_value(); | 279 return isolate_->factory()->undefined_value(); |
| 279 case SUCCESS: { | 280 case SUCCESS: { |
| 280 ShrinkCurrentPart(); | 281 ShrinkCurrentPart(); |
| 281 Accumulate(); | 282 Accumulate(); |
| 282 if (overflowed_) return isolate_->ThrowInvalidStringLength(); | 283 if (overflowed_) { |
| 283 return *accumulator(); | 284 return isolate_->Throw<Object>( |
| 285 isolate_->factory()->NewInvalidStringLengthError()); | |
| 286 } | |
| 287 return accumulator(); | |
| 284 } | 288 } |
| 285 case CIRCULAR: | 289 case CIRCULAR: |
| 286 return isolate_->Throw(*factory_->NewTypeError( | 290 return isolate_->Throw<Object>(factory_->NewTypeError( |
| 287 "circular_structure", HandleVector<Object>(NULL, 0))); | 291 "circular_structure", HandleVector<Object>(NULL, 0))); |
| 288 case STACK_OVERFLOW: | 292 case STACK_OVERFLOW: |
| 289 return isolate_->StackOverflow(); | 293 isolate_->StackOverflow(); |
| 294 return MaybeHandle<Object>(); | |
| 290 default: | 295 default: |
|
Igor Sheludko
2014/04/15 11:50:51
"case EXCEPTION:" instead of "default:" to indicat
| |
| 291 return Failure::Exception(); | 296 return MaybeHandle<Object>(); |
| 292 } | 297 } |
| 293 } | 298 } |
| 294 | 299 |
| 295 | 300 |
| 296 MaybeObject* BasicJsonStringifier::StringifyString(Isolate* isolate, | 301 MaybeHandle<Object> BasicJsonStringifier::StringifyString( |
| 297 Handle<String> object) { | 302 Isolate* isolate, Handle<String> object) { |
| 298 static const int kJsonQuoteWorstCaseBlowup = 6; | 303 static const int kJsonQuoteWorstCaseBlowup = 6; |
| 299 static const int kSpaceForQuotes = 2; | 304 static const int kSpaceForQuotes = 2; |
| 300 int worst_case_length = | 305 int worst_case_length = |
| 301 object->length() * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; | 306 object->length() * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; |
| 302 | 307 |
| 303 if (worst_case_length > 32 * KB) { // Slow path if too large. | 308 if (worst_case_length > 32 * KB) { // Slow path if too large. |
| 304 BasicJsonStringifier stringifier(isolate); | 309 BasicJsonStringifier stringifier(isolate); |
| 305 return stringifier.Stringify(object); | 310 return stringifier.Stringify(object); |
| 306 } | 311 } |
| 307 | 312 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 321 DisallowHeapAllocation no_gc; | 326 DisallowHeapAllocation no_gc; |
| 322 return StringifyString_<SeqTwoByteString>( | 327 return StringifyString_<SeqTwoByteString>( |
| 323 isolate, | 328 isolate, |
| 324 object->GetFlatContent().ToUC16Vector(), | 329 object->GetFlatContent().ToUC16Vector(), |
| 325 result); | 330 result); |
| 326 } | 331 } |
| 327 } | 332 } |
| 328 | 333 |
| 329 | 334 |
| 330 template <typename ResultType, typename Char> | 335 template <typename ResultType, typename Char> |
| 331 MaybeObject* BasicJsonStringifier::StringifyString_(Isolate* isolate, | 336 Handle<String> BasicJsonStringifier::StringifyString_(Isolate* isolate, |
| 332 Vector<Char> vector, | 337 Vector<Char> vector, |
| 333 Handle<String> result) { | 338 Handle<String> result) { |
| 334 DisallowHeapAllocation no_gc; | 339 DisallowHeapAllocation no_gc; |
| 335 int final_size = 0; | 340 int final_size = 0; |
| 336 ResultType* dest = ResultType::cast(*result); | 341 ResultType* dest = ResultType::cast(*result); |
| 337 dest->Set(final_size++, '\"'); | 342 dest->Set(final_size++, '\"'); |
| 338 final_size += SerializeStringUnchecked_(vector.start(), | 343 final_size += SerializeStringUnchecked_(vector.start(), |
| 339 dest->GetChars() + 1, | 344 dest->GetChars() + 1, |
| 340 vector.length()); | 345 vector.length()); |
| 341 dest->Set(final_size++, '\"'); | 346 dest->Set(final_size++, '\"'); |
| 342 return *SeqString::Truncate(Handle<SeqString>::cast(result), final_size); | 347 return SeqString::Truncate(Handle<SeqString>::cast(result), final_size); |
| 343 } | 348 } |
| 344 | 349 |
| 345 | 350 |
| 346 template <bool is_ascii, typename Char> | 351 template <bool is_ascii, typename Char> |
| 347 void BasicJsonStringifier::Append_(Char c) { | 352 void BasicJsonStringifier::Append_(Char c) { |
| 348 if (is_ascii) { | 353 if (is_ascii) { |
| 349 SeqOneByteString::cast(*current_part_)->SeqOneByteStringSet( | 354 SeqOneByteString::cast(*current_part_)->SeqOneByteStringSet( |
| 350 current_index_++, c); | 355 current_index_++, c); |
| 351 } else { | 356 } else { |
| 352 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( | 357 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( |
| (...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 894 SerializeString_<false, uint8_t>(object); | 899 SerializeString_<false, uint8_t>(object); |
| 895 } else { | 900 } else { |
| 896 SerializeString_<false, uc16>(object); | 901 SerializeString_<false, uc16>(object); |
| 897 } | 902 } |
| 898 } | 903 } |
| 899 } | 904 } |
| 900 | 905 |
| 901 } } // namespace v8::internal | 906 } } // namespace v8::internal |
| 902 | 907 |
| 903 #endif // V8_JSON_STRINGIFIER_H_ | 908 #endif // V8_JSON_STRINGIFIER_H_ |
| OLD | NEW |