Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: runtime/bin/vmservice_impl.cc

Issue 21880004: Cleanup vm_service library loading. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/vmservice_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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
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 static bool IsVMServiceURL(const char* url) {
274 static const intptr_t kLibraryResourceNamePrefixLen =
275 strlen(kLibraryResourceNamePrefix);
276 return 0 == strncmp(kLibraryResourceNamePrefix, url,
277 kLibraryResourceNamePrefixLen);
278 }
279
280
281 static bool IsVMServiceLibrary(const char* url) {
282 return 0 == strcmp(kVMServiceLibraryName, url);
283 }
284
285
286 Dart_Handle VmService::LibraryTagHandler(Dart_LibraryTag tag,
287 Dart_Handle library,
288 Dart_Handle url) {
289 if (!Dart_IsLibrary(library)) {
290 return Dart_Error("not a library");
291 }
292 if (!Dart_IsString(url)) {
293 return Dart_Error("url is not a string");
294 }
295 const char* url_string = NULL;
296 Dart_Handle result = Dart_StringToCString(url, &url_string);
297 if (Dart_IsError(result)) {
298 return result;
299 }
300 Dart_Handle library_url = Dart_LibraryUrl(library);
301 const char* library_url_string = NULL;
302 result = Dart_StringToCString(library_url, &library_url_string);
303 if (Dart_IsError(result)) {
304 return result;
305 }
306 bool is_vm_service_url = IsVMServiceURL(url_string);
307 if (!is_vm_service_url) {
308 // Pass to DartUtils.
309 return DartUtils::LibraryTagHandler(tag, library, url);
310 }
311 switch (tag) {
312 case Dart_kCanonicalizeUrl:
313 // The URL is already canonicalized.
314 return url;
315 break;
316 case Dart_kImportTag: {
317 Dart_Handle source = GetSource(url_string);
318 if (Dart_IsError(source)) {
319 return source;
320 }
321 Dart_Handle lib = Dart_LoadLibrary(url, source);
322 if (Dart_IsError(lib)) {
323 return lib;
324 }
325 if (IsVMServiceLibrary(url_string)) {
326 // Install native resolver for this library.
327 result = Dart_SetNativeResolver(lib, VmServiceNativeResolver);
328 if (Dart_IsError(result)) {
329 return result;
330 }
331 }
332 return lib;
333 }
334 break;
335 case Dart_kSourceTag: {
336 Dart_Handle source = GetSource(url_string);
337 if (Dart_IsError(source)) {
338 return source;
339 }
340 return Dart_LoadSource(library, url, source);
341 }
342 break;
343 default:
344 UNIMPLEMENTED();
345 break;
346 }
347 UNREACHABLE();
348 return result;
349 }
350
351
288 void VmService::ThreadMain(uword parameters) { 352 void VmService::ThreadMain(uword parameters) {
289 ASSERT(Dart_CurrentIsolate() == NULL); 353 ASSERT(Dart_CurrentIsolate() == NULL);
290 ASSERT(isolate_ == NULL); 354 ASSERT(isolate_ == NULL);
291 355
292 intptr_t server_port = static_cast<intptr_t>(parameters); 356 intptr_t server_port = static_cast<intptr_t>(parameters);
293 ASSERT(server_port >= 0); 357 ASSERT(server_port >= 0);
294 358
295 // Lock scope. 359 // Lock scope.
296 { 360 {
297 MonitorLocker ml(monitor_); 361 MonitorLocker ml(monitor_);
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 if (!strcmp(function_name, entry.name) && 508 if (!strcmp(function_name, entry.name) &&
445 (num_arguments == entry.num_arguments)) { 509 (num_arguments == entry.num_arguments)) {
446 return entry.function; 510 return entry.function;
447 } 511 }
448 } 512 }
449 return NULL; 513 return NULL;
450 } 514 }
451 515
452 } // namespace bin 516 } // namespace bin
453 } // namespace dart 517 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/vmservice_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698