1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/JSONPrinter.h"
#include "mozilla/Assertions.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/IntegerPrintfMacros.h"
#include <stdarg.h>
#include "jsnum.h"
using namespace js;
void JSONPrinter::indent() {
MOZ_ASSERT(indentLevel_ >= 0);
if (inlineLevel_ > 0) {
out_.putChar(' ');
return;
}
if (indent_) {
out_.putChar('\n');
for (int i = 0; i < indentLevel_; i++) {
out_.put(" ");
}
}
}
void JSONPrinter::beforeValue() {
if (!first_) {
out_.putChar(',');
}
indent();
}
void JSONPrinter::propertyName(const char* name) {
beforeValue();
out_.printf("\"%s\":", name);
if (indent_) {
out_.put(" ");
}
first_ = false;
}
void JSONPrinter::beginObject() {
beforeValue();
out_.putChar('{');
indentLevel_++;
first_ = true;
}
void JSONPrinter::beginList() {
beforeValue();
out_.putChar('[');
indentLevel_++;
first_ = true;
}
void JSONPrinter::beginObjectProperty(const char* name) {
propertyName(name);
out_.putChar('{');
indentLevel_++;
first_ = true;
}
void JSONPrinter::beginListProperty(const char* name) {
propertyName(name);
out_.putChar('[');
indentLevel_++;
first_ = true;
}
void JSONPrinter::beginInlineListProperty(const char* name) {
beginListProperty(name);
beginInline();
}
GenericPrinter& JSONPrinter::beginStringProperty(const char* name) {
propertyName(name);
out_.putChar('"');
return out_;
}
void JSONPrinter::endStringProperty() {
endString();
first_ = false;
}
GenericPrinter& JSONPrinter::beginString() {
beforeValue();
out_.putChar('"');
return out_;
}
void JSONPrinter::endString() { out_.putChar('"'); }
void JSONPrinter::boolProperty(const char* name, bool value) {
propertyName(name);
out_.put(value ? "true" : "false");
}
template <typename CharT>
static void JSONString(GenericPrinter& out, const CharT* s, size_t length) {
const CharT* end = s + length;
for (const CharT* t = s; t < end; s = ++t) {
// This quote implementation is probably correct,
// but uses \u even when not strictly necessary.
char16_t c = *t;
if (c == '"' || c == '\\') {
out.printf("\\");
out.printf("%c", char(c));
} else if (!IsAsciiPrintable(c)) {
out.printf("\\u%04x", c);
} else {
out.printf("%c", char(c));
}
}
}
void JSONPrinter::property(const char* name, JSLinearString* str) {
JS::AutoCheckCannotGC nogc;
beginStringProperty(name);
// Limit the string length to reduce the JSON file size.
size_t length = std::min(str->length(), size_t(128));
if (str->hasLatin1Chars()) {
JSONString(out_, str->latin1Chars(nogc), length);
} else {
JSONString(out_, str->twoByteChars(nogc), length);
}
endStringProperty();
}
void JSONPrinter::property(const char* name, const char* value) {
beginStringProperty(name);
out_.put(value);
endStringProperty();
}
void JSONPrinter::formatProperty(const char* name, const char* format, ...) {
va_list ap;
va_start(ap, format);
beginStringProperty(name);
out_.vprintf(format, ap);
endStringProperty();
va_end(ap);
}
void JSONPrinter::formatPropertyVA(const char* name, const char* format,
va_list ap) {
beginStringProperty(name);
out_.vprintf(format, ap);
endStringProperty();
}
void JSONPrinter::value(const char* format, ...) {
va_list ap;
va_start(ap, format);
beforeValue();
out_.putChar('"');
out_.vprintf(format, ap);
out_.putChar('"');
va_end(ap);
first_ = false;
}
void JSONPrinter::property(const char* name, int32_t value) {
propertyName(name);
out_.printf("%" PRId32, value);
}
void JSONPrinter::value(int val) {
beforeValue();
out_.printf("%d", val);
first_ = false;
}
void JSONPrinter::property(const char* name, uint32_t value) {
propertyName(name);
out_.printf("%" PRIu32, value);
}
void JSONPrinter::property(const char* name, int64_t value) {
propertyName(name);
out_.printf("%" PRId64, value);
}
void JSONPrinter::property(const char* name, uint64_t value) {
propertyName(name);
out_.printf("%" PRIu64, value);
}
#if defined(XP_DARWIN) || defined(__OpenBSD__) || defined(__wasi__)
void JSONPrinter::property(const char* name, size_t value) {
propertyName(name);
out_.printf("%zu", value);
}
#endif
void JSONPrinter::floatProperty(const char* name, double value,
size_t precision) {
if (!std::isfinite(value)) {
propertyName(name);
out_.put("null");
return;
}
ToCStringBuf cbuf;
const char* str = NumberToCString(&cbuf, value);
MOZ_ASSERT(str);
property(name, str);
}
void JSONPrinter::property(const char* name, const mozilla::TimeDuration& dur,
TimePrecision precision) {
if (precision == MICROSECONDS) {
property(name, static_cast<int64_t>(dur.ToMicroseconds()));
return;
}
propertyName(name);
lldiv_t split;
switch (precision) {
case SECONDS:
split = lldiv(static_cast<int64_t>(dur.ToMilliseconds()), 1000);
break;
case MILLISECONDS:
split = lldiv(static_cast<int64_t>(dur.ToMicroseconds()), 1000);
break;
case MICROSECONDS:
MOZ_ASSERT_UNREACHABLE("");
};
out_.printf("%lld.%03lld", split.quot, split.rem);
}
void JSONPrinter::nullProperty(const char* name) {
propertyName(name);
out_.put("null");
}
void JSONPrinter::nullValue() {
beforeValue();
out_.put("null");
first_ = false;
}
void JSONPrinter::endObject() {
indentLevel_--;
indent();
out_.putChar('}');
first_ = false;
}
void JSONPrinter::endList() {
indentLevel_--;
indent();
out_.putChar(']');
first_ = false;
}
void JSONPrinter::endInlineList() {
endList();
endInline();
}
void JSONPrinter::beginInline() { inlineLevel_++; }
void JSONPrinter::endInline() { inlineLevel_--; }
|