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 if (queue_ == NULL) { | |
145 current_ = NULL; | |
146 return; | |
147 } | |
148 current_ = queue_->head_; | |
149 } | |
150 | |
151 // returns false when iterator has reached end of message queue. | |
152 bool MessageQueue::Iterator::HasNext() { | |
153 return current() != NULL; | |
154 } | |
155 | |
156 // Moves forward and then returns current(). | |
157 Message* MessageQueue::Iterator::Next() { | |
158 if (!HasNext()) { | |
159 return current(); | |
turnidge
2015/05/04 17:47:13
Your Next() and HasNext() are a bit different than
Cutch
2015/05/04 19:56:51
Done.
| |
160 } | |
161 current_ = current_->next_; | |
162 return current(); | |
163 } | |
164 | |
165 | |
166 intptr_t MessageQueue::Length() const { | |
167 MessageQueue::Iterator it(this); | |
168 intptr_t length = 0; | |
169 for (; it.HasNext(); it.Next()) { | |
170 length++; | |
171 } | |
172 return length; | |
173 } | |
174 | |
175 | |
176 Message* MessageQueue::FindMessageById(intptr_t id) { | |
177 MessageQueue::Iterator it(this); | |
178 for (; it.HasNext(); it.Next()) { | |
179 Message* message = it.current(); | |
180 ASSERT(message != NULL); | |
181 if (message->Id() == id) { | |
182 return message; | |
183 } | |
184 } | |
185 return NULL; | |
186 } | |
187 | |
188 | |
189 void MessageQueue::PrintJSON(JSONStream* stream) { | |
190 Isolate* isolate = Isolate::Current(); | |
191 JSONArray messages(stream); | |
192 | |
193 MessageQueue::Iterator it(this); | |
194 intptr_t depth = 0; | |
195 | |
196 Object& msg_handler = Object::Handle(isolate); | |
197 for (; it.HasNext(); it.Next()) { | |
198 Message* current = it.current(); | |
199 JSONObject message(&messages); | |
200 message.AddProperty("type", "IsolateMessage"); | |
turnidge
2015/05/04 17:47:13
IsolateMessage? I don't like the "Isolate". Mayb
Cutch
2015/05/04 19:56:51
Done.
| |
201 message.AddPropertyF("name", "Isolate Message (%" Px ")", current->Id()); | |
202 message.AddPropertyF("messageObjectId", "messages/0x%" Px "", | |
203 current->Id()); | |
204 message.AddProperty("size", current->len()); | |
205 message.AddProperty("depth", depth++); | |
206 message.AddProperty("destinationPort", | |
turnidge
2015/05/04 17:47:13
Make this _destinationPort.
Cutch
2015/05/04 19:56:51
Done.
| |
207 static_cast<intptr_t>(current->dest_port())); | |
208 message.AddProperty("priority", | |
209 Message::PriorityAsString(current->priority())); | |
210 msg_handler = DartLibraryCalls::LookupHandler(current->dest_port()); | |
turnidge
2015/05/04 17:47:13
Add a TODO to move this map into the VM so we can
Cutch
2015/05/04 19:56:51
Done.
| |
211 if (msg_handler.IsInstance() && Instance::Cast(msg_handler).IsClosure()) { | |
212 // Grab function from closure. | |
213 msg_handler = Closure::function(Instance::Cast(msg_handler)); | |
214 } | |
215 if (!msg_handler.IsFunction()) { | |
216 // No handler function. | |
217 continue; | |
218 } | |
219 const Function& function = Function::Cast(msg_handler); | |
220 const Script& script = Script::Handle(isolate, function.script()); | |
221 message.AddProperty("handlerFunction", function); | |
222 message.AddProperty("handlerScript", script); | |
turnidge
2015/05/04 17:47:13
Consider dropping handlerScript. Up to you.
Cutch
2015/05/04 19:56:51
Done.
| |
223 } | |
224 } | |
225 | |
109 } // namespace dart | 226 } // namespace dart |
OLD | NEW |