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 "bin/vmservice_impl.h" | 5 #include "bin/vmservice_impl.h" |
6 | 6 |
7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 | 8 |
9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
11 #include "bin/isolate_data.h" | 11 #include "bin/isolate_data.h" |
12 #include "bin/platform.h" | 12 #include "bin/platform.h" |
13 #include "bin/thread.h" | 13 #include "bin/thread.h" |
| 14 #include "bin/utils.h" |
14 #include "platform/json.h" | 15 #include "platform/json.h" |
15 | 16 |
16 namespace dart { | 17 namespace dart { |
17 namespace bin { | 18 namespace bin { |
18 | 19 |
19 #define RETURN_ERROR_HANDLE(handle) \ | 20 #define RETURN_ERROR_HANDLE(handle) \ |
20 if (Dart_IsError(handle)) { \ | 21 if (Dart_IsError(handle)) { \ |
21 return handle; \ | 22 return handle; \ |
22 } | 23 } |
23 | 24 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 88 |
88 | 89 |
89 void TriggerResourceLoad(Dart_NativeArguments args) { | 90 void TriggerResourceLoad(Dart_NativeArguments args) { |
90 Dart_Handle library = Dart_RootLibrary(); | 91 Dart_Handle library = Dart_RootLibrary(); |
91 ASSERT(!Dart_IsError(library)); | 92 ASSERT(!Dart_IsError(library)); |
92 Dart_Handle result = VmService::LoadResources(library); | 93 Dart_Handle result = VmService::LoadResources(library); |
93 ASSERT(!Dart_IsError(result)); | 94 ASSERT(!Dart_IsError(result)); |
94 } | 95 } |
95 | 96 |
96 | 97 |
| 98 void NotifyServerState(Dart_NativeArguments args) { |
| 99 Dart_EnterScope(); |
| 100 const char* ip_chars; |
| 101 Dart_Handle ip_arg = Dart_GetNativeArgument(args, 0); |
| 102 if (Dart_IsError(ip_arg)) { |
| 103 VmService::SetServerIPAndPort("", 0); |
| 104 Dart_ExitScope(); |
| 105 return; |
| 106 } |
| 107 Dart_Handle result = Dart_StringToCString(ip_arg, &ip_chars); |
| 108 if (Dart_IsError(result)) { |
| 109 VmService::SetServerIPAndPort("", 0); |
| 110 Dart_ExitScope(); |
| 111 return; |
| 112 } |
| 113 Dart_Handle port_arg = Dart_GetNativeArgument(args, 1); |
| 114 if (Dart_IsError(port_arg)) { |
| 115 VmService::SetServerIPAndPort("", 0); |
| 116 Dart_ExitScope(); |
| 117 return; |
| 118 } |
| 119 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
| 120 VmService::SetServerIPAndPort(ip_chars, port); |
| 121 Dart_ExitScope(); |
| 122 } |
| 123 |
97 struct VmServiceIONativeEntry { | 124 struct VmServiceIONativeEntry { |
98 const char* name; | 125 const char* name; |
99 int num_arguments; | 126 int num_arguments; |
100 Dart_NativeFunction function; | 127 Dart_NativeFunction function; |
101 }; | 128 }; |
102 | 129 |
103 | 130 |
104 static VmServiceIONativeEntry _VmServiceIONativeEntries[] = { | 131 static VmServiceIONativeEntry _VmServiceIONativeEntries[] = { |
105 {"VMServiceIO_TriggerResourceLoad", 0, TriggerResourceLoad}, | 132 {"VMServiceIO_TriggerResourceLoad", 0, TriggerResourceLoad}, |
| 133 {"VMServiceIO_NotifyServerState", 2, NotifyServerState}, |
106 }; | 134 }; |
107 | 135 |
108 | 136 |
109 static Dart_NativeFunction VmServiceIONativeResolver(Dart_Handle name, | 137 static Dart_NativeFunction VmServiceIONativeResolver(Dart_Handle name, |
110 int num_arguments, | 138 int num_arguments, |
111 bool* auto_setup_scope) { | 139 bool* auto_setup_scope) { |
112 const char* function_name = NULL; | 140 const char* function_name = NULL; |
113 Dart_Handle result = Dart_StringToCString(name, &function_name); | 141 Dart_Handle result = Dart_StringToCString(name, &function_name); |
114 ASSERT(!Dart_IsError(result)); | 142 ASSERT(!Dart_IsError(result)); |
115 ASSERT(function_name != NULL); | 143 ASSERT(function_name != NULL); |
116 *auto_setup_scope = true; | 144 *auto_setup_scope = true; |
117 intptr_t n = | 145 intptr_t n = |
118 sizeof(_VmServiceIONativeEntries) / sizeof(_VmServiceIONativeEntries[0]); | 146 sizeof(_VmServiceIONativeEntries) / sizeof(_VmServiceIONativeEntries[0]); |
119 for (intptr_t i = 0; i < n; i++) { | 147 for (intptr_t i = 0; i < n; i++) { |
120 VmServiceIONativeEntry entry = _VmServiceIONativeEntries[i]; | 148 VmServiceIONativeEntry entry = _VmServiceIONativeEntries[i]; |
121 if ((strcmp(function_name, entry.name) == 0) && | 149 if ((strcmp(function_name, entry.name) == 0) && |
122 (num_arguments == entry.num_arguments)) { | 150 (num_arguments == entry.num_arguments)) { |
123 return entry.function; | 151 return entry.function; |
124 } | 152 } |
125 } | 153 } |
126 return NULL; | 154 return NULL; |
127 } | 155 } |
128 | 156 |
129 | 157 |
130 const char* VmService::error_msg_ = NULL; | 158 const char* VmService::error_msg_ = NULL; |
| 159 char VmService::server_ip_[kServerIpStringBufferSize]; |
| 160 intptr_t VmService::server_port_ = 0; |
131 | 161 |
132 bool VmService::Start(const char *server_ip, intptr_t server_port) { | 162 bool VmService::Setup(const char* server_ip, intptr_t server_port) { |
133 bool r = _Start(server_ip, server_port); | 163 Dart_Isolate isolate = Dart_CurrentIsolate(); |
134 if (!r) { | 164 ASSERT(isolate != NULL); |
135 return r; | 165 SetServerIPAndPort("", 0); |
136 } | |
137 // Start processing messages in a new thread. | |
138 Thread::Start(ThreadMain, static_cast<uword>(NULL)); | |
139 return true; | |
140 } | |
141 | 166 |
| 167 Dart_Handle result; |
142 | 168 |
143 bool VmService::_Start(const char *server_ip, intptr_t server_port) { | 169 Dart_Handle builtin_lib = |
144 ASSERT(Dart_CurrentIsolate() == NULL); | 170 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
145 Dart_Isolate isolate = Dart_GetServiceIsolate(NULL); | 171 SHUTDOWN_ON_ERROR(builtin_lib); |
146 if (isolate == NULL) { | 172 |
147 error_msg_ = "Dart_GetServiceIsolate failed."; | 173 // Prepare for script loading by setting up the 'print' and 'timer' |
148 return false; | 174 // closures and setting up 'package root' for URI resolution. |
149 } | 175 result = DartUtils::PrepareForScriptLoading(NULL, true, builtin_lib); |
150 Dart_EnterIsolate(isolate); | 176 SHUTDOWN_ON_ERROR(result); |
151 Dart_EnterScope(); | 177 |
152 // Install our own library tag handler. | 178 // Load main script. |
153 Dart_SetLibraryTagHandler(LibraryTagHandler); | 179 Dart_SetLibraryTagHandler(LibraryTagHandler); |
154 Dart_Handle result; | 180 Dart_Handle library = LoadScript(kVMServiceIOLibraryScriptResourceName); |
155 Dart_Handle library; | |
156 library = LoadScript(kVMServiceIOLibraryScriptResourceName); | |
157 // Expect a library. | |
158 ASSERT(library != Dart_Null()); | 181 ASSERT(library != Dart_Null()); |
159 SHUTDOWN_ON_ERROR(library); | 182 SHUTDOWN_ON_ERROR(library); |
| 183 result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); |
| 184 SHUTDOWN_ON_ERROR(result); |
160 result = Dart_FinalizeLoading(false); | 185 result = Dart_FinalizeLoading(false); |
161 ASSERT(!Dart_IsError(result)); | 186 SHUTDOWN_ON_ERROR(result); |
| 187 |
| 188 // Make runnable. |
162 Dart_ExitScope(); | 189 Dart_ExitScope(); |
163 Dart_ExitIsolate(); | 190 Dart_ExitIsolate(); |
164 bool retval = Dart_IsolateMakeRunnable(isolate); | 191 bool retval = Dart_IsolateMakeRunnable(isolate); |
165 if (!retval) { | 192 if (!retval) { |
166 Dart_EnterIsolate(isolate); | 193 Dart_EnterIsolate(isolate); |
167 Dart_ShutdownIsolate(); | 194 Dart_ShutdownIsolate(); |
168 error_msg_ = "Invalid isolate state - Unable to make it runnable."; | 195 error_msg_ = "Invalid isolate state - Unable to make it runnable."; |
169 return false; | 196 return false; |
170 } | 197 } |
171 | |
172 Dart_EnterIsolate(isolate); | 198 Dart_EnterIsolate(isolate); |
173 Dart_EnterScope(); | 199 Dart_EnterScope(); |
| 200 |
174 library = Dart_RootLibrary(); | 201 library = Dart_RootLibrary(); |
175 result = Dart_SetNativeResolver(library, VmServiceIONativeResolver, NULL); | 202 SHUTDOWN_ON_ERROR(library); |
176 ASSERT(!Dart_IsError(result)); | 203 |
177 // Set requested TCP port. | 204 // Set HTTP server state. |
178 DartUtils::SetStringField(library, "_ip", server_ip); | 205 DartUtils::SetStringField(library, "_ip", server_ip); |
179 // If we have a port specified, start the server immediately. | 206 // If we have a port specified, start the server immediately. |
180 bool auto_start = server_port >= 0; | 207 bool auto_start = server_port >= 0; |
181 if (server_port < 0) { | 208 if (server_port < 0) { |
182 // Adjust server_port to port 0 which will result in the first available | 209 // Adjust server_port to port 0 which will result in the first available |
183 // port when the HTTP server is started. | 210 // port when the HTTP server is started. |
184 server_port = 0; | 211 server_port = 0; |
185 } | 212 } |
186 // Set initial state. | |
187 DartUtils::SetIntegerField(library, "_port", server_port); | 213 DartUtils::SetIntegerField(library, "_port", server_port); |
188 Dart_SetField(library, | 214 result = Dart_SetField(library, |
189 DartUtils::NewString("_autoStart"), | 215 DartUtils::NewString("_autoStart"), |
190 Dart_NewBoolean(auto_start)); | 216 Dart_NewBoolean(auto_start)); |
191 // We cannot register for signals on windows. | 217 SHUTDOWN_ON_ERROR(result); |
| 218 |
| 219 // Are we running on Windows? |
192 #if defined(TARGET_OS_WINDOWS) | 220 #if defined(TARGET_OS_WINDOWS) |
193 Dart_Handle is_windows = Dart_True(); | 221 Dart_Handle is_windows = Dart_True(); |
194 #else | 222 #else |
195 Dart_Handle is_windows = Dart_False(); | 223 Dart_Handle is_windows = Dart_False(); |
196 #endif | 224 #endif |
197 Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows); | 225 result = |
198 | 226 Dart_SetField(library, DartUtils::NewString("_isWindows"), is_windows); |
| 227 SHUTDOWN_ON_ERROR(result); |
199 | 228 |
200 // Get _getWatchSignalInternal from dart:io. | 229 // Get _getWatchSignalInternal from dart:io. |
201 Dart_Handle dart_io_str = Dart_NewStringFromCString(DartUtils::kIOLibURL); | 230 Dart_Handle dart_io_str = Dart_NewStringFromCString(DartUtils::kIOLibURL); |
| 231 SHUTDOWN_ON_ERROR(dart_io_str); |
202 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); | 232 Dart_Handle io_lib = Dart_LookupLibrary(dart_io_str); |
| 233 SHUTDOWN_ON_ERROR(io_lib); |
203 Dart_Handle function_name = | 234 Dart_Handle function_name = |
204 Dart_NewStringFromCString("_getWatchSignalInternal"); | 235 Dart_NewStringFromCString("_getWatchSignalInternal"); |
| 236 SHUTDOWN_ON_ERROR(function_name); |
205 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); | 237 Dart_Handle signal_watch = Dart_Invoke(io_lib, function_name, 0, NULL); |
206 // Invoke main. | 238 SHUTDOWN_ON_ERROR(signal_watch); |
207 result = Dart_Invoke(library, DartUtils::NewString("main"), 1, &signal_watch); | 239 Dart_Handle field_name = Dart_NewStringFromCString("_signalWatch"); |
208 SHUTDOWN_ON_ERROR(result); | 240 SHUTDOWN_ON_ERROR(field_name); |
209 | 241 result = |
210 Dart_ExitScope(); | 242 Dart_SetField(library, field_name, signal_watch); |
211 Dart_ExitIsolate(); | 243 SHUTDOWN_ON_ERROR(field_name); |
212 | |
213 return true; | 244 return true; |
214 } | 245 } |
215 | 246 |
216 | 247 |
217 const char* VmService::GetErrorMessage() { | 248 const char* VmService::GetErrorMessage() { |
218 return error_msg_ == NULL ? "No error." : error_msg_; | 249 return error_msg_ == NULL ? "No error." : error_msg_; |
219 } | 250 } |
220 | 251 |
221 | 252 |
| 253 void VmService::SetServerIPAndPort(const char* ip, intptr_t port) { |
| 254 if (ip == NULL) { |
| 255 ip = ""; |
| 256 } |
| 257 strncpy(server_ip_, ip, kServerIpStringBufferSize); |
| 258 server_ip_[kServerIpStringBufferSize - 1] = '\0'; |
| 259 server_port_ = port; |
| 260 } |
| 261 |
| 262 |
222 Dart_Handle VmService::GetSource(const char* name) { | 263 Dart_Handle VmService::GetSource(const char* name) { |
223 const intptr_t kBufferSize = 512; | 264 const intptr_t kBufferSize = 512; |
224 char buffer[kBufferSize]; | 265 char buffer[kBufferSize]; |
225 snprintf(&buffer[0], kBufferSize-1, "%s/%s", kLibrarySourceNamePrefix, name); | 266 snprintf(&buffer[0], kBufferSize-1, "%s/%s", kLibrarySourceNamePrefix, name); |
226 const char* vmservice_source = NULL; | 267 const char* vmservice_source = NULL; |
227 int r = Resources::ResourceLookup(buffer, &vmservice_source); | 268 int r = Resources::ResourceLookup(buffer, &vmservice_source); |
228 ASSERT(r != Resources::kNoSuchInstance); | 269 ASSERT(r != Resources::kNoSuchInstance); |
229 return Dart_NewStringFromCString(vmservice_source); | 270 return Dart_NewStringFromCString(vmservice_source); |
230 } | 271 } |
231 | 272 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 return url; | 371 return url; |
331 } | 372 } |
332 Dart_Handle source = GetSource(url_string); | 373 Dart_Handle source = GetSource(url_string); |
333 if (Dart_IsError(source)) { | 374 if (Dart_IsError(source)) { |
334 return source; | 375 return source; |
335 } | 376 } |
336 return Dart_LoadSource(library, url, source, 0, 0); | 377 return Dart_LoadSource(library, url, source, 0, 0); |
337 } | 378 } |
338 | 379 |
339 | 380 |
340 void VmService::ThreadMain(uword parameters) { | |
341 ASSERT(Dart_CurrentIsolate() == NULL); | |
342 Dart_Isolate service_isolate = Dart_GetServiceIsolate(NULL); | |
343 Dart_EnterIsolate(service_isolate); | |
344 Dart_EnterScope(); | |
345 Dart_Handle result = Dart_RunLoop(); | |
346 if (Dart_IsError(result)) { | |
347 printf("Service exited with an error:\n%s\n", Dart_GetError(result)); | |
348 } | |
349 Dart_ExitScope(); | |
350 Dart_ExitIsolate(); | |
351 } | |
352 | |
353 | |
354 | |
355 } // namespace bin | 381 } // namespace bin |
356 } // namespace dart | 382 } // namespace dart |
OLD | NEW |