| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <iostream> | |
| 6 | |
| 7 #include "base/at_exit.h" | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/process/memory.h" | |
| 14 #include "dart/runtime/include/dart_api.h" | |
| 15 #include "mojo/dart/dart_snapshotter/vm.h" | |
| 16 #include "mojo/edk/embedder/embedder.h" | |
| 17 #include "mojo/edk/embedder/simple_platform_support.h" | |
| 18 #include "tonic/dart_error.h" | |
| 19 #include "tonic/dart_isolate_scope.h" | |
| 20 #include "tonic/dart_library_loader.h" | |
| 21 #include "tonic/dart_library_provider_files.h" | |
| 22 #include "tonic/dart_script_loader_sync.h" | |
| 23 #include "tonic/dart_state.h" | |
| 24 | |
| 25 const char kHelp[] = "help"; | |
| 26 const char kPackageRoot[] = "package-root"; | |
| 27 const char kSnapshot[] = "snapshot"; | |
| 28 | |
| 29 const uint8_t magic_number[] = { 0xf5, 0xf5, 0xdc, 0xdc }; | |
| 30 | |
| 31 void Usage() { | |
| 32 std::cerr << "Usage: dart_snapshotter" | |
| 33 << " --" << kPackageRoot << " --" << kSnapshot | |
| 34 << " <dart-app>" << std::endl; | |
| 35 } | |
| 36 | |
| 37 void WriteSnapshot(base::FilePath path) { | |
| 38 uint8_t* buffer; | |
| 39 intptr_t size; | |
| 40 CHECK(!tonic::LogIfError(Dart_CreateScriptSnapshot(&buffer, &size))); | |
| 41 | |
| 42 intptr_t magic_number_len = sizeof(magic_number); | |
| 43 CHECK_EQ(base::WriteFile( | |
| 44 path, reinterpret_cast<const char*>(magic_number), sizeof(magic_number)), | |
| 45 magic_number_len); | |
| 46 CHECK(base::AppendToFile( | |
| 47 path, reinterpret_cast<const char*>(buffer), size)); | |
| 48 } | |
| 49 | |
| 50 int main(int argc, const char* argv[]) { | |
| 51 base::AtExitManager exit_manager; | |
| 52 base::EnableTerminationOnHeapCorruption(); | |
| 53 base::CommandLine::Init(argc, argv); | |
| 54 | |
| 55 const base::CommandLine& command_line = | |
| 56 *base::CommandLine::ForCurrentProcess(); | |
| 57 | |
| 58 if (command_line.HasSwitch(kHelp) || command_line.GetArgs().empty()) { | |
| 59 Usage(); | |
| 60 return 0; | |
| 61 } | |
| 62 | |
| 63 // Initialize mojo. | |
| 64 // TODO(vtl): Use make_unique when C++14 is available. | |
| 65 mojo::embedder::Init(std::unique_ptr<mojo::embedder::PlatformSupport>( | |
| 66 new mojo::embedder::SimplePlatformSupport())); | |
| 67 | |
| 68 InitDartVM(); | |
| 69 | |
| 70 CHECK(command_line.HasSwitch(kPackageRoot)) << "Need --package-root"; | |
| 71 CHECK(command_line.HasSwitch(kSnapshot)) << "Need --snapshot"; | |
| 72 auto args = command_line.GetArgs(); | |
| 73 CHECK(args.size() == 1); | |
| 74 | |
| 75 Dart_Isolate isolate = CreateDartIsolate(); | |
| 76 CHECK(isolate); | |
| 77 | |
| 78 tonic::DartIsolateScope scope(isolate); | |
| 79 tonic::DartApiScope api_scope; | |
| 80 | |
| 81 auto isolate_data = SnapshotterDartState::Current(); | |
| 82 CHECK(isolate_data != nullptr); | |
| 83 | |
| 84 // Use tonic's library tag handler. | |
| 85 CHECK(!tonic::LogIfError(Dart_SetLibraryTagHandler( | |
| 86 tonic::DartLibraryLoader::HandleLibraryTag))); | |
| 87 | |
| 88 // Use tonic's file system library provider. | |
| 89 isolate_data->set_library_provider( | |
| 90 new tonic::DartLibraryProviderFiles( | |
| 91 command_line.GetSwitchValuePath(kPackageRoot))); | |
| 92 | |
| 93 // Load script. | |
| 94 tonic::DartScriptLoaderSync::LoadScript(args[0], | |
| 95 isolate_data->library_provider()); | |
| 96 | |
| 97 // Write snapshot. | |
| 98 WriteSnapshot(command_line.GetSwitchValuePath(kSnapshot)); | |
| 99 | |
| 100 return 0; | |
| 101 } | |
| OLD | NEW |