Chromium Code Reviews| 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 | 6 |
| 7 #include "vm/dart_entry.h" | 7 #include "vm/dart_entry.h" |
| 8 #include "vm/debugger.h" | 8 #include "vm/debugger.h" |
| 9 #include "vm/json_stream.h" | 9 #include "vm/json_stream.h" |
| 10 #include "vm/message.h" | 10 #include "vm/message.h" |
| 11 #include "vm/object.h" | 11 #include "vm/object.h" |
| 12 #include "vm/unicode.h" | |
| 12 | 13 |
| 13 | 14 |
| 14 namespace dart { | 15 namespace dart { |
| 15 | 16 |
| 16 DECLARE_FLAG(bool, trace_service); | 17 DECLARE_FLAG(bool, trace_service); |
| 17 | 18 |
| 18 JSONStream::JSONStream(intptr_t buf_size) | 19 JSONStream::JSONStream(intptr_t buf_size) |
| 19 : open_objects_(0), | 20 : open_objects_(0), |
| 20 buffer_(buf_size), | 21 buffer_(buf_size), |
| 21 reply_port_(ILLEGAL_PORT), | 22 reply_port_(ILLEGAL_PORT), |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 string_iterator ^= option_values.At(i); | 74 string_iterator ^= option_values.At(i); |
| 74 option_values_native[i] = | 75 option_values_native[i] = |
| 75 zone->MakeCopyOfString(string_iterator.ToCString()); | 76 zone->MakeCopyOfString(string_iterator.ToCString()); |
| 76 } | 77 } |
| 77 SetOptions(option_keys_native, option_values_native, option_keys.Length()); | 78 SetOptions(option_keys_native, option_values_native, option_keys.Length()); |
| 78 } | 79 } |
| 79 if (FLAG_trace_service) { | 80 if (FLAG_trace_service) { |
| 80 Isolate* isolate = Isolate::Current(); | 81 Isolate* isolate = Isolate::Current(); |
| 81 ASSERT(isolate != NULL); | 82 ASSERT(isolate != NULL); |
| 82 const char* isolate_name = isolate->name(); | 83 const char* isolate_name = isolate->name(); |
| 83 OS::Print("Isolate %s processing service request /%s\n", | 84 OS::Print("Isolate %s processing service request /%s", |
| 84 isolate_name, command_); | 85 isolate_name, command_); |
| 86 for (intptr_t i = 1; i < num_arguments(); i++) { | |
| 87 OS::Print("/%s", GetArgument(i)); | |
| 88 } | |
| 89 OS::Print("\n"); | |
| 85 setup_time_micros_ = OS::GetCurrentTimeMicros(); | 90 setup_time_micros_ = OS::GetCurrentTimeMicros(); |
| 86 } | 91 } |
| 87 } | 92 } |
| 88 | 93 |
| 89 | 94 |
| 90 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 95 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 91 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 96 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
| 92 return reinterpret_cast<uint8_t*>(new_ptr); | 97 return reinterpret_cast<uint8_t*>(new_ptr); |
| 93 } | 98 } |
| 94 | 99 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 107 uint8_t* data = NULL; | 112 uint8_t* data = NULL; |
| 108 MessageWriter writer(&data, &allocator); | 113 MessageWriter writer(&data, &allocator); |
| 109 writer.WriteMessage(reply); | 114 writer.WriteMessage(reply); |
| 110 PortMap::PostMessage(new Message(port, data, | 115 PortMap::PostMessage(new Message(port, data, |
| 111 writer.BytesWritten(), | 116 writer.BytesWritten(), |
| 112 Message::kNormalPriority)); | 117 Message::kNormalPriority)); |
| 113 if (FLAG_trace_service) { | 118 if (FLAG_trace_service) { |
| 114 Isolate* isolate = Isolate::Current(); | 119 Isolate* isolate = Isolate::Current(); |
| 115 ASSERT(isolate != NULL); | 120 ASSERT(isolate != NULL); |
| 116 const char* isolate_name = isolate->name(); | 121 const char* isolate_name = isolate->name(); |
| 117 OS::Print("Isolate %s processed service request /%s in %" Pd64" us.\n", | 122 OS::Print("Isolate %s processed service request /%s", |
| 118 isolate_name, command_, process_delta_micros); | 123 isolate_name, command_); |
| 124 for (intptr_t i = 1; i < num_arguments(); i++) { | |
| 125 OS::Print("/%s", GetArgument(i)); | |
| 126 } | |
| 127 OS::Print(" in %" Pd64" us.\n", process_delta_micros); | |
| 119 } | 128 } |
| 120 } | 129 } |
| 121 | 130 |
| 122 | 131 |
| 123 const char* JSONStream::LookupOption(const char* key) const { | 132 const char* JSONStream::LookupOption(const char* key) const { |
| 124 for (int i = 0; i < num_options(); i++) { | 133 for (int i = 0; i < num_options(); i++) { |
| 125 if (!strcmp(key, option_keys_[i])) { | 134 if (!strcmp(key, option_keys_[i])) { |
| 126 return option_values_[i]; | 135 return option_values_[i]; |
| 127 } | 136 } |
| 128 } | 137 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 | 199 |
| 191 void JSONStream::PrintValue(double d) { | 200 void JSONStream::PrintValue(double d) { |
| 192 PrintCommaIfNeeded(); | 201 PrintCommaIfNeeded(); |
| 193 buffer_.Printf("%f", d); | 202 buffer_.Printf("%f", d); |
| 194 } | 203 } |
| 195 | 204 |
| 196 | 205 |
| 197 void JSONStream::PrintValue(const char* s) { | 206 void JSONStream::PrintValue(const char* s) { |
| 198 PrintCommaIfNeeded(); | 207 PrintCommaIfNeeded(); |
| 199 buffer_.AddChar('"'); | 208 buffer_.AddChar('"'); |
| 200 buffer_.AddEscapedString(s); | 209 AddEscapedUTF8String(s); |
| 201 buffer_.AddChar('"'); | 210 buffer_.AddChar('"'); |
| 202 } | 211 } |
| 203 | 212 |
| 204 | 213 |
| 205 void JSONStream::PrintfValue(const char* format, ...) { | 214 void JSONStream::PrintfValue(const char* format, ...) { |
| 206 PrintCommaIfNeeded(); | 215 PrintCommaIfNeeded(); |
| 207 | 216 |
| 208 va_list args; | 217 va_list args; |
| 209 va_start(args, format); | 218 va_start(args, format); |
| 210 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 219 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 211 va_end(args); | 220 va_end(args); |
| 212 char* p = reinterpret_cast<char*>(malloc(len+1)); | 221 char* p = reinterpret_cast<char*>(malloc(len+1)); |
| 213 va_start(args, format); | 222 va_start(args, format); |
| 214 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 223 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 215 va_end(args); | 224 va_end(args); |
| 216 ASSERT(len == len2); | 225 ASSERT(len == len2); |
| 217 buffer_.AddChar('"'); | 226 buffer_.AddChar('"'); |
| 218 buffer_.AddEscapedString(p); | 227 AddEscapedUTF8String(p); |
| 219 buffer_.AddChar('"'); | 228 buffer_.AddChar('"'); |
| 220 free(p); | 229 free(p); |
| 221 } | 230 } |
| 222 | 231 |
| 223 | 232 |
| 224 void JSONStream::PrintValue(const Object& o, bool ref) { | 233 void JSONStream::PrintValue(const Object& o, bool ref) { |
| 225 PrintCommaIfNeeded(); | 234 PrintCommaIfNeeded(); |
| 226 o.PrintToJSONStream(this, ref); | 235 o.PrintToJSONStream(this, ref); |
| 227 } | 236 } |
| 228 | 237 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 va_list args; | 277 va_list args; |
| 269 va_start(args, format); | 278 va_start(args, format); |
| 270 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 279 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 271 va_end(args); | 280 va_end(args); |
| 272 char* p = reinterpret_cast<char*>(malloc(len+1)); | 281 char* p = reinterpret_cast<char*>(malloc(len+1)); |
| 273 va_start(args, format); | 282 va_start(args, format); |
| 274 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 283 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 275 va_end(args); | 284 va_end(args); |
| 276 ASSERT(len == len2); | 285 ASSERT(len == len2); |
| 277 buffer_.AddChar('"'); | 286 buffer_.AddChar('"'); |
| 278 buffer_.AddEscapedString(p); | 287 AddEscapedUTF8String(p); |
| 279 buffer_.AddChar('"'); | 288 buffer_.AddChar('"'); |
| 280 free(p); | 289 free(p); |
| 281 } | 290 } |
| 282 | 291 |
| 283 | 292 |
| 284 void JSONStream::set_reply_port(Dart_Port port) { | 293 void JSONStream::set_reply_port(Dart_Port port) { |
| 285 reply_port_ = port; | 294 reply_port_ = port; |
| 286 } | 295 } |
| 287 | 296 |
| 288 | 297 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 304 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { | 313 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { |
| 305 PrintPropertyName(name); | 314 PrintPropertyName(name); |
| 306 PrintValue(o, ref); | 315 PrintValue(o, ref); |
| 307 } | 316 } |
| 308 | 317 |
| 309 | 318 |
| 310 void JSONStream::PrintPropertyName(const char* name) { | 319 void JSONStream::PrintPropertyName(const char* name) { |
| 311 ASSERT(name != NULL); | 320 ASSERT(name != NULL); |
| 312 PrintCommaIfNeeded(); | 321 PrintCommaIfNeeded(); |
| 313 buffer_.AddChar('"'); | 322 buffer_.AddChar('"'); |
| 314 buffer_.AddEscapedString(name); | 323 AddEscapedUTF8String(name); |
| 315 buffer_.AddChar('"'); | 324 buffer_.AddChar('"'); |
| 316 buffer_.AddChar(':'); | 325 buffer_.AddChar(':'); |
| 317 } | 326 } |
| 318 | 327 |
| 319 | 328 |
| 320 void JSONStream::PrintCommaIfNeeded() { | 329 void JSONStream::PrintCommaIfNeeded() { |
| 321 if (NeedComma()) { | 330 if (NeedComma()) { |
| 322 buffer_.AddChar(','); | 331 buffer_.AddChar(','); |
| 323 } | 332 } |
| 324 } | 333 } |
| 325 | 334 |
| 326 | 335 |
| 327 bool JSONStream::NeedComma() { | 336 bool JSONStream::NeedComma() { |
| 328 const char* buffer = buffer_.buf(); | 337 const char* buffer = buffer_.buf(); |
| 329 intptr_t length = buffer_.length(); | 338 intptr_t length = buffer_.length(); |
| 330 if (length == 0) { | 339 if (length == 0) { |
| 331 return false; | 340 return false; |
| 332 } | 341 } |
| 333 char ch = buffer[length-1]; | 342 char ch = buffer[length-1]; |
| 334 return (ch != '[') && (ch != '{') && (ch != ':') && (ch != ','); | 343 return (ch != '[') && (ch != '{') && (ch != ':') && (ch != ','); |
| 335 } | 344 } |
| 336 | 345 |
| 337 | 346 |
| 347 void JSONStream::AddEscapedUTF8String(const char* s) { | |
| 348 intptr_t len = strlen(s); | |
| 349 const uint8_t* s8 = reinterpret_cast<const uint8_t*>(s); | |
| 350 intptr_t i = 0; | |
| 351 for (; i < len; ) { | |
| 352 // Extract next UTF8 character. | |
| 353 int32_t ch = 0; | |
| 354 int32_t ch_len = Utf8::Decode(&s8[i], len - i, &ch); | |
| 355 ASSERT(ch_len != 0); | |
| 356 buffer_.AddEscapedChar(ch); | |
| 357 // Move i forward. | |
| 358 i += ch_len; | |
| 359 } | |
| 360 ASSERT(i == len); | |
| 361 } | |
| 362 | |
| 363 | |
| 338 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) { | 364 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) { |
| 339 stream_->OpenObject(); | 365 stream_->OpenObject(); |
| 340 } | 366 } |
| 341 | 367 |
| 342 | 368 |
| 343 void JSONObject::AddPropertyF(const char* name, | 369 void JSONObject::AddPropertyF(const char* name, |
| 344 const char* format, ...) const { | 370 const char* format, ...) const { |
| 345 stream_->PrintPropertyName(name); | 371 stream_->PrintPropertyName(name); |
| 346 va_list args; | 372 va_list args; |
| 347 va_start(args, format); | 373 va_start(args, format); |
| 348 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 374 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 349 va_end(args); | 375 va_end(args); |
| 350 char* p = reinterpret_cast<char*>(malloc(len+1)); | 376 char* p = reinterpret_cast<char*>(malloc(len+1)); |
| 351 va_start(args, format); | 377 va_start(args, format); |
| 352 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 378 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 353 va_end(args); | 379 va_end(args); |
| 354 ASSERT(len == len2); | 380 ASSERT(len == len2); |
| 355 stream_->buffer_.AddChar('"'); | 381 stream_->buffer_.AddChar('"'); |
| 356 stream_->buffer_.AddEscapedString(p); | 382 stream_->AddEscapedUTF8String(p); |
| 357 stream_->buffer_.AddChar('"'); | 383 stream_->buffer_.AddChar('"'); |
| 358 free(p); | 384 free(p); |
| 359 } | 385 } |
| 360 | 386 |
| 361 | 387 |
| 362 void JSONArray::AddValueF(const char* format, ...) const { | 388 void JSONArray::AddValueF(const char* format, ...) const { |
| 363 stream_->PrintCommaIfNeeded(); | 389 stream_->PrintCommaIfNeeded(); |
| 364 va_list args; | 390 va_list args; |
| 365 va_start(args, format); | 391 va_start(args, format); |
| 366 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 392 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 367 va_end(args); | 393 va_end(args); |
| 368 char* p = reinterpret_cast<char*>(malloc(len+1)); | 394 char* p = reinterpret_cast<char*>(malloc(len+1)); |
| 369 va_start(args, format); | 395 va_start(args, format); |
| 370 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 396 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 371 va_end(args); | 397 va_end(args); |
| 372 ASSERT(len == len2); | 398 ASSERT(len == len2); |
| 373 stream_->buffer_.AddChar('"'); | 399 stream_->buffer_.AddChar('"'); |
| 374 stream_->buffer_.AddEscapedString(p); | 400 stream_->AddEscapedUTF8String(p); |
| 375 stream_->buffer_.AddChar('"'); | 401 stream_->buffer_.AddChar('"'); |
| 376 free(p); | 402 free(p); |
| 377 } | 403 } |
| 378 | 404 |
| 405 | |
|
Ivan Posva
2014/03/12 21:17:16
?
| |
| 406 | |
| 407 | |
| 379 } // namespace dart | 408 } // namespace dart |
| OLD | NEW |