OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_STRING_STREAM_H_ | 5 #ifndef V8_STRING_STREAM_H_ |
6 #define V8_STRING_STREAM_H_ | 6 #define V8_STRING_STREAM_H_ |
7 | 7 |
8 #include <memory> | |
9 | |
10 #include "src/allocation.h" | 8 #include "src/allocation.h" |
11 #include "src/handles.h" | 9 #include "src/handles.h" |
12 #include "src/vector.h" | 10 #include "src/vector.h" |
13 | 11 |
14 namespace v8 { | 12 namespace v8 { |
15 namespace internal { | 13 namespace internal { |
16 | 14 |
17 // Forward declarations. | 15 // Forward declarations. |
18 class ByteArray; | 16 class ByteArray; |
19 | 17 |
(...skipping 29 matching lines...) Expand all Loading... | |
49 ~FixedStringAllocator() override{}; | 47 ~FixedStringAllocator() override{}; |
50 char* allocate(unsigned bytes) override; | 48 char* allocate(unsigned bytes) override; |
51 char* grow(unsigned* bytes) override; | 49 char* grow(unsigned* bytes) override; |
52 | 50 |
53 private: | 51 private: |
54 char* buffer_; | 52 char* buffer_; |
55 unsigned length_; | 53 unsigned length_; |
56 DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator); | 54 DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator); |
57 }; | 55 }; |
58 | 56 |
57 class StringStream final { | |
58 class FmtElm final { | |
59 public: | |
60 FmtElm(int value) : FmtElm(INT) { // NOLINT | |
61 data_.u_int_ = value; | |
62 } | |
63 explicit FmtElm(double value) : FmtElm(DOUBLE) { // NOLINT | |
64 data_.u_double_ = value; | |
65 } | |
66 FmtElm(const char* value) : FmtElm(C_STR) { // NOLINT | |
67 data_.u_c_str_ = value; | |
68 } | |
69 FmtElm(const Vector<const uc16>& value) : FmtElm(LC_STR) { // NOLINT | |
70 data_.u_lc_str_ = &value; | |
71 } | |
72 FmtElm(Object* value) : FmtElm(OBJ) { // NOLINT | |
73 data_.u_obj_ = value; | |
74 } | |
75 FmtElm(Handle<Object> value) : FmtElm(HANDLE) { // NOLINT | |
76 data_.u_handle_ = value.location(); | |
77 } | |
78 FmtElm(void* value) : FmtElm(POINTER) { // NOLINT | |
79 data_.u_pointer_ = value; | |
80 } | |
59 | 81 |
60 class FmtElm final { | 82 private: |
61 public: | 83 friend class StringStream; |
62 FmtElm(int value) : type_(INT) { // NOLINT | 84 enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER }; |
63 data_.u_int_ = value; | |
64 } | |
65 explicit FmtElm(double value) : type_(DOUBLE) { | |
66 data_.u_double_ = value; | |
67 } | |
68 FmtElm(const char* value) : type_(C_STR) { // NOLINT | |
69 data_.u_c_str_ = value; | |
70 } | |
71 FmtElm(const Vector<const uc16>& value) : type_(LC_STR) { // NOLINT | |
72 data_.u_lc_str_ = &value; | |
73 } | |
74 FmtElm(Object* value) : type_(OBJ) { // NOLINT | |
75 data_.u_obj_ = value; | |
76 } | |
77 FmtElm(Handle<Object> value) : type_(HANDLE) { // NOLINT | |
78 data_.u_handle_ = value.location(); | |
79 } | |
80 FmtElm(void* value) : type_(POINTER) { // NOLINT | |
81 data_.u_pointer_ = value; | |
82 } | |
83 | 85 |
84 private: | 86 #ifdef DEBUG |
85 friend class StringStream; | 87 Type type_; |
86 enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER }; | 88 explicit FmtElm(Type type) : type_(type) {} |
titzer
2016/11/18 10:40:43
Cool, that's a neat trick!
| |
87 Type type_; | 89 #else |
88 union { | 90 explicit FmtElm(Type) {} |
89 int u_int_; | 91 #endif |
90 double u_double_; | |
91 const char* u_c_str_; | |
92 const Vector<const uc16>* u_lc_str_; | |
93 Object* u_obj_; | |
94 Object** u_handle_; | |
95 void* u_pointer_; | |
96 } data_; | |
97 }; | |
98 | 92 |
93 union { | |
94 int u_int_; | |
95 double u_double_; | |
96 const char* u_c_str_; | |
97 const Vector<const uc16>* u_lc_str_; | |
98 Object* u_obj_; | |
99 Object** u_handle_; | |
100 void* u_pointer_; | |
101 } data_; | |
102 }; | |
99 | 103 |
100 class StringStream final { | |
101 public: | 104 public: |
102 enum ObjectPrintMode { kPrintObjectConcise, kPrintObjectVerbose }; | 105 enum ObjectPrintMode { kPrintObjectConcise, kPrintObjectVerbose }; |
103 StringStream(StringAllocator* allocator, | 106 StringStream(StringAllocator* allocator, |
104 ObjectPrintMode object_print_mode = kPrintObjectVerbose) | 107 ObjectPrintMode object_print_mode = kPrintObjectVerbose) |
105 : allocator_(allocator), | 108 : allocator_(allocator), |
106 object_print_mode_(object_print_mode), | 109 object_print_mode_(object_print_mode), |
107 capacity_(kInitialCapacity), | 110 capacity_(kInitialCapacity), |
108 length_(0), | 111 length_(0), |
109 buffer_(allocator_->allocate(kInitialCapacity)) { | 112 buffer_(allocator_->allocate(kInitialCapacity)) { |
110 buffer_[0] = 0; | 113 buffer_[0] = 0; |
111 } | 114 } |
112 | 115 |
113 bool Put(char c); | 116 bool Put(char c); |
114 bool Put(String* str); | 117 bool Put(String* str); |
115 bool Put(String* str, int start, int end); | 118 bool Put(String* str, int start, int end); |
116 void Add(Vector<const char> format, Vector<FmtElm> elms); | 119 void Add(const char* format) { Add(CStrVector(format)); } |
117 void Add(const char* format); | 120 void Add(Vector<const char> format) { Add(format, Vector<FmtElm>()); } |
118 void Add(Vector<const char> format); | 121 |
119 void Add(const char* format, FmtElm arg0); | 122 template <typename... Args> |
120 void Add(const char* format, FmtElm arg0, FmtElm arg1); | 123 void Add(const char* format, Args... args) { |
121 void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2); | 124 Add(CStrVector(format), args...); |
122 void Add(const char* format, | 125 } |
123 FmtElm arg0, | 126 |
124 FmtElm arg1, | 127 template <typename... Args> |
125 FmtElm arg2, | 128 void Add(Vector<const char> format, Args... args) { |
126 FmtElm arg3); | 129 FmtElm elems[]{args...}; |
127 void Add(const char* format, | 130 Add(format, ArrayVector(elems)); |
128 FmtElm arg0, | 131 } |
129 FmtElm arg1, | |
130 FmtElm arg2, | |
131 FmtElm arg3, | |
132 FmtElm arg4); | |
133 | 132 |
134 // Getting the message out. | 133 // Getting the message out. |
135 void OutputToFile(FILE* out); | 134 void OutputToFile(FILE* out); |
136 void OutputToStdOut() { OutputToFile(stdout); } | 135 void OutputToStdOut() { OutputToFile(stdout); } |
137 void Log(Isolate* isolate); | 136 void Log(Isolate* isolate); |
138 Handle<String> ToString(Isolate* isolate); | 137 Handle<String> ToString(Isolate* isolate); |
139 std::unique_ptr<char[]> ToCString() const; | 138 std::unique_ptr<char[]> ToCString() const; |
140 int length() const { return length_; } | 139 int length() const { return length_; } |
141 | 140 |
142 // Object printing support. | 141 // Object printing support. |
(...skipping 15 matching lines...) Expand all Loading... | |
158 // Mentioned object cache support. | 157 // Mentioned object cache support. |
159 void PrintMentionedObjectCache(Isolate* isolate); | 158 void PrintMentionedObjectCache(Isolate* isolate); |
160 static void ClearMentionedObjectCache(Isolate* isolate); | 159 static void ClearMentionedObjectCache(Isolate* isolate); |
161 #ifdef DEBUG | 160 #ifdef DEBUG |
162 bool IsMentionedObjectCacheClear(Isolate* isolate); | 161 bool IsMentionedObjectCacheClear(Isolate* isolate); |
163 #endif | 162 #endif |
164 | 163 |
165 static const int kInitialCapacity = 16; | 164 static const int kInitialCapacity = 16; |
166 | 165 |
167 private: | 166 private: |
167 void Add(Vector<const char> format, Vector<FmtElm> elms); | |
168 void PrintObject(Object* obj); | 168 void PrintObject(Object* obj); |
169 | 169 |
170 StringAllocator* allocator_; | 170 StringAllocator* allocator_; |
171 ObjectPrintMode object_print_mode_; | 171 ObjectPrintMode object_print_mode_; |
172 unsigned capacity_; | 172 unsigned capacity_; |
173 unsigned length_; // does not include terminating 0-character | 173 unsigned length_; // does not include terminating 0-character |
174 char* buffer_; | 174 char* buffer_; |
175 | 175 |
176 bool full() const { return (capacity_ - length_) == 1; } | 176 bool full() const { return (capacity_ - length_) == 1; } |
177 int space() const { return capacity_ - length_; } | 177 int space() const { return capacity_ - length_; } |
178 | 178 |
179 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream); | 179 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream); |
180 }; | 180 }; |
181 | 181 |
182 } // namespace internal | 182 } // namespace internal |
183 } // namespace v8 | 183 } // namespace v8 |
184 | 184 |
185 #endif // V8_STRING_STREAM_H_ | 185 #endif // V8_STRING_STREAM_H_ |
OLD | NEW |