// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_JSON_STRINGIFIER_H_ #define V8_JSON_STRINGIFIER_H_ #include "src/objects.h" #include "src/string-builder.h" namespace v8 { namespace internal { class JsonStringifier BASE_EMBEDDED { public: explicit JsonStringifier(Isolate* isolate); ~JsonStringifier() { DeleteArray(gap_); } V8_WARN_UNUSED_RESULT MaybeHandle Stringify(Handle object, Handle replacer, Handle gap); private: enum Result { UNCHANGED, SUCCESS, EXCEPTION }; bool InitializeReplacer(Handle replacer); bool InitializeGap(Handle gap); V8_WARN_UNUSED_RESULT MaybeHandle ApplyToJsonFunction( Handle object, Handle key); V8_WARN_UNUSED_RESULT MaybeHandle ApplyReplacerFunction( Handle value, Handle key, Handle initial_holder); // Entry point to serialize the object. INLINE(Result SerializeObject(Handle obj)) { return Serialize_(obj, false, factory()->empty_string()); } // Serialize an array element. // The index may serve as argument for the toJSON function. INLINE(Result SerializeElement(Isolate* isolate, Handle object, int i)) { return Serialize_(object, false, Handle(Smi::FromInt(i), isolate)); } // Serialize a object property. // The key may or may not be serialized depending on the property. // The key may also serve as argument for the toJSON function. INLINE(Result SerializeProperty(Handle object, bool deferred_comma, Handle deferred_key)) { DCHECK(!deferred_key.is_null()); return Serialize_(object, deferred_comma, deferred_key); } template Result Serialize_(Handle object, bool comma, Handle key); INLINE(void SerializeDeferredKey(bool deferred_comma, Handle deferred_key)); Result SerializeSmi(Smi* object); Result SerializeDouble(double number); INLINE(Result SerializeHeapNumber(Handle object)) { return SerializeDouble(object->value()); } Result SerializeJSValue(Handle object); INLINE(Result SerializeJSArray(Handle object)); INLINE(Result SerializeJSObject(Handle object)); Result SerializeJSProxy(Handle object); Result SerializeJSReceiverSlow(Handle object); Result SerializeArrayLikeSlow(Handle object, uint32_t start, uint32_t length); void SerializeString(Handle object); template INLINE(static void SerializeStringUnchecked_( Vector src, IncrementalStringBuilder::NoExtend* dest)); template INLINE(void SerializeString_(Handle string)); template INLINE(static bool DoNotEscape(Char c)); INLINE(void NewLine()); INLINE(void Indent() { indent_++; }); INLINE(void Unindent() { indent_--; }); INLINE(void Separator(bool first)); Handle CurrentHolder(Handle value, Handle inital_holder); Result StackPush(Handle object); void StackPop(); Factory* factory() { return isolate_->factory(); } Isolate* isolate_; IncrementalStringBuilder builder_; Handle tojson_string_; Handle stack_; Handle property_list_; Handle replacer_function_; uc16* gap_; int indent_; static const int kJsonEscapeTableEntrySize = 8; static const char* const JsonEscapeTable; }; } // namespace internal } // namespace v8 #endif // V8_JSON_STRINGIFIER_H_