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