1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_STRING_STREAM_H_
6 #define V8_STRING_STREAM_H_
8 #include "src/allocation.h"
9 #include "src/base/smart-pointers.h"
10 #include "src/handles.h"
11 #include "src/vector.h"
16 // Forward declarations.
19 class StringAllocator {
21 virtual ~StringAllocator() { }
22 // Allocate a number of bytes.
23 virtual char* allocate(unsigned bytes) = 0;
24 // Allocate a larger number of bytes and copy the old buffer to the new one.
25 // bytes is an input and output parameter passing the old size of the buffer
26 // and returning the new size. If allocation fails then we return the old
27 // buffer and do not increase the size.
28 virtual char* grow(unsigned* bytes) = 0;
32 // Normal allocator uses new[] and delete[].
33 class HeapStringAllocator final : public StringAllocator {
35 ~HeapStringAllocator() { DeleteArray(space_); }
36 char* allocate(unsigned bytes) override;
37 char* grow(unsigned* bytes) override;
44 class FixedStringAllocator final : public StringAllocator {
46 FixedStringAllocator(char* buffer, unsigned length)
47 : buffer_(buffer), length_(length) {}
48 ~FixedStringAllocator() override{};
49 char* allocate(unsigned bytes) override;
50 char* grow(unsigned* bytes) override;
55 DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator);
61 FmtElm(int value) : type_(INT) { // NOLINT
64 explicit FmtElm(double value) : type_(DOUBLE) {
65 data_.u_double_ = value;
67 FmtElm(const char* value) : type_(C_STR) { // NOLINT
68 data_.u_c_str_ = value;
70 FmtElm(const Vector<const uc16>& value) : type_(LC_STR) { // NOLINT
71 data_.u_lc_str_ = &value;
73 FmtElm(Object* value) : type_(OBJ) { // NOLINT
76 FmtElm(Handle<Object> value) : type_(HANDLE) { // NOLINT
77 data_.u_handle_ = value.location();
79 FmtElm(void* value) : type_(POINTER) { // NOLINT
80 data_.u_pointer_ = value;
84 friend class StringStream;
85 enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER };
91 const Vector<const uc16>* u_lc_str_;
99 class StringStream final {
101 enum ObjectPrintMode { kPrintObjectConcise, kPrintObjectVerbose };
102 StringStream(StringAllocator* allocator,
103 ObjectPrintMode object_print_mode = kPrintObjectVerbose)
104 : allocator_(allocator),
105 object_print_mode_(object_print_mode),
106 capacity_(kInitialCapacity),
108 buffer_(allocator_->allocate(kInitialCapacity)) {
113 bool Put(String* str);
114 bool Put(String* str, int start, int end);
115 void Add(Vector<const char> format, Vector<FmtElm> elms);
116 void Add(const char* format);
117 void Add(Vector<const char> format);
118 void Add(const char* format, FmtElm arg0);
119 void Add(const char* format, FmtElm arg0, FmtElm arg1);
120 void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2);
121 void Add(const char* format,
126 void Add(const char* format,
133 // Getting the message out.
134 void OutputToFile(FILE* out);
135 void OutputToStdOut() { OutputToFile(stdout); }
136 void Log(Isolate* isolate);
137 Handle<String> ToString(Isolate* isolate);
138 base::SmartArrayPointer<const char> ToCString() const;
139 int length() const { return length_; }
141 // Object printing support.
142 void PrintName(Object* o);
143 void PrintFixedArray(FixedArray* array, unsigned int limit);
144 void PrintByteArray(ByteArray* ba);
145 void PrintUsingMap(JSObject* js_object);
146 void PrintPrototype(JSFunction* fun, Object* receiver);
147 void PrintSecurityTokenIfChanged(Object* function);
148 // NOTE: Returns the code in the output parameter.
149 void PrintFunction(Object* function, Object* receiver, Code** code);
157 // Mentioned object cache support.
158 void PrintMentionedObjectCache(Isolate* isolate);
159 static void ClearMentionedObjectCache(Isolate* isolate);
161 bool IsMentionedObjectCacheClear(Isolate* isolate);
164 static const int kInitialCapacity = 16;
167 void PrintObject(Object* obj);
169 StringAllocator* allocator_;
170 ObjectPrintMode object_print_mode_;
172 unsigned length_; // does not include terminating 0-character
175 bool full() const { return (capacity_ - length_) == 1; }
176 int space() const { return capacity_ - length_; }
178 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream);
181 } } // namespace v8::internal
183 #endif // V8_STRING_STREAM_H_