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" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "bin/process.h" | 22 #include "bin/process.h" |
| 23 #include "platform/globals.h" | 23 #include "platform/globals.h" |
| 24 | 24 |
| 25 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise | 25 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise |
| 26 // it is initialized to NULL. | 26 // it is initialized to NULL. |
| 27 extern const uint8_t* snapshot_buffer; | 27 extern const uint8_t* snapshot_buffer; |
| 28 | 28 |
| 29 | 29 |
| 30 // Global state that stores a pointer to the application script snapshot. | 30 // Global state that stores a pointer to the application script snapshot. |
| 31 static bool use_script_snapshot = false; | 31 static bool use_script_snapshot = false; |
| 32 static bool generate_script_snapshot = false; | |
| 32 static File* snapshot_file = NULL; | 33 static File* snapshot_file = NULL; |
| 33 | 34 |
| 34 | 35 |
| 35 // Global state that indicates whether there is a debug breakpoint. | 36 // Global state that indicates whether there is a debug breakpoint. |
| 36 // This pointer points into an argv buffer and does not need to be | 37 // This pointer points into an argv buffer and does not need to be |
| 37 // free'd. | 38 // free'd. |
| 38 static const char* breakpoint_at = NULL; | 39 static const char* breakpoint_at = NULL; |
| 39 | 40 |
| 40 | 41 |
| 41 // Global state that indicates whether we should open a connection | 42 // Global state that indicates whether we should open a connection |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 Log::PrintErr("unrecognized --debug option syntax. " | 138 Log::PrintErr("unrecognized --debug option syntax. " |
| 138 "Use --debug[:<port number>]\n"); | 139 "Use --debug[:<port number>]\n"); |
| 139 return false; | 140 return false; |
| 140 } | 141 } |
| 141 breakpoint_at = "main"; | 142 breakpoint_at = "main"; |
| 142 start_debugger = true; | 143 start_debugger = true; |
| 143 return true; | 144 return true; |
| 144 } | 145 } |
| 145 | 146 |
| 146 | 147 |
| 147 static bool ProcessScriptSnapshotOption(const char* filename) { | 148 static bool ProcessUseScriptSnapshotOption(const char* filename) { |
| 148 if (filename != NULL && strlen(filename) != 0) { | 149 if (filename != NULL && strlen(filename) != 0) { |
| 149 use_script_snapshot = true; | 150 use_script_snapshot = true; |
| 151 if (generate_script_snapshot) { | |
| 152 Log::PrintErr("Incompatible options specified --generate_script_snapshot " | |
| 153 "and --use_script_snapshot\n"); | |
| 154 return false; | |
| 155 } | |
| 150 snapshot_file = File::Open(filename, File::kRead); | 156 snapshot_file = File::Open(filename, File::kRead); |
| 157 if (snapshot_file == NULL) { | |
| 158 Log::PrintErr("Unable to open file %s for reading the snapshot\n", | |
| 159 filename); | |
| 160 return false; | |
| 161 } | |
| 162 } | |
| 163 return true; | |
| 164 } | |
| 165 | |
| 166 | |
| 167 static bool ProcessGenScriptSnapshotOption(const char* filename) { | |
| 168 if (filename != NULL && strlen(filename) != 0) { | |
| 169 // Ensure that are already running using a full snapshot. | |
| 170 if (use_script_snapshot) { | |
| 171 Log::PrintErr("Incompatible options specified --use_script_snapshot " | |
| 172 "and --generate_script_snapshot\n"); | |
| 173 return false; | |
| 174 } | |
| 175 if (snapshot_buffer == NULL) { | |
|
Ivan Posva
2013/01/25 22:29:55
Move this up to sit next to the corresponding comm
siva
2013/01/26 00:39:32
Done.
| |
| 176 Log::PrintErr("Script snapshots cannot be generated in this version of" | |
| 177 " dart\n"); | |
| 178 return false; | |
| 179 } | |
| 180 snapshot_file = File::Open(filename, File::kWriteTruncate); | |
| 181 if (snapshot_file == NULL) { | |
| 182 Log::PrintErr("Unable to open file %s for writing the snapshot\n", | |
| 183 filename); | |
| 184 return false; | |
| 185 } | |
| 186 generate_script_snapshot = true; | |
| 151 } | 187 } |
| 152 return true; | 188 return true; |
| 153 } | 189 } |
| 154 | 190 |
| 155 | 191 |
| 156 static struct { | 192 static struct { |
| 157 const char* option_name; | 193 const char* option_name; |
| 158 bool (*process)(const char* option); | 194 bool (*process)(const char* option); |
| 159 } main_options[] = { | 195 } main_options[] = { |
| 160 // Standard options shared with dart2js. | 196 // Standard options shared with dart2js. |
| 161 { "--version", ProcessVersionOption }, | 197 { "--version", ProcessVersionOption }, |
| 162 { "--help", ProcessHelpOption }, | 198 { "--help", ProcessHelpOption }, |
| 163 { "-h", ProcessHelpOption }, | 199 { "-h", ProcessHelpOption }, |
| 164 { "--verbose", ProcessVerboseOption }, | 200 { "--verbose", ProcessVerboseOption }, |
| 165 { "-v", ProcessVerboseOption }, | 201 { "-v", ProcessVerboseOption }, |
| 166 { "--package-root=", ProcessPackageRootOption }, | 202 { "--package-root=", ProcessPackageRootOption }, |
| 167 { "-p", ProcessPackageRootOption }, | 203 { "-p", ProcessPackageRootOption }, |
| 168 // VM specific options to the standalone dart program. | 204 // VM specific options to the standalone dart program. |
| 169 { "--break_at=", ProcessBreakpointOption }, | 205 { "--break_at=", ProcessBreakpointOption }, |
| 170 { "--compile_all", ProcessCompileAllOption }, | 206 { "--compile_all", ProcessCompileAllOption }, |
| 171 { "--debug", ProcessDebugOption }, | 207 { "--debug", ProcessDebugOption }, |
| 172 { "--use_script_snapshot=", ProcessScriptSnapshotOption }, | 208 { "--use_script_snapshot=", ProcessUseScriptSnapshotOption }, |
| 209 { "--use-script-snapshot=", ProcessUseScriptSnapshotOption }, | |
| 210 { "--generate_script_snapshot=", ProcessGenScriptSnapshotOption }, | |
|
Ivan Posva
2013/01/25 22:29:55
Why the duplication here?
siva
2013/01/26 00:39:32
I have dropped generate_script_snapshot,
I will dr
| |
| 211 { "--generate-script-snapshot=", ProcessGenScriptSnapshotOption }, | |
| 173 { NULL, NULL } | 212 { NULL, NULL } |
| 174 }; | 213 }; |
| 175 | 214 |
| 176 | 215 |
| 177 static bool ProcessMainOptions(const char* option) { | 216 static bool ProcessMainOptions(const char* option) { |
| 178 int i = 0; | 217 int i = 0; |
| 179 const char* name = main_options[0].option_name; | 218 const char* name = main_options[0].option_name; |
| 180 while (name != NULL) { | 219 while (name != NULL) { |
| 181 int length = strlen(name); | 220 int length = strlen(name); |
| 182 if (strncmp(option, name, length) == 0) { | 221 if (strncmp(option, name, length) == 0) { |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 503 " (default port number is 5858)\n" | 542 " (default port number is 5858)\n" |
| 504 "\n" | 543 "\n" |
| 505 "--break_at=<location>\n" | 544 "--break_at=<location>\n" |
| 506 " sets a breakpoint at specified location where <location> is one of :\n" | 545 " sets a breakpoint at specified location where <location> is one of :\n" |
| 507 " url:<line_num> e.g. test.dart:10\n" | 546 " url:<line_num> e.g. test.dart:10\n" |
| 508 " [<class_name>.]<function_name> e.g. B.foo\n" | 547 " [<class_name>.]<function_name> e.g. B.foo\n" |
| 509 "\n" | 548 "\n" |
| 510 "--use_script_snapshot=<file_name>\n" | 549 "--use_script_snapshot=<file_name>\n" |
| 511 " executes Dart script present in the specified snapshot file\n" | 550 " executes Dart script present in the specified snapshot file\n" |
| 512 "\n" | 551 "\n" |
| 552 "--generate_script_snapshot=<file_name>\n" | |
| 553 " loads Dart script and generates a snapshot in the specified file\n" | |
| 554 "\n" | |
| 513 "The following options are only used for VM development and may\n" | 555 "The following options are only used for VM development and may\n" |
| 514 "be changed in any future version:\n"); | 556 "be changed in any future version:\n"); |
| 515 const char* print_flags = "--print_flags"; | 557 const char* print_flags = "--print_flags"; |
| 516 Dart_SetVMFlags(1, &print_flags); | 558 Dart_SetVMFlags(1, &print_flags); |
| 517 } | 559 } |
| 518 } | 560 } |
| 519 | 561 |
| 520 | 562 |
| 521 static Dart_Handle SetBreakpoint(const char* breakpoint_at, | 563 static Dart_Handle SetBreakpoint(const char* breakpoint_at, |
| 522 Dart_Handle library) { | 564 Dart_Handle library) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 666 return kErrorExitCode; // Indicates we encountered an error. | 708 return kErrorExitCode; // Indicates we encountered an error. |
| 667 } | 709 } |
| 668 delete [] isolate_name; | 710 delete [] isolate_name; |
| 669 | 711 |
| 670 Dart_Isolate isolate = Dart_CurrentIsolate(); | 712 Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 671 ASSERT(isolate != NULL); | 713 ASSERT(isolate != NULL); |
| 672 Dart_Handle result; | 714 Dart_Handle result; |
| 673 | 715 |
| 674 Dart_EnterScope(); | 716 Dart_EnterScope(); |
| 675 | 717 |
| 676 if (has_compile_all) { | 718 if (generate_script_snapshot) { |
| 677 result = Dart_CompileAll(); | 719 // First create a snapshot. |
| 720 Dart_Handle result; | |
| 721 uint8_t* buffer = NULL; | |
| 722 intptr_t size = 0; | |
| 723 result = Dart_CreateScriptSnapshot(&buffer, &size); | |
| 724 if (Dart_IsError(result)) { | |
| 725 Log::PrintErr("%s\n", Dart_GetError(result)); | |
| 726 Dart_ExitScope(); | |
| 727 Dart_ShutdownIsolate(); | |
| 728 return kErrorExitCode; // Indicates we encountered an error. | |
| 729 } | |
| 730 | |
| 731 // Now write the snapshot out to specified file. | |
| 732 bool bytes_written = snapshot_file->WriteFully(buffer, size); | |
| 733 ASSERT(bytes_written); | |
| 734 delete snapshot_file; | |
| 735 } else { | |
| 736 if (has_compile_all) { | |
| 737 result = Dart_CompileAll(); | |
| 738 if (Dart_IsError(result)) { | |
| 739 return ErrorExit("%s\n", Dart_GetError(result)); | |
| 740 } | |
| 741 } | |
| 742 | |
| 743 // Create a dart options object that can be accessed from dart code. | |
| 744 Dart_Handle options_result = | |
| 745 SetupRuntimeOptions(&dart_options, executable_name, script_name); | |
| 746 if (Dart_IsError(options_result)) { | |
| 747 return ErrorExit("%s\n", Dart_GetError(options_result)); | |
| 748 } | |
| 749 // Lookup the library of the root script. | |
| 750 Dart_Handle library = Dart_RootLibrary(); | |
| 751 if (Dart_IsNull(library)) { | |
| 752 return ErrorExit("Unable to find root library for '%s'\n", | |
| 753 script_name); | |
| 754 } | |
| 755 // Set debug breakpoint if specified on the command line. | |
| 756 if (breakpoint_at != NULL) { | |
| 757 result = SetBreakpoint(breakpoint_at, library); | |
| 758 if (Dart_IsError(result)) { | |
| 759 return ErrorExit("Error setting breakpoint at '%s': %s\n", | |
| 760 breakpoint_at, | |
| 761 Dart_GetError(result)); | |
| 762 } | |
| 763 } | |
| 764 | |
| 765 // Lookup and invoke the top level main function. | |
| 766 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); | |
| 767 if (Dart_IsError(result)) { | |
| 768 return ErrorExit("%s\n", Dart_GetError(result)); | |
| 769 } | |
| 770 // Keep handling messages until the last active receive port is closed. | |
| 771 result = Dart_RunLoop(); | |
| 678 if (Dart_IsError(result)) { | 772 if (Dart_IsError(result)) { |
| 679 return ErrorExit("%s\n", Dart_GetError(result)); | 773 return ErrorExit("%s\n", Dart_GetError(result)); |
| 680 } | 774 } |
| 681 } | 775 } |
| 682 | 776 |
| 683 // Create a dart options object that can be accessed from dart code. | |
| 684 Dart_Handle options_result = | |
| 685 SetupRuntimeOptions(&dart_options, executable_name, script_name); | |
| 686 if (Dart_IsError(options_result)) { | |
| 687 return ErrorExit("%s\n", Dart_GetError(options_result)); | |
| 688 } | |
| 689 // Lookup the library of the root script. | |
| 690 Dart_Handle library = Dart_RootLibrary(); | |
| 691 if (Dart_IsNull(library)) { | |
| 692 return ErrorExit("Unable to find root library for '%s'\n", | |
| 693 script_name); | |
| 694 } | |
| 695 // Set debug breakpoint if specified on the command line. | |
| 696 if (breakpoint_at != NULL) { | |
| 697 result = SetBreakpoint(breakpoint_at, library); | |
| 698 if (Dart_IsError(result)) { | |
| 699 return ErrorExit("Error setting breakpoint at '%s': %s\n", | |
| 700 breakpoint_at, | |
| 701 Dart_GetError(result)); | |
| 702 } | |
| 703 } | |
| 704 | |
| 705 // Lookup and invoke the top level main function. | |
| 706 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); | |
| 707 if (Dart_IsError(result)) { | |
| 708 return ErrorExit("%s\n", Dart_GetError(result)); | |
| 709 } | |
| 710 // Keep handling messages until the last active receive port is closed. | |
| 711 result = Dart_RunLoop(); | |
| 712 if (Dart_IsError(result)) { | |
| 713 return ErrorExit("%s\n", Dart_GetError(result)); | |
| 714 } | |
| 715 | |
| 716 Dart_ExitScope(); | 777 Dart_ExitScope(); |
| 717 // Shutdown the isolate. | 778 // Shutdown the isolate. |
| 718 Dart_ShutdownIsolate(); | 779 Dart_ShutdownIsolate(); |
| 719 // Terminate process exit-code handler. | 780 // Terminate process exit-code handler. |
| 720 Process::TerminateExitCodeHandler(); | 781 Process::TerminateExitCodeHandler(); |
| 721 // Free copied argument strings if converted. | 782 // Free copied argument strings if converted. |
| 722 if (argv_converted) { | 783 if (argv_converted) { |
| 723 for (int i = 0; i < argc; i++) free(argv[i]); | 784 for (int i = 0; i < argc; i++) free(argv[i]); |
| 724 } | 785 } |
| 725 | 786 |
| 726 return Process::GlobalExitCode(); | 787 return Process::GlobalExitCode(); |
| 727 } | 788 } |
| OLD | NEW |