v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
string-stream.h
Go to the documentation of this file.
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_STRINGS_STRING_STREAM_H_
6#define V8_STRINGS_STRING_STREAM_H_
7
8#include <memory>
9
11#include "src/base/strings.h"
12#include "src/base/vector.h"
13#include "src/handles/handles.h"
14#include "src/objects/objects.h"
15#include "src/objects/tagged.h"
17
18namespace v8 {
19namespace internal {
20
21// Forward declarations.
22class ByteArray;
23
25 public:
26 virtual ~StringAllocator() = default;
27 // Allocate a number of bytes.
28 virtual char* allocate(unsigned bytes) = 0;
29 // Allocate a larger number of bytes and copy the old buffer to the new one.
30 // bytes is an input and output parameter passing the old size of the buffer
31 // and returning the new size. If allocation fails then we return the old
32 // buffer and do not increase the size.
33 virtual char* grow(unsigned* bytes) = 0;
34};
35
36// Normal allocator uses new[] and delete[].
38 public:
40 char* allocate(unsigned bytes) override;
41 char* grow(unsigned* bytes) override;
42
43 private:
44 char* space_;
45};
46
48 public:
49 FixedStringAllocator(char* buffer, unsigned length)
50 : buffer_(buffer), length_(length) {}
51 ~FixedStringAllocator() override = default;
54
55 char* allocate(unsigned bytes) override;
56 char* grow(unsigned* bytes) override;
57
58 private:
59 char* buffer_;
60 unsigned length_;
61};
62
63template <std::size_t kInlineSize>
65 public:
67
70
71 char* allocate(unsigned bytes) override {
72 vector_->resize(bytes);
73 return vector_->data();
74 }
75
76 char* grow(unsigned* bytes) override {
77 unsigned new_bytes = *bytes * 2;
78 // Check for overflow.
79 if (new_bytes <= *bytes) {
80 return vector_->data();
81 }
82 vector_->resize(new_bytes);
83 *bytes = new_bytes;
84 return vector_->data();
85 }
86
87 private:
89};
90
91class StringStream final {
92 class FmtElm final {
93 public:
94 FmtElm(int value) : FmtElm(INT) { // NOLINT
96 }
97 explicit FmtElm(double value) : FmtElm(DOUBLE) { // NOLINT
99 }
100 FmtElm(const char* value) : FmtElm(C_STR) { // NOLINT
102 }
104 : FmtElm(LC_STR) {
106 }
107 template <typename T>
108 FmtElm(Tagged<T> value) : FmtElm(OBJ) { // NOLINT
109 data_.u_obj_ = value.ptr();
110 }
111 template <typename T>
112 FmtElm(Handle<T> value) : FmtElm(HANDLE) { // NOLINT
113 data_.u_handle_ = value.location();
114 }
115 FmtElm(void* value) : FmtElm(POINTER) { // NOLINT
117 }
118
119 private:
120 friend class StringStream;
122
123#ifdef DEBUG
124 Type type_;
125 explicit FmtElm(Type type) : type_(type) {}
126#else
127 explicit FmtElm(Type) {}
128#endif
129
130 union {
132 double u_double_;
133 const char* u_c_str_;
139 };
140
141 public:
143 explicit StringStream(StringAllocator* allocator,
144 ObjectPrintMode object_print_mode = kPrintObjectVerbose)
145 : allocator_(allocator),
146 object_print_mode_(object_print_mode),
148 length_(0),
150 buffer_[0] = 0;
151 }
152
153 bool Put(char c);
154 bool Put(Tagged<String> str);
155 bool Put(Tagged<String> str, int start, int end);
156 void Add(const char* format) { Add(base::CStrVector(format)); }
158 Add(format, base::Vector<FmtElm>());
159 }
160
161 template <typename... Args>
162 void Add(const char* format, Args... args) {
163 Add(base::CStrVector(format), args...);
164 }
165
166 template <typename... Args>
167 void Add(base::Vector<const char> format, Args... args) {
168 FmtElm elems[]{args...};
169 Add(format, base::ArrayVector(elems));
170 }
171
172 // Getting the message out.
173 void OutputToFile(FILE* out);
174 void OutputToStdOut() { OutputToFile(stdout); }
175 void Log(Isolate* isolate);
177 std::unique_ptr<char[]> ToCString() const;
178 int length() const { return length_; }
179
180 // Object printing support.
182 void PrintFixedArray(Tagged<FixedArray> array, unsigned int limit);
184 void PrintUsingMap(Isolate* isolate, Tagged<JSObject> js_object);
185 void PrintPrototype(Isolate* isolate, Tagged<JSFunction> fun,
188 Tagged<JSFunction> function);
189 void PrintFunction(Isolate* isolate, Tagged<JSFunction> function,
191
192 // Reset the stream.
193 void Reset() {
194 length_ = 0;
195 buffer_[0] = 0;
196 }
197
198 // Mentioned object cache support.
199 void PrintMentionedObjectCache(Isolate* isolate);
201#ifdef DEBUG
202 bool IsMentionedObjectCacheClear(Isolate* isolate);
203#endif
204
205 static const int kInitialCapacity = 16;
206
207 private:
209 void PrintObject(Tagged<Object> obj);
210
213 unsigned capacity_;
214 unsigned length_; // does not include terminating 0-character
215 char* buffer_;
216
217 bool full() const { return (capacity_ - length_) == 1; }
218 int space() const { return capacity_ - length_; }
219
221};
222
223} // namespace internal
224} // namespace v8
225
226#endif // V8_STRINGS_STRING_STREAM_H_
void resize(size_t new_size)
FixedStringAllocator(const FixedStringAllocator &)=delete
FixedStringAllocator(char *buffer, unsigned length)
char * allocate(unsigned bytes) override
~FixedStringAllocator() override=default
char * grow(unsigned *bytes) override
FixedStringAllocator & operator=(const FixedStringAllocator &)=delete
char * allocate(unsigned bytes) override
char * grow(unsigned *bytes) override
char * allocate(unsigned bytes) override
char * grow(unsigned *bytes) override
SmallStringOptimizedAllocator(SmallVector *vector) V8_NOEXCEPT
virtual char * grow(unsigned *bytes)=0
virtual ~StringAllocator()=default
virtual char * allocate(unsigned bytes)=0
FmtElm(const base::Vector< const base::uc16 > &value)
const base::Vector< const base::uc16 > * u_lc_str_
union v8::internal::StringStream::FmtElm::@155 data_
void PrintObject(Tagged< Object > obj)
void PrintFunction(Isolate *isolate, Tagged< JSFunction > function, Tagged< Object > receiver)
void PrintByteArray(Tagged< ByteArray > ba)
void Add(base::Vector< const char > format)
void Add(const char *format, Args... args)
static V8_EXPORT_PRIVATE void ClearMentionedObjectCache(Isolate *isolate)
static const int kInitialCapacity
DirectHandle< String > ToString(Isolate *isolate)
void PrintPrototype(Isolate *isolate, Tagged< JSFunction > fun, Tagged< Object > receiver)
void PrintMentionedObjectCache(Isolate *isolate)
DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream)
void PrintSecurityTokenIfChanged(Isolate *isolate, Tagged< JSFunction > function)
std::unique_ptr< char[]> ToCString() const
StringStream(StringAllocator *allocator, ObjectPrintMode object_print_mode=kPrintObjectVerbose)
void PrintFixedArray(Tagged< FixedArray > array, unsigned int limit)
void Add(const char *format)
void PrintName(Tagged< Object > o)
void PrintUsingMap(Isolate *isolate, Tagged< JSObject > js_object)
void Log(Isolate *isolate)
StringAllocator * allocator_
ObjectPrintMode object_print_mode_
void Add(base::Vector< const char > format, Args... args)
const ObjectRef type_
int start
int end
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
TNode< Object > receiver
constexpr Vector< T > ArrayVector(T(&arr)[N])
Definition vector.h:354
Vector< const char > CStrVector(const char *data)
Definition vector.h:331
void DeleteArray(T *array)
Definition allocation.h:63
return value
Definition map-inl.h:893
#define V8_NOEXCEPT
#define V8_EXPORT_PRIVATE
Definition macros.h:460
void * HANDLE