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 #ifndef vm_JSONPrinter_h
8 #define vm_JSONPrinter_h
9 
10 #include "mozilla/TimeStamp.h"
11 
12 #include <stdio.h>
13 
14 #include "js/TypeDecls.h"
15 #include "vm/Printer.h"
16 
17 namespace js {
18 
19 class JSONPrinter {
20  protected:
21   int indentLevel_;
22   bool indent_;
23   bool first_;
24   GenericPrinter& out_;
25 
26   void indent();
27 
28  public:
29   explicit JSONPrinter(GenericPrinter& out, bool indent = true)
30       : indentLevel_(0), indent_(indent), first_(true), out_(out) {}
31 
setIndentLevel(int indentLevel)32   void setIndentLevel(int indentLevel) { indentLevel_ = indentLevel; }
33 
34   void beginObject();
35   void beginList();
36   void beginObjectProperty(const char* name);
37   void beginListProperty(const char* name);
38 
39   void value(const char* format, ...) MOZ_FORMAT_PRINTF(2, 3);
40   void value(int value);
41 
42   void boolProperty(const char* name, bool value);
43 
44   void property(const char* name, const char* value);
45   void property(const char* name, int32_t value);
46   void property(const char* name, uint32_t value);
47   void property(const char* name, int64_t value);
48   void property(const char* name, uint64_t value);
49 #if defined(XP_DARWIN) || defined(__OpenBSD__) || defined(__wasi__)
50   // On OSX and OpenBSD, size_t is long unsigned, uint32_t is unsigned, and
51   // uint64_t is long long unsigned. Everywhere else, size_t matches either
52   // uint32_t or uint64_t.
53   void property(const char* name, size_t value);
54 #endif
55 
56   void formatProperty(const char* name, const char* format, ...)
57       MOZ_FORMAT_PRINTF(3, 4);
58   void formatProperty(const char* name, const char* format, va_list ap);
59 
60   // JSON requires decimals to be separated by periods, but the LC_NUMERIC
61   // setting may cause printf to use commas in some locales.
62   enum TimePrecision { SECONDS, MILLISECONDS, MICROSECONDS };
63   void property(const char* name, const mozilla::TimeDuration& dur,
64                 TimePrecision precision);
65 
66   void floatProperty(const char* name, double value, size_t precision);
67 
68   GenericPrinter& beginStringProperty(const char* name);
69   void endStringProperty();
70 
71   GenericPrinter& beginString();
72   void endString();
73 
74   void nullProperty(const char* name);
75   void nullValue();
76 
77   void endObject();
78   void endList();
79 
80   // Notify the output that the caller has detected OOM and should transition
81   // to its saw-OOM state.
outOfMemory()82   void outOfMemory() { out_.reportOutOfMemory(); }
83 
84  protected:
85   void propertyName(const char* name);
86 };
87 
88 }  // namespace js
89 
90 #endif /* vm_JSONPrinter_h */
91