ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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 | |
| 31 | namespace v8 { namespace internal { |
| 32 | |
| 33 | |
| 34 | class 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[]. |
| 48 | class 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. |
| 61 | class NoAllocationStringAllocator: public StringAllocator { |
| 62 | public: |
| 63 | explicit NoAllocationStringAllocator(unsigned bytes); |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 64 | NoAllocationStringAllocator(char* memory, unsigned size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | char* allocate(unsigned bytes) { return space_; } |
| 66 | char* grow(unsigned* bytes); |
| 67 | private: |
| 68 | unsigned size_; |
| 69 | char* space_; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | class FmtElm { |
| 74 | public: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 75 | FmtElm(int value) : type_(INT) { data_.u_int_ = value; } // NOLINT |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 76 | explicit FmtElm(double value) : type_(DOUBLE) { data_.u_double_ = value; } // NOLINT |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 77 | FmtElm(const char* value) : type_(C_STR) { data_.u_c_str_ = value; } // NOLINT |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 78 | FmtElm(const Vector<const uc16>& value) : type_(LC_STR) { data_.u_lc_str_ = &value; } // NOLINT |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 79 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | private: |
| 83 | friend class StringStream; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 84 | enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | Type type_; |
| 86 | union { |
| 87 | int u_int_; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 88 | double u_double_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | const char* u_c_str_; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 90 | const Vector<const uc16>* u_lc_str_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | Object* u_obj_; |
| 92 | Object** u_handle_; |
| 93 | } data_; |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | class 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.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 113 | void Add(Vector<const char> format, Vector<FmtElm> elms); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | void Add(const char* format); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 115 | void Add(Vector<const char> format); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | 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.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 119 | void Add(const char* format, |
| 120 | FmtElm arg0, |
| 121 | FmtElm arg1, |
| 122 | FmtElm arg2, |
| 123 | FmtElm arg3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | |
| 125 | // Getting the message out. |
| 126 | void OutputToStdOut(); |
| 127 | void Log(); |
| 128 | Handle<String> ToString(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 129 | SmartPointer<const char> ToCString(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | |
| 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_ |