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

Side by Side Diff: bin/main.cc

Issue 11953115: Add --generate-script-snapshot option to dart so that this binary can be used for generating script… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 7 years, 11 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 | no next file » | 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 11 matching lines...) Expand all
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
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 (snapshot_buffer == NULL) {
171 Log::PrintErr("Script snapshots cannot be generated in this version of"
172 " dart\n");
173 return false;
174 }
175 if (use_script_snapshot) {
176 Log::PrintErr("Incompatible options specified --use_script_snapshot "
177 "and --generate_script_snapshot\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 },
173 { NULL, NULL } 211 { NULL, NULL }
174 }; 212 };
175 213
176 214
177 static bool ProcessMainOptions(const char* option) { 215 static bool ProcessMainOptions(const char* option) {
178 int i = 0; 216 int i = 0;
179 const char* name = main_options[0].option_name; 217 const char* name = main_options[0].option_name;
180 while (name != NULL) { 218 while (name != NULL) {
181 int length = strlen(name); 219 int length = strlen(name);
182 if (strncmp(option, name, length) == 0) { 220 if (strncmp(option, name, length) == 0) {
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 " (default port number is 5858)\n" 541 " (default port number is 5858)\n"
504 "\n" 542 "\n"
505 "--break_at=<location>\n" 543 "--break_at=<location>\n"
506 " sets a breakpoint at specified location where <location> is one of :\n" 544 " sets a breakpoint at specified location where <location> is one of :\n"
507 " url:<line_num> e.g. test.dart:10\n" 545 " url:<line_num> e.g. test.dart:10\n"
508 " [<class_name>.]<function_name> e.g. B.foo\n" 546 " [<class_name>.]<function_name> e.g. B.foo\n"
509 "\n" 547 "\n"
510 "--use_script_snapshot=<file_name>\n" 548 "--use_script_snapshot=<file_name>\n"
511 " executes Dart script present in the specified snapshot file\n" 549 " executes Dart script present in the specified snapshot file\n"
512 "\n" 550 "\n"
551 "--generate_script_snapshot=<file_name>\n"
552 " loads Dart script and generates a snapshot in the specified file\n"
553 "\n"
513 "The following options are only used for VM development and may\n" 554 "The following options are only used for VM development and may\n"
514 "be changed in any future version:\n"); 555 "be changed in any future version:\n");
515 const char* print_flags = "--print_flags"; 556 const char* print_flags = "--print_flags";
516 Dart_SetVMFlags(1, &print_flags); 557 Dart_SetVMFlags(1, &print_flags);
517 } 558 }
518 } 559 }
519 560
520 561
521 static Dart_Handle SetBreakpoint(const char* breakpoint_at, 562 static Dart_Handle SetBreakpoint(const char* breakpoint_at,
522 Dart_Handle library) { 563 Dart_Handle library) {
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 return kErrorExitCode; // Indicates we encountered an error. 707 return kErrorExitCode; // Indicates we encountered an error.
667 } 708 }
668 delete [] isolate_name; 709 delete [] isolate_name;
669 710
670 Dart_Isolate isolate = Dart_CurrentIsolate(); 711 Dart_Isolate isolate = Dart_CurrentIsolate();
671 ASSERT(isolate != NULL); 712 ASSERT(isolate != NULL);
672 Dart_Handle result; 713 Dart_Handle result;
673 714
674 Dart_EnterScope(); 715 Dart_EnterScope();
675 716
676 if (has_compile_all) { 717 if (generate_script_snapshot) {
677 result = Dart_CompileAll(); 718 // First create a snapshot.
719 Dart_Handle result;
720 uint8_t* buffer = NULL;
721 intptr_t size = 0;
722 result = Dart_CreateScriptSnapshot(&buffer, &size);
723 if (Dart_IsError(result)) {
724 Log::PrintErr("%s\n", Dart_GetError(result));
725 Dart_ExitScope();
726 Dart_ShutdownIsolate();
727 return kErrorExitCode; // Indicates we encountered an error.
728 }
729
730 // Now write the snapshot out to specified file.
731 bool bytes_written = snapshot_file->WriteFully(buffer, size);
732 ASSERT(bytes_written);
733 delete snapshot_file;
734 } else {
735 if (has_compile_all) {
736 result = Dart_CompileAll();
737 if (Dart_IsError(result)) {
738 return ErrorExit("%s\n", Dart_GetError(result));
739 }
740 }
741
742 // Create a dart options object that can be accessed from dart code.
743 Dart_Handle options_result =
744 SetupRuntimeOptions(&dart_options, executable_name, script_name);
745 if (Dart_IsError(options_result)) {
746 return ErrorExit("%s\n", Dart_GetError(options_result));
747 }
748 // Lookup the library of the root script.
749 Dart_Handle library = Dart_RootLibrary();
750 if (Dart_IsNull(library)) {
751 return ErrorExit("Unable to find root library for '%s'\n",
752 script_name);
753 }
754 // Set debug breakpoint if specified on the command line.
755 if (breakpoint_at != NULL) {
756 result = SetBreakpoint(breakpoint_at, library);
757 if (Dart_IsError(result)) {
758 return ErrorExit("Error setting breakpoint at '%s': %s\n",
759 breakpoint_at,
760 Dart_GetError(result));
761 }
762 }
763
764 // Lookup and invoke the top level main function.
765 result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL);
766 if (Dart_IsError(result)) {
767 return ErrorExit("%s\n", Dart_GetError(result));
768 }
769 // Keep handling messages until the last active receive port is closed.
770 result = Dart_RunLoop();
678 if (Dart_IsError(result)) { 771 if (Dart_IsError(result)) {
679 return ErrorExit("%s\n", Dart_GetError(result)); 772 return ErrorExit("%s\n", Dart_GetError(result));
680 } 773 }
681 } 774 }
682 775
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(); 776 Dart_ExitScope();
717 // Shutdown the isolate. 777 // Shutdown the isolate.
718 Dart_ShutdownIsolate(); 778 Dart_ShutdownIsolate();
719 // Terminate process exit-code handler. 779 // Terminate process exit-code handler.
720 Process::TerminateExitCodeHandler(); 780 Process::TerminateExitCodeHandler();
721 // Free copied argument strings if converted. 781 // Free copied argument strings if converted.
722 if (argv_converted) { 782 if (argv_converted) {
723 for (int i = 0; i < argc; i++) free(argv[i]); 783 for (int i = 0; i < argc; i++) free(argv[i]);
724 } 784 }
725 785
726 return Process::GlobalExitCode(); 786 return Process::GlobalExitCode();
727 } 787 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698