| 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 |
| 7 #include "vm/dart_entry.h" |
| 7 #include "vm/debugger.h" | 8 #include "vm/debugger.h" |
| 8 #include "vm/json_stream.h" | 9 #include "vm/json_stream.h" |
| 10 #include "vm/message.h" |
| 11 #include "vm/object.h" |
| 9 | 12 |
| 10 | 13 |
| 11 namespace dart { | 14 namespace dart { |
| 12 | 15 |
| 16 DECLARE_FLAG(bool, trace_service); |
| 17 |
| 13 JSONStream::JSONStream(intptr_t buf_size) | 18 JSONStream::JSONStream(intptr_t buf_size) |
| 14 : open_objects_(0), | 19 : open_objects_(0), |
| 15 buffer_(buf_size), | 20 buffer_(buf_size), |
| 16 reply_port_(ILLEGAL_PORT), | 21 reply_port_(ILLEGAL_PORT), |
| 22 command_(""), |
| 17 arguments_(NULL), | 23 arguments_(NULL), |
| 18 num_arguments_(0), | 24 num_arguments_(0), |
| 19 option_keys_(NULL), | 25 option_keys_(NULL), |
| 20 option_values_(NULL), | 26 option_values_(NULL), |
| 21 num_options_(0) { | 27 num_options_(0) { |
| 22 } | 28 } |
| 23 | 29 |
| 24 | 30 |
| 25 JSONStream::~JSONStream() { | 31 JSONStream::~JSONStream() { |
| 26 } | 32 } |
| 27 | 33 |
| 28 | 34 |
| 35 void JSONStream::Setup(Zone* zone, |
| 36 const Instance& reply_port, |
| 37 const GrowableObjectArray& path, |
| 38 const GrowableObjectArray& option_keys, |
| 39 const GrowableObjectArray& option_values) { |
| 40 // Setup the reply port. |
| 41 const Object& id_obj = Object::Handle( |
| 42 DartLibraryCalls::PortGetId(reply_port)); |
| 43 if (id_obj.IsError()) { |
| 44 Exceptions::PropagateError(Error::Cast(id_obj)); |
| 45 } |
| 46 const Integer& id = Integer::Cast(id_obj); |
| 47 Dart_Port port = static_cast<Dart_Port>(id.AsInt64Value()); |
| 48 ASSERT(port != ILLEGAL_PORT); |
| 49 set_reply_port(port); |
| 50 |
| 51 // Setup JSONStream arguments and options. The arguments and options |
| 52 // are zone allocated and will be freed immediately after handling the |
| 53 // message. |
| 54 const char** arguments = zone->Alloc<const char*>(path.Length()); |
| 55 String& string_iterator = String::Handle(); |
| 56 for (intptr_t i = 0; i < path.Length(); i++) { |
| 57 string_iterator ^= path.At(i); |
| 58 arguments[i] = zone->MakeCopyOfString(string_iterator.ToCString()); |
| 59 if (i == 0) { |
| 60 command_ = arguments[i]; |
| 61 } |
| 62 } |
| 63 SetArguments(arguments, path.Length()); |
| 64 if (option_keys.Length() > 0) { |
| 65 const char** option_keys_native = |
| 66 zone->Alloc<const char*>(option_keys.Length()); |
| 67 const char** option_values_native = |
| 68 zone->Alloc<const char*>(option_keys.Length()); |
| 69 for (intptr_t i = 0; i < option_keys.Length(); i++) { |
| 70 string_iterator ^= option_keys.At(i); |
| 71 option_keys_native[i] = |
| 72 zone->MakeCopyOfString(string_iterator.ToCString()); |
| 73 string_iterator ^= option_values.At(i); |
| 74 option_values_native[i] = |
| 75 zone->MakeCopyOfString(string_iterator.ToCString()); |
| 76 } |
| 77 SetOptions(option_keys_native, option_values_native, option_keys.Length()); |
| 78 } |
| 79 if (FLAG_trace_service) { |
| 80 Isolate* isolate = Isolate::Current(); |
| 81 ASSERT(isolate != NULL); |
| 82 const char* isolate_name = isolate->name(); |
| 83 OS::Print("Isolate %s processing service request /%s\n", |
| 84 isolate_name, command_); |
| 85 setup_time_micros_ = OS::GetCurrentTimeMicros(); |
| 86 } |
| 87 } |
| 88 |
| 89 |
| 90 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); |
| 92 return reinterpret_cast<uint8_t*>(new_ptr); |
| 93 } |
| 94 |
| 95 |
| 96 void JSONStream::PostReply() { |
| 97 Dart_Port port = reply_port(); |
| 98 ASSERT(port != ILLEGAL_PORT); |
| 99 set_reply_port(ILLEGAL_PORT); // Prevent double replies. |
| 100 int64_t process_delta_micros = 0; |
| 101 if (FLAG_trace_service) { |
| 102 process_delta_micros = OS::GetCurrentTimeMicros() - setup_time_micros_; |
| 103 } |
| 104 const String& reply = String::Handle(String::New(ToCString())); |
| 105 ASSERT(!reply.IsNull()); |
| 106 |
| 107 uint8_t* data = NULL; |
| 108 MessageWriter writer(&data, &allocator); |
| 109 writer.WriteMessage(reply); |
| 110 PortMap::PostMessage(new Message(port, data, |
| 111 writer.BytesWritten(), |
| 112 Message::kNormalPriority)); |
| 113 if (FLAG_trace_service) { |
| 114 Isolate* isolate = Isolate::Current(); |
| 115 ASSERT(isolate != NULL); |
| 116 const char* isolate_name = isolate->name(); |
| 117 OS::Print("Isolate %s processed service request /%s in %" Pd64" us.\n", |
| 118 isolate_name, command_, process_delta_micros); |
| 119 } |
| 120 } |
| 121 |
| 122 |
| 29 const char* JSONStream::LookupOption(const char* key) const { | 123 const char* JSONStream::LookupOption(const char* key) const { |
| 30 for (int i = 0; i < num_options(); i++) { | 124 for (int i = 0; i < num_options(); i++) { |
| 31 if (!strcmp(key, option_keys_[i])) { | 125 if (!strcmp(key, option_keys_[i])) { |
| 32 return option_values_[i]; | 126 return option_values_[i]; |
| 33 } | 127 } |
| 34 } | 128 } |
| 35 return NULL; | 129 return NULL; |
| 36 } | 130 } |
| 37 | 131 |
| 38 | 132 |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 358 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 265 va_end(args); | 359 va_end(args); |
| 266 ASSERT(len == len2); | 360 ASSERT(len == len2); |
| 267 stream_->buffer_.AddChar('"'); | 361 stream_->buffer_.AddChar('"'); |
| 268 stream_->buffer_.AddEscapedString(p); | 362 stream_->buffer_.AddEscapedString(p); |
| 269 stream_->buffer_.AddChar('"'); | 363 stream_->buffer_.AddChar('"'); |
| 270 free(p); | 364 free(p); |
| 271 } | 365 } |
| 272 | 366 |
| 273 } // namespace dart | 367 } // namespace dart |
| OLD | NEW |