Index: gin/isolate_holder.cc |
diff --git a/gin/isolate_holder.cc b/gin/isolate_holder.cc |
index db4473ec0f9ee3d3976f8521523fd6a5459dbb18..346a05effc94ed0ad869637a0676e36f95a023b9 100644 |
--- a/gin/isolate_holder.cc |
+++ b/gin/isolate_holder.cc |
@@ -8,6 +8,9 @@ |
#include <string.h> |
#include "base/logging.h" |
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
+#include "base/path_service.h" |
+#endif // V8_USE_EXTERNAL_STARTUP_DATA |
#include "base/rand_util.h" |
#include "base/sys_info.h" |
#include "gin/array_buffer.h" |
@@ -29,6 +32,80 @@ bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
} // namespace |
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
+// static |
+base::MemoryMappedFile IsolateHolder::mapped_natives; |
+base::MemoryMappedFile IsolateHolder::mapped_snapshot; |
rmcilroy
2014/09/23 10:04:20
We only allow POD types (not class objects) as sta
baixo
2014/09/23 11:36:46
Done.
|
+bool IsolateHolder::read_snapshot = false; |
+#endif // V8_USE_EXTERNAL_STARTUP_DATA |
+ |
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA) && defined(OS_ANDROID) |
+//static |
+bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { |
+ if (read_snapshot) |
+ return true; |
+ |
+ if (!mapped_natives.IsValid()) { |
+ if (!mapped_natives.Initialize(base::File(natives_fd))) { |
+ LOG(ERROR) << "Couldn't mmap v8 natives data file"; |
+ return false; |
+ } |
+ } |
+ |
+ if (!mapped_snapshot.IsValid()) { |
+ if (!mapped_snapshot.Initialize(base::File(snapshot_fd))) { |
+ LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
+ return false; |
+ } |
+ } |
+ |
+ read_snapshot = true; |
+ return true; |
+} |
+#endif // V8_USE_EXTERNAL_STARTUP_DATA && OS_ANDROID |
+ |
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
+// static |
+bool IsolateHolder::LoadV8Snapshot() { |
+ if (read_snapshot) |
+ return true; |
+ |
+#if !defined(OS_MACOSX) |
+ base::FilePath data_path; |
+ PathService::Get( |
+#if defined(OS_ANDROID) |
+ base::DIR_ANDROID_APP_DATA, |
+#elif defined(OS_POSIX) || defined(OS_WIN) |
+ base::DIR_EXE, |
+#endif |
+ &data_path); |
+#else |
+ base::FilePath data_path = base::mac::FrameworkBundlePath(); |
+#endif |
+ DCHECK(!data_path.empty()); |
+ |
+ if (!mapped_natives.IsValid()) { |
+ base::FilePath natives_data_path = |
+ data_path.AppendASCII("natives_blob.bin"); |
+ if (!mapped_natives.Initialize(natives_data_path)) { |
+ LOG(ERROR) << "Couldn't mmap v8 natives data file"; |
+ return false; |
+ } |
+ } |
+ if (!mapped_snapshot.IsValid()) { |
+ base::FilePath snapshot_data_path = |
+ data_path.AppendASCII("snapshot_blob.bin"); |
+ if (!mapped_snapshot.Initialize(snapshot_data_path)) { |
+ LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
+ return false; |
+ } |
+ } |
+ |
+ read_snapshot = true; |
+ return true; |
+} |
+#endif // V8_USE_EXTERNAL_STARTUP_DATA |
+ |
IsolateHolder::IsolateHolder() { |
CHECK(g_array_buffer_allocator) |
<< "You need to invoke gin::IsolateHolder::Initialize first"; |
@@ -62,6 +139,19 @@ void IsolateHolder::Initialize(ScriptMode mode, |
v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
} |
v8::V8::SetEntropySource(&GenerateEntropy); |
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
+ v8::StartupData natives; |
+ natives.data = reinterpret_cast<const char*>(mapped_natives.data()); |
+ natives.raw_size = mapped_natives.length(); |
+ natives.compressed_size = mapped_natives.length(); |
+ v8::V8::SetNativesDataBlob(&natives); |
+ |
+ v8::StartupData snapshot; |
+ snapshot.data = reinterpret_cast<const char*>(mapped_snapshot.data()); |
+ snapshot.raw_size = mapped_snapshot.length(); |
+ snapshot.compressed_size = mapped_snapshot.length(); |
+ v8::V8::SetSnapshotDataBlob(&snapshot); |
+#endif // V8_USE_EXTERNAL_STARTUP_DATA |
v8::V8::Initialize(); |
v8_is_initialized = true; |
} |