Chromium Code Reviews| 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 24 matching lines...) Expand all Loading... | |
| 35 | 35 |
| 36 #define SHUTDOWN_ON_ERROR(handle) \ | 36 #define SHUTDOWN_ON_ERROR(handle) \ |
| 37 if (Dart_IsError(handle)) { \ | 37 if (Dart_IsError(handle)) { \ |
| 38 error_msg_ = strdup(Dart_GetError(handle)); \ | 38 error_msg_ = strdup(Dart_GetError(handle)); \ |
| 39 Dart_ExitScope(); \ | 39 Dart_ExitScope(); \ |
| 40 Dart_ShutdownIsolate(); \ | 40 Dart_ShutdownIsolate(); \ |
| 41 return false; \ | 41 return false; \ |
| 42 } | 42 } |
| 43 | 43 |
| 44 #define kLibraryResourceNamePrefix "/vmservice" | 44 #define kLibraryResourceNamePrefix "/vmservice" |
| 45 static const char* kLibraryScriptResourceName = | 45 static const char* kVMServiceIOLibraryScriptResourceName = |
| 46 kLibraryResourceNamePrefix "/vmservice_io.dart"; | |
| 47 static const char* kVMServiceLibraryName = | |
| 46 kLibraryResourceNamePrefix "/vmservice.dart"; | 48 kLibraryResourceNamePrefix "/vmservice.dart"; |
| 47 static const char* kLibrarySourceResourceNames[] = { | |
| 48 kLibraryResourceNamePrefix "/constants.dart", | |
| 49 kLibraryResourceNamePrefix "/resources.dart", | |
| 50 kLibraryResourceNamePrefix "/running_isolate.dart", | |
| 51 kLibraryResourceNamePrefix "/running_isolates.dart", | |
| 52 kLibraryResourceNamePrefix "/server.dart", | |
| 53 kLibraryResourceNamePrefix "/service_request.dart", | |
| 54 kLibraryResourceNamePrefix "/service_request_router.dart", | |
| 55 kLibraryResourceNamePrefix "/vmservice_io.dart", | |
| 56 NULL | |
| 57 }; | |
| 58 | 49 |
| 59 #define kClientResourceNamePrefix "/client/web/out" | 50 #define kClientResourceNamePrefix "/client/web/out" |
| 60 | 51 |
| 61 Dart_Isolate VmService::isolate_ = NULL; | 52 Dart_Isolate VmService::isolate_ = NULL; |
| 62 Dart_Port VmService::port_ = ILLEGAL_PORT; | 53 Dart_Port VmService::port_ = ILLEGAL_PORT; |
| 63 dart::Monitor* VmService::monitor_ = NULL; | 54 dart::Monitor* VmService::monitor_ = NULL; |
| 64 const char* VmService::error_msg_ = NULL; | 55 const char* VmService::error_msg_ = NULL; |
| 65 | 56 |
| 66 // These must be kept in sync with vmservice/constants.dart | 57 // These must be kept in sync with vmservice/constants.dart |
| 67 #define VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID 1 | 58 #define VM_SERVICE_ISOLATE_STARTUP_MESSAGE_ID 1 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 | 94 |
| 104 Dart_EnterScope(); | 95 Dart_EnterScope(); |
| 105 | 96 |
| 106 if (snapshot_buffer != NULL) { | 97 if (snapshot_buffer != NULL) { |
| 107 // Setup the native resolver as the snapshot does not carry it. | 98 // Setup the native resolver as the snapshot does not carry it. |
| 108 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | 99 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 109 Builtin::SetNativeResolver(Builtin::kIOLibrary); | 100 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
| 110 } | 101 } |
| 111 | 102 |
| 112 // Set up the library tag handler for this isolate. | 103 // Set up the library tag handler for this isolate. |
| 113 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 104 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler); |
| 114 SHUTDOWN_ON_ERROR(result); | 105 SHUTDOWN_ON_ERROR(result); |
| 115 | 106 |
| 116 // Load the specified application script into the newly created isolate. | 107 // Load the specified application script into the newly created isolate. |
| 117 | 108 |
| 118 // Prepare builtin and its dependent libraries for use to resolve URIs. | 109 // Prepare builtin and its dependent libraries for use to resolve URIs. |
| 119 // The builtin library is part of the core snapshot and would already be | 110 // The builtin library is part of the core snapshot and would already be |
| 120 // available here in the case of script snapshot loading. | 111 // available here in the case of script snapshot loading. |
| 121 Dart_Handle builtin_lib = | 112 Dart_Handle builtin_lib = |
| 122 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 113 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 123 SHUTDOWN_ON_ERROR(builtin_lib); | 114 SHUTDOWN_ON_ERROR(builtin_lib); |
| 124 | 115 |
| 125 // Prepare for script loading by setting up the 'print' and 'timer' | 116 // Prepare for script loading by setting up the 'print' and 'timer' |
| 126 // closures and setting up 'package root' for URI resolution. | 117 // closures and setting up 'package root' for URI resolution. |
| 127 result = DartUtils::PrepareForScriptLoading("", builtin_lib); | 118 result = DartUtils::PrepareForScriptLoading("", builtin_lib); |
| 128 SHUTDOWN_ON_ERROR(result); | 119 SHUTDOWN_ON_ERROR(result); |
| 129 | 120 |
| 130 { | 121 { |
| 131 // Load source into service isolate. | 122 // Load source into service isolate. |
| 132 Dart_Handle library = LoadScript(kLibraryScriptResourceName); | 123 Dart_Handle library = LoadScript(kVMServiceIOLibraryScriptResourceName); |
| 133 SHUTDOWN_ON_ERROR(library); | 124 SHUTDOWN_ON_ERROR(library); |
| 134 result = LoadSources(library, kLibrarySourceResourceNames); | |
| 135 SHUTDOWN_ON_ERROR(result); | |
| 136 } | 125 } |
| 137 | 126 |
| 138 // Make the isolate runnable so that it is ready to handle messages. | 127 // Make the isolate runnable so that it is ready to handle messages. |
| 139 Dart_ExitScope(); | 128 Dart_ExitScope(); |
| 140 Dart_ExitIsolate(); | 129 Dart_ExitIsolate(); |
| 141 | 130 |
| 142 bool retval = Dart_IsolateMakeRunnable(isolate_); | 131 bool retval = Dart_IsolateMakeRunnable(isolate_); |
| 143 if (!retval) { | 132 if (!retval) { |
| 144 Dart_EnterIsolate(isolate_); | 133 Dart_EnterIsolate(isolate_); |
| 145 Dart_ShutdownIsolate(); | 134 Dart_ShutdownIsolate(); |
| 146 error_msg_ = "Invalid isolate state - Unable to make it runnable."; | 135 error_msg_ = "Invalid isolate state - Unable to make it runnable."; |
| 147 return false; | 136 return false; |
| 148 } | 137 } |
| 149 | 138 |
| 150 Dart_EnterIsolate(isolate_); | 139 Dart_EnterIsolate(isolate_); |
| 151 Dart_EnterScope(); | 140 Dart_EnterScope(); |
| 152 | 141 |
| 153 | 142 |
| 154 Dart_Handle library = Dart_RootLibrary(); | 143 Dart_Handle library = Dart_RootLibrary(); |
| 155 // Set requested port. | 144 // Set requested port. |
| 156 DartUtils::SetIntegerField(library, "_port", server_port); | 145 DartUtils::SetIntegerField(library, "_port", server_port); |
| 157 // Install native resolver. | |
| 158 result = Dart_SetNativeResolver(library, VmServiceNativeResolver); | |
| 159 SHUTDOWN_ON_ERROR(result); | |
| 160 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); | 146 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); |
| 161 SHUTDOWN_ON_ERROR(result); | 147 SHUTDOWN_ON_ERROR(result); |
| 162 | 148 |
| 163 result = LoadResources(library); | 149 result = LoadResources(library); |
| 164 SHUTDOWN_ON_ERROR(result); | 150 SHUTDOWN_ON_ERROR(result); |
| 165 result = Dart_CompileAll(); | 151 result = Dart_CompileAll(); |
| 166 SHUTDOWN_ON_ERROR(result); | 152 SHUTDOWN_ON_ERROR(result); |
| 167 | 153 |
| 168 port_ = Dart_GetMainPortId(); | 154 port_ = Dart_GetMainPortId(); |
| 169 | 155 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 187 Dart_Port VmService::port() { | 173 Dart_Port VmService::port() { |
| 188 return port_; | 174 return port_; |
| 189 } | 175 } |
| 190 | 176 |
| 191 | 177 |
| 192 bool VmService::IsRunning() { | 178 bool VmService::IsRunning() { |
| 193 return port_ != ILLEGAL_PORT; | 179 return port_ != ILLEGAL_PORT; |
| 194 } | 180 } |
| 195 | 181 |
| 196 | 182 |
| 183 Dart_Handle VmService::GetSource(const char* name) { | |
| 184 const char* vmservice_source = NULL; | |
| 185 int r = Resources::ResourceLookup(name, &vmservice_source); | |
| 186 ASSERT(r != Resources::kNoSuchInstance); | |
| 187 return Dart_NewStringFromCString(vmservice_source); | |
| 188 } | |
| 189 | |
| 190 | |
| 197 Dart_Handle VmService::LoadScript(const char* name) { | 191 Dart_Handle VmService::LoadScript(const char* name) { |
| 198 Dart_Handle url = Dart_NewStringFromCString(name); | 192 Dart_Handle url = Dart_NewStringFromCString(name); |
| 199 const char* vmservice_source = NULL; | 193 Dart_Handle source = GetSource(name); |
| 200 int r = Resources::ResourceLookup(name, &vmservice_source); | |
| 201 ASSERT(r != Resources::kNoSuchInstance); | |
| 202 Dart_Handle source = Dart_NewStringFromCString(vmservice_source); | |
| 203 return Dart_LoadScript(url, source, 0, 0); | 194 return Dart_LoadScript(url, source, 0, 0); |
| 204 } | 195 } |
| 205 | 196 |
| 206 | 197 |
| 207 Dart_Handle VmService::LoadSource(Dart_Handle library, const char* name) { | 198 Dart_Handle VmService::LoadSource(Dart_Handle library, const char* name) { |
| 208 Dart_Handle url = Dart_NewStringFromCString(name); | 199 Dart_Handle url = Dart_NewStringFromCString(name); |
| 209 const char* vmservice_source = NULL; | 200 Dart_Handle source = GetSource(name); |
| 210 int r = Resources::ResourceLookup(name, &vmservice_source); | |
| 211 if (r == Resources::kNoSuchInstance) { | |
| 212 printf("Can't find %s\n", name); | |
| 213 } | |
| 214 ASSERT(r != Resources::kNoSuchInstance); | |
| 215 Dart_Handle source = Dart_NewStringFromCString(vmservice_source); | |
| 216 return Dart_LoadSource(library, url, source); | 201 return Dart_LoadSource(library, url, source); |
| 217 } | 202 } |
| 218 | 203 |
| 219 | 204 |
| 220 Dart_Handle VmService::LoadSources(Dart_Handle library, const char* names[]) { | 205 Dart_Handle VmService::LoadSources(Dart_Handle library, const char* names[]) { |
| 221 Dart_Handle result = Dart_Null(); | 206 Dart_Handle result = Dart_Null(); |
| 222 for (int i = 0; names[i] != NULL; i++) { | 207 for (int i = 0; names[i] != NULL; i++) { |
| 223 result = LoadSource(library, names[i]); | 208 result = LoadSource(library, names[i]); |
| 224 if (Dart_IsError(result)) { | 209 if (Dart_IsError(result)) { |
| 225 break; | 210 break; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 result = LoadResource(library, path, kClientResourceNamePrefix); | 263 result = LoadResource(library, path, kClientResourceNamePrefix); |
| 279 if (Dart_IsError(result)) { | 264 if (Dart_IsError(result)) { |
| 280 break; | 265 break; |
| 281 } | 266 } |
| 282 } | 267 } |
| 283 } | 268 } |
| 284 return result; | 269 return result; |
| 285 } | 270 } |
| 286 | 271 |
| 287 | 272 |
| 273 Dart_Handle VmService::LibraryTagHandler(Dart_LibraryTag tag, | |
| 274 Dart_Handle library, | |
| 275 Dart_Handle url) { | |
| 276 if (!Dart_IsLibrary(library)) { | |
| 277 return Dart_Error("not a library"); | |
| 278 } | |
| 279 if (!Dart_IsString(url)) { | |
| 280 return Dart_Error("url is not a string"); | |
| 281 } | |
| 282 const char* url_string = NULL; | |
| 283 Dart_Handle result = Dart_StringToCString(url, &url_string); | |
| 284 if (Dart_IsError(result)) { | |
| 285 return result; | |
| 286 } | |
| 287 Dart_Handle library_url = Dart_LibraryUrl(library); | |
| 288 const char* library_url_string = NULL; | |
| 289 result = Dart_StringToCString(library_url, &library_url_string); | |
| 290 if (Dart_IsError(result)) { | |
| 291 return result; | |
| 292 } | |
| 293 bool is_vm_service_url = | |
| 294 (strncmp(kLibraryResourceNamePrefix, url_string, | |
| 295 strlen(kLibraryResourceNamePrefix)) == 0); | |
| 296 bool is_vm_service_library = | |
| 297 (strcmp(kVMServiceLibraryName, url_string) == 0); | |
|
siva
2013/08/02 21:32:44
This seems to be computed too eagerly. Is is neede
Cutch
2013/08/02 21:39:13
Done.
| |
| 298 if (!is_vm_service_url) { | |
| 299 // Pass to DartUtils. | |
| 300 return DartUtils::LibraryTagHandler(tag, library, url); | |
| 301 } | |
| 302 switch (tag) { | |
| 303 case Dart_kCanonicalizeUrl: | |
| 304 // The URL is already canonicalized. | |
| 305 return url; | |
| 306 break; | |
| 307 case Dart_kImportTag: { | |
| 308 Dart_Handle source = GetSource(url_string); | |
| 309 if (Dart_IsError(source)) { | |
| 310 return source; | |
| 311 } | |
| 312 Dart_Handle lib = Dart_LoadLibrary(url, source); | |
| 313 if (Dart_IsError(lib)) { | |
| 314 return lib; | |
| 315 } | |
| 316 if (is_vm_service_library) { | |
| 317 // Install native resolver for this library. | |
| 318 result = Dart_SetNativeResolver(lib, VmServiceNativeResolver); | |
| 319 if (Dart_IsError(result)) { | |
| 320 return result; | |
| 321 } | |
| 322 } | |
| 323 return lib; | |
| 324 } | |
| 325 break; | |
| 326 case Dart_kSourceTag: { | |
| 327 Dart_Handle source = GetSource(url_string); | |
| 328 if (Dart_IsError(source)) { | |
| 329 return source; | |
| 330 } | |
| 331 return Dart_LoadSource(library, url, source); | |
| 332 } | |
| 333 break; | |
| 334 default: | |
| 335 UNIMPLEMENTED(); | |
| 336 break; | |
| 337 } | |
| 338 UNREACHABLE(); | |
| 339 return result; | |
| 340 } | |
| 341 | |
| 342 | |
| 288 void VmService::ThreadMain(uword parameters) { | 343 void VmService::ThreadMain(uword parameters) { |
| 289 ASSERT(Dart_CurrentIsolate() == NULL); | 344 ASSERT(Dart_CurrentIsolate() == NULL); |
| 290 ASSERT(isolate_ == NULL); | 345 ASSERT(isolate_ == NULL); |
| 291 | 346 |
| 292 intptr_t server_port = static_cast<intptr_t>(parameters); | 347 intptr_t server_port = static_cast<intptr_t>(parameters); |
| 293 ASSERT(server_port >= 0); | 348 ASSERT(server_port >= 0); |
| 294 | 349 |
| 295 // Lock scope. | 350 // Lock scope. |
| 296 { | 351 { |
| 297 MonitorLocker ml(monitor_); | 352 MonitorLocker ml(monitor_); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 if (!strcmp(function_name, entry.name) && | 499 if (!strcmp(function_name, entry.name) && |
| 445 (num_arguments == entry.num_arguments)) { | 500 (num_arguments == entry.num_arguments)) { |
| 446 return entry.function; | 501 return entry.function; |
| 447 } | 502 } |
| 448 } | 503 } |
| 449 return NULL; | 504 return NULL; |
| 450 } | 505 } |
| 451 | 506 |
| 452 } // namespace bin | 507 } // namespace bin |
| 453 } // namespace dart | 508 } // namespace dart |
| OLD | NEW |