Index: tools/blink_v8_snapshot/blink_v8_snapshot_generator.cc |
diff --git a/tools/blink_v8_snapshot/blink_v8_snapshot_generator.cc b/tools/blink_v8_snapshot/blink_v8_snapshot_generator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c119893281145988abfa3a1be99dd09b11826cdf |
--- /dev/null |
+++ b/tools/blink_v8_snapshot/blink_v8_snapshot_generator.cc |
@@ -0,0 +1,69 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/at_exit.h" |
+#include "base/command_line.h" |
+#include "base/files/file_util.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/task_scheduler/task_scheduler.h" |
+#include "gin/v8_initializer.h" |
+#include "mojo/edk/embedder/embedder.h" |
+#include "third_party/WebKit/public/platform/WebThread.h" |
+#include "third_party/WebKit/public/web/WebKit.h" |
+#include "third_party/WebKit/public/web/WebV8SnapshotUtil.h" |
+#include "v8/include/v8.h" |
+ |
+namespace { |
+ |
+class SnapshotThread : public blink::WebThread { |
+ public: |
+ bool IsCurrentThread() const override { return true; } |
+ blink::WebScheduler* Scheduler() const override { return nullptr; } |
+ blink::WebTaskRunner* GetWebTaskRunner() override { return nullptr; } |
+}; |
+ |
+class SnapshotPlatform final : public blink::Platform { |
+ public: |
+ bool IsToTakeV8Snapshot() override { return true; } |
+ blink::WebThread* CurrentThread() override { |
+ static SnapshotThread dummy_thread; |
+ return &dummy_thread; |
+ } |
+}; |
+ |
+} // namespace |
+ |
+// This program takes a snapshot of V8 contexts and writes it out as a file. |
+// We assume those contexts made in this program are used in Blink. |
haraken
2017/07/04 15:04:20
We assume ... => The snapshot file is consumed by
peria
2017/07/07 06:22:04
Done.
|
+// |
+// Usage: |
+// % blink_v8_snapshot_generator --output_file=<filename> |
+int main(int argc, char** argv) { |
+ base::AtExitManager at_exit; |
+ base::CommandLine::Init(argc, argv); |
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
+ gin::V8Initializer::LoadV8Snapshot(); |
+ gin::V8Initializer::LoadV8Natives(); |
+#endif |
+ |
+ // Set up environment to make Blink and V8 workable. |
+ base::MessageLoop message_loop; |
+ base::TaskScheduler::CreateAndStartWithDefaultParams("TakeSnapshot"); |
+ mojo::edk::Init(); |
+ |
+ // Take snapshot |
+ SnapshotPlatform platform; |
+ blink::Initialize(&platform); |
+ v8::StartupData blob = blink::WebV8SnapshotUtil::TakeSnapshot(); |
+ |
+ // Save snapshot as a file. Filename is given in a command line option. |
+ base::FilePath file_path = |
+ base::CommandLine::ForCurrentProcess()->GetSwitchValuePath("output_file"); |
+ CHECK(!file_path.empty()); |
+ base::WriteFile(file_path, blob.data, blob.raw_size); |
haraken
2017/07/04 15:04:20
Shall we assert that we've written non-zero-byte d
peria
2017/07/07 06:22:04
Done.
|
+ |
+ delete[] blob.data; |
+ |
+ return 0; |
+} |