OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 #include "vm/bootstrap_natives.h" |
| 6 |
| 7 #include "vm/dart_api_impl.h" |
| 8 #include "vm/exceptions.h" |
| 9 #include "vm/message.h" |
| 10 #include "vm/native_entry.h" |
| 11 #include "vm/object.h" |
| 12 #include "vm/port.h" |
| 13 #include "vm/service_isolate.h" |
| 14 #include "vm/symbols.h" |
| 15 |
| 16 namespace dart { |
| 17 |
| 18 DECLARE_FLAG(bool, trace_service); |
| 19 |
| 20 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 21 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); |
| 22 return reinterpret_cast<uint8_t*>(new_ptr); |
| 23 } |
| 24 |
| 25 |
| 26 class RegisterRunningIsolatesVisitor : public IsolateVisitor { |
| 27 public: |
| 28 explicit RegisterRunningIsolatesVisitor(Isolate* service_isolate) |
| 29 : IsolateVisitor(), |
| 30 register_function_(Function::Handle(service_isolate)), |
| 31 service_isolate_(service_isolate) { |
| 32 ASSERT(ServiceIsolate::IsServiceIsolate(Isolate::Current())); |
| 33 // Get library. |
| 34 const String& library_url = Symbols::DartVMService(); |
| 35 ASSERT(!library_url.IsNull()); |
| 36 const Library& library = |
| 37 Library::Handle(Library::LookupLibrary(library_url)); |
| 38 ASSERT(!library.IsNull()); |
| 39 // Get function. |
| 40 const String& function_name = |
| 41 String::Handle(String::New("_registerIsolate")); |
| 42 ASSERT(!function_name.IsNull()); |
| 43 register_function_ = library.LookupFunctionAllowPrivate(function_name); |
| 44 ASSERT(!register_function_.IsNull()); |
| 45 } |
| 46 |
| 47 virtual void VisitIsolate(Isolate* isolate) { |
| 48 ASSERT(ServiceIsolate::IsServiceIsolate(Isolate::Current())); |
| 49 if (ServiceIsolate::IsServiceIsolateDescendant(isolate) || |
| 50 (isolate == Dart::vm_isolate())) { |
| 51 // We do not register the service (and descendants) or the vm-isolate. |
| 52 return; |
| 53 } |
| 54 // Setup arguments for call. |
| 55 Dart_Port port_id = isolate->main_port(); |
| 56 const Integer& port_int = Integer::Handle(Integer::New(port_id)); |
| 57 ASSERT(!port_int.IsNull()); |
| 58 const SendPort& send_port = SendPort::Handle(SendPort::New(port_id)); |
| 59 const String& name = String::Handle(String::New(isolate->name())); |
| 60 ASSERT(!name.IsNull()); |
| 61 const Array& args = Array::Handle(Array::New(3)); |
| 62 ASSERT(!args.IsNull()); |
| 63 args.SetAt(0, port_int); |
| 64 args.SetAt(1, send_port); |
| 65 args.SetAt(2, name); |
| 66 Object& r = Object::Handle(service_isolate_); |
| 67 r = DartEntry::InvokeFunction(register_function_, args); |
| 68 if (FLAG_trace_service) { |
| 69 OS::Print("vm-service: Isolate %s %" Pd64 " registered.\n", |
| 70 name.ToCString(), |
| 71 port_id); |
| 72 } |
| 73 ASSERT(!r.IsError()); |
| 74 } |
| 75 |
| 76 private: |
| 77 Function& register_function_; |
| 78 Isolate* service_isolate_; |
| 79 }; |
| 80 |
| 81 |
| 82 DEFINE_NATIVE_ENTRY(VMService_SendIsolateServiceMessage, 2) { |
| 83 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, sp, arguments->NativeArgAt(0)); |
| 84 GET_NON_NULL_NATIVE_ARGUMENT(Array, message, arguments->NativeArgAt(1)); |
| 85 |
| 86 // Set the type of the OOB message. |
| 87 message.SetAt(0, Smi::Handle(thread->zone(), |
| 88 Smi::New(Message::kServiceOOBMsg))); |
| 89 |
| 90 // Serialize message. |
| 91 uint8_t* data = NULL; |
| 92 MessageWriter writer(&data, &allocator, false); |
| 93 writer.WriteMessage(message); |
| 94 |
| 95 // TODO(turnidge): Throw an exception when the return value is false? |
| 96 bool result = PortMap::PostMessage( |
| 97 new Message(sp.Id(), data, writer.BytesWritten(), |
| 98 Message::kOOBPriority)); |
| 99 return Bool::Get(result).raw(); |
| 100 } |
| 101 |
| 102 |
| 103 DEFINE_NATIVE_ENTRY(VMService_SendRootServiceMessage, 1) { |
| 104 GET_NON_NULL_NATIVE_ARGUMENT(Array, message, arguments->NativeArgAt(0)); |
| 105 Service::HandleRootMessage(message); |
| 106 return Object::null(); |
| 107 } |
| 108 |
| 109 |
| 110 DEFINE_NATIVE_ENTRY(VMService_OnStart, 0) { |
| 111 if (FLAG_trace_service) { |
| 112 OS::Print("vm-service: Booting dart:vmservice library.\n"); |
| 113 } |
| 114 // Boot the dart:vmservice library. |
| 115 ServiceIsolate::BootVmServiceLibrary(); |
| 116 |
| 117 // Register running isolates with service. |
| 118 RegisterRunningIsolatesVisitor register_isolates(isolate); |
| 119 if (FLAG_trace_service) { |
| 120 OS::Print("vm-service: Registering running isolates.\n"); |
| 121 } |
| 122 Isolate::VisitIsolates(®ister_isolates); |
| 123 |
| 124 return Object::null(); |
| 125 } |
| 126 |
| 127 |
| 128 DEFINE_NATIVE_ENTRY(VMService_OnExit, 0) { |
| 129 if (FLAG_trace_service) { |
| 130 OS::Print("vm-service: processed exit message.\n"); |
| 131 } |
| 132 return Object::null(); |
| 133 } |
| 134 |
| 135 |
| 136 DEFINE_NATIVE_ENTRY(VMService_ListenStream, 1) { |
| 137 GET_NON_NULL_NATIVE_ARGUMENT(String, stream_id, arguments->NativeArgAt(0)); |
| 138 bool result = Service::ListenStream(stream_id.ToCString()); |
| 139 return Bool::Get(result).raw(); |
| 140 } |
| 141 |
| 142 |
| 143 DEFINE_NATIVE_ENTRY(VMService_CancelStream, 1) { |
| 144 GET_NON_NULL_NATIVE_ARGUMENT(String, stream_id, arguments->NativeArgAt(0)); |
| 145 Service::CancelStream(stream_id.ToCString()); |
| 146 return Object::null(); |
| 147 } |
| 148 |
| 149 } // namespace dart |
OLD | NEW |