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

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') | runtime/include/dart_debugger_api.h » ('J')
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 },
bakster 2013/03/14 08:42:16 Why are some flags using - and others using _? How
siva 2013/03/14 09:38:46 We are moving to a "-" pattern. Will change the ot
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"
bakster 2013/03/14 08:42:16 Consistency: use lowercase "generates"?
siva 2013/03/14 09:38:46 Done.
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 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 } 817 }
799 // Set debug breakpoint if specified on the command line. 818 // Set debug breakpoint if specified on the command line.
800 if (breakpoint_at != NULL) { 819 if (breakpoint_at != NULL) {
801 result = SetBreakpoint(breakpoint_at, library); 820 result = SetBreakpoint(breakpoint_at, library);
802 if (Dart_IsError(result)) { 821 if (Dart_IsError(result)) {
803 return ErrorExit("Error setting breakpoint at '%s': %s\n", 822 return ErrorExit("Error setting breakpoint at '%s': %s\n",
804 breakpoint_at, 823 breakpoint_at,
805 Dart_GetError(result)); 824 Dart_GetError(result));
806 } 825 }
807 } 826 }
827 if (has_print_script) {
bakster 2013/03/14 08:42:16 I would move this chunk of code out in a function.
siva 2013/03/14 09:38:46 Done.
828 Dart_Handle library_url = Dart_LibraryUrl(Dart_RootLibrary());
829 if (Dart_IsError(library_url)) {
830 return ErrorExit("%s\n", Dart_GetError(library_url));
831 }
832 Dart_Handle script_urls = Dart_GetScriptURLs(library_url);
833 if (Dart_IsError(script_urls)) {
834 return ErrorExit("%s\n", Dart_GetError(script_urls));
835 }
836 intptr_t length;
837 result = Dart_ListLength(script_urls, &length);
838 if (Dart_IsError(result)) {
839 return ErrorExit("%s\n", Dart_GetError(result));
840 }
841 for (intptr_t i = 0; i < length; i++) {
842 Dart_Handle script_url = Dart_ListGetAt(script_urls, i);
843 if (Dart_IsError(script_url)) {
844 return ErrorExit("%s\n", Dart_GetError(script_url));
845 }
846 result = Dart_GenerateScriptSource(library_url, script_url);
847 if (Dart_IsError(result)) {
848 return ErrorExit("%s\n", Dart_GetError(result));
849 }
850 const char* script_source = NULL;
851 result = Dart_StringToCString(result, &script_source);
852 if (Dart_IsError(result)) {
853 return ErrorExit("%s\n", Dart_GetError(result));
854 }
855 Log::Print("%s\n", script_source);
856 }
857 }
808 858
809 // Lookup and invoke the top level main function. 859 // Lookup and invoke the top level main function.
810 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); 860 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL);
811 if (Dart_IsError(result)) { 861 if (Dart_IsError(result)) {
812 return ErrorExit("%s\n", Dart_GetError(result)); 862 return ErrorExit("%s\n", Dart_GetError(result));
813 } 863 }
814 // Keep handling messages until the last active receive port is closed. 864 // Keep handling messages until the last active receive port is closed.
815 result = Dart_RunLoop(); 865 result = Dart_RunLoop();
816 if (Dart_IsError(result)) { 866 if (Dart_IsError(result)) {
817 return ErrorExit("%s\n", Dart_GetError(result)); 867 return ErrorExit("%s\n", Dart_GetError(result));
818 } 868 }
819 } 869 }
820 870
821 Dart_ExitScope(); 871 Dart_ExitScope();
822 if (vmstats_port >= 0) { 872 if (vmstats_port >= 0) {
823 VmStats::Stop(); 873 VmStats::Stop();
824 } 874 }
825 // Shutdown the isolate. 875 // Shutdown the isolate.
826 Dart_ShutdownIsolate(); 876 Dart_ShutdownIsolate();
827 // Terminate process exit-code handler. 877 // Terminate process exit-code handler.
828 Process::TerminateExitCodeHandler(); 878 Process::TerminateExitCodeHandler();
829 // Free copied argument strings if converted. 879 // Free copied argument strings if converted.
830 if (argv_converted) { 880 if (argv_converted) {
831 for (int i = 0; i < argc; i++) free(argv[i]); 881 for (int i = 0; i < argc; i++) free(argv[i]);
832 } 882 }
833 883
834 return Process::GlobalExitCode(); 884 return Process::GlobalExitCode();
835 } 885 }
OLDNEW
« no previous file with comments | « no previous file | runtime/include/dart_debugger_api.h » ('j') | runtime/include/dart_debugger_api.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698