| 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/metrics.h" | 11 #include "vm/metrics.h" |
| 12 #include "vm/object.h" | 12 #include "vm/object.h" |
| 13 #include "vm/unicode.h" | 13 #include "vm/unicode.h" |
| 14 | 14 |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 DECLARE_FLAG(bool, trace_service); | 18 DECLARE_FLAG(bool, trace_service); |
| 19 | 19 |
| 20 JSONStream::JSONStream(intptr_t buf_size) | 20 JSONStream::JSONStream(intptr_t buf_size) |
| 21 : open_objects_(0), | 21 : open_objects_(0), |
| 22 buffer_(buf_size), | 22 buffer_(buf_size), |
| 23 reply_port_(ILLEGAL_PORT), | 23 reply_port_(ILLEGAL_PORT), |
| 24 command_(""), | 24 method_(""), |
| 25 arguments_(NULL), | 25 param_keys_(NULL), |
| 26 num_arguments_(0), | 26 param_values_(NULL), |
| 27 option_keys_(NULL), | 27 num_params_(0) { |
| 28 option_values_(NULL), | |
| 29 num_options_(0) { | |
| 30 } | 28 } |
| 31 | 29 |
| 32 | 30 |
| 33 JSONStream::~JSONStream() { | 31 JSONStream::~JSONStream() { |
| 34 } | 32 } |
| 35 | 33 |
| 36 | 34 |
| 37 void JSONStream::Setup(Zone* zone, | 35 void JSONStream::Setup(Zone* zone, |
| 38 Dart_Port reply_port, | 36 Dart_Port reply_port, |
| 39 const GrowableObjectArray& path, | 37 const String& method, |
| 40 const Array& option_keys, | 38 const Array& param_keys, |
| 41 const Array& option_values) { | 39 const Array& param_values) { |
| 42 set_reply_port(reply_port); | 40 set_reply_port(reply_port); |
| 41 method_ = method.ToCString(); |
| 43 | 42 |
| 44 // Setup JSONStream arguments and options. The arguments and options | |
| 45 // are zone allocated and will be freed immediately after handling the | |
| 46 // message. | |
| 47 const char** arguments = zone->Alloc<const char*>(path.Length()); | |
| 48 String& string_iterator = String::Handle(); | 43 String& string_iterator = String::Handle(); |
| 49 for (intptr_t i = 0; i < path.Length(); i++) { | 44 if (param_keys.Length() > 0) { |
| 50 string_iterator ^= path.At(i); | 45 ASSERT(param_keys.Length() == param_values.Length()); |
| 51 arguments[i] = zone->MakeCopyOfString(string_iterator.ToCString()); | 46 const char** param_keys_native = |
| 52 if (i == 0) { | 47 zone->Alloc<const char*>(param_keys.Length()); |
| 53 command_ = arguments[i]; | 48 const char** param_values_native = |
| 54 } | 49 zone->Alloc<const char*>(param_keys.Length()); |
| 55 } | 50 for (intptr_t i = 0; i < param_keys.Length(); i++) { |
| 56 SetArguments(arguments, path.Length()); | 51 string_iterator ^= param_keys.At(i); |
| 57 if (option_keys.Length() > 0) { | 52 param_keys_native[i] = |
| 58 const char** option_keys_native = | |
| 59 zone->Alloc<const char*>(option_keys.Length()); | |
| 60 const char** option_values_native = | |
| 61 zone->Alloc<const char*>(option_keys.Length()); | |
| 62 for (intptr_t i = 0; i < option_keys.Length(); i++) { | |
| 63 string_iterator ^= option_keys.At(i); | |
| 64 option_keys_native[i] = | |
| 65 zone->MakeCopyOfString(string_iterator.ToCString()); | 53 zone->MakeCopyOfString(string_iterator.ToCString()); |
| 66 string_iterator ^= option_values.At(i); | 54 string_iterator ^= param_values.At(i); |
| 67 option_values_native[i] = | 55 param_values_native[i] = |
| 68 zone->MakeCopyOfString(string_iterator.ToCString()); | 56 zone->MakeCopyOfString(string_iterator.ToCString()); |
| 69 } | 57 } |
| 70 SetOptions(option_keys_native, option_values_native, option_keys.Length()); | 58 SetParams(param_keys_native, param_values_native, param_keys.Length()); |
| 71 } | 59 } |
| 72 if (FLAG_trace_service) { | 60 if (FLAG_trace_service) { |
| 73 Isolate* isolate = Isolate::Current(); | 61 Isolate* isolate = Isolate::Current(); |
| 74 ASSERT(isolate != NULL); | 62 ASSERT(isolate != NULL); |
| 75 const char* isolate_name = isolate->name(); | 63 const char* isolate_name = isolate->name(); |
| 76 OS::Print("Isolate %s processing service request %s", | 64 OS::Print("Isolate %s processing service request %s\n", |
| 77 isolate_name, command_); | 65 isolate_name, method_); |
| 78 for (intptr_t i = 1; i < num_arguments(); i++) { | |
| 79 OS::Print("/%s", GetArgument(i)); | |
| 80 } | |
| 81 OS::Print("\n"); | |
| 82 setup_time_micros_ = OS::GetCurrentTimeMicros(); | 66 setup_time_micros_ = OS::GetCurrentTimeMicros(); |
| 83 } | 67 } |
| 84 } | 68 } |
| 85 | |
| 86 | |
| 87 void JSONStream::SetupNew(Zone* zone, | |
| 88 Dart_Port reply_port, | |
| 89 const String& method, | |
| 90 const Array& option_keys, | |
| 91 const Array& option_values) { | |
| 92 set_reply_port(reply_port); | |
| 93 command_ = method.ToCString(); | |
| 94 | |
| 95 String& string_iterator = String::Handle(); | |
| 96 if (option_keys.Length() > 0) { | |
| 97 ASSERT(option_keys.Length() == option_values.Length()); | |
| 98 const char** option_keys_native = | |
| 99 zone->Alloc<const char*>(option_keys.Length()); | |
| 100 const char** option_values_native = | |
| 101 zone->Alloc<const char*>(option_keys.Length()); | |
| 102 for (intptr_t i = 0; i < option_keys.Length(); i++) { | |
| 103 string_iterator ^= option_keys.At(i); | |
| 104 option_keys_native[i] = | |
| 105 zone->MakeCopyOfString(string_iterator.ToCString()); | |
| 106 string_iterator ^= option_values.At(i); | |
| 107 option_values_native[i] = | |
| 108 zone->MakeCopyOfString(string_iterator.ToCString()); | |
| 109 } | |
| 110 SetOptions(option_keys_native, option_values_native, option_keys.Length()); | |
| 111 } | |
| 112 if (FLAG_trace_service) { | |
| 113 Isolate* isolate = Isolate::Current(); | |
| 114 ASSERT(isolate != NULL); | |
| 115 const char* isolate_name = isolate->name(); | |
| 116 OS::Print("Isolate %s processing service request %s", | |
| 117 isolate_name, command_); | |
| 118 for (intptr_t i = 1; i < num_arguments(); i++) { | |
| 119 OS::Print("/%s", GetArgument(i)); | |
| 120 } | |
| 121 OS::Print("\n"); | |
| 122 setup_time_micros_ = OS::GetCurrentTimeMicros(); | |
| 123 } | |
| 124 } | |
| 125 | 69 |
| 126 | 70 |
| 127 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 71 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 128 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 72 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
| 129 return reinterpret_cast<uint8_t*>(new_ptr); | 73 return reinterpret_cast<uint8_t*>(new_ptr); |
| 130 } | 74 } |
| 131 | 75 |
| 132 | 76 |
| 133 void JSONStream::PostReply() { | 77 void JSONStream::PostReply() { |
| 134 Dart_Port port = reply_port(); | 78 Dart_Port port = reply_port(); |
| 135 ASSERT(port != ILLEGAL_PORT); | 79 ASSERT(port != ILLEGAL_PORT); |
| 136 set_reply_port(ILLEGAL_PORT); // Prevent double replies. | 80 set_reply_port(ILLEGAL_PORT); // Prevent double replies. |
| 137 int64_t process_delta_micros = 0; | 81 int64_t process_delta_micros = 0; |
| 138 if (FLAG_trace_service) { | 82 if (FLAG_trace_service) { |
| 139 process_delta_micros = OS::GetCurrentTimeMicros() - setup_time_micros_; | 83 process_delta_micros = OS::GetCurrentTimeMicros() - setup_time_micros_; |
| 140 } | 84 } |
| 141 const String& reply = String::Handle(String::New(ToCString())); | 85 const String& reply = String::Handle(String::New(ToCString())); |
| 142 ASSERT(!reply.IsNull()); | 86 ASSERT(!reply.IsNull()); |
| 143 | 87 |
| 144 uint8_t* data = NULL; | 88 uint8_t* data = NULL; |
| 145 MessageWriter writer(&data, &allocator, false); | 89 MessageWriter writer(&data, &allocator, false); |
| 146 writer.WriteMessage(reply); | 90 writer.WriteMessage(reply); |
| 147 PortMap::PostMessage(new Message(port, data, | 91 PortMap::PostMessage(new Message(port, data, |
| 148 writer.BytesWritten(), | 92 writer.BytesWritten(), |
| 149 Message::kNormalPriority)); | 93 Message::kNormalPriority)); |
| 150 if (FLAG_trace_service) { | 94 if (FLAG_trace_service) { |
| 151 Isolate* isolate = Isolate::Current(); | 95 Isolate* isolate = Isolate::Current(); |
| 152 ASSERT(isolate != NULL); | 96 ASSERT(isolate != NULL); |
| 153 const char* isolate_name = isolate->name(); | 97 const char* isolate_name = isolate->name(); |
| 154 OS::Print("Isolate %s processed service request %s", | 98 OS::Print("Isolate %s processed service request %s in %" Pd64" us.\n", |
| 155 isolate_name, command_); | 99 isolate_name, method_, process_delta_micros); |
| 156 for (intptr_t i = 1; i < num_arguments(); i++) { | |
| 157 OS::Print("/%s", GetArgument(i)); | |
| 158 } | |
| 159 OS::Print(" in %" Pd64" us.\n", process_delta_micros); | |
| 160 } | 100 } |
| 161 } | 101 } |
| 162 | 102 |
| 163 | 103 |
| 164 const char* JSONStream::LookupOption(const char* key) const { | 104 const char* JSONStream::LookupParam(const char* key) const { |
| 165 for (int i = 0; i < num_options(); i++) { | 105 for (int i = 0; i < num_params(); i++) { |
| 166 if (!strcmp(key, option_keys_[i])) { | 106 if (!strcmp(key, param_keys_[i])) { |
| 167 return option_values_[i]; | 107 return param_values_[i]; |
| 168 } | 108 } |
| 169 } | 109 } |
| 170 return NULL; | 110 return NULL; |
| 171 } | 111 } |
| 172 | 112 |
| 173 | 113 |
| 174 bool JSONStream::HasOption(const char* key) const { | 114 bool JSONStream::HasParam(const char* key) const { |
| 175 ASSERT(key); | 115 ASSERT(key); |
| 176 return LookupOption(key) != NULL; | 116 return LookupParam(key) != NULL; |
| 177 } | 117 } |
| 178 | 118 |
| 179 | 119 |
| 180 bool JSONStream::OptionIs(const char* key, const char* value) const { | 120 bool JSONStream::ParamIs(const char* key, const char* value) const { |
| 181 ASSERT(key); | 121 ASSERT(key); |
| 182 ASSERT(value); | 122 ASSERT(value); |
| 183 const char* key_value = LookupOption(key); | 123 const char* key_value = LookupParam(key); |
| 184 return (key_value != NULL) && (strcmp(key_value, value) == 0); | 124 return (key_value != NULL) && (strcmp(key_value, value) == 0); |
| 185 } | 125 } |
| 186 | 126 |
| 187 | 127 |
| 188 void JSONStream::Clear() { | 128 void JSONStream::Clear() { |
| 189 buffer_.Clear(); | 129 buffer_.Clear(); |
| 190 open_objects_ = 0; | 130 open_objects_ = 0; |
| 191 } | 131 } |
| 192 | 132 |
| 193 | 133 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 buffer_.AddChar('"'); | 344 buffer_.AddChar('"'); |
| 405 free(p); | 345 free(p); |
| 406 } | 346 } |
| 407 | 347 |
| 408 | 348 |
| 409 void JSONStream::set_reply_port(Dart_Port port) { | 349 void JSONStream::set_reply_port(Dart_Port port) { |
| 410 reply_port_ = port; | 350 reply_port_ = port; |
| 411 } | 351 } |
| 412 | 352 |
| 413 | 353 |
| 414 void JSONStream::SetArguments(const char** arguments, intptr_t num_arguments) { | 354 void JSONStream::SetParams(const char** param_keys, |
| 415 if (num_arguments > 0) { | 355 const char** param_values, |
| 416 // Set command. | 356 intptr_t num_params) { |
| 417 command_ = arguments[0]; | 357 param_keys_ = param_keys; |
| 418 } | 358 param_values_ = param_values; |
| 419 arguments_ = arguments; | 359 num_params_ = num_params; |
| 420 num_arguments_ = num_arguments; | |
| 421 } | 360 } |
| 422 | 361 |
| 423 | 362 |
| 424 void JSONStream::SetOptions(const char** option_keys, | |
| 425 const char** option_values, | |
| 426 intptr_t num_options) { | |
| 427 option_keys_ = option_keys; | |
| 428 option_values_ = option_values; | |
| 429 num_options_ = num_options; | |
| 430 } | |
| 431 | |
| 432 | |
| 433 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { | 363 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { |
| 434 PrintPropertyName(name); | 364 PrintPropertyName(name); |
| 435 PrintValue(o, ref); | 365 PrintValue(o, ref); |
| 436 } | 366 } |
| 437 | 367 |
| 438 | 368 |
| 439 void JSONStream::PrintPropertyName(const char* name) { | 369 void JSONStream::PrintPropertyName(const char* name) { |
| 440 ASSERT(name != NULL); | 370 ASSERT(name != NULL); |
| 441 PrintCommaIfNeeded(); | 371 PrintCommaIfNeeded(); |
| 442 buffer_.AddChar('"'); | 372 buffer_.AddChar('"'); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 477 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 548 va_end(args); | 478 va_end(args); |
| 549 ASSERT(len == len2); | 479 ASSERT(len == len2); |
| 550 stream_->buffer_.AddChar('"'); | 480 stream_->buffer_.AddChar('"'); |
| 551 stream_->AddEscapedUTF8String(p); | 481 stream_->AddEscapedUTF8String(p); |
| 552 stream_->buffer_.AddChar('"'); | 482 stream_->buffer_.AddChar('"'); |
| 553 free(p); | 483 free(p); |
| 554 } | 484 } |
| 555 | 485 |
| 556 } // namespace dart | 486 } // namespace dart |
| OLD | NEW |