Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_STRING_STREAM_H_ |
| 6 | #define V8_STRING_STREAM_H_ |
| 7 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/allocation.h" |
| 9 | #include "src/base/smart-pointers.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/handles.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11 | #include "src/vector.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | // Forward declarations. |
| 17 | class ByteArray; |
| 18 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19 | class StringAllocator { |
| 20 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 21 | virtual ~StringAllocator() { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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; |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | // Normal allocator uses new[] and delete[]. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | class HeapStringAllocator final : public StringAllocator { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | public: |
| 35 | ~HeapStringAllocator() { DeleteArray(space_); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | char* allocate(unsigned bytes) override; |
| 37 | char* grow(unsigned* bytes) override; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | private: |
| 40 | char* space_; |
| 41 | }; |
| 42 | |
| 43 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 44 | class 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 | |
| 59 | class FmtElm final { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | 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 Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 82 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | class StringStream final { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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), |
| 107 | length_(0), |
| 108 | buffer_(allocator_->allocate(kInitialCapacity)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | buffer_[0] = 0; |
| 110 | } |
| 111 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | void Add(const char* format, |
| 127 | FmtElm arg0, |
| 128 | FmtElm arg1, |
| 129 | FmtElm arg2, |
| 130 | FmtElm arg3, |
| 131 | FmtElm arg4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | |
| 133 | // Getting the message out. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 134 | void OutputToFile(FILE* out); |
| 135 | void OutputToStdOut() { OutputToFile(stdout); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | void Log(Isolate* isolate); |
| 137 | Handle<String> ToString(Isolate* isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | base::SmartArrayPointer<const char> ToCString() const; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 139 | int length() const { return length_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | void PrintMentionedObjectCache(Isolate* isolate); |
| 159 | static void ClearMentionedObjectCache(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 161 | bool IsMentionedObjectCacheClear(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | #endif |
| 163 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | static const int kInitialCapacity = 16; |
| 165 | |
| 166 | private: |
| 167 | void PrintObject(Object* obj); |
| 168 | |
| 169 | StringAllocator* allocator_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | ObjectPrintMode object_print_mode_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | } // namespace internal |
| 182 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | |
| 184 | #endif // V8_STRING_STREAM_H_ |