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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 INLINE(void set_accumulator(Handle<String> string)) { | 154 INLINE(void set_accumulator(Handle<String> string)) { |
155 return accumulator_store_->set_value(*string); | 155 return accumulator_store_->set_value(*string); |
156 } | 156 } |
157 | 157 |
158 Isolate* isolate_; | 158 Isolate* isolate_; |
159 Factory* factory_; | 159 Factory* factory_; |
160 // We use a value wrapper for the string accumulator to keep the | 160 // We use a value wrapper for the string accumulator to keep the |
161 // (indirect) handle to it in the outermost handle scope. | 161 // (indirect) handle to it in the outermost handle scope. |
162 Handle<JSValue> accumulator_store_; | 162 Handle<JSValue> accumulator_store_; |
163 Handle<String> current_part_; | 163 Handle<String> current_part_; |
164 Handle<String> tojson_symbol_; | 164 Handle<String> tojson_string_; |
165 Handle<JSArray> stack_; | 165 Handle<JSArray> stack_; |
166 int current_index_; | 166 int current_index_; |
167 int part_length_; | 167 int part_length_; |
168 bool is_ascii_; | 168 bool is_ascii_; |
169 | 169 |
170 static const int kJsonEscapeTableEntrySize = 8; | 170 static const int kJsonEscapeTableEntrySize = 8; |
171 static const char* const JsonEscapeTable; | 171 static const char* const JsonEscapeTable; |
172 }; | 172 }; |
173 | 173 |
174 | 174 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 "|\0 }\0 ~\0 \177\0 "; | 209 "|\0 }\0 ~\0 \177\0 "; |
210 | 210 |
211 | 211 |
212 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) | 212 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) |
213 : isolate_(isolate), current_index_(0), is_ascii_(true) { | 213 : isolate_(isolate), current_index_(0), is_ascii_(true) { |
214 factory_ = isolate_->factory(); | 214 factory_ = isolate_->factory(); |
215 accumulator_store_ = Handle<JSValue>::cast( | 215 accumulator_store_ = Handle<JSValue>::cast( |
216 factory_->ToObject(factory_->empty_string())); | 216 factory_->ToObject(factory_->empty_string())); |
217 part_length_ = kInitialPartLength; | 217 part_length_ = kInitialPartLength; |
218 current_part_ = factory_->NewRawOneByteString(kInitialPartLength); | 218 current_part_ = factory_->NewRawOneByteString(kInitialPartLength); |
219 tojson_symbol_ = | 219 tojson_string_ = |
220 factory_->LookupOneByteSymbol(STATIC_ASCII_VECTOR("toJSON")); | 220 factory_->InternalizeOneByteString(STATIC_ASCII_VECTOR("toJSON")); |
221 stack_ = factory_->NewJSArray(8); | 221 stack_ = factory_->NewJSArray(8); |
222 } | 222 } |
223 | 223 |
224 | 224 |
225 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { | 225 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { |
226 switch (SerializeObject(object)) { | 226 switch (SerializeObject(object)) { |
227 case UNCHANGED: | 227 case UNCHANGED: |
228 return isolate_->heap()->undefined_value(); | 228 return isolate_->heap()->undefined_value(); |
229 case SUCCESS: | 229 case SUCCESS: |
230 ShrinkCurrentPart(); | 230 ShrinkCurrentPart(); |
(...skipping 24 matching lines...) Expand all Loading... |
255 | 255 |
256 template <bool is_ascii, typename Char> | 256 template <bool is_ascii, typename Char> |
257 void BasicJsonStringifier::Append_(const Char* chars) { | 257 void BasicJsonStringifier::Append_(const Char* chars) { |
258 for ( ; *chars != '\0'; chars++) Append_<is_ascii, Char>(*chars); | 258 for ( ; *chars != '\0'; chars++) Append_<is_ascii, Char>(*chars); |
259 } | 259 } |
260 | 260 |
261 | 261 |
262 Handle<Object> BasicJsonStringifier::ApplyToJsonFunction( | 262 Handle<Object> BasicJsonStringifier::ApplyToJsonFunction( |
263 Handle<Object> object, Handle<Object> key) { | 263 Handle<Object> object, Handle<Object> key) { |
264 LookupResult lookup(isolate_); | 264 LookupResult lookup(isolate_); |
265 JSObject::cast(*object)->LookupRealNamedProperty(*tojson_symbol_, &lookup); | 265 JSObject::cast(*object)->LookupRealNamedProperty(*tojson_string_, &lookup); |
266 if (!lookup.IsProperty()) return object; | 266 if (!lookup.IsProperty()) return object; |
267 PropertyAttributes attr; | 267 PropertyAttributes attr; |
268 Handle<Object> fun = | 268 Handle<Object> fun = |
269 Object::GetProperty(object, object, &lookup, tojson_symbol_, &attr); | 269 Object::GetProperty(object, object, &lookup, tojson_string_, &attr); |
270 if (!fun->IsJSFunction()) return object; | 270 if (!fun->IsJSFunction()) return object; |
271 | 271 |
272 // Call toJSON function. | 272 // Call toJSON function. |
273 if (key->IsSmi()) key = factory_->NumberToString(key); | 273 if (key->IsSmi()) key = factory_->NumberToString(key); |
274 Handle<Object> argv[] = { key }; | 274 Handle<Object> argv[] = { key }; |
275 bool has_exception = false; | 275 bool has_exception = false; |
276 HandleScope scope(isolate_); | 276 HandleScope scope(isolate_); |
277 object = Execution::Call(fun, object, 1, argv, &has_exception); | 277 object = Execution::Call(fun, object, 1, argv, &has_exception); |
278 // Return empty handle to signal an exception. | 278 // Return empty handle to signal an exception. |
279 if (has_exception) return Handle<Object>::null(); | 279 if (has_exception) return Handle<Object>::null(); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 // Attach result string to the accumulator. | 392 // Attach result string to the accumulator. |
393 set_accumulator(factory_->NewConsString(accumulator(), result_string)); | 393 set_accumulator(factory_->NewConsString(accumulator(), result_string)); |
394 return SUCCESS; | 394 return SUCCESS; |
395 } | 395 } |
396 | 396 |
397 | 397 |
398 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( | 398 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( |
399 Handle<JSValue> object) { | 399 Handle<JSValue> object) { |
400 bool has_exception = false; | 400 bool has_exception = false; |
401 String* class_name = object->class_name(); | 401 String* class_name = object->class_name(); |
402 if (class_name == isolate_->heap()->String_symbol()) { | 402 if (class_name == isolate_->heap()->String_string()) { |
403 Handle<Object> value = Execution::ToString(object, &has_exception); | 403 Handle<Object> value = Execution::ToString(object, &has_exception); |
404 if (has_exception) return EXCEPTION; | 404 if (has_exception) return EXCEPTION; |
405 SerializeString(Handle<String>::cast(value)); | 405 SerializeString(Handle<String>::cast(value)); |
406 } else if (class_name == isolate_->heap()->Number_symbol()) { | 406 } else if (class_name == isolate_->heap()->Number_string()) { |
407 Handle<Object> value = Execution::ToNumber(object, &has_exception); | 407 Handle<Object> value = Execution::ToNumber(object, &has_exception); |
408 if (has_exception) return EXCEPTION; | 408 if (has_exception) return EXCEPTION; |
409 if (value->IsSmi()) return SerializeSmi(Smi::cast(*value)); | 409 if (value->IsSmi()) return SerializeSmi(Smi::cast(*value)); |
410 SerializeHeapNumber(Handle<HeapNumber>::cast(value)); | 410 SerializeHeapNumber(Handle<HeapNumber>::cast(value)); |
411 } else { | 411 } else { |
412 ASSERT(class_name == isolate_->heap()->Boolean_symbol()); | 412 ASSERT(class_name == isolate_->heap()->Boolean_string()); |
413 Object* value = JSValue::cast(*object)->value(); | 413 Object* value = JSValue::cast(*object)->value(); |
414 ASSERT(value->IsBoolean()); | 414 ASSERT(value->IsBoolean()); |
415 AppendAscii(value->IsTrue() ? "true" : "false"); | 415 AppendAscii(value->IsTrue() ? "true" : "false"); |
416 } | 416 } |
417 return SUCCESS; | 417 return SUCCESS; |
418 } | 418 } |
419 | 419 |
420 | 420 |
421 BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) { | 421 BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) { |
422 static const int kBufferSize = 100; | 422 static const int kBufferSize = 100; |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 SerializeString_<false, uint8_t>(object); | 736 SerializeString_<false, uint8_t>(object); |
737 } else { | 737 } else { |
738 SerializeString_<false, uc16>(object); | 738 SerializeString_<false, uc16>(object); |
739 } | 739 } |
740 } | 740 } |
741 } | 741 } |
742 | 742 |
743 } } // namespace v8::internal | 743 } } // namespace v8::internal |
744 | 744 |
745 #endif // V8_JSON_STRINGIFIER_H_ | 745 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |