| 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/service_event.h" | 13 #include "vm/service_event.h" |
| 14 #include "vm/unicode.h" | 14 #include "vm/unicode.h" |
| 15 | 15 |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 DECLARE_FLAG(bool, trace_service); | 19 DECLARE_FLAG(bool, trace_service); |
| 20 | 20 |
| 21 JSONStream::JSONStream(intptr_t buf_size) | 21 JSONStream::JSONStream(intptr_t buf_size) |
| 22 : open_objects_(0), | 22 : open_objects_(0), |
| 23 buffer_(buf_size), | 23 buffer_(buf_size), |
| 24 reply_port_(ILLEGAL_PORT), | 24 reply_port_(ILLEGAL_PORT), |
| 25 seq_(""), |
| 25 method_(""), | 26 method_(""), |
| 26 param_keys_(NULL), | 27 param_keys_(NULL), |
| 27 param_values_(NULL), | 28 param_values_(NULL), |
| 28 num_params_(0) { | 29 num_params_(0) { |
| 29 } | 30 } |
| 30 | 31 |
| 31 | 32 |
| 32 JSONStream::~JSONStream() { | 33 JSONStream::~JSONStream() { |
| 33 } | 34 } |
| 34 | 35 |
| 35 | 36 |
| 36 void JSONStream::Setup(Zone* zone, | 37 void JSONStream::Setup(Zone* zone, |
| 37 Dart_Port reply_port, | 38 Dart_Port reply_port, |
| 39 const String& seq, |
| 38 const String& method, | 40 const String& method, |
| 39 const Array& param_keys, | 41 const Array& param_keys, |
| 40 const Array& param_values) { | 42 const Array& param_values) { |
| 41 set_reply_port(reply_port); | 43 set_reply_port(reply_port); |
| 44 seq_ = seq.ToCString(); |
| 42 method_ = method.ToCString(); | 45 method_ = method.ToCString(); |
| 43 | 46 |
| 44 String& string_iterator = String::Handle(); | 47 String& string_iterator = String::Handle(); |
| 45 if (param_keys.Length() > 0) { | 48 if (param_keys.Length() > 0) { |
| 46 ASSERT(param_keys.Length() == param_values.Length()); | 49 ASSERT(param_keys.Length() == param_values.Length()); |
| 47 const char** param_keys_native = | 50 const char** param_keys_native = |
| 48 zone->Alloc<const char*>(param_keys.Length()); | 51 zone->Alloc<const char*>(param_keys.Length()); |
| 49 const char** param_values_native = | 52 const char** param_values_native = |
| 50 zone->Alloc<const char*>(param_keys.Length()); | 53 zone->Alloc<const char*>(param_keys.Length()); |
| 51 for (intptr_t i = 0; i < param_keys.Length(); i++) { | 54 for (intptr_t i = 0; i < param_keys.Length(); i++) { |
| 52 string_iterator ^= param_keys.At(i); | 55 string_iterator ^= param_keys.At(i); |
| 53 param_keys_native[i] = | 56 param_keys_native[i] = |
| 54 zone->MakeCopyOfString(string_iterator.ToCString()); | 57 zone->MakeCopyOfString(string_iterator.ToCString()); |
| 55 string_iterator ^= param_values.At(i); | 58 string_iterator ^= param_values.At(i); |
| 56 param_values_native[i] = | 59 param_values_native[i] = |
| 57 zone->MakeCopyOfString(string_iterator.ToCString()); | 60 zone->MakeCopyOfString(string_iterator.ToCString()); |
| 58 } | 61 } |
| 59 SetParams(param_keys_native, param_values_native, param_keys.Length()); | 62 SetParams(param_keys_native, param_values_native, param_keys.Length()); |
| 60 } | 63 } |
| 61 if (FLAG_trace_service) { | 64 if (FLAG_trace_service) { |
| 62 Isolate* isolate = Isolate::Current(); | 65 Isolate* isolate = Isolate::Current(); |
| 63 ASSERT(isolate != NULL); | 66 ASSERT(isolate != NULL); |
| 64 const char* isolate_name = isolate->name(); | 67 const char* isolate_name = isolate->name(); |
| 65 OS::Print("Isolate %s processing service request %s\n", | 68 OS::Print("Isolate %s processing service request %s\n", |
| 66 isolate_name, method_); | 69 isolate_name, method_); |
| 67 setup_time_micros_ = OS::GetCurrentTimeMicros(); | 70 setup_time_micros_ = OS::GetCurrentTimeMicros(); |
| 68 } | 71 } |
| 72 buffer_.Printf("{\"result\":"); |
| 69 } | 73 } |
| 70 | 74 |
| 71 | 75 |
| 76 void JSONStream::SetupError() { |
| 77 buffer_.Clear(); |
| 78 buffer_.Printf("{\"error\":"); |
| 79 } |
| 80 |
| 81 |
| 72 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 82 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 73 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 83 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
| 74 return reinterpret_cast<uint8_t*>(new_ptr); | 84 return reinterpret_cast<uint8_t*>(new_ptr); |
| 75 } | 85 } |
| 76 | 86 |
| 77 | 87 |
| 78 void JSONStream::PostReply() { | 88 void JSONStream::PostReply() { |
| 79 Dart_Port port = reply_port(); | 89 Dart_Port port = reply_port(); |
| 80 ASSERT(port != ILLEGAL_PORT); | 90 ASSERT(port != ILLEGAL_PORT); |
| 81 set_reply_port(ILLEGAL_PORT); // Prevent double replies. | 91 set_reply_port(ILLEGAL_PORT); // Prevent double replies. |
| 82 int64_t process_delta_micros = 0; | 92 int64_t process_delta_micros = 0; |
| 83 if (FLAG_trace_service) { | 93 if (FLAG_trace_service) { |
| 84 process_delta_micros = OS::GetCurrentTimeMicros() - setup_time_micros_; | 94 process_delta_micros = OS::GetCurrentTimeMicros() - setup_time_micros_; |
| 85 } | 95 } |
| 96 // TODO(turnidge): Handle non-string sequence numbers. |
| 97 buffer_.Printf(", \"id\":\"%s\"}", seq()); |
| 86 const String& reply = String::Handle(String::New(ToCString())); | 98 const String& reply = String::Handle(String::New(ToCString())); |
| 87 ASSERT(!reply.IsNull()); | 99 ASSERT(!reply.IsNull()); |
| 88 | 100 |
| 89 uint8_t* data = NULL; | 101 uint8_t* data = NULL; |
| 90 MessageWriter writer(&data, &allocator, false); | 102 MessageWriter writer(&data, &allocator, false); |
| 91 writer.WriteMessage(reply); | 103 writer.WriteMessage(reply); |
| 92 bool result = PortMap::PostMessage(new Message(port, data, | 104 bool result = PortMap::PostMessage(new Message(port, data, |
| 93 writer.BytesWritten(), | 105 writer.BytesWritten(), |
| 94 Message::kNormalPriority)); | 106 Message::kNormalPriority)); |
| 95 if (FLAG_trace_service) { | 107 if (FLAG_trace_service) { |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); | 497 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); |
| 486 va_end(args); | 498 va_end(args); |
| 487 ASSERT(len == len2); | 499 ASSERT(len == len2); |
| 488 stream_->buffer_.AddChar('"'); | 500 stream_->buffer_.AddChar('"'); |
| 489 stream_->AddEscapedUTF8String(p); | 501 stream_->AddEscapedUTF8String(p); |
| 490 stream_->buffer_.AddChar('"'); | 502 stream_->buffer_.AddChar('"'); |
| 491 free(p); | 503 free(p); |
| 492 } | 504 } |
| 493 | 505 |
| 494 } // namespace dart | 506 } // namespace dart |
| OLD | NEW |