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 "vm/service.h" | 5 #include "vm/service.h" |
6 | 6 |
7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
9 | 9 |
10 #include "vm/compiler.h" | 10 #include "vm/compiler.h" |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); | 151 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
152 return reinterpret_cast<uint8_t*>(new_ptr); | 152 return reinterpret_cast<uint8_t*>(new_ptr); |
153 } | 153 } |
154 | 154 |
155 | 155 |
156 static void SendIsolateServiceMessage(Dart_NativeArguments args) { | 156 static void SendIsolateServiceMessage(Dart_NativeArguments args) { |
157 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 157 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
158 Isolate* isolate = arguments->isolate(); | 158 Isolate* isolate = arguments->isolate(); |
159 StackZone zone(isolate); | 159 StackZone zone(isolate); |
160 HANDLESCOPE(isolate); | 160 HANDLESCOPE(isolate); |
161 GET_NON_NULL_NATIVE_ARGUMENT(Instance, sp, arguments->NativeArgAt(0)); | 161 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, sp, arguments->NativeArgAt(0)); |
162 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(1)); | 162 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(1)); |
163 | 163 |
164 // Extract SendPort port id. | |
165 const Object& sp_id_obj = Object::Handle(DartLibraryCalls::PortGetId(sp)); | |
166 if (sp_id_obj.IsError()) { | |
167 Exceptions::PropagateError(Error::Cast(sp_id_obj)); | |
168 } | |
169 Integer& id = Integer::Handle(isolate); | |
170 id ^= sp_id_obj.raw(); | |
171 Dart_Port sp_id = static_cast<Dart_Port>(id.AsInt64Value()); | |
172 ASSERT(sp_id != ILLEGAL_PORT); | |
173 | |
174 // Serialize message. | 164 // Serialize message. |
175 uint8_t* data = NULL; | 165 uint8_t* data = NULL; |
176 MessageWriter writer(&data, &allocator); | 166 MessageWriter writer(&data, &allocator); |
177 writer.WriteMessage(message); | 167 writer.WriteMessage(message); |
178 | 168 |
179 // TODO(turnidge): Throw an exception when the return value is false? | 169 // TODO(turnidge): Throw an exception when the return value is false? |
180 PortMap::PostMessage(new Message(sp_id, data, writer.BytesWritten(), | 170 PortMap::PostMessage(new Message(sp.Id(), data, writer.BytesWritten(), |
181 Message::kOOBPriority)); | 171 Message::kOOBPriority)); |
182 } | 172 } |
183 | 173 |
184 | 174 |
185 static void SendRootServiceMessage(Dart_NativeArguments args) { | 175 static void SendRootServiceMessage(Dart_NativeArguments args) { |
186 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 176 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
187 Isolate* isolate = arguments->isolate(); | 177 Isolate* isolate = arguments->isolate(); |
188 StackZone zone(isolate); | 178 StackZone zone(isolate); |
189 HANDLESCOPE(isolate); | 179 HANDLESCOPE(isolate); |
190 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(0)); | 180 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(0)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 Isolate* Service::service_isolate_ = NULL; | 224 Isolate* Service::service_isolate_ = NULL; |
235 Dart_LibraryTagHandler Service::default_handler_ = NULL; | 225 Dart_LibraryTagHandler Service::default_handler_ = NULL; |
236 Dart_Port Service::port_ = ILLEGAL_PORT; | 226 Dart_Port Service::port_ = ILLEGAL_PORT; |
237 | 227 |
238 | 228 |
239 static Dart_Port ExtractPort(Dart_Handle receivePort) { | 229 static Dart_Port ExtractPort(Dart_Handle receivePort) { |
240 HANDLESCOPE(Isolate::Current()); | 230 HANDLESCOPE(Isolate::Current()); |
241 const Object& unwrapped_rp = Object::Handle(Api::UnwrapHandle(receivePort)); | 231 const Object& unwrapped_rp = Object::Handle(Api::UnwrapHandle(receivePort)); |
242 const Instance& rp = Instance::Cast(unwrapped_rp); | 232 const Instance& rp = Instance::Cast(unwrapped_rp); |
243 // Extract RawReceivePort port id. | 233 // Extract RawReceivePort port id. |
244 const Object& rp_id_obj = Object::Handle(DartLibraryCalls::PortGetId(rp)); | 234 if (!rp.IsReceivePort()) { |
245 if (rp_id_obj.IsError()) { | |
246 return ILLEGAL_PORT; | 235 return ILLEGAL_PORT; |
247 } | 236 } |
248 ASSERT(rp_id_obj.IsSmi() || rp_id_obj.IsMint()); | 237 return ReceivePort::Cast(rp).Id(); |
249 const Integer& id = Integer::Cast(rp_id_obj); | |
250 return static_cast<Dart_Port>(id.AsInt64Value()); | |
251 } | 238 } |
252 | 239 |
253 | 240 |
254 // These must be kept in sync with service/constants.dart | 241 // These must be kept in sync with service/constants.dart |
255 #define VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID 1 | 242 #define VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID 1 |
256 #define VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID 2 | 243 #define VM_SERVICE_ISOLATE_SHUTDOWN_MESSAGE_ID 2 |
257 | 244 |
258 | 245 |
259 static RawArray* MakeServiceControlMessage(Dart_Port port_id, intptr_t code, | 246 static RawArray* MakeServiceControlMessage(Dart_Port port_id, intptr_t code, |
260 const String& name) { | 247 const String& name) { |
261 const Array& list = Array::Handle(Array::New(4)); | 248 const Array& list = Array::Handle(Array::New(4)); |
262 ASSERT(!list.IsNull()); | 249 ASSERT(!list.IsNull()); |
263 const Integer& code_int = Integer::Handle(Integer::New(code)); | 250 const Integer& code_int = Integer::Handle(Integer::New(code)); |
264 const Integer& port_int = Integer::Handle(Integer::New(port_id)); | 251 const Integer& port_int = Integer::Handle(Integer::New(port_id)); |
265 const Object& send_port = Object::Handle( | 252 const SendPort& send_port = SendPort::Handle(SendPort::New(port_id)); |
266 DartLibraryCalls::NewSendPort(port_id)); | |
267 ASSERT(!send_port.IsNull()); | |
268 list.SetAt(0, code_int); | 253 list.SetAt(0, code_int); |
269 list.SetAt(1, port_int); | 254 list.SetAt(1, port_int); |
270 list.SetAt(2, send_port); | 255 list.SetAt(2, send_port); |
271 list.SetAt(3, name); | 256 list.SetAt(3, name); |
272 return list.raw(); | 257 return list.raw(); |
273 } | 258 } |
274 | 259 |
275 | 260 |
276 class RegisterRunningIsolatesVisitor : public IsolateVisitor { | 261 class RegisterRunningIsolatesVisitor : public IsolateVisitor { |
277 public: | 262 public: |
(...skipping 20 matching lines...) Expand all Loading... |
298 ASSERT(Isolate::Current() == service_isolate_); | 283 ASSERT(Isolate::Current() == service_isolate_); |
299 if ((isolate == service_isolate_) || | 284 if ((isolate == service_isolate_) || |
300 (isolate == Dart::vm_isolate())) { | 285 (isolate == Dart::vm_isolate())) { |
301 // We do not register the service or vm isolate. | 286 // We do not register the service or vm isolate. |
302 return; | 287 return; |
303 } | 288 } |
304 // Setup arguments for call. | 289 // Setup arguments for call. |
305 intptr_t port_id = isolate->main_port(); | 290 intptr_t port_id = isolate->main_port(); |
306 const Integer& port_int = Integer::Handle(Integer::New(port_id)); | 291 const Integer& port_int = Integer::Handle(Integer::New(port_id)); |
307 ASSERT(!port_int.IsNull()); | 292 ASSERT(!port_int.IsNull()); |
308 const Object& send_port = Object::Handle( | 293 const SendPort& send_port = SendPort::Handle(SendPort::New(port_id)); |
309 DartLibraryCalls::NewSendPort(port_id)); | |
310 ASSERT(!send_port.IsNull()); | |
311 const String& name = String::Handle(String::New(isolate->name())); | 294 const String& name = String::Handle(String::New(isolate->name())); |
312 ASSERT(!name.IsNull()); | 295 ASSERT(!name.IsNull()); |
313 const Array& args = Array::Handle(Array::New(3)); | 296 const Array& args = Array::Handle(Array::New(3)); |
314 ASSERT(!args.IsNull()); | 297 ASSERT(!args.IsNull()); |
315 args.SetAt(0, port_int); | 298 args.SetAt(0, port_int); |
316 args.SetAt(1, send_port); | 299 args.SetAt(1, send_port); |
317 args.SetAt(2, name); | 300 args.SetAt(2, name); |
318 Object& r = Object::Handle(service_isolate_); | 301 Object& r = Object::Handle(service_isolate_); |
319 r = DartEntry::InvokeFunction(register_function_, args); | 302 r = DartEntry::InvokeFunction(register_function_, args); |
320 ASSERT(!r.IsError()); | 303 ASSERT(!r.IsError()); |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 path ^= message.At(1); | 637 path ^= message.At(1); |
655 option_keys ^= message.At(2); | 638 option_keys ^= message.At(2); |
656 option_values ^= message.At(3); | 639 option_values ^= message.At(3); |
657 | 640 |
658 ASSERT(!path.IsNull()); | 641 ASSERT(!path.IsNull()); |
659 ASSERT(!option_keys.IsNull()); | 642 ASSERT(!option_keys.IsNull()); |
660 ASSERT(!option_values.IsNull()); | 643 ASSERT(!option_values.IsNull()); |
661 // Same number of option keys as values. | 644 // Same number of option keys as values. |
662 ASSERT(option_keys.Length() == option_values.Length()); | 645 ASSERT(option_keys.Length() == option_values.Length()); |
663 | 646 |
| 647 if (!reply_port.IsSendPort()) { |
| 648 FATAL("SendPort expected."); |
| 649 } |
| 650 |
664 String& path_segment = String::Handle(); | 651 String& path_segment = String::Handle(); |
665 if (path.Length() > 0) { | 652 if (path.Length() > 0) { |
666 path_segment ^= path.At(0); | 653 path_segment ^= path.At(0); |
667 } else { | 654 } else { |
668 path_segment ^= Symbols::Empty().raw(); | 655 path_segment ^= Symbols::Empty().raw(); |
669 } | 656 } |
670 ASSERT(!path_segment.IsNull()); | 657 ASSERT(!path_segment.IsNull()); |
671 const char* path_segment_c = path_segment.ToCString(); | 658 const char* path_segment_c = path_segment.ToCString(); |
672 | 659 |
673 IsolateMessageHandler handler = | 660 IsolateMessageHandler handler = |
674 FindIsolateMessageHandler(path_segment_c); | 661 FindIsolateMessageHandler(path_segment_c); |
675 { | 662 { |
676 JSONStream js; | 663 JSONStream js; |
677 js.Setup(zone.GetZone(), reply_port, path, option_keys, option_values); | 664 js.Setup(zone.GetZone(), SendPort::Cast(reply_port).Id(), |
| 665 path, option_keys, option_values); |
678 if (handler == NULL) { | 666 if (handler == NULL) { |
679 // Check for an embedder handler. | 667 // Check for an embedder handler. |
680 EmbedderServiceHandler* e_handler = | 668 EmbedderServiceHandler* e_handler = |
681 FindIsolateEmbedderHandler(path_segment_c); | 669 FindIsolateEmbedderHandler(path_segment_c); |
682 if (e_handler != NULL) { | 670 if (e_handler != NULL) { |
683 EmbedderHandleMessage(e_handler, &js); | 671 EmbedderHandleMessage(e_handler, &js); |
684 } else { | 672 } else { |
685 PrintError(&js, "Unrecognized path"); | 673 PrintError(&js, "Unrecognized path"); |
686 } | 674 } |
687 js.PostReply(); | 675 js.PostReply(); |
(...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1751 HANDLESCOPE(isolate); | 1739 HANDLESCOPE(isolate); |
1752 | 1740 |
1753 const GrowableObjectArray& message = GrowableObjectArray::Cast(msg); | 1741 const GrowableObjectArray& message = GrowableObjectArray::Cast(msg); |
1754 // Message is a list with four entries. | 1742 // Message is a list with four entries. |
1755 ASSERT(message.Length() == 4); | 1743 ASSERT(message.Length() == 4); |
1756 | 1744 |
1757 Instance& reply_port = Instance::Handle(isolate); | 1745 Instance& reply_port = Instance::Handle(isolate); |
1758 GrowableObjectArray& path = GrowableObjectArray::Handle(isolate); | 1746 GrowableObjectArray& path = GrowableObjectArray::Handle(isolate); |
1759 GrowableObjectArray& option_keys = GrowableObjectArray::Handle(isolate); | 1747 GrowableObjectArray& option_keys = GrowableObjectArray::Handle(isolate); |
1760 GrowableObjectArray& option_values = GrowableObjectArray::Handle(isolate); | 1748 GrowableObjectArray& option_values = GrowableObjectArray::Handle(isolate); |
| 1749 |
1761 reply_port ^= message.At(0); | 1750 reply_port ^= message.At(0); |
1762 path ^= message.At(1); | 1751 path ^= message.At(1); |
1763 option_keys ^= message.At(2); | 1752 option_keys ^= message.At(2); |
1764 option_values ^= message.At(3); | 1753 option_values ^= message.At(3); |
1765 | 1754 |
1766 ASSERT(!path.IsNull()); | 1755 ASSERT(!path.IsNull()); |
1767 ASSERT(!option_keys.IsNull()); | 1756 ASSERT(!option_keys.IsNull()); |
1768 ASSERT(!option_values.IsNull()); | 1757 ASSERT(!option_values.IsNull()); |
1769 // Path always has at least one entry in it. | 1758 // Path always has at least one entry in it. |
1770 ASSERT(path.Length() > 0); | 1759 ASSERT(path.Length() > 0); |
1771 // Same number of option keys as values. | 1760 // Same number of option keys as values. |
1772 ASSERT(option_keys.Length() == option_values.Length()); | 1761 ASSERT(option_keys.Length() == option_values.Length()); |
1773 | 1762 |
| 1763 if (!reply_port.IsSendPort()) { |
| 1764 FATAL("SendPort expected."); |
| 1765 } |
| 1766 |
1774 String& path_segment = String::Handle(); | 1767 String& path_segment = String::Handle(); |
1775 if (path.Length() > 0) { | 1768 if (path.Length() > 0) { |
1776 path_segment ^= path.At(0); | 1769 path_segment ^= path.At(0); |
1777 } else { | 1770 } else { |
1778 path_segment ^= Symbols::Empty().raw(); | 1771 path_segment ^= Symbols::Empty().raw(); |
1779 } | 1772 } |
1780 ASSERT(!path_segment.IsNull()); | 1773 ASSERT(!path_segment.IsNull()); |
1781 const char* path_segment_c = path_segment.ToCString(); | 1774 const char* path_segment_c = path_segment.ToCString(); |
1782 | 1775 |
1783 RootMessageHandler handler = | 1776 RootMessageHandler handler = |
1784 FindRootMessageHandler(path_segment_c); | 1777 FindRootMessageHandler(path_segment_c); |
1785 { | 1778 { |
1786 JSONStream js; | 1779 JSONStream js; |
1787 js.Setup(zone.GetZone(), reply_port, path, option_keys, option_values); | 1780 js.Setup(zone.GetZone(), SendPort::Cast(reply_port).Id(), |
| 1781 path, option_keys, option_values); |
1788 if (handler == NULL) { | 1782 if (handler == NULL) { |
1789 // Check for an embedder handler. | 1783 // Check for an embedder handler. |
1790 EmbedderServiceHandler* e_handler = | 1784 EmbedderServiceHandler* e_handler = |
1791 FindRootEmbedderHandler(path_segment_c); | 1785 FindRootEmbedderHandler(path_segment_c); |
1792 if (e_handler != NULL) { | 1786 if (e_handler != NULL) { |
1793 EmbedderHandleMessage(e_handler, &js); | 1787 EmbedderHandleMessage(e_handler, &js); |
1794 } else { | 1788 } else { |
1795 PrintError(&js, "Unrecognized path"); | 1789 PrintError(&js, "Unrecognized path"); |
1796 } | 1790 } |
1797 js.PostReply(); | 1791 js.PostReply(); |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1969 while (current != NULL) { | 1963 while (current != NULL) { |
1970 if (strcmp(name, current->name()) == 0) { | 1964 if (strcmp(name, current->name()) == 0) { |
1971 return current; | 1965 return current; |
1972 } | 1966 } |
1973 current = current->next(); | 1967 current = current->next(); |
1974 } | 1968 } |
1975 return NULL; | 1969 return NULL; |
1976 } | 1970 } |
1977 | 1971 |
1978 } // namespace dart | 1972 } // namespace dart |
OLD | NEW |