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