V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
traced-value.cc
1 // Copyright 2016 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 #include "src/tracing/traced-value.h"
6 
7 #include "src/base/platform/platform.h"
8 #include "src/conversions.h"
9 
10 namespace v8 {
11 namespace tracing {
12 
13 namespace {
14 
15 #define DCHECK_CURRENT_CONTAINER_IS(x) DCHECK_EQ(x, nesting_stack_.back())
16 #define DCHECK_CONTAINER_STACK_DEPTH_EQ(x) DCHECK_EQ(x, nesting_stack_.size())
17 #ifdef DEBUG
18 const bool kStackTypeDict = false;
19 const bool kStackTypeArray = true;
20 #define DEBUG_PUSH_CONTAINER(x) nesting_stack_.push_back(x)
21 #define DEBUG_POP_CONTAINER() nesting_stack_.pop_back()
22 #else
23 #define DEBUG_PUSH_CONTAINER(x) ((void)0)
24 #define DEBUG_POP_CONTAINER() ((void)0)
25 #endif
26 
27 void EscapeAndAppendString(const char* value, std::string* result) {
28  *result += '"';
29  while (*value) {
30  unsigned char c = *value++;
31  switch (c) {
32  case '\b':
33  *result += "\\b";
34  break;
35  case '\f':
36  *result += "\\f";
37  break;
38  case '\n':
39  *result += "\\n";
40  break;
41  case '\r':
42  *result += "\\r";
43  break;
44  case '\t':
45  *result += "\\t";
46  break;
47  case '\"':
48  *result += "\\\"";
49  break;
50  case '\\':
51  *result += "\\\\";
52  break;
53  default:
54  if (c < '\x20' || c == '\x7F') {
55  char number_buffer[8];
56  base::OS::SNPrintF(number_buffer, arraysize(number_buffer), "\\u%04X",
57  static_cast<unsigned>(c));
58  *result += number_buffer;
59  } else {
60  *result += c;
61  }
62  }
63  }
64  *result += '"';
65 }
66 
67 } // namespace
68 
69 std::unique_ptr<TracedValue> TracedValue::Create() {
70  return std::unique_ptr<TracedValue>(new TracedValue());
71 }
72 
73 TracedValue::TracedValue() : first_item_(true) {
74  DEBUG_PUSH_CONTAINER(kStackTypeDict);
75 }
76 
77 TracedValue::~TracedValue() {
78  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
79  DEBUG_POP_CONTAINER();
80  DCHECK_CONTAINER_STACK_DEPTH_EQ(0u);
81 }
82 
83 void TracedValue::SetInteger(const char* name, int value) {
84  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
85  WriteName(name);
86  data_ += std::to_string(value);
87 }
88 
89 void TracedValue::SetDouble(const char* name, double value) {
90  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
91  WriteName(name);
93  data_ += DoubleToCString(value, buffer);
94 }
95 
96 void TracedValue::SetBoolean(const char* name, bool value) {
97  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
98  WriteName(name);
99  data_ += value ? "true" : "false";
100 }
101 
102 void TracedValue::SetString(const char* name, const char* value) {
103  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
104  WriteName(name);
105  EscapeAndAppendString(value, &data_);
106 }
107 
108 void TracedValue::BeginDictionary(const char* name) {
109  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
110  DEBUG_PUSH_CONTAINER(kStackTypeDict);
111  WriteName(name);
112  data_ += '{';
113  first_item_ = true;
114 }
115 
116 void TracedValue::BeginArray(const char* name) {
117  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
118  DEBUG_PUSH_CONTAINER(kStackTypeArray);
119  WriteName(name);
120  data_ += '[';
121  first_item_ = true;
122 }
123 
124 void TracedValue::AppendInteger(int value) {
125  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
126  WriteComma();
127  data_ += std::to_string(value);
128 }
129 
130 void TracedValue::AppendDouble(double value) {
131  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
132  WriteComma();
134  data_ += DoubleToCString(value, buffer);
135 }
136 
137 void TracedValue::AppendBoolean(bool value) {
138  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
139  WriteComma();
140  data_ += value ? "true" : "false";
141 }
142 
143 void TracedValue::AppendString(const char* value) {
144  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
145  WriteComma();
146  EscapeAndAppendString(value, &data_);
147 }
148 
149 void TracedValue::BeginDictionary() {
150  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
151  DEBUG_PUSH_CONTAINER(kStackTypeDict);
152  WriteComma();
153  data_ += '{';
154  first_item_ = true;
155 }
156 
157 void TracedValue::BeginArray() {
158  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
159  DEBUG_PUSH_CONTAINER(kStackTypeArray);
160  WriteComma();
161  data_ += '[';
162  first_item_ = true;
163 }
164 
165 void TracedValue::EndDictionary() {
166  DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
167  DEBUG_POP_CONTAINER();
168  data_ += '}';
169  first_item_ = false;
170 }
171 
172 void TracedValue::EndArray() {
173  DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
174  DEBUG_POP_CONTAINER();
175  data_ += ']';
176  first_item_ = false;
177 }
178 
179 void TracedValue::WriteComma() {
180  if (first_item_) {
181  first_item_ = false;
182  } else {
183  data_ += ',';
184  }
185 }
186 
187 void TracedValue::WriteName(const char* name) {
188  WriteComma();
189  data_ += '"';
190  data_ += name;
191  data_ += "\":";
192 }
193 
194 void TracedValue::AppendAsTraceFormat(std::string* out) const {
195  *out += '{';
196  *out += data_;
197  *out += '}';
198 }
199 
200 } // namespace tracing
201 } // namespace v8
void AppendAsTraceFormat(std::string *out) const override
Definition: libplatform.h:13