Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 "vm/message.h" | 5 #include "vm/message.h" |
| 6 | 6 |
| 7 #include "vm/dart_entry.h" | |
| 8 #include "vm/json_stream.h" | |
| 9 #include "vm/object.h" | |
| 7 #include "vm/port.h" | 10 #include "vm/port.h" |
| 8 | 11 |
| 9 namespace dart { | 12 namespace dart { |
| 10 | 13 |
| 11 bool Message::RedirectToDeliveryFailurePort() { | 14 bool Message::RedirectToDeliveryFailurePort() { |
| 12 if (delivery_failure_port_ == kIllegalPort) { | 15 if (delivery_failure_port_ == kIllegalPort) { |
| 13 return false; | 16 return false; |
| 14 } | 17 } |
| 15 dest_port_ = delivery_failure_port_; | 18 dest_port_ = delivery_failure_port_; |
| 16 delivery_failure_port_ = kIllegalPort; | 19 delivery_failure_port_ = kIllegalPort; |
| 17 return true; | 20 return true; |
| 18 } | 21 } |
| 19 | 22 |
| 20 | 23 |
| 24 intptr_t Message::Id() const { | |
| 25 // Messages are allocated on the C heap. Use the raw address as the id. | |
| 26 return reinterpret_cast<intptr_t>(this); | |
| 27 } | |
| 28 | |
| 29 const char* Message::PriorityAsString(Priority priority) { | |
| 30 switch (priority) { | |
| 31 case kNormalPriority: | |
| 32 return "Normal"; | |
| 33 break; | |
| 34 case kOOBPriority: | |
| 35 return "OOB"; | |
| 36 break; | |
| 37 default: | |
| 38 UNIMPLEMENTED(); | |
| 39 return NULL; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 | |
| 21 MessageQueue::MessageQueue() { | 44 MessageQueue::MessageQueue() { |
| 22 head_ = NULL; | 45 head_ = NULL; |
| 23 tail_ = NULL; | 46 tail_ = NULL; |
| 24 } | 47 } |
| 25 | 48 |
| 26 | 49 |
| 27 MessageQueue::~MessageQueue() { | 50 MessageQueue::~MessageQueue() { |
| 28 // Ensure that all pending messages have been released. | 51 // Ensure that all pending messages have been released. |
| 29 Clear(); | 52 Clear(); |
| 30 ASSERT(head_ == NULL); | 53 ASSERT(head_ == NULL); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 if (cur->RedirectToDeliveryFailurePort()) { | 122 if (cur->RedirectToDeliveryFailurePort()) { |
| 100 PortMap::PostMessage(cur); | 123 PortMap::PostMessage(cur); |
| 101 } else { | 124 } else { |
| 102 delete cur; | 125 delete cur; |
| 103 } | 126 } |
| 104 cur = next; | 127 cur = next; |
| 105 } | 128 } |
| 106 } | 129 } |
| 107 | 130 |
| 108 | 131 |
| 132 MessageQueue::Iterator::Iterator(const MessageQueue* queue) | |
| 133 : current_(NULL), | |
| 134 queue_(queue) { | |
| 135 Reset(queue); | |
| 136 } | |
| 137 | |
| 138 | |
| 139 MessageQueue::Iterator::~Iterator() { | |
| 140 } | |
| 141 | |
| 142 void MessageQueue::Iterator::Reset(const MessageQueue* queue) { | |
| 143 queue_ = queue; | |
| 144 current_ = NULL; | |
|
turnidge
2015/05/04 20:11:51
next_ = queue_->head_;
| |
| 145 } | |
| 146 | |
| 147 // returns false when there are no more messages left. | |
| 148 bool MessageQueue::Iterator::HasNext() { | |
|
turnidge
2015/05/04 20:11:51
return next_ != NULL
| |
| 149 if (current() == NULL) { | |
| 150 if (queue_ == NULL) { | |
| 151 return false; | |
| 152 } | |
| 153 // Does the queue contain anything? | |
| 154 return (queue_->head_ != NULL); | |
| 155 } | |
| 156 ASSERT(current_ != NULL); | |
| 157 return (current_->next_ != NULL); | |
| 158 } | |
| 159 | |
| 160 // Moves forward and then returns current(). | |
| 161 Message* MessageQueue::Iterator::Next() { | |
|
turnidge
2015/05/04 20:11:51
ASSERT(next_ != NULL);
Message* current = next_;
n
| |
| 162 if (queue_ != NULL) { | |
| 163 // First call to Next(). | |
| 164 current_ = queue_->head_; | |
| 165 queue_ = NULL; | |
| 166 } else { | |
| 167 current_ = current_->next_; | |
| 168 } | |
| 169 return current(); | |
| 170 } | |
| 171 | |
| 172 | |
| 173 intptr_t MessageQueue::Length() const { | |
| 174 MessageQueue::Iterator it(this); | |
| 175 intptr_t length = 0; | |
| 176 while (it.HasNext()) { | |
| 177 it.Next(); | |
| 178 length++; | |
| 179 } | |
| 180 return length; | |
| 181 } | |
| 182 | |
| 183 | |
| 184 Message* MessageQueue::FindMessageById(intptr_t id) { | |
| 185 MessageQueue::Iterator it(this); | |
| 186 while (it.HasNext()) { | |
| 187 Message* current = it.Next(); | |
| 188 ASSERT(current != NULL); | |
| 189 if (current->Id() == id) { | |
| 190 return current; | |
| 191 } | |
| 192 } | |
| 193 return NULL; | |
| 194 } | |
| 195 | |
| 196 | |
| 197 void MessageQueue::PrintJSON(JSONStream* stream) { | |
| 198 Isolate* isolate = Isolate::Current(); | |
| 199 JSONArray messages(stream); | |
| 200 | |
| 201 Object& msg_handler = Object::Handle(isolate); | |
| 202 | |
| 203 MessageQueue::Iterator it(this); | |
| 204 intptr_t depth = 0; | |
| 205 while (it.HasNext()) { | |
| 206 Message* current = it.Next(); | |
| 207 JSONObject message(&messages); | |
| 208 message.AddProperty("type", "Message"); | |
| 209 message.AddPropertyF("name", "Isolate Message (%" Px ")", current->Id()); | |
| 210 message.AddPropertyF("messageObjectId", "messages/%" Px "", | |
| 211 current->Id()); | |
| 212 message.AddProperty("size", current->len()); | |
| 213 message.AddProperty("depth", depth++); | |
| 214 message.AddProperty("_destinationPort", | |
| 215 static_cast<intptr_t>(current->dest_port())); | |
| 216 message.AddProperty("priority", | |
| 217 Message::PriorityAsString(current->priority())); | |
| 218 // TODO(johnmccutchan): Move port -> handler map out of Dart and into the | |
| 219 // VM, that way we can lookup the handler without invoking Dart code. | |
| 220 msg_handler = DartLibraryCalls::LookupHandler(current->dest_port()); | |
| 221 if (msg_handler.IsInstance() && Instance::Cast(msg_handler).IsClosure()) { | |
| 222 // Grab function from closure. | |
| 223 msg_handler = Closure::function(Instance::Cast(msg_handler)); | |
| 224 } | |
| 225 if (!msg_handler.IsFunction()) { | |
| 226 // No handler function. | |
| 227 continue; | |
| 228 } | |
| 229 const Function& function = Function::Cast(msg_handler); | |
| 230 const Script& script = Script::Handle(function.script()); | |
| 231 message.AddProperty("handlerFunction", function); | |
| 232 message.AddProperty("handlerScript", script); | |
| 233 message.AddProperty("handlerTokenPos", function.token_pos()); | |
| 234 } | |
| 235 } | |
| 236 | |
| 109 } // namespace dart | 237 } // namespace dart |
| OLD | NEW |