V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
string-stream.h
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.
4 
5 #ifndef V8_STRING_STREAM_H_
6 #define V8_STRING_STREAM_H_
7 
8 #include "src/allocation.h"
9 #include "src/handles.h"
10 #include "src/objects/heap-object.h"
11 #include "src/vector.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 // Forward declarations.
17 class ByteArray;
18 
20  public:
21  virtual ~StringAllocator() = default;
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[].
33 class HeapStringAllocator final : public StringAllocator {
34  public:
35  ~HeapStringAllocator() override { DeleteArray(space_); }
36  char* allocate(unsigned bytes) override;
37  char* grow(unsigned* bytes) override;
38 
39  private:
40  char* space_;
41 };
42 
43 
44 class FixedStringAllocator final : public StringAllocator {
45  public:
46  FixedStringAllocator(char* buffer, unsigned length)
47  : buffer_(buffer), length_(length) {}
48  ~FixedStringAllocator() override = default;
49 
50  char* allocate(unsigned bytes) override;
51  char* grow(unsigned* bytes) override;
52 
53  private:
54  char* buffer_;
55  unsigned length_;
56  DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator);
57 };
58 
59 class StringStream final {
60  class FmtElm final {
61  public:
62  FmtElm(int value) : FmtElm(INT) { // NOLINT
63  data_.u_int_ = value;
64  }
65  explicit FmtElm(double value) : FmtElm(DOUBLE) { // NOLINT
66  data_.u_double_ = value;
67  }
68  FmtElm(const char* value) : FmtElm(C_STR) { // NOLINT
69  data_.u_c_str_ = value;
70  }
71  FmtElm(const Vector<const uc16>& value) : FmtElm(LC_STR) { // NOLINT
72  data_.u_lc_str_ = &value;
73  }
74  FmtElm(Object* value) : FmtElm(OBJ) { // NOLINT
75  data_.u_obj_ = value;
76  }
77  FmtElm(ObjectPtr value) : FmtElm(OBJ) { // NOLINT
78  data_.u_obj_ = reinterpret_cast<Object*>(value.ptr());
79  }
80  FmtElm(Handle<Object> value) : FmtElm(HANDLE) { // NOLINT
81  data_.u_handle_ = value.location();
82  }
83  FmtElm(void* value) : FmtElm(POINTER) { // NOLINT
84  data_.u_pointer_ = value;
85  }
86 
87  private:
88  friend class StringStream;
89  enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER };
90 
91 #ifdef DEBUG
92  Type type_;
93  explicit FmtElm(Type type) : type_(type) {}
94 #else
95  explicit FmtElm(Type) {}
96 #endif
97 
98  union {
99  int u_int_;
100  double u_double_;
101  const char* u_c_str_;
102  const Vector<const uc16>* u_lc_str_;
103  Object* u_obj_;
104  Address* u_handle_;
105  void* u_pointer_;
106  } data_;
107  };
108 
109  public:
110  enum ObjectPrintMode { kPrintObjectConcise, kPrintObjectVerbose };
111  StringStream(StringAllocator* allocator,
112  ObjectPrintMode object_print_mode = kPrintObjectVerbose)
113  : allocator_(allocator),
114  object_print_mode_(object_print_mode),
115  capacity_(kInitialCapacity),
116  length_(0),
117  buffer_(allocator_->allocate(kInitialCapacity)) {
118  buffer_[0] = 0;
119  }
120 
121  bool Put(char c);
122  bool Put(String str);
123  bool Put(String str, int start, int end);
124  void Add(const char* format) { Add(CStrVector(format)); }
125  void Add(Vector<const char> format) { Add(format, Vector<FmtElm>()); }
126 
127  template <typename... Args>
128  void Add(const char* format, Args... args) {
129  Add(CStrVector(format), args...);
130  }
131 
132  template <typename... Args>
133  void Add(Vector<const char> format, Args... args) {
134  FmtElm elems[]{args...};
135  Add(format, ArrayVector(elems));
136  }
137 
138  // Getting the message out.
139  void OutputToFile(FILE* out);
140  void OutputToStdOut() { OutputToFile(stdout); }
141  void Log(Isolate* isolate);
142  Handle<String> ToString(Isolate* isolate);
143  std::unique_ptr<char[]> ToCString() const;
144  int length() const { return length_; }
145 
146  // Object printing support.
147  void PrintName(Object* o);
148  void PrintFixedArray(FixedArray array, unsigned int limit);
149  void PrintByteArray(ByteArray ba);
150  void PrintUsingMap(JSObject* js_object);
151  void PrintPrototype(JSFunction* fun, Object* receiver);
152  void PrintSecurityTokenIfChanged(JSFunction* function);
153  // NOTE: Returns the code in the output parameter.
154  void PrintFunction(JSFunction* function, Object* receiver, Code* code);
155 
156  // Reset the stream.
157  void Reset() {
158  length_ = 0;
159  buffer_[0] = 0;
160  }
161 
162  // Mentioned object cache support.
163  void PrintMentionedObjectCache(Isolate* isolate);
164  static void ClearMentionedObjectCache(Isolate* isolate);
165 #ifdef DEBUG
166  bool IsMentionedObjectCacheClear(Isolate* isolate);
167 #endif
168 
169  static const int kInitialCapacity = 16;
170 
171  private:
172  void Add(Vector<const char> format, Vector<FmtElm> elms);
173  void PrintObject(Object* obj);
174 
175  StringAllocator* allocator_;
176  ObjectPrintMode object_print_mode_;
177  unsigned capacity_;
178  unsigned length_; // does not include terminating 0-character
179  char* buffer_;
180 
181  bool full() const { return (capacity_ - length_) == 1; }
182  int space() const { return capacity_ - length_; }
183 
184  DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream);
185 };
186 
187 } // namespace internal
188 } // namespace v8
189 
190 #endif // V8_STRING_STREAM_H_
Definition: libplatform.h:13