| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Generate a snapshot file after loading all the scripts specified on the | 5 // Generate a snapshot file after loading all the scripts specified on the |
| 6 // command line. | 6 // command line. |
| 7 | 7 |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 Log::PrintErr("Error: %s", Dart_GetError(result)); \ | 24 Log::PrintErr("Error: %s", Dart_GetError(result)); \ |
| 25 Dart_ExitScope(); \ | 25 Dart_ExitScope(); \ |
| 26 Dart_ShutdownIsolate(); \ | 26 Dart_ShutdownIsolate(); \ |
| 27 exit(255); \ | 27 exit(255); \ |
| 28 } \ | 28 } \ |
| 29 | 29 |
| 30 | 30 |
| 31 // Global state that indicates whether a snapshot is to be created and | 31 // Global state that indicates whether a snapshot is to be created and |
| 32 // if so which file to write the snapshot into. | 32 // if so which file to write the snapshot into. |
| 33 static const char* snapshot_filename = NULL; | 33 static const char* snapshot_filename = NULL; |
| 34 static bool script_snapshot = false; | |
| 35 static const char* package_root = NULL; | 34 static const char* package_root = NULL; |
| 36 static uint8_t* snapshot_buffer = NULL; | 35 static uint8_t* snapshot_buffer = NULL; |
| 37 | 36 |
| 38 | 37 |
| 39 // Global state which contains a pointer to the script name for which | 38 // Global state which contains a pointer to the script name for which |
| 40 // a snapshot needs to be created (NULL would result in the creation | 39 // a snapshot needs to be created (NULL would result in the creation |
| 41 // of a generic snapshot that contains only the corelibs). | 40 // of a generic snapshot that contains only the corelibs). |
| 42 static char* app_script_name = NULL; | 41 static char* app_script_name = NULL; |
| 43 | 42 |
| 44 | 43 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 59 if (strncmp(option, name, length) == 0) { | 58 if (strncmp(option, name, length) == 0) { |
| 60 return (option + length); | 59 return (option + length); |
| 61 } | 60 } |
| 62 return NULL; | 61 return NULL; |
| 63 } | 62 } |
| 64 | 63 |
| 65 | 64 |
| 66 static bool ProcessSnapshotOption(const char* option) { | 65 static bool ProcessSnapshotOption(const char* option) { |
| 67 const char* name = ProcessOption(option, "--snapshot="); | 66 const char* name = ProcessOption(option, "--snapshot="); |
| 68 if (name != NULL) { | 67 if (name != NULL) { |
| 69 script_snapshot = false; | |
| 70 snapshot_filename = name; | |
| 71 return true; | |
| 72 } | |
| 73 name = ProcessOption(option, "--script_snapshot="); | |
| 74 if (name != NULL) { | |
| 75 script_snapshot = true; | |
| 76 snapshot_filename = name; | 68 snapshot_filename = name; |
| 77 return true; | 69 return true; |
| 78 } | 70 } |
| 79 return false; | 71 return false; |
| 80 } | 72 } |
| 81 | 73 |
| 82 | 74 |
| 83 static bool ProcessPackageRootOption(const char* option) { | 75 static bool ProcessPackageRootOption(const char* option) { |
| 84 const char* name = ProcessOption(option, "--package_root="); | 76 const char* name = ProcessOption(option, "--package_root="); |
| 85 if (name != NULL) { | 77 if (name != NULL) { |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 ASSERT(!Dart_IsError(lib)); | 352 ASSERT(!Dart_IsError(lib)); |
| 361 return lib; | 353 return lib; |
| 362 } | 354 } |
| 363 | 355 |
| 364 | 356 |
| 365 static void PrintUsage() { | 357 static void PrintUsage() { |
| 366 Log::PrintErr( | 358 Log::PrintErr( |
| 367 "Usage:\n" | 359 "Usage:\n" |
| 368 "\n" | 360 "\n" |
| 369 " gen_snapshot [<vm-flags>] [<options>] \\\n" | 361 " gen_snapshot [<vm-flags>] [<options>] \\\n" |
| 370 " {--script_snapshot=<out-file> | --snapshot=<out-file>} \\\n" | 362 " --snapshot=<out-file> [<dart-script-file>]\n" |
| 371 " [<dart-script-file>]\n" | |
| 372 "\n" | 363 "\n" |
| 373 " Writes a snapshot of <dart-script-file> to <out-file>. If no\n" | 364 " Writes a snapshot of <dart-script-file> to <out-file>. If no\n" |
| 374 " <dart-script-file> is passed, a generic snapshot of all the corelibs is\n" | 365 " <dart-script-file> is passed, a generic snapshot of all the corelibs is\n" |
| 375 " created. One of the following is required:\n" | 366 " created. It is required to specify an output file name:\n" |
| 376 "\n" | 367 "\n" |
| 377 " --script_snapshot=<file> Generates a script snapshot.\n" | |
| 378 " --snapshot=<file> Generates a complete snapshot. Uses the url\n" | 368 " --snapshot=<file> Generates a complete snapshot. Uses the url\n" |
| 379 " mapping specified on the command line to load\n" | 369 " mapping specified on the command line to load\n" |
| 380 " the libraries.\n" | 370 " the libraries.\n" |
| 381 "Supported options:\n" | 371 "Supported options:\n" |
| 382 "\n" | 372 "\n" |
| 383 "--package_root=<path>\n" | 373 "--package_root=<path>\n" |
| 384 " Where to find packages, that is, \"package:...\" imports.\n" | 374 " Where to find packages, that is, \"package:...\" imports.\n" |
| 385 "\n" | 375 "\n" |
| 386 "--url_mapping=<mapping>\n" | 376 "--url_mapping=<mapping>\n" |
| 387 " Uses the URL mapping(s) specified on the command line to load the\n" | 377 " Uses the URL mapping(s) specified on the command line to load the\n" |
| 388 " libraries. For use only with --snapshot=.\n"); | 378 " libraries. For use only with --snapshot=.\n"); |
| 389 } | 379 } |
| 390 | 380 |
| 391 | 381 |
| 392 static void VerifyLoaded(Dart_Handle library) { | 382 static void VerifyLoaded(Dart_Handle library) { |
| 393 if (Dart_IsError(library)) { | 383 if (Dart_IsError(library)) { |
| 394 const char* err_msg = Dart_GetError(library); | 384 const char* err_msg = Dart_GetError(library); |
| 395 Log::PrintErr("Errors encountered while loading: %s\n", err_msg); | 385 Log::PrintErr("Errors encountered while loading: %s\n", err_msg); |
| 396 Dart_ExitScope(); | 386 Dart_ExitScope(); |
| 397 Dart_ShutdownIsolate(); | 387 Dart_ShutdownIsolate(); |
| 398 exit(255); | 388 exit(255); |
| 399 } | 389 } |
| 400 ASSERT(Dart_IsLibrary(library)); | 390 ASSERT(Dart_IsLibrary(library)); |
| 401 } | 391 } |
| 402 | 392 |
| 403 | 393 |
| 404 static void CreateAndWriteSnapshot(bool script_snapshot) { | 394 static void CreateAndWriteSnapshot() { |
| 405 Dart_Handle result; | 395 Dart_Handle result; |
| 406 uint8_t* buffer = NULL; | 396 uint8_t* buffer = NULL; |
| 407 intptr_t size = 0; | 397 intptr_t size = 0; |
| 408 | 398 |
| 409 // First create a snapshot. | 399 // First create a snapshot. |
| 410 if (script_snapshot) { | 400 result = Dart_CreateSnapshot(&buffer, &size); |
| 411 // Script snapshot specified so create a script snapshot. | |
| 412 result = Dart_CreateScriptSnapshot(&buffer, &size); | |
| 413 } else { | |
| 414 // Create a full snapshot. | |
| 415 result = Dart_CreateSnapshot(&buffer, &size); | |
| 416 } | |
| 417 CHECK_RESULT(result); | 401 CHECK_RESULT(result); |
| 418 | 402 |
| 419 // Now write the snapshot out to specified file and exit. | 403 // Now write the snapshot out to specified file and exit. |
| 420 WriteSnapshotFile(buffer, size); | 404 WriteSnapshotFile(buffer, size); |
| 421 Dart_ExitScope(); | 405 Dart_ExitScope(); |
| 422 | 406 |
| 423 // Shutdown the isolate. | 407 // Shutdown the isolate. |
| 424 Dart_ShutdownIsolate(); | 408 Dart_ShutdownIsolate(); |
| 425 } | 409 } |
| 426 | 410 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 exit(255); | 482 exit(255); |
| 499 } | 483 } |
| 500 | 484 |
| 501 Dart_Handle result; | 485 Dart_Handle result; |
| 502 Dart_Handle library; | 486 Dart_Handle library; |
| 503 Dart_EnterScope(); | 487 Dart_EnterScope(); |
| 504 | 488 |
| 505 ASSERT(snapshot_filename != NULL); | 489 ASSERT(snapshot_filename != NULL); |
| 506 // Load up the script before a snapshot is created. | 490 // Load up the script before a snapshot is created. |
| 507 if (app_script_name != NULL) { | 491 if (app_script_name != NULL) { |
| 508 if (!script_snapshot) { | 492 // This is the case of a custom embedder (e.g: dartium) trying to |
| 509 // This is the case of a custom embedder (e.g: dartium) trying to | 493 // create a full snapshot. The current isolate is set up so that we can |
| 510 // create a full snapshot. The current isolate is set up so that we can | 494 // invoke the dart uri resolution code like _resolveURI. App script is |
| 511 // invoke the dart uri resolution code like _resolveURI. App script is | 495 // loaded into a separate isolate. |
| 512 // loaded into a separate isolate. | |
| 513 | 496 |
| 514 SetupForUriResolution(); | 497 SetupForUriResolution(); |
| 515 | 498 |
| 516 // Get handle to builtin library. | 499 // Get handle to builtin library. |
| 517 Dart_Handle builtin_lib = | 500 Dart_Handle builtin_lib = |
| 518 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 501 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 519 CHECK_RESULT(builtin_lib); | 502 CHECK_RESULT(builtin_lib); |
| 520 | 503 |
| 521 // Prepare for script loading by setting up the 'print' and 'timer' | 504 // Prepare for script loading by setting up the 'print' and 'timer' |
| 522 // closures and setting up 'package root' for URI resolution. | 505 // closures and setting up 'package root' for URI resolution. |
| 523 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | 506 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); |
| 524 CHECK_RESULT(result); | 507 CHECK_RESULT(result); |
| 525 Dart_ExitScope(); | 508 Dart_ExitScope(); |
| 526 | 509 |
| 527 UriResolverIsolateScope::isolate = isolate; | 510 UriResolverIsolateScope::isolate = isolate; |
| 528 | 511 |
| 529 // Now we create an isolate into which we load all the code that needs to | 512 // Now we create an isolate into which we load all the code that needs to |
| 530 // be in the snapshot. | 513 // be in the snapshot. |
| 531 if (Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error) == NULL) { | 514 if (Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error) == NULL) { |
| 532 fprintf(stderr, "%s", error); | 515 fprintf(stderr, "%s", error); |
| 533 free(error); | 516 free(error); |
| 534 exit(255); | 517 exit(255); |
| 535 } | 518 } |
| 536 Dart_EnterScope(); | 519 Dart_EnterScope(); |
| 537 | 520 |
| 538 // Set up the library tag handler in such a manner that it will use the | 521 // Set up the library tag handler in such a manner that it will use the |
| 539 // URL mapping specified on the command line to load the libraries. | 522 // URL mapping specified on the command line to load the libraries. |
| 540 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); | 523 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); |
| 541 CHECK_RESULT(result); | 524 CHECK_RESULT(result); |
| 542 // Load the specified script. | 525 // Load the specified script. |
| 543 library = LoadSnapshotCreationScript(app_script_name); | 526 library = LoadSnapshotCreationScript(app_script_name); |
| 544 VerifyLoaded(library); | 527 VerifyLoaded(library); |
| 545 CreateAndWriteSnapshot(false); | 528 CreateAndWriteSnapshot(); |
| 546 | 529 |
| 547 Dart_EnterIsolate(UriResolverIsolateScope::isolate); | 530 Dart_EnterIsolate(UriResolverIsolateScope::isolate); |
| 548 Dart_ShutdownIsolate(); | 531 Dart_ShutdownIsolate(); |
| 549 } else { | |
| 550 // This is the case where we want to create a script snapshot of | |
| 551 // the specified script. There will be no URL mapping specified for | |
| 552 // this case, use the generic library tag handler. | |
| 553 | |
| 554 // First setup and create a generic full snapshot. | |
| 555 SetupForGenericSnapshotCreation(); | |
| 556 uint8_t* buffer = NULL; | |
| 557 intptr_t size = 0; | |
| 558 result = Dart_CreateSnapshot(&buffer, &size); | |
| 559 CHECK_RESULT(result); | |
| 560 | |
| 561 // Save the snapshot buffer as we are about to shutdown the isolate. | |
| 562 snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); | |
| 563 ASSERT(snapshot_buffer != NULL); | |
| 564 memmove(snapshot_buffer, buffer, size); | |
| 565 | |
| 566 // Shutdown the isolate. | |
| 567 Dart_ExitScope(); | |
| 568 Dart_ShutdownIsolate(); | |
| 569 | |
| 570 // Now load the specified script and create a script snapshot. | |
| 571 if (Dart_CreateIsolate( | |
| 572 NULL, NULL, snapshot_buffer, NULL, &error) == NULL) { | |
| 573 Log::PrintErr("%s", error); | |
| 574 free(error); | |
| 575 free(snapshot_buffer); | |
| 576 exit(255); | |
| 577 } | |
| 578 Dart_EnterScope(); | |
| 579 | |
| 580 // Setup generic library tag handler. | |
| 581 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | |
| 582 CHECK_RESULT(result); | |
| 583 | |
| 584 // Get handle to builtin library. | |
| 585 Dart_Handle builtin_lib = | |
| 586 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | |
| 587 CHECK_RESULT(builtin_lib); | |
| 588 | |
| 589 // Prepare for script loading by setting up the 'print' and 'timer' | |
| 590 // closures and setting up 'package root' for URI resolution. | |
| 591 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | |
| 592 CHECK_RESULT(result); | |
| 593 | |
| 594 // Load specified script. | |
| 595 library = DartUtils::LoadScript(app_script_name, builtin_lib); | |
| 596 | |
| 597 // Now create and write snapshot of script. | |
| 598 CreateAndWriteSnapshot(true); | |
| 599 | |
| 600 free(snapshot_buffer); | |
| 601 } | |
| 602 } else { | 532 } else { |
| 603 SetupForGenericSnapshotCreation(); | 533 SetupForGenericSnapshotCreation(); |
| 604 CreateAndWriteSnapshot(false); | 534 CreateAndWriteSnapshot(); |
| 605 } | 535 } |
| 606 return 0; | 536 return 0; |
| 607 } | 537 } |
| OLD | NEW |