| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/object.h" | 6 #include "vm/object.h" |
| 7 #include "vm/json_stream.h" | 7 #include "vm/json_stream.h" |
| 8 | 8 |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 78 |
| 79 | 79 |
| 80 void JSONStream::PrintValue(double d) { | 80 void JSONStream::PrintValue(double d) { |
| 81 PrintCommaIfNeeded(); | 81 PrintCommaIfNeeded(); |
| 82 buffer_->Printf("%f", d); | 82 buffer_->Printf("%f", d); |
| 83 } | 83 } |
| 84 | 84 |
| 85 | 85 |
| 86 void JSONStream::PrintValue(const char* s) { | 86 void JSONStream::PrintValue(const char* s) { |
| 87 PrintCommaIfNeeded(); | 87 PrintCommaIfNeeded(); |
| 88 buffer_->Printf("\"%s\"", s); | 88 buffer_->AddChar('"'); |
| 89 buffer_->AddEscapedString(s); |
| 90 buffer_->AddChar('"'); |
| 89 } | 91 } |
| 90 | 92 |
| 91 | 93 |
| 92 void JSONStream::PrintfValue(const char* format, ...) { | 94 void JSONStream::PrintfValue(const char* format, ...) { |
| 93 PrintCommaIfNeeded(); | 95 PrintCommaIfNeeded(); |
| 94 | 96 |
| 95 va_list args; | 97 va_list args; |
| 96 va_start(args, format); | 98 va_start(args, format); |
| 97 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 99 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 98 va_end(args); | 100 va_end(args); |
| 99 char* p = reinterpret_cast<char*>(malloc(len+1)); | 101 char* p = reinterpret_cast<char*>(malloc(len+1)); |
| 100 va_start(args, format); | 102 va_start(args, format); |
| 101 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 103 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 102 va_end(args); | 104 va_end(args); |
| 103 ASSERT(len == len2); | 105 ASSERT(len == len2); |
| 104 buffer_->Printf("\"%s\"", p); | 106 buffer_->AddChar('"'); |
| 107 buffer_->AddEscapedString(p); |
| 108 buffer_->AddChar('"'); |
| 105 free(p); | 109 free(p); |
| 106 } | 110 } |
| 107 | 111 |
| 108 | 112 |
| 109 void JSONStream::PrintValue(const Object& o, bool ref) { | 113 void JSONStream::PrintValue(const Object& o, bool ref) { |
| 110 PrintCommaIfNeeded(); | 114 PrintCommaIfNeeded(); |
| 111 o.PrintToJSONStream(this, ref); | 115 o.PrintToJSONStream(this, ref); |
| 112 } | 116 } |
| 113 | 117 |
| 114 | 118 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 140 PrintPropertyName(name); | 144 PrintPropertyName(name); |
| 141 va_list args; | 145 va_list args; |
| 142 va_start(args, format); | 146 va_start(args, format); |
| 143 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 147 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 144 va_end(args); | 148 va_end(args); |
| 145 char* p = reinterpret_cast<char*>(malloc(len+1)); | 149 char* p = reinterpret_cast<char*>(malloc(len+1)); |
| 146 va_start(args, format); | 150 va_start(args, format); |
| 147 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 151 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 148 va_end(args); | 152 va_end(args); |
| 149 ASSERT(len == len2); | 153 ASSERT(len == len2); |
| 150 buffer_->Printf("\"%s\"", p); | 154 buffer_->AddChar('"'); |
| 155 buffer_->AddEscapedString(p); |
| 156 buffer_->AddChar('"'); |
| 151 free(p); | 157 free(p); |
| 152 } | 158 } |
| 153 | 159 |
| 154 | 160 |
| 155 void JSONStream::SetArguments(const char** arguments, intptr_t num_arguments) { | 161 void JSONStream::SetArguments(const char** arguments, intptr_t num_arguments) { |
| 156 arguments_ = arguments; | 162 arguments_ = arguments; |
| 157 num_arguments_ = num_arguments; | 163 num_arguments_ = num_arguments; |
| 158 } | 164 } |
| 159 | 165 |
| 160 | 166 |
| 161 void JSONStream::SetOptions(const char** option_keys, | 167 void JSONStream::SetOptions(const char** option_keys, |
| 162 const char** option_values, | 168 const char** option_values, |
| 163 intptr_t num_options) { | 169 intptr_t num_options) { |
| 164 option_keys_ = option_keys; | 170 option_keys_ = option_keys; |
| 165 option_values_ = option_values; | 171 option_values_ = option_values; |
| 166 num_options_ = num_options; | 172 num_options_ = num_options; |
| 167 } | 173 } |
| 168 | 174 |
| 169 | 175 |
| 170 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { | 176 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { |
| 171 PrintPropertyName(name); | 177 PrintPropertyName(name); |
| 172 PrintValue(o, ref); | 178 PrintValue(o, ref); |
| 173 } | 179 } |
| 174 | 180 |
| 175 | 181 |
| 176 void JSONStream::PrintPropertyName(const char* name) { | 182 void JSONStream::PrintPropertyName(const char* name) { |
| 177 ASSERT(name != NULL); | 183 ASSERT(name != NULL); |
| 178 PrintCommaIfNeeded(); | 184 PrintCommaIfNeeded(); |
| 179 buffer_->Printf("\"%s\":", name); | 185 buffer_->AddChar('"'); |
| 186 buffer_->AddEscapedString(name); |
| 187 buffer_->AddChar('"'); |
| 188 buffer_->AddChar(':'); |
| 180 } | 189 } |
| 181 | 190 |
| 182 | 191 |
| 183 void JSONStream::PrintCommaIfNeeded() { | 192 void JSONStream::PrintCommaIfNeeded() { |
| 184 if (NeedComma()) { | 193 if (NeedComma()) { |
| 185 buffer_->AddChar(','); | 194 buffer_->AddChar(','); |
| 186 } | 195 } |
| 187 } | 196 } |
| 188 | 197 |
| 189 | 198 |
| 190 bool JSONStream::NeedComma() { | 199 bool JSONStream::NeedComma() { |
| 191 const char* buffer = buffer_->buf(); | 200 const char* buffer = buffer_->buf(); |
| 192 intptr_t length = buffer_->length(); | 201 intptr_t length = buffer_->length(); |
| 193 if (length == 0) { | 202 if (length == 0) { |
| 194 return false; | 203 return false; |
| 195 } | 204 } |
| 196 char ch = buffer[length-1]; | 205 char ch = buffer[length-1]; |
| 197 return ch != '[' && ch != '{' && ch != ':' && ch != ','; | 206 return ch != '[' && ch != '{' && ch != ':' && ch != ','; |
| 198 } | 207 } |
| 199 | 208 |
| 200 } // namespace dart | 209 } // namespace dart |
| 201 | 210 |
| OLD | NEW |