| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
| 10 #include "include/dart_debugger_api.h" | 10 #include "include/dart_debugger_api.h" |
| 11 | 11 |
| 12 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
| 13 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
| 14 #include "bin/directory.h" |
| 14 #include "bin/eventhandler.h" | 15 #include "bin/eventhandler.h" |
| 15 #include "bin/extensions.h" | 16 #include "bin/extensions.h" |
| 16 #include "bin/file.h" | 17 #include "bin/file.h" |
| 17 #include "bin/platform.h" | 18 #include "bin/platform.h" |
| 18 #include "bin/process.h" | 19 #include "bin/process.h" |
| 19 #include "platform/globals.h" | 20 #include "platform/globals.h" |
| 20 | 21 |
| 21 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 22 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 22 // it is initialized to NULL. | 23 // it is initialized to NULL. |
| 23 extern const uint8_t* snapshot_buffer; | 24 extern const uint8_t* snapshot_buffer; |
| 24 | 25 |
| 25 | 26 |
| 26 // Global state that stores a pointer to the application script file. | 27 // Global state that stores a pointer to the application script file. |
| 27 static char* canonical_script_name = NULL; | 28 static const char* original_script_name = NULL; |
| 29 static const char* original_working_directory = NULL; |
| 30 static const char* original_script_url = NULL; |
| 28 | 31 |
| 29 | 32 |
| 30 // Global state that stores the import URL map specified on the | 33 // Global state that stores the import URL map specified on the |
| 31 // command line. | 34 // command line. |
| 32 static CommandLineOptions* import_map_options = NULL; | 35 static CommandLineOptions* import_map_options = NULL; |
| 33 | 36 |
| 34 | 37 |
| 35 // Global state that stores a pointer to the application script snapshot. | |
| 36 static bool use_script_snapshot = false; | |
| 37 static uint8_t* script_snapshot_buffer = NULL; | |
| 38 | |
| 39 | |
| 40 // Global state that indicates whether pprof symbol information is | 38 // Global state that indicates whether pprof symbol information is |
| 41 // to be generated or not. | 39 // to be generated or not. |
| 42 static const char* generate_pprof_symbols_filename = NULL; | 40 static const char* generate_pprof_symbols_filename = NULL; |
| 43 | 41 |
| 44 | 42 |
| 45 // Global state that indicates whether there is a debug breakpoint. | 43 // Global state that indicates whether there is a debug breakpoint. |
| 46 // This pointer points into an argv buffer and does not need to be | 44 // This pointer points into an argv buffer and does not need to be |
| 47 // free'd. | 45 // free'd. |
| 48 static const char* breakpoint_at = NULL; | 46 static const char* breakpoint_at = NULL; |
| 49 | 47 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 73 has_compile_all = true; | 71 has_compile_all = true; |
| 74 } | 72 } |
| 75 | 73 |
| 76 | 74 |
| 77 static void ProcessPprofOption(const char* filename) { | 75 static void ProcessPprofOption(const char* filename) { |
| 78 ASSERT(filename != NULL); | 76 ASSERT(filename != NULL); |
| 79 generate_pprof_symbols_filename = filename; | 77 generate_pprof_symbols_filename = filename; |
| 80 } | 78 } |
| 81 | 79 |
| 82 | 80 |
| 83 static void ProcessSnapshotOption(const char* snapshot) { | |
| 84 ASSERT(snapshot != NULL); | |
| 85 use_script_snapshot = true; | |
| 86 } | |
| 87 | |
| 88 | |
| 89 static void ProcessImportMapOption(const char* map) { | 81 static void ProcessImportMapOption(const char* map) { |
| 90 ASSERT(map != NULL); | 82 ASSERT(map != NULL); |
| 91 import_map_options->AddArgument(map); | 83 import_map_options->AddArgument(map); |
| 92 } | 84 } |
| 93 | 85 |
| 94 | 86 |
| 95 static struct { | 87 static struct { |
| 96 const char* option_name; | 88 const char* option_name; |
| 97 void (*process)(const char* option); | 89 void (*process)(const char* option); |
| 98 } main_options[] = { | 90 } main_options[] = { |
| 99 { "--break_at=", ProcessBreakpointOption }, | 91 { "--break_at=", ProcessBreakpointOption }, |
| 100 { "--compile_all", ProcessCompileAllOption }, | 92 { "--compile_all", ProcessCompileAllOption }, |
| 101 { "--generate_pprof_symbols=", ProcessPprofOption }, | 93 { "--generate_pprof_symbols=", ProcessPprofOption }, |
| 102 { "--use_script_snapshot", ProcessSnapshotOption }, | |
| 103 { "--import_map=", ProcessImportMapOption }, | 94 { "--import_map=", ProcessImportMapOption }, |
| 104 { NULL, NULL } | 95 { NULL, NULL } |
| 105 }; | 96 }; |
| 106 | 97 |
| 107 | 98 |
| 108 static bool ProcessMainOptions(const char* option) { | 99 static bool ProcessMainOptions(const char* option) { |
| 109 int i = 0; | 100 int i = 0; |
| 110 const char* name = main_options[0].option_name; | 101 const char* name = main_options[0].option_name; |
| 111 while (name != NULL) { | 102 while (name != NULL) { |
| 112 int length = strlen(name); | 103 int length = strlen(name); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 while (i < argc) { | 150 while (i < argc) { |
| 160 dart_options->AddArgument(argv[i]); | 151 dart_options->AddArgument(argv[i]); |
| 161 i += 1; | 152 i += 1; |
| 162 } | 153 } |
| 163 | 154 |
| 164 return 0; | 155 return 0; |
| 165 } | 156 } |
| 166 | 157 |
| 167 | 158 |
| 168 static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options, | 159 static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options, |
| 169 char* script_name) { | 160 const char* script_name) { |
| 170 int options_count = options->count(); | 161 int options_count = options->count(); |
| 171 Dart_Handle dart_script = Dart_NewString(script_name); | 162 Dart_Handle dart_script = Dart_NewString(script_name); |
| 172 if (Dart_IsError(dart_script)) { | 163 if (Dart_IsError(dart_script)) { |
| 173 return dart_script; | 164 return dart_script; |
| 174 } | 165 } |
| 175 Dart_Handle dart_arguments = Dart_NewList(options_count); | 166 Dart_Handle dart_arguments = Dart_NewList(options_count); |
| 176 if (Dart_IsError(dart_arguments)) { | 167 if (Dart_IsError(dart_arguments)) { |
| 177 return dart_arguments; | 168 return dart_arguments; |
| 178 } | 169 } |
| 179 for (int i = 0; i < options_count; i++) { | 170 for (int i = 0; i < options_count; i++) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 return Dart_Error("accessing url characters failed"); | 249 return Dart_Error("accessing url characters failed"); |
| 259 } | 250 } |
| 260 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); | 251 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); |
| 261 bool is_dart_extension_url = DartUtils::IsDartExtensionSchemeURL(url_string); | 252 bool is_dart_extension_url = DartUtils::IsDartExtensionSchemeURL(url_string); |
| 262 if (tag == kCanonicalizeUrl) { | 253 if (tag == kCanonicalizeUrl) { |
| 263 // If this is a Dart Scheme URL then it is not modified as it will be | 254 // If this is a Dart Scheme URL then it is not modified as it will be |
| 264 // handled by the VM internally. | 255 // handled by the VM internally. |
| 265 if (is_dart_scheme_url || is_dart_extension_url) { | 256 if (is_dart_scheme_url || is_dart_extension_url) { |
| 266 return url; | 257 return url; |
| 267 } | 258 } |
| 268 // Create a canonical path based on the including library and current url. | 259 // Resolve the url within the context of the library's URL. |
| 269 return DartUtils::CanonicalizeURL(NULL, library, url_string); | 260 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
| 261 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 262 if (Dart_IsError(library_url)) { |
| 263 return Dart_Error("accessing library url failed"); |
| 264 } |
| 265 Dart_Handle dart_args[2]; |
| 266 dart_args[0] = library_url; |
| 267 dart_args[1] = url; |
| 268 return Dart_InvokeStatic(builtin_lib, |
| 269 Dart_NewString(""), Dart_NewString("resolveUri"), |
| 270 2, dart_args); |
| 270 } | 271 } |
| 271 if (is_dart_scheme_url) { | 272 if (is_dart_scheme_url) { |
| 272 ASSERT(tag == kImportTag); | 273 ASSERT(tag == kImportTag); |
| 273 // Handle imports of other built-in libraries present in the SDK. | 274 // Handle imports of other built-in libraries present in the SDK. |
| 274 if (DartUtils::IsDartIOLibURL(url_string)) { | 275 if (DartUtils::IsDartIOLibURL(url_string)) { |
| 275 return Builtin::LoadLibrary(Builtin::kIOLibrary); | 276 return Builtin::LoadLibrary(Builtin::kIOLibrary); |
| 276 } else if (DartUtils::IsDartJsonLibURL(url_string)) { | 277 } else if (DartUtils::IsDartJsonLibURL(url_string)) { |
| 277 return Builtin::LoadLibrary(Builtin::kJsonLibrary); | 278 return Builtin::LoadLibrary(Builtin::kJsonLibrary); |
| 278 } else if (DartUtils::IsDartUriLibURL(url_string)) { | 279 } else if (DartUtils::IsDartUriLibURL(url_string)) { |
| 279 return Builtin::LoadLibrary(Builtin::kUriLibrary); | 280 return Builtin::LoadLibrary(Builtin::kUriLibrary); |
| 280 } else if (DartUtils::IsDartUtf8LibURL(url_string)) { | 281 } else if (DartUtils::IsDartUtf8LibURL(url_string)) { |
| 281 return Builtin::LoadLibrary(Builtin::kUtf8Library); | 282 return Builtin::LoadLibrary(Builtin::kUtf8Library); |
| 282 } else { | 283 } else { |
| 283 return Dart_Error("Do not know how to load '%s'", url_string); | 284 return Dart_Error("Do not know how to load '%s'", url_string); |
| 284 } | 285 } |
| 285 } else if (is_dart_extension_url) { | 286 } else if (is_dart_extension_url) { |
| 286 if (tag != kImportTag) { | 287 if (tag != kImportTag) { |
| 287 return Dart_Error("Dart extensions must use import: '%s'", url_string); | 288 return Dart_Error("Dart extensions must use import: '%s'", url_string); |
| 288 } | 289 } |
| 289 return Extensions::LoadExtension(url_string, library); | 290 return Extensions::LoadExtension(url_string, library); |
| 291 } else { |
| 292 // Get the file path out of the url. |
| 293 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
| 294 Dart_Handle dart_args[1]; |
| 295 dart_args[0] = url; |
| 296 Dart_Handle file_path = Dart_InvokeStatic(builtin_lib, |
| 297 Dart_NewString(""), |
| 298 Dart_NewString("filePathFromUri"), |
| 299 1, dart_args); |
| 300 if (Dart_IsError(file_path)) { |
| 301 return file_path; |
| 302 } |
| 303 Dart_StringToCString(file_path, &url_string); |
| 290 } | 304 } |
| 291 result = DartUtils::LoadSource(NULL, | 305 result = DartUtils::LoadSource(NULL, |
| 292 library, | 306 library, |
| 293 url, | 307 url, |
| 294 tag, | 308 tag, |
| 295 url_string, | 309 url_string, |
| 296 import_map); | 310 import_map); |
| 297 if (!Dart_IsError(result) && (tag == kImportTag)) { | 311 if (!Dart_IsError(result) && (tag == kImportTag)) { |
| 298 Builtin::ImportLibrary(result, Builtin::kBuiltinLibrary); | 312 Builtin::ImportLibrary(result, Builtin::kBuiltinLibrary); |
| 299 } | 313 } |
| 300 return result; | 314 return result; |
| 301 } | 315 } |
| 302 | 316 |
| 303 | 317 |
| 304 static Dart_Handle LoadScript(const char* script_name, | 318 static Dart_Handle LoadScript(Dart_Handle builtin_lib, |
| 305 CommandLineOptions* map) { | 319 CommandLineOptions* map) { |
| 306 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); | 320 Dart_Handle dart_args[2]; |
| 321 dart_args[0] = Dart_NewString(original_working_directory); |
| 322 dart_args[1] = Dart_NewString(original_script_name); |
| 323 Dart_Handle script_url = Dart_InvokeStatic(builtin_lib, |
| 324 Dart_NewString(""), |
| 325 Dart_NewString("resolveScriptUri"), |
| 326 2, dart_args); |
| 327 if (Dart_IsError(script_url)) { |
| 328 fprintf(stderr, "%s", Dart_GetError(script_url)); |
| 329 return false; |
| 330 } |
| 331 if (original_script_url == NULL) { |
| 332 const char* script_url_cstr; |
| 333 Dart_StringToCString(script_url, &script_url_cstr); |
| 334 original_script_url = strdup(script_url_cstr); |
| 335 } |
| 336 dart_args[0] = script_url; |
| 337 Dart_Handle script_path = Dart_InvokeStatic(builtin_lib, |
| 338 Dart_NewString(""), |
| 339 Dart_NewString("filePathFromUri"), |
| 340 1, dart_args); |
| 341 if (Dart_IsError(script_path)) { |
| 342 return script_path; |
| 343 } |
| 344 const char* script_path_cstr; |
| 345 Dart_StringToCString(script_path, &script_path_cstr); |
| 346 Dart_Handle source = DartUtils::ReadStringFromFile(script_path_cstr); |
| 307 if (Dart_IsError(source)) { | 347 if (Dart_IsError(source)) { |
| 308 return source; | 348 return source; |
| 309 } | 349 } |
| 310 Dart_Handle url = Dart_NewString(script_name); | |
| 311 intptr_t length = (map == NULL) ? 0 : map->count(); | 350 intptr_t length = (map == NULL) ? 0 : map->count(); |
| 312 Dart_Handle import_map = Dart_NewList(length * 2); | 351 Dart_Handle import_map = Dart_NewList(length * 2); |
| 313 for (intptr_t i = 0; i < length; i++) { | 352 for (intptr_t i = 0; i < length; i++) { |
| 314 ASSERT(map->GetArgument(i) != NULL); | 353 ASSERT(map->GetArgument(i) != NULL); |
| 315 char* name = strdup(map->GetArgument(i)); | 354 char* name = strdup(map->GetArgument(i)); |
| 316 ASSERT(name != NULL); | 355 ASSERT(name != NULL); |
| 317 char* map_name = strchr(name, ','); | 356 char* map_name = strchr(name, ','); |
| 318 intptr_t index = i * 2; | 357 intptr_t index = i * 2; |
| 319 if (map_name != NULL) { | 358 if (map_name != NULL) { |
| 320 *map_name = '\0'; | 359 *map_name = '\0'; |
| 321 map_name += 1; | 360 map_name += 1; |
| 322 Dart_ListSetAt(import_map, index, Dart_NewString(name)); | 361 Dart_ListSetAt(import_map, index, Dart_NewString(name)); |
| 323 Dart_ListSetAt(import_map, (index + 1), Dart_NewString(map_name)); | 362 Dart_ListSetAt(import_map, (index + 1), Dart_NewString(map_name)); |
| 324 } else { | 363 } else { |
| 325 Dart_ListSetAt(import_map, index, Dart_NewString(name)); | 364 Dart_ListSetAt(import_map, index, Dart_NewString(name)); |
| 326 Dart_ListSetAt(import_map, (index + 1), Dart_NewString("")); | 365 Dart_ListSetAt(import_map, (index + 1), Dart_NewString("")); |
| 327 } | 366 } |
| 328 free(name); | 367 free(name); |
| 329 } | 368 } |
| 330 return Dart_LoadScript(url, source, LibraryTagHandler, import_map); | 369 return Dart_LoadScript(script_url, source, LibraryTagHandler, import_map); |
| 331 } | 370 } |
| 332 | 371 |
| 333 | 372 |
| 334 static bool CreateIsolateAndSetup(const char* name_prefix, | 373 static bool CreateIsolateAndSetup(const char* name_prefix, |
| 335 void* data, char** error) { | 374 void* data, char** error) { |
| 336 Dart_Isolate isolate = | 375 Dart_Isolate isolate = |
| 337 Dart_CreateIsolate(name_prefix, snapshot_buffer, data, error); | 376 Dart_CreateIsolate(name_prefix, snapshot_buffer, data, error); |
| 338 if (isolate == NULL) { | 377 if (isolate == NULL) { |
| 339 return false; | 378 return false; |
| 340 } | 379 } |
| 341 | 380 |
| 342 Dart_Handle library; | |
| 343 Dart_EnterScope(); | 381 Dart_EnterScope(); |
| 344 | 382 |
| 383 if (snapshot_buffer != NULL) { |
| 384 // Setup the native resolver as the snapshot does not carry it. |
| 385 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 386 Builtin::SetNativeResolver(Builtin::kIOLibrary); |
| 387 } |
| 388 |
| 389 // Prepare builtin for use to resolve URIs. |
| 390 Dart_Handle uri_lib = Builtin::LoadLibrary(Builtin::kUriLibrary); |
| 391 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
| 392 Builtin::ImportLibrary(builtin_lib, Builtin::kUriLibrary); |
| 393 |
| 345 // Load the specified application script into the newly created isolate. | 394 // Load the specified application script into the newly created isolate. |
| 346 if (script_snapshot_buffer != NULL) { | 395 Dart_Handle library = LoadScript(builtin_lib, import_map_options); |
| 347 library = Dart_LoadScriptFromSnapshot(script_snapshot_buffer); | |
| 348 } else { | |
| 349 library = LoadScript(canonical_script_name, import_map_options); | |
| 350 } | |
| 351 if (Dart_IsError(library)) { | 396 if (Dart_IsError(library)) { |
| 352 *error = strdup(Dart_GetError(library)); | 397 *error = strdup(Dart_GetError(library)); |
| 353 Dart_ExitScope(); | 398 Dart_ExitScope(); |
| 354 Dart_ShutdownIsolate(); | 399 Dart_ShutdownIsolate(); |
| 355 return false; | 400 return false; |
| 356 } | 401 } |
| 357 if (!Dart_IsLibrary(library)) { | 402 if (!Dart_IsLibrary(library)) { |
| 358 char errbuf[256]; | 403 char errbuf[256]; |
| 359 snprintf(errbuf, sizeof(errbuf), | 404 snprintf(errbuf, sizeof(errbuf), |
| 360 "Expected a library when loading script: %s", | 405 "Expected a library when loading script: %s", |
| 361 canonical_script_name); | 406 original_script_name); |
| 362 *error = strdup(errbuf); | 407 *error = strdup(errbuf); |
| 363 Dart_ExitScope(); | 408 Dart_ExitScope(); |
| 364 Dart_ShutdownIsolate(); | 409 Dart_ShutdownIsolate(); |
| 365 return false; | 410 return false; |
| 366 } | 411 } |
| 367 if (script_snapshot_buffer == NULL) { | 412 // Implicitly import builtin into app. |
| 368 // Implicitly import builtin into app. | 413 Builtin::ImportLibrary(library, Builtin::kBuiltinLibrary); |
| 369 Builtin::ImportLibrary(library, Builtin::kBuiltinLibrary); | |
| 370 } | |
| 371 if (snapshot_buffer != NULL) { | |
| 372 // Setup the native resolver as the snapshot does not carry it. | |
| 373 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | |
| 374 Builtin::SetNativeResolver(Builtin::kIOLibrary); | |
| 375 } | |
| 376 Dart_ExitScope(); | 414 Dart_ExitScope(); |
| 377 return true; | 415 return true; |
| 378 } | 416 } |
| 379 | 417 |
| 380 | 418 |
| 381 static bool CaptureScriptSnapshot() { | |
| 382 char* error = NULL; | |
| 383 Dart_Handle result; | |
| 384 | |
| 385 // First create an isolate and load up the specified script in it. | |
| 386 if (!CreateIsolateAndSetup(NULL, NULL, &error)) { | |
| 387 fprintf(stderr, "%s\n", error); | |
| 388 free(canonical_script_name); | |
| 389 free(error); | |
| 390 return false; // Indicates we encountered an error. | |
| 391 } | |
| 392 | |
| 393 Dart_EnterScope(); | |
| 394 | |
| 395 #if 0 | |
| 396 // Lookup the library of the main script. | |
| 397 Dart_Handle script_url = Dart_NewString(canonical_script_name); | |
| 398 Dart_Handle library = Dart_LookupLibrary(script_url); | |
| 399 if (Dart_IsError(library)) { | |
| 400 fprintf(stderr, "%s\n", Dart_GetError(library)); | |
| 401 Dart_ExitScope(); | |
| 402 Dart_ShutdownIsolate(); | |
| 403 free(canonical_script_name); | |
| 404 return false; // Indicates we encountered an error. | |
| 405 } | |
| 406 #endif | |
| 407 | |
| 408 // Now create the script snapshot and save into a buffer. | |
| 409 uint8_t* buffer; | |
| 410 intptr_t size; | |
| 411 result = Dart_CreateScriptSnapshot(&buffer, &size); | |
| 412 if (Dart_IsError(result)) { | |
| 413 fprintf(stderr, "%s\n", Dart_GetError(result)); | |
| 414 Dart_ExitScope(); | |
| 415 Dart_ShutdownIsolate(); | |
| 416 return false; // Indicates we encountered an error. | |
| 417 } | |
| 418 | |
| 419 // Save the script snapshot as we are about to shutdown the isolate. | |
| 420 script_snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); | |
| 421 ASSERT(script_snapshot_buffer != NULL); | |
| 422 memmove(script_snapshot_buffer, buffer, size); | |
| 423 | |
| 424 // Shutdown this isolate. | |
| 425 Dart_ExitScope(); | |
| 426 Dart_ShutdownIsolate(); | |
| 427 return true; | |
| 428 } | |
| 429 | |
| 430 | |
| 431 static void PrintUsage() { | 419 static void PrintUsage() { |
| 432 fprintf(stderr, | 420 fprintf(stderr, |
| 433 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); | 421 "dart [<vm-flags>] <dart-script-file> [<dart-options>]\n"); |
| 434 } | 422 } |
| 435 | 423 |
| 436 | 424 |
| 437 static Dart_Handle SetBreakpoint(const char* breakpoint_at, | 425 static Dart_Handle SetBreakpoint(const char* breakpoint_at, |
| 438 Dart_Handle library) { | 426 Dart_Handle library) { |
| 439 Dart_Handle result; | 427 Dart_Handle result; |
| 440 if (strchr(breakpoint_at, ':')) { | 428 if (strchr(breakpoint_at, ':')) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 | 467 |
| 480 const char* kFormat = "%s/%s"; | 468 const char* kFormat = "%s/%s"; |
| 481 intptr_t len = strlen(script_name) + strlen(func_name) + 2; | 469 intptr_t len = strlen(script_name) + strlen(func_name) + 2; |
| 482 char* buffer = new char[len]; | 470 char* buffer = new char[len]; |
| 483 ASSERT(buffer != NULL); | 471 ASSERT(buffer != NULL); |
| 484 snprintf(buffer, len, kFormat, script_name, func_name); | 472 snprintf(buffer, len, kFormat, script_name, func_name); |
| 485 return buffer; | 473 return buffer; |
| 486 } | 474 } |
| 487 | 475 |
| 488 | 476 |
| 477 static const int kErrorExitCode = 255; // Indicates we encountered an error. |
| 478 |
| 479 |
| 480 static int ErrorExit(const char* format, ...) { |
| 481 va_list arguments; |
| 482 va_start(arguments, format); |
| 483 vfprintf(stderr, format, arguments); |
| 484 va_end(arguments); |
| 485 |
| 486 Dart_ExitScope(); |
| 487 Dart_ShutdownIsolate(); |
| 488 |
| 489 free(const_cast<char*>(original_script_name)); |
| 490 free(const_cast<char*>(original_working_directory)); |
| 491 free(const_cast<char*>(original_script_url)); |
| 492 |
| 493 return kErrorExitCode; |
| 494 } |
| 495 |
| 496 |
| 489 int main(int argc, char** argv) { | 497 int main(int argc, char** argv) { |
| 490 char* script_name; | 498 char* script_name; |
| 491 CommandLineOptions vm_options(argc); | 499 CommandLineOptions vm_options(argc); |
| 492 CommandLineOptions dart_options(argc); | 500 CommandLineOptions dart_options(argc); |
| 493 CommandLineOptions import_map(argc); | 501 CommandLineOptions import_map(argc); |
| 494 import_map_options = &import_map; | 502 import_map_options = &import_map; |
| 495 | 503 |
| 496 // Perform platform specific initialization. | 504 // Perform platform specific initialization. |
| 497 if (!Platform::Initialize()) { | 505 if (!Platform::Initialize()) { |
| 498 fprintf(stderr, "Initialization failed\n"); | 506 fprintf(stderr, "Initialization failed\n"); |
| 499 } | 507 } |
| 500 | 508 |
| 501 // Parse command line arguments. | 509 // Parse command line arguments. |
| 502 if (ParseArguments(argc, | 510 if (ParseArguments(argc, |
| 503 argv, | 511 argv, |
| 504 &vm_options, | 512 &vm_options, |
| 505 &script_name, | 513 &script_name, |
| 506 &dart_options) < 0) { | 514 &dart_options) < 0) { |
| 507 PrintUsage(); | 515 PrintUsage(); |
| 508 return 255; | 516 return kErrorExitCode; |
| 509 } | 517 } |
| 510 | 518 |
| 511 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 519 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 512 | 520 |
| 513 // Initialize event handler. | 521 // Initialize event handler. |
| 514 EventHandler::Initialize(); | 522 EventHandler::Initialize(); |
| 515 | 523 |
| 516 // Initialize the Dart VM. | 524 // Initialize the Dart VM. |
| 517 Dart_Initialize(CreateIsolateAndSetup, NULL); | 525 Dart_Initialize(CreateIsolateAndSetup, NULL); |
| 518 | 526 |
| 519 canonical_script_name = File::GetCanonicalPath(script_name); | 527 original_script_name = strdup(script_name); |
| 520 if (canonical_script_name == NULL) { | 528 original_working_directory = Directory::Current(); |
| 521 fprintf(stderr, "Unable to find '%s'\n", script_name); | |
| 522 return 255; // Indicates we encountered an error. | |
| 523 } | |
| 524 | |
| 525 // If application snapshot option is specified, first create the | |
| 526 // application snapshot and then load the script using the snapshot | |
| 527 // created. | |
| 528 if (use_script_snapshot) { | |
| 529 if (!CaptureScriptSnapshot()) { | |
| 530 return 255; // Error capturing script snapshot, error already reported. | |
| 531 } | |
| 532 } | |
| 533 | 529 |
| 534 // Call CreateIsolateAndSetup which creates an isolate and loads up | 530 // Call CreateIsolateAndSetup which creates an isolate and loads up |
| 535 // the specified application script. | 531 // the specified application script. |
| 536 char* error = NULL; | 532 char* error = NULL; |
| 537 char* isolate_name = BuildIsolateName(canonical_script_name, "main"); | 533 char* isolate_name = BuildIsolateName(original_script_name, "main"); |
| 538 if (!CreateIsolateAndSetup(isolate_name, NULL, &error)) { | 534 if (!CreateIsolateAndSetup(isolate_name, NULL, &error)) { |
| 539 fprintf(stderr, "%s\n", error); | 535 fprintf(stderr, "%s\n", error); |
| 540 free(canonical_script_name); | 536 free(const_cast<char*>(original_script_name)); |
| 537 free(const_cast<char*>(original_working_directory)); |
| 541 free(error); | 538 free(error); |
| 542 free(script_snapshot_buffer); | |
| 543 delete [] isolate_name; | 539 delete [] isolate_name; |
| 544 return 255; // Indicates we encountered an error. | 540 return 255; // Indicates we encountered an error. |
| 545 } | 541 } |
| 546 free(script_snapshot_buffer); // Don't need it anymore. | |
| 547 delete [] isolate_name; | 542 delete [] isolate_name; |
| 548 | 543 |
| 549 Dart_Isolate isolate = Dart_CurrentIsolate(); | 544 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 550 ASSERT(isolate != NULL); | 545 ASSERT(isolate != NULL); |
| 551 Dart_Handle result; | 546 Dart_Handle result; |
| 552 | 547 |
| 553 Dart_EnterScope(); | 548 Dart_EnterScope(); |
| 554 | 549 |
| 555 if (has_compile_all) { | 550 if (has_compile_all) { |
| 556 result = Dart_CompileAll(); | 551 result = Dart_CompileAll(); |
| 557 if (Dart_IsError(result)) { | 552 if (Dart_IsError(result)) { |
| 558 fprintf(stderr, "%s\n", Dart_GetError(result)); | 553 return ErrorExit("%s\n", Dart_GetError(result)); |
| 559 Dart_ExitScope(); | |
| 560 Dart_ShutdownIsolate(); | |
| 561 free(canonical_script_name); | |
| 562 return 255; // Indicates we encountered an error. | |
| 563 } | 554 } |
| 564 } | 555 } |
| 565 | 556 |
| 566 // Create a dart options object that can be accessed from dart code. | 557 // Create a dart options object that can be accessed from dart code. |
| 567 Dart_Handle options_result = | 558 Dart_Handle options_result = |
| 568 SetupRuntimeOptions(&dart_options, canonical_script_name); | 559 SetupRuntimeOptions(&dart_options, original_script_name); |
| 569 if (Dart_IsError(options_result)) { | 560 if (Dart_IsError(options_result)) { |
| 570 fprintf(stderr, "%s\n", Dart_GetError(options_result)); | 561 return ErrorExit("%s\n", Dart_GetError(options_result)); |
| 571 Dart_ExitScope(); | |
| 572 Dart_ShutdownIsolate(); | |
| 573 free(canonical_script_name); | |
| 574 return 255; // Indicates we encountered an error. | |
| 575 } | 562 } |
| 576 // Lookup the library of the main script. | 563 // Lookup the library of the main script. |
| 577 Dart_Handle script_url = Dart_NewString(canonical_script_name); | 564 Dart_Handle script_url = Dart_NewString(original_script_url); |
| 578 Dart_Handle library = Dart_LookupLibrary(script_url); | 565 Dart_Handle library = Dart_LookupLibrary(script_url); |
| 579 if (Dart_IsError(library)) { | 566 if (Dart_IsError(library)) { |
| 580 fprintf(stderr, "%s\n", Dart_GetError(library)); | 567 return ErrorExit("%s\n", Dart_GetError(library)); |
| 581 Dart_ExitScope(); | |
| 582 Dart_ShutdownIsolate(); | |
| 583 free(canonical_script_name); | |
| 584 return 255; // Indicates we encountered an error. | |
| 585 } | 568 } |
| 586 // Set debug breakpoint if specified on the command line. | 569 // Set debug breakpoint if specified on the command line. |
| 587 if (breakpoint_at != NULL) { | 570 if (breakpoint_at != NULL) { |
| 588 result = SetBreakpoint(breakpoint_at, library); | 571 result = SetBreakpoint(breakpoint_at, library); |
| 589 if (Dart_IsError(result)) { | 572 if (Dart_IsError(result)) { |
| 590 fprintf(stderr, "Error setting breakpoint at '%s': %s\n", | 573 return ErrorExit("Error setting breakpoint at '%s': %s\n", |
| 591 breakpoint_at, | 574 breakpoint_at, |
| 592 Dart_GetError(result)); | 575 Dart_GetError(result)); |
| 593 Dart_ExitScope(); | |
| 594 Dart_ShutdownIsolate(); | |
| 595 free(canonical_script_name); | |
| 596 return 255; // Indicates we encountered an error. | |
| 597 } | 576 } |
| 598 } | 577 } |
| 599 // Lookup and invoke the top level main function. | 578 // Lookup and invoke the top level main function. |
| 600 result = Dart_InvokeStatic(library, | 579 result = Dart_InvokeStatic(library, |
| 601 Dart_NewString(""), | 580 Dart_NewString(""), |
| 602 Dart_NewString("main"), | 581 Dart_NewString("main"), |
| 603 0, | 582 0, |
| 604 NULL); | 583 NULL); |
| 605 if (Dart_IsError(result)) { | 584 if (Dart_IsError(result)) { |
| 606 fprintf(stderr, "%s\n", Dart_GetError(result)); | 585 return ErrorExit("%s\n", Dart_GetError(result)); |
| 607 Dart_ExitScope(); | |
| 608 Dart_ShutdownIsolate(); | |
| 609 free(canonical_script_name); | |
| 610 return 255; // Indicates we encountered an error. | |
| 611 } | 586 } |
| 612 // Keep handling messages until the last active receive port is closed. | 587 // Keep handling messages until the last active receive port is closed. |
| 613 result = Dart_RunLoop(); | 588 result = Dart_RunLoop(); |
| 614 if (Dart_IsError(result)) { | 589 if (Dart_IsError(result)) { |
| 615 fprintf(stderr, "%s\n", Dart_GetError(result)); | 590 return ErrorExit("%s\n", Dart_GetError(result)); |
| 616 Dart_ExitScope(); | |
| 617 Dart_ShutdownIsolate(); | |
| 618 free(canonical_script_name); | |
| 619 return 255; // Indicates we encountered an error. | |
| 620 } | 591 } |
| 621 free(canonical_script_name); | 592 |
| 622 Dart_ExitScope(); | 593 Dart_ExitScope(); |
| 623 // Dump symbol information for the profiler. | 594 // Dump symbol information for the profiler. |
| 624 DumpPprofSymbolInfo(); | 595 DumpPprofSymbolInfo(); |
| 625 // Shutdown the isolate. | 596 // Shutdown the isolate. |
| 626 Dart_ShutdownIsolate(); | 597 Dart_ShutdownIsolate(); |
| 627 // Terminate event handler. | 598 // Terminate event handler. |
| 628 EventHandler::Terminate(); | 599 EventHandler::Terminate(); |
| 629 // Terminate process exit-code handler. | 600 // Terminate process exit-code handler. |
| 630 Process::TerminateExitCodeHandler(); | 601 Process::TerminateExitCodeHandler(); |
| 631 | 602 |
| 603 free(const_cast<char*>(original_script_name)); |
| 604 free(const_cast<char*>(original_working_directory)); |
| 605 free(const_cast<char*>(original_script_url)); |
| 606 |
| 632 return 0; | 607 return 0; |
| 633 } | 608 } |
| OLD | NEW |