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/loader.h" |
| 16 #include "mojo/dart/dart_snapshotter/vm.h" |
| 17 #include "tonic/dart_error.h" |
| 18 #include "tonic/dart_isolate_scope.h" |
| 19 |
| 20 const char kHelp[] = "help"; |
| 21 const char kPackageRoot[] = "package-root"; |
| 22 const char kSnapshot[] = "snapshot"; |
| 23 |
| 24 void Usage() { |
| 25 std::cerr << "Usage: dart_snapshotter" |
| 26 << " --" << kPackageRoot << " --" << kSnapshot |
| 27 << " <dart-app>" << std::endl; |
| 28 } |
| 29 |
| 30 void WriteSnapshot(base::FilePath path) { |
| 31 uint8_t* buffer; |
| 32 intptr_t size; |
| 33 CHECK(!tonic::LogIfError(Dart_CreateScriptSnapshot(&buffer, &size))); |
| 34 |
| 35 CHECK_EQ(base::WriteFile(path, reinterpret_cast<const char*>(buffer), size), |
| 36 size); |
| 37 } |
| 38 |
| 39 int main(int argc, const char* argv[]) { |
| 40 base::AtExitManager exit_manager; |
| 41 base::EnableTerminationOnHeapCorruption(); |
| 42 base::CommandLine::Init(argc, argv); |
| 43 |
| 44 const base::CommandLine& command_line = |
| 45 *base::CommandLine::ForCurrentProcess(); |
| 46 |
| 47 if (command_line.HasSwitch(kHelp) || command_line.GetArgs().empty()) { |
| 48 Usage(); |
| 49 return 0; |
| 50 } |
| 51 |
| 52 InitDartVM(); |
| 53 |
| 54 CHECK(command_line.HasSwitch(kPackageRoot)) << "Need --package-root"; |
| 55 InitLoader(command_line.GetSwitchValuePath(kPackageRoot)); |
| 56 |
| 57 Dart_Isolate isolate = CreateDartIsolate(); |
| 58 CHECK(isolate); |
| 59 |
| 60 tonic::DartIsolateScope scope(isolate); |
| 61 tonic::DartApiScope api_scope; |
| 62 |
| 63 auto args = command_line.GetArgs(); |
| 64 CHECK(args.size() == 1); |
| 65 LoadScript(args[0]); |
| 66 |
| 67 CHECK(!tonic::LogIfError(Dart_FinalizeLoading(true))); |
| 68 |
| 69 CHECK(command_line.HasSwitch(kSnapshot)) << "Need --snapshot"; |
| 70 WriteSnapshot(command_line.GetSwitchValuePath(kSnapshot)); |
| 71 |
| 72 return 0; |
| 73 } |
OLD | NEW |