blob: 37ae93e2c6f6582dc008263754a3acd61f5955f8 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_STRING_STREAM_H_
29#define V8_STRING_STREAM_H_
30
31namespace v8 { namespace internal {
32
33
34class StringAllocator {
35 public:
36 virtual ~StringAllocator() {}
37 // Allocate a number of bytes.
38 virtual char* allocate(unsigned bytes) = 0;
39 // Allocate a larger number of bytes and copy the old buffer to the new one.
40 // bytes is an input and output parameter passing the old size of the buffer
41 // and returning the new size. If allocation fails then we return the old
42 // buffer and do not increase the size.
43 virtual char* grow(unsigned* bytes) = 0;
44};
45
46
47// Normal allocator uses new[] and delete[].
48class HeapStringAllocator: public StringAllocator {
49 public:
50 ~HeapStringAllocator() { DeleteArray(space_); }
51 char* allocate(unsigned bytes);
52 char* grow(unsigned* bytes);
53 private:
54 char* space_;
55};
56
57
58// Allocator for use when no new c++ heap allocation is allowed.
59// Allocates all space up front and does no allocation while building
60// message.
61class NoAllocationStringAllocator: public StringAllocator {
62 public:
63 explicit NoAllocationStringAllocator(unsigned bytes);
kasper.lundaf4734f2008-07-28 12:50:18 +000064 NoAllocationStringAllocator(char* memory, unsigned size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065 char* allocate(unsigned bytes) { return space_; }
66 char* grow(unsigned* bytes);
67 private:
68 unsigned size_;
69 char* space_;
70};
71
72
73class FmtElm {
74 public:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000075 FmtElm(int value) : type_(INT) { data_.u_int_ = value; } // NOLINT
ager@chromium.org3bf7b912008-11-17 09:09:45 +000076 explicit FmtElm(double value) : type_(DOUBLE) { data_.u_double_ = value; } // NOLINT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000077 FmtElm(const char* value) : type_(C_STR) { data_.u_c_str_ = value; } // NOLINT
ager@chromium.orga74f0da2008-12-03 16:05:52 +000078 FmtElm(const Vector<const uc16>& value) : type_(LC_STR) { data_.u_lc_str_ = &value; } // NOLINT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079 FmtElm(Object* value) : type_(OBJ) { data_.u_obj_ = value; } // NOLINT
80 FmtElm(Handle<Object> value) : type_(HANDLE) { data_.u_handle_ = value.location(); } // NOLINT
81 FmtElm(void* value) : type_(INT) { data_.u_int_ = reinterpret_cast<int>(value); } // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082 private:
83 friend class StringStream;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000084 enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085 Type type_;
86 union {
87 int u_int_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +000088 double u_double_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 const char* u_c_str_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000090 const Vector<const uc16>* u_lc_str_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 Object* u_obj_;
92 Object** u_handle_;
93 } data_;
94};
95
96
97class StringStream {
98 public:
99 explicit StringStream(StringAllocator* allocator):
100 allocator_(allocator),
101 capacity_(kInitialCapacity),
102 length_(0),
103 buffer_(allocator_->allocate(kInitialCapacity)) {
104 buffer_[0] = 0;
105 }
106
107 ~StringStream() {
108 }
109
110 bool Put(char c);
111 bool Put(String* str);
112 bool Put(String* str, int start, int end);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000113 void Add(Vector<const char> format, Vector<FmtElm> elms);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114 void Add(const char* format);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000115 void Add(Vector<const char> format);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116 void Add(const char* format, FmtElm arg0);
117 void Add(const char* format, FmtElm arg0, FmtElm arg1);
118 void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000119 void Add(const char* format,
120 FmtElm arg0,
121 FmtElm arg1,
122 FmtElm arg2,
123 FmtElm arg3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
125 // Getting the message out.
126 void OutputToStdOut();
127 void Log();
128 Handle<String> ToString();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000129 SmartPointer<const char> ToCString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130
131 // Object printing support.
132 void PrintName(Object* o);
133 void PrintFixedArray(FixedArray* array, unsigned int limit);
134 void PrintByteArray(ByteArray* ba);
135 void PrintUsingMap(JSObject* js_object);
136 void PrintPrototype(JSFunction* fun, Object* receiver);
137 void PrintSecurityTokenIfChanged(Object* function);
138 // NOTE: Returns the code in the output parameter.
139 void PrintFunction(Object* function, Object* receiver, Code** code);
140
141 // Reset the stream.
142 void Reset() {
143 length_ = 0;
144 buffer_[0] = 0;
145 }
146
147 // Mentioned object cache support.
148 void PrintMentionedObjectCache();
149 static void ClearMentionedObjectCache();
150#ifdef DEBUG
151 static bool IsMentionedObjectCacheClear();
152#endif
153
154
155 static const int kInitialCapacity = 16;
156
157 private:
158 void PrintObject(Object* obj);
159
160 StringAllocator* allocator_;
161 unsigned capacity_;
162 unsigned length_; // does not include terminating 0-character
163 char* buffer_;
164
165 int space() const { return capacity_ - length_; }
166 char* cursor() const { return buffer_ + length_; }
167
168 DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream);
169};
170
171
172} } // namespace v8::internal
173
174#endif // V8_STRING_STREAM_H_