| 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_tools_api.h" | 10 #include "include/dart_tools_api.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 // Global flag that is used to indicate that we want to compile all the | 64 // Global flag that is used to indicate that we want to compile all the |
| 65 // dart functions before running main and not compile anything thereafter. | 65 // dart functions before running main and not compile anything thereafter. |
| 66 static bool has_gen_precompiled_snapshot = false; | 66 static bool has_gen_precompiled_snapshot = false; |
| 67 | 67 |
| 68 | 68 |
| 69 // Global flag that is used to indicate that we want to run from a precompiled | 69 // Global flag that is used to indicate that we want to run from a precompiled |
| 70 // snapshot. | 70 // snapshot. |
| 71 static bool has_run_precompiled_snapshot = false; | 71 static bool has_run_precompiled_snapshot = false; |
| 72 | 72 |
| 73 | 73 |
| 74 // Value of the --gen/run_precompiled_snapshot flag. |
| 75 // (This pointer points into an argv buffer and does not need to be |
| 76 // free'd.) |
| 77 static const char* precompiled_snapshot_directory = NULL; |
| 78 |
| 79 |
| 74 // Global flag that is used to indicate that we want to compile everything in | 80 // Global flag that is used to indicate that we want to compile everything in |
| 75 // the same way as precompilation before main, then continue running in the | 81 // the same way as precompilation before main, then continue running in the |
| 76 // same process. | 82 // same process. |
| 77 static bool has_noopt = false; | 83 static bool has_noopt = false; |
| 78 | 84 |
| 79 | 85 |
| 80 extern const char* kPrecompiledLibraryName; | 86 extern const char* kPrecompiledLibraryName; |
| 81 extern const char* kPrecompiledSymbolName; | 87 extern const char* kPrecompiledSymbolName; |
| 82 static const char* kPrecompiledVmIsolateName = "precompiled.vmisolate"; | 88 static const char* kPrecompiledVmIsolateName = "precompiled.vmisolate"; |
| 83 static const char* kPrecompiledIsolateName = "precompiled.isolate"; | 89 static const char* kPrecompiledIsolateName = "precompiled.isolate"; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 return false; | 303 return false; |
| 298 } | 304 } |
| 299 has_compile_all = true; | 305 has_compile_all = true; |
| 300 return true; | 306 return true; |
| 301 } | 307 } |
| 302 | 308 |
| 303 | 309 |
| 304 static bool ProcessGenPrecompiledSnapshotOption( | 310 static bool ProcessGenPrecompiledSnapshotOption( |
| 305 const char* arg, | 311 const char* arg, |
| 306 CommandLineOptions* vm_options) { | 312 CommandLineOptions* vm_options) { |
| 307 ASSERT(arg != NULL); | |
| 308 if (*arg != '\0') { | |
| 309 return false; | |
| 310 } | |
| 311 // Ensure that we are not already running using a full snapshot. | 313 // Ensure that we are not already running using a full snapshot. |
| 312 if (isolate_snapshot_buffer != NULL) { | 314 if (isolate_snapshot_buffer != NULL) { |
| 313 Log::PrintErr("Precompiled snapshots must be generated with" | 315 Log::PrintErr("Precompiled snapshots must be generated with" |
| 314 " dart_no_snapshot.\n"); | 316 " dart_no_snapshot.\n"); |
| 315 return false; | 317 return false; |
| 316 } | 318 } |
| 319 ASSERT(arg != NULL); |
| 320 if ((arg[0] == '=') || (arg[0] == ':')) { |
| 321 precompiled_snapshot_directory = &arg[1]; |
| 322 } else { |
| 323 precompiled_snapshot_directory = arg; |
| 324 } |
| 317 has_gen_precompiled_snapshot = true; | 325 has_gen_precompiled_snapshot = true; |
| 318 vm_options->AddArgument("--precompilation"); | 326 vm_options->AddArgument("--precompilation"); |
| 319 return true; | 327 return true; |
| 320 } | 328 } |
| 321 | 329 |
| 322 | 330 |
| 323 static bool ProcessRunPrecompiledSnapshotOption( | 331 static bool ProcessRunPrecompiledSnapshotOption( |
| 324 const char* arg, | 332 const char* arg, |
| 325 CommandLineOptions* vm_options) { | 333 CommandLineOptions* vm_options) { |
| 326 ASSERT(arg != NULL); | 334 ASSERT(arg != NULL); |
| 327 if (*arg != '\0') { | 335 precompiled_snapshot_directory = arg; |
| 328 return false; | 336 if ((precompiled_snapshot_directory[0] == '=') || |
| 337 (precompiled_snapshot_directory[0] == ':')) { |
| 338 precompiled_snapshot_directory = &precompiled_snapshot_directory[1]; |
| 329 } | 339 } |
| 330 has_run_precompiled_snapshot = true; | 340 has_run_precompiled_snapshot = true; |
| 331 vm_options->AddArgument("--precompilation"); | 341 vm_options->AddArgument("--precompilation"); |
| 332 return true; | 342 return true; |
| 333 } | 343 } |
| 334 | 344 |
| 335 | 345 |
| 336 static bool ProcessNooptOption( | 346 static bool ProcessNooptOption( |
| 337 const char* arg, | 347 const char* arg, |
| 338 CommandLineOptions* vm_options) { | 348 CommandLineOptions* vm_options) { |
| (...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1002 | 1012 |
| 1003 static void ServiceStreamCancelCallback(const char* stream_id) { | 1013 static void ServiceStreamCancelCallback(const char* stream_id) { |
| 1004 if (strcmp(stream_id, kStdoutStreamId) == 0) { | 1014 if (strcmp(stream_id, kStdoutStreamId) == 0) { |
| 1005 SetCaptureStdout(false); | 1015 SetCaptureStdout(false); |
| 1006 } else if (strcmp(stream_id, kStderrStreamId) == 0) { | 1016 } else if (strcmp(stream_id, kStderrStreamId) == 0) { |
| 1007 SetCaptureStderr(false); | 1017 SetCaptureStderr(false); |
| 1008 } | 1018 } |
| 1009 } | 1019 } |
| 1010 | 1020 |
| 1011 | 1021 |
| 1012 static void WriteSnapshotFile(const char* filename, | 1022 static void WritePrecompiledSnapshotFile(const char* filename, |
| 1013 const uint8_t* buffer, | 1023 const uint8_t* buffer, |
| 1014 const intptr_t size) { | 1024 const intptr_t size) { |
| 1015 File* file = File::Open(filename, File::kWriteTruncate); | 1025 char* concat = NULL; |
| 1026 const char* qualified_filename; |
| 1027 if (strlen(precompiled_snapshot_directory) > 0) { |
| 1028 intptr_t len = snprintf(NULL, 0, "%s/%s", |
| 1029 precompiled_snapshot_directory, filename); |
| 1030 concat = new char[len + 1]; |
| 1031 snprintf(concat, len + 1, "%s/%s", |
| 1032 precompiled_snapshot_directory, filename); |
| 1033 qualified_filename = concat; |
| 1034 } else { |
| 1035 qualified_filename = filename; |
| 1036 } |
| 1037 |
| 1038 File* file = File::Open(qualified_filename, File::kWriteTruncate); |
| 1016 ASSERT(file != NULL); | 1039 ASSERT(file != NULL); |
| 1017 if (!file->WriteFully(buffer, size)) { | 1040 if (!file->WriteFully(buffer, size)) { |
| 1018 ErrorExit(kErrorExitCode, | 1041 ErrorExit(kErrorExitCode, |
| 1019 "Unable to open file %s for writing snapshot\n", | 1042 "Unable to open file %s for writing snapshot\n", |
| 1020 filename); | 1043 qualified_filename); |
| 1021 } | 1044 } |
| 1022 delete file; | 1045 delete file; |
| 1046 if (concat != NULL) { |
| 1047 delete concat; |
| 1048 } |
| 1023 } | 1049 } |
| 1024 | 1050 |
| 1025 | 1051 |
| 1026 static void ReadSnapshotFile(const char* filename, | 1052 static void ReadPrecompiledSnapshotFile(const char* filename, |
| 1027 const uint8_t** buffer) { | 1053 const uint8_t** buffer) { |
| 1028 void* file = DartUtils::OpenFile(filename, false); | 1054 char* concat = NULL; |
| 1055 const char* qualified_filename; |
| 1056 if (strlen(precompiled_snapshot_directory) > 0) { |
| 1057 intptr_t len = snprintf(NULL, 0, "%s/%s", |
| 1058 precompiled_snapshot_directory, filename); |
| 1059 concat = new char[len + 1]; |
| 1060 snprintf(concat, len + 1, "%s/%s", |
| 1061 precompiled_snapshot_directory, filename); |
| 1062 qualified_filename = concat; |
| 1063 } else { |
| 1064 qualified_filename = filename; |
| 1065 } |
| 1066 |
| 1067 void* file = DartUtils::OpenFile(qualified_filename, false); |
| 1029 if (file == NULL) { | 1068 if (file == NULL) { |
| 1030 ErrorExit(kErrorExitCode, | 1069 ErrorExit(kErrorExitCode, |
| 1031 "Error: Unable to open file %s for reading snapshot\n", filename); | 1070 "Error: Unable to open file %s for reading snapshot\n", |
| 1071 qualified_filename); |
| 1032 } | 1072 } |
| 1033 intptr_t len = -1; | 1073 intptr_t len = -1; |
| 1034 DartUtils::ReadFile(buffer, &len, file); | 1074 DartUtils::ReadFile(buffer, &len, file); |
| 1035 if (*buffer == NULL || len == -1) { | 1075 if (*buffer == NULL || len == -1) { |
| 1036 ErrorExit(kErrorExitCode, | 1076 ErrorExit(kErrorExitCode, |
| 1037 "Error: Unable to read snapshot file %s\n", filename); | 1077 "Error: Unable to read snapshot file %s\n", qualified_filename); |
| 1038 } | 1078 } |
| 1039 DartUtils::CloseFile(file); | 1079 DartUtils::CloseFile(file); |
| 1080 if (concat != NULL) { |
| 1081 delete concat; |
| 1082 } |
| 1040 } | 1083 } |
| 1041 | 1084 |
| 1042 | 1085 |
| 1043 static void* LoadLibrarySymbol(const char* libname, const char* symname) { | 1086 static void* LoadLibrarySymbol(const char* libname, const char* symname) { |
| 1044 void* library = Extensions::LoadExtensionLibrary(libname); | 1087 void* library = Extensions::LoadExtensionLibrary(libname); |
| 1045 if (library == NULL) { | 1088 if (library == NULL) { |
| 1046 Log::PrintErr("Error: Failed to load library '%s'\n", libname); | 1089 Log::PrintErr("Error: Failed to load library '%s'\n", libname); |
| 1047 Platform::Exit(kErrorExitCode); | 1090 Platform::Exit(kErrorExitCode); |
| 1048 } | 1091 } |
| 1049 void* symbol = Extensions::ResolveSymbol(library, symname); | 1092 void* symbol = Extensions::ResolveSymbol(library, symname); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1208 intptr_t isolate_size = 0; | 1251 intptr_t isolate_size = 0; |
| 1209 uint8_t* instructions_buffer = NULL; | 1252 uint8_t* instructions_buffer = NULL; |
| 1210 intptr_t instructions_size = 0; | 1253 intptr_t instructions_size = 0; |
| 1211 result = Dart_CreatePrecompiledSnapshot(&vm_isolate_buffer, | 1254 result = Dart_CreatePrecompiledSnapshot(&vm_isolate_buffer, |
| 1212 &vm_isolate_size, | 1255 &vm_isolate_size, |
| 1213 &isolate_buffer, | 1256 &isolate_buffer, |
| 1214 &isolate_size, | 1257 &isolate_size, |
| 1215 &instructions_buffer, | 1258 &instructions_buffer, |
| 1216 &instructions_size); | 1259 &instructions_size); |
| 1217 CHECK_RESULT(result); | 1260 CHECK_RESULT(result); |
| 1218 WriteSnapshotFile(kPrecompiledVmIsolateName, | 1261 WritePrecompiledSnapshotFile(kPrecompiledVmIsolateName, |
| 1219 vm_isolate_buffer, | 1262 vm_isolate_buffer, |
| 1220 vm_isolate_size); | 1263 vm_isolate_size); |
| 1221 WriteSnapshotFile(kPrecompiledIsolateName, | 1264 WritePrecompiledSnapshotFile(kPrecompiledIsolateName, |
| 1222 isolate_buffer, | 1265 isolate_buffer, |
| 1223 isolate_size); | 1266 isolate_size); |
| 1224 WriteSnapshotFile(kPrecompiledInstructionsName, | 1267 WritePrecompiledSnapshotFile(kPrecompiledInstructionsName, |
| 1225 instructions_buffer, | 1268 instructions_buffer, |
| 1226 instructions_size); | 1269 instructions_size); |
| 1227 } else { | 1270 } else { |
| 1228 if (has_compile_all) { | 1271 if (has_compile_all) { |
| 1229 result = Dart_CompileAll(); | 1272 result = Dart_CompileAll(); |
| 1230 CHECK_RESULT(result); | 1273 CHECK_RESULT(result); |
| 1231 } | 1274 } |
| 1232 | 1275 |
| 1233 if (Dart_IsNull(root_lib)) { | 1276 if (Dart_IsNull(root_lib)) { |
| 1234 ErrorExit(kErrorExitCode, | 1277 ErrorExit(kErrorExitCode, |
| 1235 "Unable to find root library for '%s'\n", | 1278 "Unable to find root library for '%s'\n", |
| 1236 script_name); | 1279 script_name); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1419 | 1462 |
| 1420 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 1463 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 1421 | 1464 |
| 1422 // Start event handler. | 1465 // Start event handler. |
| 1423 EventHandler::Start(); | 1466 EventHandler::Start(); |
| 1424 | 1467 |
| 1425 const uint8_t* instructions_snapshot = NULL; | 1468 const uint8_t* instructions_snapshot = NULL; |
| 1426 if (has_run_precompiled_snapshot) { | 1469 if (has_run_precompiled_snapshot) { |
| 1427 instructions_snapshot = reinterpret_cast<const uint8_t*>( | 1470 instructions_snapshot = reinterpret_cast<const uint8_t*>( |
| 1428 LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName)); | 1471 LoadLibrarySymbol(kPrecompiledLibraryName, kPrecompiledSymbolName)); |
| 1429 ReadSnapshotFile(kPrecompiledVmIsolateName, &vm_isolate_snapshot_buffer); | 1472 ReadPrecompiledSnapshotFile(kPrecompiledVmIsolateName, |
| 1430 ReadSnapshotFile(kPrecompiledIsolateName, &isolate_snapshot_buffer); | 1473 &vm_isolate_snapshot_buffer); |
| 1474 ReadPrecompiledSnapshotFile(kPrecompiledIsolateName, |
| 1475 &isolate_snapshot_buffer); |
| 1431 } | 1476 } |
| 1432 | 1477 |
| 1433 // Initialize the Dart VM. | 1478 // Initialize the Dart VM. |
| 1434 char* error = Dart_Initialize( | 1479 char* error = Dart_Initialize( |
| 1435 vm_isolate_snapshot_buffer, instructions_snapshot, | 1480 vm_isolate_snapshot_buffer, instructions_snapshot, |
| 1436 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, | 1481 CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate, |
| 1437 DartUtils::OpenFile, | 1482 DartUtils::OpenFile, |
| 1438 DartUtils::ReadFile, | 1483 DartUtils::ReadFile, |
| 1439 DartUtils::WriteFile, | 1484 DartUtils::WriteFile, |
| 1440 DartUtils::CloseFile, | 1485 DartUtils::CloseFile, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1491 Platform::Exit(Process::GlobalExitCode()); | 1536 Platform::Exit(Process::GlobalExitCode()); |
| 1492 } | 1537 } |
| 1493 | 1538 |
| 1494 } // namespace bin | 1539 } // namespace bin |
| 1495 } // namespace dart | 1540 } // namespace dart |
| 1496 | 1541 |
| 1497 int main(int argc, char** argv) { | 1542 int main(int argc, char** argv) { |
| 1498 dart::bin::main(argc, argv); | 1543 dart::bin::main(argc, argv); |
| 1499 UNREACHABLE(); | 1544 UNREACHABLE(); |
| 1500 } | 1545 } |
| OLD | NEW |