Flutter Engine
The Flutter Engine
json_writer.cc
Go to the documentation of this file.
1// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/assert.h"
6
7#include "platform/unicode.h"
9#include "vm/json_writer.h"
10#include "vm/object.h"
11
12namespace dart {
13
15 public:
16 explicit MaybeOnStackBuffer(intptr_t size) {
17 if (size > kOnStackBufferCapacity) {
18 p_ = reinterpret_cast<char*>(malloc(size));
19 } else {
20 p_ = &buffer_[0];
21 }
22 }
24 if (p_ != &buffer_[0]) free(p_);
25 }
26
27 char* p() { return p_; }
28
29 private:
30 static constexpr intptr_t kOnStackBufferCapacity = 4096;
31 char* p_;
32 char buffer_[kOnStackBufferCapacity];
33};
34
35JSONWriter::JSONWriter(intptr_t buf_size)
36 : open_objects_(0), buffer_(buf_size) {}
37
38void JSONWriter::AppendBytes(const uint8_t* buffer, intptr_t buffer_length) {
39 buffer_.AddRaw(buffer, buffer_length);
40}
41
42static const char base64_digits[65] =
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
44static const char base64_pad = '=';
45
46void JSONWriter::AppendBytesInBase64(const uint8_t* bytes, intptr_t length) {
47 ASSERT(bytes != nullptr);
48 intptr_t odd_bits = length % 3;
49 intptr_t even_bits = length - odd_bits;
50 for (intptr_t i = 0; i < even_bits; i += 3) {
51 intptr_t triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
52 buffer_.AddChar(base64_digits[triplet >> 18]);
53 buffer_.AddChar(base64_digits[(triplet >> 12) & 63]);
54 buffer_.AddChar(base64_digits[(triplet >> 6) & 63]);
55 buffer_.AddChar(base64_digits[triplet & 63]);
56 }
57 if (odd_bits == 1) {
58 intptr_t triplet = bytes[even_bits] << 16;
59 buffer_.AddChar(base64_digits[triplet >> 18]);
60 buffer_.AddChar(base64_digits[(triplet >> 12) & 63]);
61 buffer_.AddChar(base64_pad);
62 buffer_.AddChar(base64_pad);
63 } else if (odd_bits == 2) {
64 intptr_t triplet = (bytes[even_bits] << 16) | (bytes[even_bits + 1] << 8);
65 buffer_.AddChar(base64_digits[triplet >> 18]);
66 buffer_.AddChar(base64_digits[(triplet >> 12) & 63]);
67 buffer_.AddChar(base64_digits[(triplet >> 6) & 63]);
68 buffer_.AddChar(base64_pad);
69 }
70}
71
72void JSONWriter::AppendSerializedObject(const char* serialized_object) {
74 buffer_.AddString(serialized_object);
75}
76
77void JSONWriter::AppendSerializedObject(const char* property_name,
78 const char* serialized_object) {
80 PrintPropertyName(property_name);
81 buffer_.AddString(serialized_object);
82}
83
85 buffer_.Clear();
86 open_objects_ = 0;
87}
88
89void JSONWriter::OpenObject(const char* property_name) {
91 open_objects_++;
92 if (property_name != nullptr) {
93 PrintPropertyName(property_name);
94 }
95 buffer_.AddChar('{');
96}
97
99 intptr_t len = buffer_.length();
100 ASSERT(len > 0);
101 ASSERT(buffer_.buffer()[len - 1] == '}');
102 open_objects_++;
103 buffer_.set_length(len - 1);
104}
105
107 ASSERT(open_objects_ > 0);
108 open_objects_--;
109 buffer_.AddChar('}');
110}
111
112void JSONWriter::OpenArray(const char* property_name) {
114 if (property_name != nullptr) {
115 PrintPropertyName(property_name);
116 }
117 open_objects_++;
118 buffer_.AddChar('[');
119}
120
122 ASSERT(open_objects_ > 0);
123 open_objects_--;
124 buffer_.AddChar(']');
125}
126
129 buffer_.Printf("null");
130}
131
134 buffer_.Printf("%s", b ? "true" : "false");
135}
136
137void JSONWriter::PrintValue(intptr_t i) {
138 EnsureIntegerIsRepresentableInJavaScript(static_cast<int64_t>(i));
140 buffer_.Printf("%" Pd "", i);
141}
142
144 EnsureIntegerIsRepresentableInJavaScript(i);
146 buffer_.Printf("%" Pd64 "", i);
147}
148
150 // Max length of a double in characters (including \0).
151 // See double_conversion.cc.
152 const size_t kBufferLen = 25;
153 char buffer[kBufferLen];
154 DoubleToCString(d, buffer, kBufferLen);
156 buffer_.Printf("%s", buffer);
157}
158
159void JSONWriter::PrintValueBase64(const uint8_t* bytes, intptr_t length) {
161 buffer_.AddChar('"');
163 buffer_.AddChar('"');
164}
165
166void JSONWriter::PrintValue(const char* s) {
168 buffer_.AddChar('"');
170 buffer_.AddChar('"');
171}
172
173void JSONWriter::PrintValue(const char* s, intptr_t i) {
175 buffer_.AddChar('"');
177 buffer_.AddChar('"');
178}
179
181 intptr_t offset,
182 intptr_t count) {
184 buffer_.AddChar('"');
185 bool did_truncate = AddDartString(s, offset, count);
186 buffer_.AddChar('"');
187 return did_truncate;
188}
189
192 buffer_.Printf("%s", s);
193}
194
195void JSONWriter::PrintfValue(const char* format, ...) {
196 va_list args;
199 va_end(args);
200}
201
202void JSONWriter::VPrintfValue(const char* format, va_list args) {
204
205 va_list measure_args;
206 va_copy(measure_args, args);
207 intptr_t len = Utils::VSNPrint(nullptr, 0, format, measure_args);
208 va_end(measure_args);
209
210 MaybeOnStackBuffer mosb(len + 1);
211 char* p = mosb.p();
212
213 va_list print_args;
214 va_copy(print_args, args);
215 intptr_t len2 = Utils::VSNPrint(p, len + 1, format, print_args);
216 va_end(print_args);
217 ASSERT(len == len2);
218
219 buffer_.AddChar('"');
221 buffer_.AddChar('"');
222}
223
224void JSONWriter::PrintPropertyBool(const char* name, bool b) {
227}
228
229void JSONWriter::PrintProperty(const char* name, intptr_t i) {
231 PrintValue(i);
232}
233
234void JSONWriter::PrintProperty64(const char* name, int64_t i) {
237}
238
239void JSONWriter::PrintProperty(const char* name, double d) {
241 PrintValue(d);
242}
243
244void JSONWriter::PrintProperty(const char* name, const char* s) {
246 PrintValue(s);
247}
248
250 const uint8_t* b,
251 intptr_t len) {
254}
255
257 const String& s,
258 intptr_t offset,
259 intptr_t count) {
261 return PrintValueStr(s, offset, count);
262}
263
264void JSONWriter::PrintPropertyNoEscape(const char* name, const char* s) {
267}
268
269void JSONWriter::PrintfProperty(const char* name, const char* format, ...) {
270 va_list args;
273 va_end(args);
274}
275
277 const char* format,
278 va_list args) {
280
281 va_list measure_args;
282 va_copy(measure_args, args);
283 intptr_t len = Utils::VSNPrint(nullptr, 0, format, measure_args);
284 va_end(measure_args);
285
286 MaybeOnStackBuffer mosb(len + 1);
287 char* p = mosb.p();
288
289 va_list print_args;
290 va_copy(print_args, args);
291 intptr_t len2 = Utils::VSNPrint(p, len + 1, format, print_args);
292 va_end(print_args);
293 ASSERT(len == len2);
294
295 buffer_.AddChar('"');
297 buffer_.AddChar('"');
298}
299
300void JSONWriter::Steal(char** buffer, intptr_t* buffer_length) {
301 ASSERT(buffer != nullptr);
302 ASSERT(buffer_length != nullptr);
303 *buffer_length = buffer_.length();
304 *buffer = buffer_.Steal();
305}
306
308 ASSERT(name != nullptr);
310 buffer_.AddChar('"');
312 buffer_.AddChar('"');
313 buffer_.AddChar(':');
314}
315
317 buffer_.AddChar('\n');
318}
319
321 if (NeedComma()) {
322 buffer_.AddChar(',');
323 }
324}
325
326bool JSONWriter::NeedComma() {
327 const char* buffer = buffer_.buffer();
328 intptr_t length = buffer_.length();
329 if (length == 0) {
330 return false;
331 }
332 char ch = buffer[length - 1];
333 return (ch != '[') && (ch != '{') && (ch != ':') && (ch != ',');
334}
335
336void JSONWriter::EnsureIntegerIsRepresentableInJavaScript(int64_t i) {
337#ifdef DEBUG
340 "JSONWriter::EnsureIntegerIsRepresentableInJavaScript failed on "
341 "%" Pd64 "\n",
342 i);
343 UNREACHABLE();
344 }
345#endif
346}
347
349 if (s == nullptr) {
350 return;
351 }
352 intptr_t len = strlen(s);
354}
355
356void JSONWriter::AddEscapedUTF8String(const char* s, intptr_t len) {
357 if (s == nullptr) {
358 return;
359 }
360 buffer_.AddEscapedUTF8(s, len);
361}
362
363bool JSONWriter::AddDartString(const String& s,
364 intptr_t offset,
365 intptr_t count) {
366 intptr_t length = s.Length();
367 ASSERT(offset >= 0);
368 if (offset > length) {
369 offset = length;
370 }
372 count = length - offset;
373 }
374
375 if (count > 0) { // Avoid asserts about harmless out-of-bounds index.
376 NoSafepointScope no_safepoint;
377 if (s.IsOneByteString()) {
378 buffer_.AddEscapedLatin1(OneByteString::CharAddr(s, offset), count);
379 } else if (s.IsTwoByteString()) {
380 buffer_.AddEscapedUTF16(TwoByteString::CharAddr(s, offset), count);
381 } else {
382 UNREACHABLE();
383 }
384 }
385
386 // Return value indicates whether the string is truncated.
387 intptr_t limit = offset + count;
388 return (offset > 0) || (limit < length);
389}
390
391} // namespace dart
int count
Definition: FontMgrTest.cpp:50
#define UNREACHABLE()
Definition: assert.h:248
void AddString(const char *s)
Definition: text_buffer.cc:263
intptr_t Printf(const char *format,...) PRINTF_ATTRIBUTE(2
Definition: text_buffer.cc:14
char * buffer() const
Definition: text_buffer.h:35
void AddEscapedLatin1(const uint8_t *code_units, intptr_t len)
Definition: text_buffer.cc:94
intptr_t length() const
Definition: text_buffer.h:36
void AddChar(char ch)
Definition: text_buffer.cc:49
void AddRaw(const uint8_t *buffer, intptr_t buffer_length)
Definition: text_buffer.cc:56
void AddEscapedUTF8(const char *s, intptr_t len)
Definition: text_buffer.cc:66
void AddEscapedUTF16(const uint16_t *code_units, intptr_t len)
Definition: text_buffer.cc:112
void AppendBytesInBase64(const uint8_t *bytes, intptr_t length)
Definition: json_writer.cc:46
bool PrintPropertyStr(const char *name, const String &s, intptr_t offset=0, intptr_t count=-1)
Definition: json_writer.cc:256
JSONWriter(intptr_t buf_size=256)
Definition: json_writer.cc:35
void PrintPropertyBool(const char *name, bool b)
Definition: json_writer.cc:224
void PrintProperty64(const char *name, int64_t i)
Definition: json_writer.cc:234
void UncloseObject()
Definition: json_writer.cc:98
void PrintPropertyBase64(const char *name, const uint8_t *bytes, intptr_t length)
Definition: json_writer.cc:249
void PrintCommaIfNeeded()
Definition: json_writer.cc:320
void Steal(char **buffer, intptr_t *buffer_length)
Definition: json_writer.cc:300
void PrintValue(intptr_t i)
Definition: json_writer.cc:137
void PrintValueBase64(const uint8_t *bytes, intptr_t length)
Definition: json_writer.cc:159
void PrintValueBool(bool b)
Definition: json_writer.cc:132
void PrintProperty(const char *name, intptr_t i)
Definition: json_writer.cc:229
void PrintPropertyName(const char *name)
Definition: json_writer.cc:307
void void VPrintfValue(const char *format, va_list args)
Definition: json_writer.cc:202
void PrintfValue(const char *format,...) PRINTF_ATTRIBUTE(2
Definition: json_writer.cc:195
void OpenArray(const char *property_name=nullptr)
Definition: json_writer.cc:112
void AppendBytes(const uint8_t *buffer, intptr_t buffer_length)
Definition: json_writer.cc:38
TextBuffer * buffer()
Definition: json_writer.h:19
bool PrintValueStr(const String &s, intptr_t offset, intptr_t count)
Definition: json_writer.cc:180
void AppendSerializedObject(const char *serialized_object)
Definition: json_writer.cc:72
void void VPrintfProperty(const char *name, const char *format, va_list args)
Definition: json_writer.cc:276
void PrintfProperty(const char *name, const char *format,...) PRINTF_ATTRIBUTE(3
Definition: json_writer.cc:269
void AddEscapedUTF8String(const char *s)
Definition: json_writer.cc:348
void PrintValueNoEscape(const char *s)
Definition: json_writer.cc:190
void OpenObject(const char *property_name=nullptr)
Definition: json_writer.cc:89
void PrintValue64(int64_t i)
Definition: json_writer.cc:143
void PrintPropertyNoEscape(const char *name, const char *s)
Definition: json_writer.cc:264
MaybeOnStackBuffer(intptr_t size)
Definition: json_writer.cc:16
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
void set_length(intptr_t len)
Definition: text_buffer.h:67
static bool IsJavaScriptInt(int64_t value)
Definition: utils.h:542
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
static bool RangeCheck(intptr_t offset, intptr_t count, intptr_t length)
Definition: utils.h:411
#define ASSERT(E)
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
struct MyStruct s
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format
size_t length
va_start(args, format)
va_end(args)
Definition: dart_vm.cc:33
void DoubleToCString(double d, char *buffer, int buffer_size)
const char *const name
void * malloc(size_t size)
Definition: allocation.cc:19
static const char base64_pad
Definition: json_writer.cc:44
static const char base64_digits[65]
Definition: json_writer.cc:42
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
#define Pd64
Definition: globals.h:416
#define Pd
Definition: globals.h:408
SeparatedVector2 offset