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

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

Issue 12517005: Add a command line option to generate source code for a script (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 9 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 | « no previous file | runtime/include/dart_debugger_api.h » ('j') | 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) 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 // (This pointer points into an argv buffer and does not need to be 56 // (This pointer points into an argv buffer and does not need to be
57 // free'd.) 57 // free'd.)
58 static const char* package_root = NULL; 58 static const char* package_root = NULL;
59 59
60 60
61 // Global flag that is used to indicate that we want to compile all the 61 // Global flag that is used to indicate that we want to compile all the
62 // dart functions and not run anything. 62 // dart functions and not run anything.
63 static bool has_compile_all = false; 63 static bool has_compile_all = false;
64 64
65 65
66 // Global flag that is used to indicate that we want to print the source code
67 // for script that is being run.
68 static bool has_print_script = false;
69
70
66 static bool IsValidFlag(const char* name, 71 static bool IsValidFlag(const char* name,
67 const char* prefix, 72 const char* prefix,
68 intptr_t prefix_length) { 73 intptr_t prefix_length) {
69 intptr_t name_length = strlen(name); 74 intptr_t name_length = strlen(name);
70 return ((name_length > prefix_length) && 75 return ((name_length > prefix_length) &&
71 (strncmp(name, prefix, prefix_length) == 0)); 76 (strncmp(name, prefix, prefix_length) == 0));
72 } 77 }
73 78
74 79
75 static bool has_version_option = false; 80 static bool has_version_option = false;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } 184 }
180 if (vmstats_port < 0) { 185 if (vmstats_port < 0) {
181 Log::PrintErr("unrecognized --stats option syntax. " 186 Log::PrintErr("unrecognized --stats option syntax. "
182 "Use --stats[:<port number>]\n"); 187 "Use --stats[:<port number>]\n");
183 return false; 188 return false;
184 } 189 }
185 return true; 190 return true;
186 } 191 }
187 192
188 193
194 static bool ProcessPrintScriptOption(const char* arg) {
195 ASSERT(arg != NULL);
196 if (*arg != '\0') {
197 return false;
198 }
199 has_print_script = true;
200 return true;
201 }
202
203
189 static bool ProcessVmStatsRootOption(const char* arg) { 204 static bool ProcessVmStatsRootOption(const char* arg) {
190 ASSERT(arg != NULL); 205 ASSERT(arg != NULL);
191 vmstats_root = arg; 206 vmstats_root = arg;
192 return true; 207 return true;
193 } 208 }
194 209
195 210
196 static bool ProcessGenScriptSnapshotOption(const char* filename) { 211 static bool ProcessGenScriptSnapshotOption(const char* filename) {
197 if (filename != NULL && strlen(filename) != 0) { 212 if (filename != NULL && strlen(filename) != 0) {
198 // Ensure that are already running using a full snapshot. 213 // Ensure that are already running using a full snapshot.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 { "--package-root=", ProcessPackageRootOption }, 246 { "--package-root=", ProcessPackageRootOption },
232 { "-p", ProcessPackageRootOption }, 247 { "-p", ProcessPackageRootOption },
233 // VM specific options to the standalone dart program. 248 // VM specific options to the standalone dart program.
234 { "--break_at=", ProcessBreakpointOption }, 249 { "--break_at=", ProcessBreakpointOption },
235 { "--compile_all", ProcessCompileAllOption }, 250 { "--compile_all", ProcessCompileAllOption },
236 { "--debug", ProcessDebugOption }, 251 { "--debug", ProcessDebugOption },
237 { "--use-script-snapshot=", ProcessUseScriptSnapshotOption }, 252 { "--use-script-snapshot=", ProcessUseScriptSnapshotOption },
238 { "--generate-script-snapshot=", ProcessGenScriptSnapshotOption }, 253 { "--generate-script-snapshot=", ProcessGenScriptSnapshotOption },
239 { "--stats-root=", ProcessVmStatsRootOption }, 254 { "--stats-root=", ProcessVmStatsRootOption },
240 { "--stats", ProcessVmStatsOption }, 255 { "--stats", ProcessVmStatsOption },
256 { "--print-script", ProcessPrintScriptOption },
241 { NULL, NULL } 257 { NULL, NULL }
242 }; 258 };
243 259
244 260
245 static bool ProcessMainOptions(const char* option) { 261 static bool ProcessMainOptions(const char* option) {
246 int i = 0; 262 int i = 0;
247 const char* name = main_options[0].option_name; 263 const char* name = main_options[0].option_name;
248 while (name != NULL) { 264 while (name != NULL) {
249 int length = strlen(name); 265 int length = strlen(name);
250 if (strncmp(option, name, length) == 0) { 266 if (strncmp(option, name, length) == 0) {
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 " sets a breakpoint at specified location where <location> is one of :\n" 592 " sets a breakpoint at specified location where <location> is one of :\n"
577 " url:<line_num> e.g. test.dart:10\n" 593 " url:<line_num> e.g. test.dart:10\n"
578 " [<class_name>.]<function_name> e.g. B.foo\n" 594 " [<class_name>.]<function_name> e.g. B.foo\n"
579 "\n" 595 "\n"
580 "--use-script-snapshot=<file_name>\n" 596 "--use-script-snapshot=<file_name>\n"
581 " executes Dart script present in the specified snapshot file\n" 597 " executes Dart script present in the specified snapshot file\n"
582 "\n" 598 "\n"
583 "--generate-script-snapshot=<file_name>\n" 599 "--generate-script-snapshot=<file_name>\n"
584 " loads Dart script and generates a snapshot in the specified file\n" 600 " loads Dart script and generates a snapshot in the specified file\n"
585 "\n" 601 "\n"
602 "--print-source\n"
603 " generates Dart source code back and prints it after parsing a Dart script\n"
604 "\n"
586 "--stats[:<port number>]\n" 605 "--stats[:<port number>]\n"
587 " enables VM stats service and listens on specified port for HTTP requests\n" 606 " enables VM stats service and listens on specified port for HTTP requests\n"
588 " (default port number is dynamically assigned)\n" 607 " (default port number is dynamically assigned)\n"
589 "\n" 608 "\n"
590 "--stats-root=<path>\n" 609 "--stats-root=<path>\n"
591 " where to find static files used by the vmstats application\n" 610 " where to find static files used by the vmstats application\n"
592 " (used during vmstats plug-in development)\n" 611 " (used during vmstats plug-in development)\n"
593 "\n" 612 "\n"
594 "The following options are only used for VM development and may\n" 613 "The following options are only used for VM development and may\n"
595 "be changed in any future version:\n"); 614 "be changed in any future version:\n");
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 687
669 static void ShutdownIsolate(void* callback_data) { 688 static void ShutdownIsolate(void* callback_data) {
670 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data); 689 IsolateData* isolate_data = reinterpret_cast<IsolateData*>(callback_data);
671 VmStats::RemoveIsolate(isolate_data); 690 VmStats::RemoveIsolate(isolate_data);
672 EventHandler* handler = isolate_data->event_handler; 691 EventHandler* handler = isolate_data->event_handler;
673 if (handler != NULL) handler->Shutdown(); 692 if (handler != NULL) handler->Shutdown();
674 delete isolate_data; 693 delete isolate_data;
675 } 694 }
676 695
677 696
697 static Dart_Handle GenerateScriptSource() {
698 Dart_Handle library_url = Dart_LibraryUrl(Dart_RootLibrary());
699 if (Dart_IsError(library_url)) {
700 return library_url;
701 }
702 Dart_Handle script_urls = Dart_GetScriptURLs(library_url);
703 if (Dart_IsError(script_urls)) {
704 return script_urls;
705 }
706 intptr_t length;
707 Dart_Handle result = Dart_ListLength(script_urls, &length);
708 if (Dart_IsError(result)) {
709 return result;
710 }
711 for (intptr_t i = 0; i < length; i++) {
712 Dart_Handle script_url = Dart_ListGetAt(script_urls, i);
713 if (Dart_IsError(script_url)) {
714 return script_url;
715 }
716 result = Dart_GenerateScriptSource(library_url, script_url);
717 if (Dart_IsError(result)) {
718 return result;
719 }
720 const char* script_source = NULL;
721 result = Dart_StringToCString(result, &script_source);
722 if (Dart_IsError(result)) {
723 return result;
724 }
725 Log::Print("%s\n", script_source);
726 }
727 return Dart_True();
728 }
729
730
678 int main(int argc, char** argv) { 731 int main(int argc, char** argv) {
679 char* executable_name; 732 char* executable_name;
680 char* script_name; 733 char* script_name;
681 CommandLineOptions vm_options(argc); 734 CommandLineOptions vm_options(argc);
682 CommandLineOptions dart_options(argc); 735 CommandLineOptions dart_options(argc);
683 bool print_flags_seen = false; 736 bool print_flags_seen = false;
684 737
685 // Perform platform specific initialization. 738 // Perform platform specific initialization.
686 if (!Platform::Initialize()) { 739 if (!Platform::Initialize()) {
687 Log::PrintErr("Initialization failed\n"); 740 Log::PrintErr("Initialization failed\n");
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 } 851 }
799 // Set debug breakpoint if specified on the command line. 852 // Set debug breakpoint if specified on the command line.
800 if (breakpoint_at != NULL) { 853 if (breakpoint_at != NULL) {
801 result = SetBreakpoint(breakpoint_at, library); 854 result = SetBreakpoint(breakpoint_at, library);
802 if (Dart_IsError(result)) { 855 if (Dart_IsError(result)) {
803 return ErrorExit("Error setting breakpoint at '%s': %s\n", 856 return ErrorExit("Error setting breakpoint at '%s': %s\n",
804 breakpoint_at, 857 breakpoint_at,
805 Dart_GetError(result)); 858 Dart_GetError(result));
806 } 859 }
807 } 860 }
861 if (has_print_script) {
862 result = GenerateScriptSource();
863 if (Dart_IsError(result)) {
864 return ErrorExit("%s\n", Dart_GetError(result));
865 }
866 }
808 867
809 // Lookup and invoke the top level main function. 868 // Lookup and invoke the top level main function.
810 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); 869 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL);
811 if (Dart_IsError(result)) { 870 if (Dart_IsError(result)) {
812 return ErrorExit("%s\n", Dart_GetError(result)); 871 return ErrorExit("%s\n", Dart_GetError(result));
813 } 872 }
814 // Keep handling messages until the last active receive port is closed. 873 // Keep handling messages until the last active receive port is closed.
815 result = Dart_RunLoop(); 874 result = Dart_RunLoop();
816 if (Dart_IsError(result)) { 875 if (Dart_IsError(result)) {
817 return ErrorExit("%s\n", Dart_GetError(result)); 876 return ErrorExit("%s\n", Dart_GetError(result));
818 } 877 }
819 } 878 }
820 879
821 Dart_ExitScope(); 880 Dart_ExitScope();
822 if (vmstats_port >= 0) { 881 if (vmstats_port >= 0) {
823 VmStats::Stop(); 882 VmStats::Stop();
824 } 883 }
825 // Shutdown the isolate. 884 // Shutdown the isolate.
826 Dart_ShutdownIsolate(); 885 Dart_ShutdownIsolate();
827 // Terminate process exit-code handler. 886 // Terminate process exit-code handler.
828 Process::TerminateExitCodeHandler(); 887 Process::TerminateExitCodeHandler();
829 // Free copied argument strings if converted. 888 // Free copied argument strings if converted.
830 if (argv_converted) { 889 if (argv_converted) {
831 for (int i = 0; i < argc; i++) free(argv[i]); 890 for (int i = 0; i < argc; i++) free(argv[i]);
832 } 891 }
833 892
834 return Process::GlobalExitCode(); 893 return Process::GlobalExitCode();
835 } 894 }
OLDNEW
« no previous file with comments | « no previous file | runtime/include/dart_debugger_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698