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

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

Issue 12438033: Write the magic number 0xf5f5dcdc into a script snapshot file and use this to distinguish whether a… (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 | « runtime/bin/dartutils.cc ('k') | runtime/include/dart_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 11 matching lines...) Expand all
22 #include "bin/process.h" 22 #include "bin/process.h"
23 #include "bin/vmstats_impl.h" 23 #include "bin/vmstats_impl.h"
24 #include "platform/globals.h" 24 #include "platform/globals.h"
25 25
26 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise 26 // snapshot_buffer points to a snapshot if we link in a snapshot otherwise
27 // it is initialized to NULL. 27 // it is initialized to NULL.
28 extern const uint8_t* snapshot_buffer; 28 extern const uint8_t* snapshot_buffer;
29 29
30 30
31 // Global state that stores a pointer to the application script snapshot. 31 // Global state that stores a pointer to the application script snapshot.
32 static bool use_script_snapshot = false;
33 static bool generate_script_snapshot = false; 32 static bool generate_script_snapshot = false;
34 static File* snapshot_file = NULL; 33 static File* snapshot_file = NULL;
35 34
36 35
37 // Global state that indicates whether there is a debug breakpoint. 36 // Global state that indicates whether there is a debug breakpoint.
38 // 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
39 // free'd. 38 // free'd.
40 static const char* breakpoint_at = NULL; 39 static const char* breakpoint_at = NULL;
41 40
42 41
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 Log::PrintErr("unrecognized --debug option syntax. " 146 Log::PrintErr("unrecognized --debug option syntax. "
148 "Use --debug[:<port number>]\n"); 147 "Use --debug[:<port number>]\n");
149 return false; 148 return false;
150 } 149 }
151 breakpoint_at = "main"; 150 breakpoint_at = "main";
152 start_debugger = true; 151 start_debugger = true;
153 return true; 152 return true;
154 } 153 }
155 154
156 155
157 static bool ProcessUseScriptSnapshotOption(const char* filename) {
158 if (filename != NULL && strlen(filename) != 0) {
159 use_script_snapshot = true;
160 if (generate_script_snapshot) {
161 Log::PrintErr("Incompatible options specified --generate-script-snapshot "
162 "and --use-script-snapshot\n");
163 return false;
164 }
165 snapshot_file = File::Open(filename, File::kRead);
166 if (snapshot_file == NULL) {
167 Log::PrintErr("Unable to open file %s for reading the snapshot\n",
168 filename);
169 return false;
170 }
171 }
172 return true;
173 }
174
175
176 static bool ProcessVmStatsOption(const char* port) { 156 static bool ProcessVmStatsOption(const char* port) {
177 ASSERT(port != NULL); 157 ASSERT(port != NULL);
178 if (*port == '\0') { 158 if (*port == '\0') {
179 vmstats_port = 0; // Dynamically assigned port number. 159 vmstats_port = 0; // Dynamically assigned port number.
180 } else { 160 } else {
181 if ((*port == '=') || (*port == ':')) { 161 if ((*port == '=') || (*port == ':')) {
182 vmstats_port = atoi(port + 1); 162 vmstats_port = atoi(port + 1);
183 } 163 }
184 } 164 }
185 if (vmstats_port < 0) { 165 if (vmstats_port < 0) {
(...skipping 23 matching lines...) Expand all
209 189
210 190
211 static bool ProcessGenScriptSnapshotOption(const char* filename) { 191 static bool ProcessGenScriptSnapshotOption(const char* filename) {
212 if (filename != NULL && strlen(filename) != 0) { 192 if (filename != NULL && strlen(filename) != 0) {
213 // Ensure that are already running using a full snapshot. 193 // Ensure that are already running using a full snapshot.
214 if (snapshot_buffer == NULL) { 194 if (snapshot_buffer == NULL) {
215 Log::PrintErr("Script snapshots cannot be generated in this version of" 195 Log::PrintErr("Script snapshots cannot be generated in this version of"
216 " dart\n"); 196 " dart\n");
217 return false; 197 return false;
218 } 198 }
219 if (use_script_snapshot) {
220 Log::PrintErr("Incompatible options specified --use-script-snapshot "
221 "and --generate-script-snapshot\n");
222 return false;
223 }
224 snapshot_file = File::Open(filename, File::kWriteTruncate); 199 snapshot_file = File::Open(filename, File::kWriteTruncate);
225 if (snapshot_file == NULL) { 200 if (snapshot_file == NULL) {
226 Log::PrintErr("Unable to open file %s for writing the snapshot\n", 201 Log::PrintErr("Unable to open file %s for writing the snapshot\n",
227 filename); 202 filename);
228 return false; 203 return false;
229 } 204 }
230 generate_script_snapshot = true; 205 generate_script_snapshot = true;
231 } 206 }
232 return true; 207 return true;
233 } 208 }
234 209
235 210
236 static struct { 211 static struct {
237 const char* option_name; 212 const char* option_name;
238 bool (*process)(const char* option); 213 bool (*process)(const char* option);
239 } main_options[] = { 214 } main_options[] = {
240 // Standard options shared with dart2js. 215 // Standard options shared with dart2js.
241 { "--version", ProcessVersionOption }, 216 { "--version", ProcessVersionOption },
242 { "--help", ProcessHelpOption }, 217 { "--help", ProcessHelpOption },
243 { "-h", ProcessHelpOption }, 218 { "-h", ProcessHelpOption },
244 { "--verbose", ProcessVerboseOption }, 219 { "--verbose", ProcessVerboseOption },
245 { "-v", ProcessVerboseOption }, 220 { "-v", ProcessVerboseOption },
246 { "--package-root=", ProcessPackageRootOption }, 221 { "--package-root=", ProcessPackageRootOption },
247 { "-p", ProcessPackageRootOption }, 222 { "-p", ProcessPackageRootOption },
248 // VM specific options to the standalone dart program. 223 // VM specific options to the standalone dart program.
249 { "--break_at=", ProcessBreakpointOption }, 224 { "--break_at=", ProcessBreakpointOption },
250 { "--compile_all", ProcessCompileAllOption }, 225 { "--compile_all", ProcessCompileAllOption },
251 { "--debug", ProcessDebugOption }, 226 { "--debug", ProcessDebugOption },
252 { "--use-script-snapshot=", ProcessUseScriptSnapshotOption },
253 { "--generate-script-snapshot=", ProcessGenScriptSnapshotOption }, 227 { "--generate-script-snapshot=", ProcessGenScriptSnapshotOption },
254 { "--stats-root=", ProcessVmStatsRootOption }, 228 { "--stats-root=", ProcessVmStatsRootOption },
255 { "--stats", ProcessVmStatsOption }, 229 { "--stats", ProcessVmStatsOption },
256 { "--print-script", ProcessPrintScriptOption }, 230 { "--print-script", ProcessPrintScriptOption },
257 { NULL, NULL } 231 { NULL, NULL }
258 }; 232 };
259 233
260 234
261 static bool ProcessMainOptions(const char* option) { 235 static bool ProcessMainOptions(const char* option) {
262 int i = 0; 236 int i = 0;
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 // Setup the native resolver as the snapshot does not carry it. 443 // Setup the native resolver as the snapshot does not carry it.
470 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); 444 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
471 Builtin::SetNativeResolver(Builtin::kIOLibrary); 445 Builtin::SetNativeResolver(Builtin::kIOLibrary);
472 } 446 }
473 447
474 // Set up the library tag handler for this isolate. 448 // Set up the library tag handler for this isolate.
475 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); 449 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler);
476 CHECK_RESULT(result); 450 CHECK_RESULT(result);
477 451
478 // Load the specified application script into the newly created isolate. 452 // Load the specified application script into the newly created isolate.
479 Dart_Handle library;
480 if (use_script_snapshot) {
481 if (snapshot_file == NULL) {
482 use_script_snapshot = false;
483 *error = strdup("Invalid script snapshot file name specified");
484 Dart_ExitScope();
485 Dart_ShutdownIsolate();
486 return false;
487 }
488 size_t len = snapshot_file->Length();
489 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(len));
490 if (buffer == NULL) {
491 delete snapshot_file;
492 snapshot_file = NULL;
493 use_script_snapshot = false;
494 *error = strdup("Unable to read contents of script snapshot file");
495 Dart_ExitScope();
496 Dart_ShutdownIsolate();
497 return false;
498 }
499 // Prepare for script loading by setting up the 'print' and 'timer'
500 // closures and setting up 'package root' for URI resolution.
501 Dart_Handle builtin_lib =
502 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
503 DartUtils::PrepareForScriptLoading(package_root, builtin_lib);
504 453
505 snapshot_file->ReadFully(buffer, len); 454 // Prepare builtin and its dependent libraries for use to resolve URIs.
506 library = Dart_LoadScriptFromSnapshot(buffer); 455 // The builtin library is part of the core snapshot and would already be
507 free(buffer); 456 // available here in the case of script snapshot loading.
508 delete snapshot_file; 457 Dart_Handle uri_url = DartUtils::NewString(DartUtils::kUriLibURL);
509 snapshot_file = NULL; 458 Dart_Handle uri_lib = Dart_LookupLibrary(uri_url);
510 use_script_snapshot = false; // No further usage of script snapshots. 459 CHECK_RESULT(uri_lib);
511 } else { 460 Dart_Handle builtin_lib =
512 // Prepare builtin and its dependent libraries for use to resolve URIs. 461 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
513 Dart_Handle uri_url = DartUtils::NewString(DartUtils::kUriLibURL); 462 CHECK_RESULT(builtin_lib);
514 Dart_Handle uri_lib = Dart_LookupLibrary(uri_url);
515 CHECK_RESULT(uri_lib);
516 Dart_Handle builtin_lib =
517 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
518 CHECK_RESULT(builtin_lib);
519 463
520 // Prepare for script loading by setting up the 'print' and 'timer' 464 // Prepare for script loading by setting up the 'print' and 'timer'
521 // closures and setting up 'package root' for URI resolution. 465 // closures and setting up 'package root' for URI resolution.
522 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); 466 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib);
523 CHECK_RESULT(result); 467 CHECK_RESULT(result);
524 468
525 library = DartUtils::LoadScript(script_uri, builtin_lib); 469 Dart_Handle library = DartUtils::LoadScript(script_uri, builtin_lib);
526 }
527 CHECK_RESULT(library); 470 CHECK_RESULT(library);
528 if (!Dart_IsLibrary(library)) { 471 if (!Dart_IsLibrary(library)) {
529 char errbuf[256]; 472 char errbuf[256];
530 snprintf(errbuf, sizeof(errbuf), 473 snprintf(errbuf, sizeof(errbuf),
531 "Expected a library when loading script: %s", 474 "Expected a library when loading script: %s",
532 script_uri); 475 script_uri);
533 *error = strdup(errbuf); 476 *error = strdup(errbuf);
534 Dart_ExitScope(); 477 Dart_ExitScope();
535 Dart_ShutdownIsolate(); 478 Dart_ShutdownIsolate();
536 return false; 479 return false;
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 uint8_t* buffer = NULL; 761 uint8_t* buffer = NULL;
819 intptr_t size = 0; 762 intptr_t size = 0;
820 result = Dart_CreateScriptSnapshot(&buffer, &size); 763 result = Dart_CreateScriptSnapshot(&buffer, &size);
821 if (Dart_IsError(result)) { 764 if (Dart_IsError(result)) {
822 Log::PrintErr("%s\n", Dart_GetError(result)); 765 Log::PrintErr("%s\n", Dart_GetError(result));
823 Dart_ExitScope(); 766 Dart_ExitScope();
824 Dart_ShutdownIsolate(); 767 Dart_ShutdownIsolate();
825 return kErrorExitCode; // Indicates we encountered an error. 768 return kErrorExitCode; // Indicates we encountered an error.
826 } 769 }
827 770
771 // Write the magic number to indicate file is a script snapshot.
772 DartUtils::WriteMagicNumber(snapshot_file);
773
828 // Now write the snapshot out to specified file. 774 // Now write the snapshot out to specified file.
829 bool bytes_written = snapshot_file->WriteFully(buffer, size); 775 bool bytes_written = snapshot_file->WriteFully(buffer, size);
830 ASSERT(bytes_written); 776 ASSERT(bytes_written);
831 delete snapshot_file; 777 delete snapshot_file;
832 } else { 778 } else {
833 if (has_compile_all) { 779 if (has_compile_all) {
834 result = Dart_CompileAll(); 780 result = Dart_CompileAll();
835 if (Dart_IsError(result)) { 781 if (Dart_IsError(result)) {
836 return ErrorExit("%s\n", Dart_GetError(result)); 782 return ErrorExit("%s\n", Dart_GetError(result));
837 } 783 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
885 Dart_ShutdownIsolate(); 831 Dart_ShutdownIsolate();
886 // Terminate process exit-code handler. 832 // Terminate process exit-code handler.
887 Process::TerminateExitCodeHandler(); 833 Process::TerminateExitCodeHandler();
888 // Free copied argument strings if converted. 834 // Free copied argument strings if converted.
889 if (argv_converted) { 835 if (argv_converted) {
890 for (int i = 0; i < argc; i++) free(argv[i]); 836 for (int i = 0; i < argc; i++) free(argv[i]);
891 } 837 }
892 838
893 return Process::GlobalExitCode(); 839 return Process::GlobalExitCode();
894 } 840 }
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | runtime/include/dart_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698