Chromium Code Reviews| 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/dbg_connection.h" | 14 #include "bin/dbg_connection.h" |
| 15 #include "bin/directory.h" | 15 #include "bin/directory.h" |
| 16 #include "bin/eventhandler.h" | 16 #include "bin/eventhandler.h" |
| 17 #include "bin/extensions.h" | 17 #include "bin/extensions.h" |
| 18 #include "bin/file.h" | 18 #include "bin/file.h" |
| 19 #include "bin/isolate_data.h" | 19 #include "bin/isolate_data.h" |
| 20 #include "bin/platform.h" | 20 #include "bin/platform.h" |
| 21 #include "bin/process.h" | 21 #include "bin/process.h" |
| 22 #include "platform/globals.h" | 22 #include "platform/globals.h" |
| 23 | 23 |
| 24 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 24 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 25 // it is initialized to NULL. | 25 // it is initialized to NULL. |
| 26 extern const uint8_t* snapshot_buffer; | 26 extern const uint8_t* snapshot_buffer; |
| 27 | 27 |
| 28 | 28 |
| 29 // Global state that stores the original working directory.. | |
| 30 static const char* original_working_directory = NULL; | |
| 31 | |
| 32 | |
| 33 // Global state that stores the import URL map specified on the | 29 // Global state that stores the import URL map specified on the |
| 34 // command line. | 30 // command line. |
| 35 static CommandLineOptions* import_map_options = NULL; | 31 static CommandLineOptions* import_map_options = NULL; |
| 36 | 32 |
| 37 | 33 |
| 38 // Global state that indicates whether perf_events symbol information | 34 // Global state that indicates whether perf_events symbol information |
| 39 // is to be generated or not. | 35 // is to be generated or not. |
| 40 static File* perf_events_symbols_file = NULL; | 36 static File* perf_events_symbols_file = NULL; |
| 41 | 37 |
| 42 | 38 |
| 43 // Global state that indicates whether pprof symbol information is | 39 // Global state that indicates whether pprof symbol information is |
| 44 // to be generated or not. | 40 // to be generated or not. |
| 45 static const char* generate_pprof_symbols_filename = NULL; | 41 static const char* generate_pprof_symbols_filename = NULL; |
| 46 | 42 |
| 47 | 43 |
| 44 // Global state that stores a pointer to the application script snapshot. | |
| 45 static bool use_script_snapshot = false; | |
| 46 static File* snapshot_file = NULL; | |
| 47 | |
| 48 | |
| 48 // Global state that stores a file name for flow graph debugging output. | 49 // Global state that stores a file name for flow graph debugging output. |
| 49 // NULL if no output is generated. | 50 // NULL if no output is generated. |
| 50 static File* flow_graph_file = NULL; | 51 static File* flow_graph_file = NULL; |
| 51 | 52 |
| 52 // Global state that indicates whether there is a debug breakpoint. | 53 // Global state that indicates whether there is a debug breakpoint. |
| 53 // This pointer points into an argv buffer and does not need to be | 54 // This pointer points into an argv buffer and does not need to be |
| 54 // free'd. | 55 // free'd. |
| 55 static const char* breakpoint_at = NULL; | 56 static const char* breakpoint_at = NULL; |
| 56 | 57 |
| 57 | 58 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 68 // (This pointer points into an argv buffer and does not need to be | 69 // (This pointer points into an argv buffer and does not need to be |
| 69 // free'd.) | 70 // free'd.) |
| 70 static const char* package_root = NULL; | 71 static const char* package_root = NULL; |
| 71 | 72 |
| 72 | 73 |
| 73 // Global flag that is used to indicate that we want to compile all the | 74 // Global flag that is used to indicate that we want to compile all the |
| 74 // dart functions and not run anything. | 75 // dart functions and not run anything. |
| 75 static bool has_compile_all = false; | 76 static bool has_compile_all = false; |
| 76 | 77 |
| 77 | 78 |
| 78 static bool IsWindowsHost() { | |
| 79 #if defined(TARGET_OS_WINDOWS) | |
| 80 return true; | |
| 81 #else // defined(TARGET_OS_WINDOWS) | |
| 82 return false; | |
| 83 #endif // defined(TARGET_OS_WINDOWS) | |
| 84 } | |
| 85 | |
| 86 | |
| 87 static bool IsValidFlag(const char* name, | 79 static bool IsValidFlag(const char* name, |
| 88 const char* prefix, | 80 const char* prefix, |
| 89 intptr_t prefix_length) { | 81 intptr_t prefix_length) { |
| 90 intptr_t name_length = strlen(name); | 82 intptr_t name_length = strlen(name); |
| 91 return ((name_length > prefix_length) && | 83 return ((name_length > prefix_length) && |
| 92 (strncmp(name, prefix, prefix_length) == 0)); | 84 (strncmp(name, prefix, prefix_length) == 0)); |
| 93 } | 85 } |
| 94 | 86 |
| 95 | 87 |
| 96 static void ProcessBreakpointOption(const char* funcname) { | 88 static void ProcessBreakpointOption(const char* funcname) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 } | 143 } |
| 152 } | 144 } |
| 153 | 145 |
| 154 | 146 |
| 155 static void ProcessPprofOption(const char* filename) { | 147 static void ProcessPprofOption(const char* filename) { |
| 156 ASSERT(filename != NULL); | 148 ASSERT(filename != NULL); |
| 157 generate_pprof_symbols_filename = filename; | 149 generate_pprof_symbols_filename = filename; |
| 158 } | 150 } |
| 159 | 151 |
| 160 | 152 |
| 153 static void ProcessScriptSnapshotOption(const char* filename) { | |
| 154 if (filename != NULL && strlen(filename) != 0) { | |
| 155 use_script_snapshot = true; | |
| 156 snapshot_file = File::Open(filename, File::kRead); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 | |
| 161 static void ProcessFlowGraphOption(const char* flowgraph_option) { | 161 static void ProcessFlowGraphOption(const char* flowgraph_option) { |
| 162 ASSERT(flowgraph_option != NULL); | 162 ASSERT(flowgraph_option != NULL); |
| 163 flow_graph_file = File::Open("flowgraph.cfg", File::kWriteTruncate); | 163 flow_graph_file = File::Open("flowgraph.cfg", File::kWriteTruncate); |
| 164 ASSERT(flow_graph_file != NULL); | 164 ASSERT(flow_graph_file != NULL); |
| 165 } | 165 } |
| 166 | 166 |
| 167 | 167 |
| 168 static void ProcessImportMapOption(const char* map) { | 168 static void ProcessImportMapOption(const char* map) { |
| 169 ASSERT(map != NULL); | 169 ASSERT(map != NULL); |
| 170 import_map_options->AddArgument(map); | 170 import_map_options->AddArgument(map); |
| 171 } | 171 } |
| 172 | 172 |
| 173 | 173 |
| 174 static struct { | 174 static struct { |
| 175 const char* option_name; | 175 const char* option_name; |
| 176 void (*process)(const char* option); | 176 void (*process)(const char* option); |
| 177 } main_options[] = { | 177 } main_options[] = { |
| 178 { "--break_at=", ProcessBreakpointOption }, | 178 { "--break_at=", ProcessBreakpointOption }, |
| 179 { "--compile_all", ProcessCompileAllOption }, | 179 { "--compile_all", ProcessCompileAllOption }, |
| 180 { "--debug", ProcessDebugOption }, | 180 { "--debug", ProcessDebugOption }, |
| 181 { "--generate_flow_graph", ProcessFlowGraphOption }, | |
| 181 { "--generate_perf_events_symbols", ProcessPerfEventsOption }, | 182 { "--generate_perf_events_symbols", ProcessPerfEventsOption }, |
| 182 { "--generate_pprof_symbols=", ProcessPprofOption }, | 183 { "--generate_pprof_symbols=", ProcessPprofOption }, |
| 183 { "--import_map=", ProcessImportMapOption }, | 184 { "--import_map=", ProcessImportMapOption }, |
| 184 { "--package-root=", ProcessPackageRootOption }, | 185 { "--package-root=", ProcessPackageRootOption }, |
| 185 { "--generate_flow_graph", ProcessFlowGraphOption }, | 186 { "--use_script_snapshot=", ProcessScriptSnapshotOption }, |
| 186 { NULL, NULL } | 187 { NULL, NULL } |
| 187 }; | 188 }; |
| 188 | 189 |
| 189 | 190 |
| 190 static bool ProcessMainOptions(const char* option) { | 191 static bool ProcessMainOptions(const char* option) { |
| 191 int i = 0; | 192 int i = 0; |
| 192 const char* name = main_options[0].option_name; | 193 const char* name = main_options[0].option_name; |
| 193 while (name != NULL) { | 194 while (name != NULL) { |
| 194 int length = strlen(name); | 195 int length = strlen(name); |
| 195 if (strncmp(option, name, length) == 0) { | 196 if (strncmp(option, name, length) == 0) { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 if (buffer_size > 0) { | 364 if (buffer_size > 0) { |
| 364 ASSERT(buffer != NULL); | 365 ASSERT(buffer != NULL); |
| 365 pprof_file->WriteFully(buffer, buffer_size); | 366 pprof_file->WriteFully(buffer, buffer_size); |
| 366 } | 367 } |
| 367 delete pprof_file; // Closes the file. | 368 delete pprof_file; // Closes the file. |
| 368 Dart_ExitScope(); | 369 Dart_ExitScope(); |
| 369 } | 370 } |
| 370 } | 371 } |
| 371 | 372 |
| 372 | 373 |
| 373 | |
| 374 static Dart_Handle ResolveScriptUri(Dart_Handle script_uri, | |
| 375 Dart_Handle builtin_lib) { | |
| 376 const int kNumArgs = 3; | |
| 377 Dart_Handle dart_args[kNumArgs]; | |
| 378 dart_args[0] = Dart_NewString(original_working_directory); | |
| 379 dart_args[1] = script_uri; | |
| 380 dart_args[2] = (IsWindowsHost() ? Dart_True() : Dart_False()); | |
| 381 return Dart_Invoke( | |
| 382 builtin_lib, Dart_NewString("_resolveScriptUri"), kNumArgs, dart_args); | |
| 383 } | |
| 384 | |
| 385 | |
| 386 static Dart_Handle FilePathFromUri(Dart_Handle script_uri, | |
| 387 Dart_Handle builtin_lib) { | |
| 388 const int kNumArgs = 2; | |
| 389 Dart_Handle dart_args[kNumArgs]; | |
| 390 dart_args[0] = script_uri; | |
| 391 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False()); | |
| 392 Dart_Handle script_path = Dart_Invoke( | |
| 393 builtin_lib, Dart_NewString("_filePathFromUri"), kNumArgs, dart_args); | |
| 394 return script_path; | |
| 395 } | |
| 396 | |
| 397 | |
| 398 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | |
| 399 Dart_Handle library, | |
| 400 Dart_Handle url) { | |
| 401 if (!Dart_IsLibrary(library)) { | |
| 402 return Dart_Error("not a library"); | |
| 403 } | |
| 404 if (!Dart_IsString8(url)) { | |
| 405 return Dart_Error("url is not a string"); | |
| 406 } | |
| 407 const char* url_string = NULL; | |
| 408 Dart_Handle result = Dart_StringToCString(url, &url_string); | |
| 409 if (Dart_IsError(result)) { | |
| 410 return result; | |
| 411 } | |
| 412 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); | |
| 413 bool is_dart_extension_url = DartUtils::IsDartExtensionSchemeURL(url_string); | |
| 414 if (tag == kCanonicalizeUrl) { | |
| 415 // If this is a Dart Scheme URL then it is not modified as it will be | |
| 416 // handled by the VM internally. | |
| 417 if (is_dart_scheme_url) { | |
| 418 return url; | |
| 419 } | |
| 420 // Resolve the url within the context of the library's URL. | |
| 421 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); | |
| 422 Dart_Handle library_url = Dart_LibraryUrl(library); | |
| 423 if (Dart_IsError(library_url)) { | |
| 424 return library_url; | |
| 425 } | |
| 426 const int kNumArgs = 2; | |
| 427 Dart_Handle dart_args[kNumArgs]; | |
| 428 dart_args[0] = library_url; | |
| 429 dart_args[1] = url; | |
| 430 return Dart_Invoke( | |
| 431 builtin_lib, Dart_NewString("_resolveUri"), kNumArgs, dart_args); | |
| 432 } | |
| 433 if (is_dart_scheme_url) { | |
| 434 ASSERT(tag == kImportTag); | |
| 435 // Handle imports of other built-in libraries present in the SDK. | |
| 436 Builtin::BuiltinLibraryId id; | |
| 437 if (DartUtils::IsDartCryptoLibURL(url_string)) { | |
| 438 id = Builtin::kCryptoLibrary; | |
| 439 } else if (DartUtils::IsDartIOLibURL(url_string)) { | |
| 440 id = Builtin::kIOLibrary; | |
| 441 } else if (DartUtils::IsDartJsonLibURL(url_string)) { | |
| 442 id = Builtin::kJsonLibrary; | |
| 443 } else if (DartUtils::IsDartUriLibURL(url_string)) { | |
| 444 id = Builtin::kUriLibrary; | |
| 445 } else if (DartUtils::IsDartUtfLibURL(url_string)) { | |
| 446 id = Builtin::kUtfLibrary; | |
| 447 } else if (DartUtils::IsDartWebLibURL(url_string)) { | |
| 448 id = Builtin::kWebLibrary; | |
| 449 } else { | |
| 450 return Dart_Error("Do not know how to load '%s'", url_string); | |
| 451 } | |
| 452 return Builtin::LoadLibrary(id); | |
| 453 } else { | |
| 454 // Get the file path out of the url. | |
| 455 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); | |
| 456 Dart_Handle file_path = FilePathFromUri(url, builtin_lib); | |
| 457 if (Dart_IsError(file_path)) { | |
| 458 return file_path; | |
| 459 } | |
| 460 Dart_StringToCString(file_path, &url_string); | |
| 461 } | |
| 462 if (is_dart_extension_url) { | |
| 463 if (tag != kImportTag) { | |
| 464 return Dart_Error("Dart extensions must use import: '%s'", url_string); | |
| 465 } | |
| 466 return Extensions::LoadExtension(url_string, library); | |
| 467 } | |
| 468 return DartUtils::LoadSource(NULL, | |
| 469 library, | |
| 470 url, | |
| 471 tag, | |
| 472 url_string); | |
| 473 } | |
| 474 | |
| 475 | |
| 476 static Dart_Handle ReadSource(Dart_Handle script_uri, | |
| 477 Dart_Handle builtin_lib) { | |
| 478 Dart_Handle script_path = FilePathFromUri(script_uri, builtin_lib); | |
| 479 if (Dart_IsError(script_path)) { | |
| 480 return script_path; | |
| 481 } | |
| 482 const char* script_path_cstr; | |
| 483 Dart_StringToCString(script_path, &script_path_cstr); | |
| 484 Dart_Handle source = DartUtils::ReadStringFromFile(script_path_cstr); | |
| 485 return source; | |
| 486 } | |
| 487 | |
| 488 | |
| 489 static Dart_Handle LoadScript(const char* script_uri, | |
| 490 bool resolve_script, | |
| 491 Dart_Handle builtin_lib) { | |
| 492 Dart_Handle resolved_script_uri; | |
| 493 if (resolve_script) { | |
| 494 resolved_script_uri = ResolveScriptUri(Dart_NewString(script_uri), | |
| 495 builtin_lib); | |
| 496 if (Dart_IsError(resolved_script_uri)) { | |
| 497 return resolved_script_uri; | |
| 498 } | |
| 499 } else { | |
| 500 resolved_script_uri = Dart_NewString(script_uri); | |
| 501 } | |
| 502 Dart_Handle source = ReadSource(resolved_script_uri, builtin_lib); | |
| 503 if (Dart_IsError(source)) { | |
| 504 return source; | |
| 505 } | |
| 506 return Dart_LoadScript(resolved_script_uri, source); | |
| 507 } | |
| 508 | |
| 509 | |
| 510 static Dart_Handle CreateImportMap(CommandLineOptions* map) { | 374 static Dart_Handle CreateImportMap(CommandLineOptions* map) { |
| 511 intptr_t length = (map == NULL) ? 0 : map->count(); | 375 intptr_t length = (map == NULL) ? 0 : map->count(); |
| 512 Dart_Handle import_map = Dart_NewList(length * 2); | 376 Dart_Handle import_map = Dart_NewList(length * 2); |
| 513 for (intptr_t i = 0; i < length; i++) { | 377 for (intptr_t i = 0; i < length; i++) { |
| 514 ASSERT(map->GetArgument(i) != NULL); | 378 ASSERT(map->GetArgument(i) != NULL); |
| 515 char* name = strdup(map->GetArgument(i)); | 379 char* name = strdup(map->GetArgument(i)); |
| 516 ASSERT(name != NULL); | 380 ASSERT(name != NULL); |
| 517 char* map_name = strchr(name, ','); | 381 char* map_name = strchr(name, ','); |
| 518 intptr_t index = i * 2; | 382 intptr_t index = i * 2; |
| 519 if (map_name != NULL) { | 383 if (map_name != NULL) { |
| 520 *map_name = '\0'; | 384 *map_name = '\0'; |
| 521 map_name += 1; | 385 map_name += 1; |
| 522 Dart_ListSetAt(import_map, index, Dart_NewString(name)); | 386 Dart_ListSetAt(import_map, index, Dart_NewString(name)); |
| 523 Dart_ListSetAt(import_map, (index + 1), Dart_NewString(map_name)); | 387 Dart_ListSetAt(import_map, (index + 1), Dart_NewString(map_name)); |
| 524 } else { | 388 } else { |
| 525 Dart_ListSetAt(import_map, index, Dart_NewString(name)); | 389 Dart_ListSetAt(import_map, index, Dart_NewString(name)); |
| 526 Dart_ListSetAt(import_map, (index + 1), Dart_NewString("")); | 390 Dart_ListSetAt(import_map, (index + 1), Dart_NewString("")); |
| 527 } | 391 } |
| 528 free(name); | 392 free(name); |
| 529 } | 393 } |
| 530 return import_map; | 394 return import_map; |
| 531 } | 395 } |
| 532 | 396 |
| 533 | 397 |
| 398 #define CHECK_RESULT(result) \ | |
| 399 if (Dart_IsError(result)) { \ | |
| 400 *error = strdup(Dart_GetError(result)); \ | |
| 401 Dart_ExitScope(); \ | |
| 402 Dart_ShutdownIsolate(); \ | |
| 403 return false; \ | |
| 404 } \ | |
| 405 | |
| 406 | |
| 534 // Returns true on success, false on failure. | 407 // Returns true on success, false on failure. |
| 535 static bool CreateIsolateAndSetupHelper(const char* script_uri, | 408 static bool CreateIsolateAndSetupHelper(const char* script_uri, |
| 536 const char* main, | 409 const char* main, |
| 537 bool resolve_script, | 410 bool resolve_script, |
| 538 void* data, | 411 void* data, |
| 539 char** error) { | 412 char** error) { |
| 540 Dart_Isolate isolate = | 413 Dart_Isolate isolate = |
| 541 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); | 414 Dart_CreateIsolate(script_uri, main, snapshot_buffer, data, error); |
| 542 if (isolate == NULL) { | 415 if (isolate == NULL) { |
| 543 return false; | 416 return false; |
| 544 } | 417 } |
| 545 | 418 |
| 546 Dart_EnterScope(); | 419 Dart_EnterScope(); |
| 547 | 420 |
| 548 if (snapshot_buffer != NULL) { | 421 if (snapshot_buffer != NULL) { |
| 549 // Setup the native resolver as the snapshot does not carry it. | 422 // Setup the native resolver as the snapshot does not carry it. |
| 550 Builtin::SetupLibrary(Builtin::LoadLibrary(Builtin::kBuiltinLibrary), | 423 Builtin::SetupLibrary(Builtin::LoadLibrary(Builtin::kBuiltinLibrary), |
| 551 Builtin::kBuiltinLibrary); | 424 Builtin::kBuiltinLibrary); |
| 552 Builtin::SetupLibrary(Builtin::LoadLibrary(Builtin::kIOLibrary), | 425 Builtin::SetupLibrary(Builtin::LoadLibrary(Builtin::kIOLibrary), |
| 553 Builtin::kIOLibrary); | 426 Builtin::kIOLibrary); |
| 554 } | 427 } |
| 555 | 428 |
| 556 // Set up the library tag handler for this isolate. | 429 // Set up the library tag handler for this isolate. |
| 557 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler); | 430 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 558 if (Dart_IsError(result)) { | 431 CHECK_RESULT(result); |
| 559 *error = strdup(Dart_GetError(result)); | |
| 560 Dart_ExitScope(); | |
| 561 Dart_ShutdownIsolate(); | |
| 562 return false; | |
| 563 } | |
| 564 | 432 |
| 565 // Set up the import map for this isolate. | 433 // Set up the import map for this isolate. |
| 566 result = Dart_SetImportMap(CreateImportMap(import_map_options)); | 434 result = Dart_SetImportMap(CreateImportMap(import_map_options)); |
| 567 if (Dart_IsError(result)) { | 435 CHECK_RESULT(result); |
| 568 *error = strdup(Dart_GetError(result)); | |
| 569 Dart_ExitScope(); | |
| 570 Dart_ShutdownIsolate(); | |
| 571 return false; | |
| 572 } | |
| 573 | 436 |
| 574 // Prepare builtin and its dependent libraries for use to resolve URIs. | 437 // Load the specified application script into the newly created isolate. |
| 575 Dart_Handle uri_lib = Builtin::LoadLibrary(Builtin::kUriLibrary); | 438 Dart_Handle library; |
| 576 if (Dart_IsError(uri_lib)) { | 439 if (use_script_snapshot) { |
| 577 *error = strdup(Dart_GetError(uri_lib)); | 440 if (snapshot_file == NULL) { |
| 578 Dart_ExitScope(); | 441 *error = strdup("Invalid script snapshot file name specified"); |
|
Ivan Posva
2012/08/31 23:20:51
You will end up here if you ever spawn another iso
siva
2012/09/01 00:25:44
I have set use_script_snapshot to false after the
| |
| 579 Dart_ShutdownIsolate(); | |
| 580 return false; | |
| 581 } | |
| 582 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); | |
| 583 if (Dart_IsError(builtin_lib)) { | |
| 584 *error = strdup(Dart_GetError(builtin_lib)); | |
| 585 Dart_ExitScope(); | |
| 586 Dart_ShutdownIsolate(); | |
| 587 return false; | |
| 588 } | |
| 589 | |
| 590 // Setup the corelib 'print' function. | |
| 591 Dart_Handle print = | |
| 592 Dart_Invoke(builtin_lib, Dart_NewString("_getPrintClosure"), 0, 0); | |
| 593 Dart_Handle coreimpl = Dart_LookupLibrary(Dart_NewString("dart:coreimpl")); | |
| 594 Dart_Handle print_impl = | |
| 595 Dart_GetClass(coreimpl, Dart_NewString("PrintImplementation")); | |
| 596 Dart_SetField(print_impl, Dart_NewString("_printClosure"), print); | |
| 597 | |
| 598 // Setup the IO library. | |
| 599 Dart_Handle io_lib = Builtin::LoadLibrary(Builtin::kIOLibrary); | |
| 600 Builtin::SetupIOLibrary(io_lib); | |
| 601 | |
| 602 if (package_root != NULL) { | |
| 603 const int kNumArgs = 1; | |
| 604 Dart_Handle dart_args[kNumArgs]; | |
| 605 | |
| 606 Dart_Handle handle = Dart_NewString(package_root); | |
| 607 if (Dart_IsError(handle)) { | |
| 608 *error = strdup(Dart_GetError(handle)); | |
| 609 Dart_ExitScope(); | 442 Dart_ExitScope(); |
| 610 Dart_ShutdownIsolate(); | 443 Dart_ShutdownIsolate(); |
| 611 return false; | 444 return false; |
| 612 } | 445 } |
| 613 dart_args[0] = handle; | 446 size_t len = snapshot_file->Length(); |
| 614 | 447 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(len)); |
| 615 Dart_Handle result = Dart_Invoke(builtin_lib, | 448 if (buffer == NULL) { |
| 616 Dart_NewString("_setPackageRoot"), kNumArgs, dart_args); | 449 delete snapshot_file; |
| 617 if (Dart_IsError(result)) { | 450 snapshot_file = NULL; |
| 618 *error = strdup(Dart_GetError(result)); | 451 *error = strdup("Unable to read contents of script snapshot file"); |
| 619 Dart_ExitScope(); | 452 Dart_ExitScope(); |
| 620 Dart_ShutdownIsolate(); | 453 Dart_ShutdownIsolate(); |
| 621 return false; | 454 return false; |
| 622 } | 455 } |
| 456 snapshot_file->ReadFully(buffer, len); | |
| 457 library = Dart_LoadScriptFromSnapshot(buffer); | |
| 458 free(buffer); | |
| 459 delete snapshot_file; | |
| 460 snapshot_file = NULL; | |
| 461 } else { | |
| 462 // Prepare builtin and its dependent libraries for use to resolve URIs. | |
| 463 Dart_Handle uri_lib = Builtin::LoadLibrary(Builtin::kUriLibrary); | |
| 464 CHECK_RESULT(uri_lib); | |
| 465 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); | |
| 466 CHECK_RESULT(builtin_lib); | |
| 467 | |
| 468 // Setup the corelib 'print' function. | |
| 469 Dart_Handle print = | |
| 470 Dart_Invoke(builtin_lib, Dart_NewString("_getPrintClosure"), 0, 0); | |
|
Ivan Posva
2012/08/31 23:20:51
This also needs to happen when setting up the isol
siva
2012/09/01 00:25:44
Good point. Move this code to dartutils and call h
| |
| 471 Dart_Handle coreimpl = Dart_LookupLibrary(Dart_NewString("dart:coreimpl")); | |
| 472 Dart_Handle print_impl = | |
| 473 Dart_GetClass(coreimpl, Dart_NewString("PrintImplementation")); | |
| 474 Dart_SetField(print_impl, Dart_NewString("_printClosure"), print); | |
| 475 | |
| 476 // Setup the IO library. | |
| 477 Dart_Handle io_lib = Builtin::LoadLibrary(Builtin::kIOLibrary); | |
| 478 Builtin::SetupIOLibrary(io_lib); | |
| 479 | |
| 480 if (package_root != NULL) { | |
| 481 const int kNumArgs = 1; | |
| 482 Dart_Handle dart_args[kNumArgs]; | |
| 483 | |
| 484 Dart_Handle handle = Dart_NewString(package_root); | |
| 485 CHECK_RESULT(handle); | |
| 486 dart_args[0] = handle; | |
| 487 Dart_Handle result = Dart_Invoke( | |
| 488 builtin_lib, Dart_NewString("_setPackageRoot"), kNumArgs, dart_args); | |
| 489 CHECK_RESULT(result); | |
| 490 } | |
| 491 library = DartUtils::LoadScript(script_uri, resolve_script, builtin_lib); | |
| 623 } | 492 } |
| 624 | 493 CHECK_RESULT(library); |
| 625 // Load the specified application script into the newly created isolate. | |
| 626 Dart_Handle library = LoadScript(script_uri, resolve_script, builtin_lib); | |
| 627 if (Dart_IsError(library)) { | |
| 628 *error = strdup(Dart_GetError(library)); | |
| 629 Dart_ExitScope(); | |
| 630 Dart_ShutdownIsolate(); | |
| 631 return false; | |
| 632 } | |
| 633 if (!Dart_IsLibrary(library)) { | 494 if (!Dart_IsLibrary(library)) { |
| 634 char errbuf[256]; | 495 char errbuf[256]; |
| 635 snprintf(errbuf, sizeof(errbuf), | 496 snprintf(errbuf, sizeof(errbuf), |
| 636 "Expected a library when loading script: %s", | 497 "Expected a library when loading script: %s", |
| 637 script_uri); | 498 script_uri); |
| 638 *error = strdup(errbuf); | 499 *error = strdup(errbuf); |
| 639 Dart_ExitScope(); | 500 Dart_ExitScope(); |
| 640 Dart_ShutdownIsolate(); | 501 Dart_ShutdownIsolate(); |
| 641 return false; | 502 return false; |
| 642 } | 503 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 718 | 579 |
| 719 | 580 |
| 720 static int ErrorExit(const char* format, ...) { | 581 static int ErrorExit(const char* format, ...) { |
| 721 va_list arguments; | 582 va_list arguments; |
| 722 va_start(arguments, format); | 583 va_start(arguments, format); |
| 723 vfprintf(stderr, format, arguments); | 584 vfprintf(stderr, format, arguments); |
| 724 va_end(arguments); | 585 va_end(arguments); |
| 725 | 586 |
| 726 Dart_ExitScope(); | 587 Dart_ExitScope(); |
| 727 Dart_ShutdownIsolate(); | 588 Dart_ShutdownIsolate(); |
| 728 free(const_cast<char*>(original_working_directory)); | |
| 729 | 589 |
| 730 return kErrorExitCode; | 590 return kErrorExitCode; |
| 731 } | 591 } |
| 732 | 592 |
| 733 | 593 |
| 734 static void ShutdownIsolate(void* callback_data) { | 594 static void ShutdownIsolate(void* callback_data) { |
| 735 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); | 595 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
| 736 EventHandler* handler = isolate_data->event_handler; | 596 EventHandler* handler = isolate_data->event_handler; |
| 737 if (handler != NULL) handler->Shutdown(); | 597 if (handler != NULL) handler->Shutdown(); |
| 738 delete isolate_data; | 598 delete isolate_data; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 772 } | 632 } |
| 773 } | 633 } |
| 774 | 634 |
| 775 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 635 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 776 | 636 |
| 777 // Initialize the Dart VM. | 637 // Initialize the Dart VM. |
| 778 Dart_Initialize(CreateIsolateAndSetup, | 638 Dart_Initialize(CreateIsolateAndSetup, |
| 779 NULL, | 639 NULL, |
| 780 ShutdownIsolate); | 640 ShutdownIsolate); |
| 781 | 641 |
| 782 original_working_directory = Directory::Current(); | 642 DartUtils::SetOriginalWorkingDirectory(); |
| 783 | 643 |
| 784 // Call CreateIsolateAndSetup which creates an isolate and loads up | 644 // Call CreateIsolateAndSetup which creates an isolate and loads up |
| 785 // the specified application script. | 645 // the specified application script. |
| 786 char* error = NULL; | 646 char* error = NULL; |
| 787 char* isolate_name = BuildIsolateName(script_name, "main"); | 647 char* isolate_name = BuildIsolateName(script_name, "main"); |
| 788 if (!CreateIsolateAndSetupHelper(script_name, | 648 if (!CreateIsolateAndSetupHelper(script_name, |
| 789 "main", | 649 "main", |
| 790 true, // Canonicalize the script name. | 650 true, // Canonicalize the script name. |
| 791 new IsolateData(), | 651 new IsolateData(), |
| 792 &error)) { | 652 &error)) { |
| 793 fprintf(stderr, "%s\n", error); | 653 fprintf(stderr, "%s\n", error); |
| 794 free(const_cast<char*>(original_working_directory)); | |
| 795 free(error); | 654 free(error); |
| 796 delete [] isolate_name; | 655 delete [] isolate_name; |
| 797 return kErrorExitCode; // Indicates we encountered an error. | 656 return kErrorExitCode; // Indicates we encountered an error. |
| 798 } | 657 } |
| 799 delete [] isolate_name; | 658 delete [] isolate_name; |
| 800 | 659 |
| 801 Dart_Isolate isolate = Dart_CurrentIsolate(); | 660 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 802 ASSERT(isolate != NULL); | 661 ASSERT(isolate != NULL); |
| 803 Dart_Handle result; | 662 Dart_Handle result; |
| 804 | 663 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 851 } | 710 } |
| 852 | 711 |
| 853 Dart_ExitScope(); | 712 Dart_ExitScope(); |
| 854 // Dump symbol information for the profiler. | 713 // Dump symbol information for the profiler. |
| 855 DumpPprofSymbolInfo(); | 714 DumpPprofSymbolInfo(); |
| 856 // Shutdown the isolate. | 715 // Shutdown the isolate. |
| 857 Dart_ShutdownIsolate(); | 716 Dart_ShutdownIsolate(); |
| 858 // Terminate process exit-code handler. | 717 // Terminate process exit-code handler. |
| 859 Process::TerminateExitCodeHandler(); | 718 Process::TerminateExitCodeHandler(); |
| 860 | 719 |
| 861 free(const_cast<char*>(original_working_directory)); | |
| 862 | |
| 863 return 0; | 720 return 0; |
| 864 } | 721 } |
| OLD | NEW |