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