blob: 03ea0620ad2f240fd977b8338b51b1124e087b8b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_STRING_STREAM_H_
6#define V8_STRING_STREAM_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/allocation.h"
9#include "src/base/smart-pointers.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/handles.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/vector.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012
Steve Blocka7e24c12009-10-30 11:49:00 +000013namespace v8 {
14namespace internal {
15
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016// Forward declarations.
17class ByteArray;
18
Steve Blocka7e24c12009-10-30 11:49:00 +000019class StringAllocator {
20 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021 virtual ~StringAllocator() { }
Steve Blocka7e24c12009-10-30 11:49:00 +000022 // 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;
29};
30
31
32// Normal allocator uses new[] and delete[].
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033class HeapStringAllocator final : public StringAllocator {
Steve Blocka7e24c12009-10-30 11:49:00 +000034 public:
35 ~HeapStringAllocator() { DeleteArray(space_); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 char* allocate(unsigned bytes) override;
37 char* grow(unsigned* bytes) override;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038
Steve Blocka7e24c12009-10-30 11:49:00 +000039 private:
40 char* space_;
41};
42
43
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044class FixedStringAllocator final : public StringAllocator {
45 public:
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;
51
52 private:
53 char* buffer_;
54 unsigned length_;
55 DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator);
56};
57
58
59class FmtElm final {
Steve Blocka7e24c12009-10-30 11:49:00 +000060 public:
61 FmtElm(int value) : type_(INT) { // NOLINT
62 data_.u_int_ = value;
63 }
64 explicit FmtElm(double value) : type_(DOUBLE) {
65 data_.u_double_ = value;
66 }
67 FmtElm(const char* value) : type_(C_STR) { // NOLINT
68 data_.u_c_str_ = value;
69 }
70 FmtElm(const Vector<const uc16>& value) : type_(LC_STR) { // NOLINT
71 data_.u_lc_str_ = &value;
72 }
73 FmtElm(Object* value) : type_(OBJ) { // NOLINT
74 data_.u_obj_ = value;
75 }
76 FmtElm(Handle<Object> value) : type_(HANDLE) { // NOLINT
77 data_.u_handle_ = value.location();
78 }
79 FmtElm(void* value) : type_(POINTER) { // NOLINT
80 data_.u_pointer_ = value;
81 }
Ben Murdoch589d6972011-11-30 16:04:58 +000082
Steve Blocka7e24c12009-10-30 11:49:00 +000083 private:
84 friend class StringStream;
85 enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER };
86 Type type_;
87 union {
88 int u_int_;
89 double u_double_;
90 const char* u_c_str_;
91 const Vector<const uc16>* u_lc_str_;
92 Object* u_obj_;
93 Object** u_handle_;
94 void* u_pointer_;
95 } data_;
96};
97
98
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099class StringStream final {
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101 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),
107 length_(0),
108 buffer_(allocator_->allocate(kInitialCapacity)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 buffer_[0] = 0;
110 }
111
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 bool Put(char c);
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,
122 FmtElm arg0,
123 FmtElm arg1,
124 FmtElm arg2,
125 FmtElm arg3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 void Add(const char* format,
127 FmtElm arg0,
128 FmtElm arg1,
129 FmtElm arg2,
130 FmtElm arg3,
131 FmtElm arg4);
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133 // Getting the message out.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100134 void OutputToFile(FILE* out);
135 void OutputToStdOut() { OutputToFile(stdout); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 void Log(Isolate* isolate);
137 Handle<String> ToString(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 base::SmartArrayPointer<const char> ToCString() const;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100139 int length() const { return length_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000140
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);
150
151 // Reset the stream.
152 void Reset() {
153 length_ = 0;
154 buffer_[0] = 0;
155 }
156
157 // Mentioned object cache support.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 void PrintMentionedObjectCache(Isolate* isolate);
159 static void ClearMentionedObjectCache(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000160#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 bool IsMentionedObjectCacheClear(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000162#endif
163
Steve Blocka7e24c12009-10-30 11:49:00 +0000164 static const int kInitialCapacity = 16;
165
166 private:
167 void PrintObject(Object* obj);
168
169 StringAllocator* allocator_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 ObjectPrintMode object_print_mode_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 unsigned capacity_;
172 unsigned length_; // does not include terminating 0-character
173 char* buffer_;
174
175 bool full() const { return (capacity_ - length_) == 1; }
176 int space() const { return capacity_ - length_; }
177
178 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream);
179};
180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181} // namespace internal
182} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
184#endif // V8_STRING_STREAM_H_