Index: gin/v8_initializer.cc |
diff --git a/gin/isolate_holder.cc b/gin/v8_initializer.cc |
similarity index 56% |
copy from gin/isolate_holder.cc |
copy to gin/v8_initializer.cc |
index 317b5822bd8f47f773a0927d1a2b18ef6c2ebcb8..1593cb548626233bc5d659d1dd5b3f8f2531d536 100644 |
--- a/gin/isolate_holder.cc |
+++ b/gin/v8_initializer.cc |
@@ -2,24 +2,16 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "gin/public/isolate_holder.h" |
- |
-#include <stdlib.h> |
-#include <string.h> |
+#include "gin/v8_initializer.h" |
+#include "base/basictypes.h" |
+#include "base/files/file.h" |
+#include "base/files/file_path.h" |
#include "base/files/memory_mapped_file.h" |
#include "base/logging.h" |
-#include "base/message_loop/message_loop.h" |
#include "base/rand_util.h" |
#include "base/strings/sys_string_conversions.h" |
-#include "base/sys_info.h" |
#include "crypto/sha2.h" |
-#include "gin/array_buffer.h" |
-#include "gin/debug_impl.h" |
-#include "gin/function_template.h" |
-#include "gin/per_isolate_data.h" |
-#include "gin/public/v8_platform.h" |
-#include "gin/run_microtasks_observer.h" |
#if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
#if defined(OS_MACOSX) |
@@ -32,7 +24,7 @@ namespace gin { |
namespace { |
-v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
+v8::ArrayBuffer::Allocator* g_array_buffer_allocator = nullptr; |
bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
base::RandBytes(buffer, amount); |
@@ -43,28 +35,15 @@ base::MemoryMappedFile* g_mapped_natives = NULL; |
base::MemoryMappedFile* g_mapped_snapshot = NULL; |
rmcilroy
2015/03/30 10:29:22
nit - could you update these to nullptr's too sinc
oth
2015/03/30 15:55:19
Done.
|
#if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
-bool MapV8Files(base::FilePath* natives_path, |
- base::FilePath* snapshot_path, |
- int natives_fd = -1, |
- int snapshot_fd = -1, |
+bool MapV8Files(base::File natives_file, |
+ base::File snapshot_file, |
base::MemoryMappedFile::Region natives_region = |
base::MemoryMappedFile::Region::kWholeFile, |
base::MemoryMappedFile::Region snapshot_region = |
base::MemoryMappedFile::Region::kWholeFile) { |
- int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
- |
g_mapped_natives = new base::MemoryMappedFile; |
if (!g_mapped_natives->IsValid()) { |
-#if !defined(OS_WIN) |
- if (natives_fd == -1 |
- ? !g_mapped_natives->Initialize(base::File(*natives_path, flags), |
- natives_region) |
- : !g_mapped_natives->Initialize(base::File(natives_fd), |
- natives_region)) { |
-#else |
- if (!g_mapped_natives->Initialize(base::File(*natives_path, flags), |
- natives_region)) { |
-#endif // !OS_WIN |
+ if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) { |
delete g_mapped_natives; |
g_mapped_natives = NULL; |
LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
@@ -74,16 +53,7 @@ bool MapV8Files(base::FilePath* natives_path, |
g_mapped_snapshot = new base::MemoryMappedFile; |
if (!g_mapped_snapshot->IsValid()) { |
-#if !defined(OS_WIN) |
- if (snapshot_fd == -1 |
- ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags), |
- snapshot_region) |
- : !g_mapped_snapshot->Initialize(base::File(snapshot_fd), |
- snapshot_region)) { |
-#else |
- if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags), |
- snapshot_region)) { |
-#endif // !OS_WIN |
+ if (!g_mapped_snapshot->Initialize(snapshot_file.Pass(), snapshot_region)) { |
delete g_mapped_snapshot; |
g_mapped_snapshot = NULL; |
LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
@@ -118,7 +88,7 @@ extern const unsigned char g_snapshot_fingerprint[]; |
#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
#if !defined(OS_MACOSX) |
-const int IsolateHolder::kV8SnapshotBasePathKey = |
+const int V8Initializer::kV8SnapshotBasePathKey = |
#if defined(OS_ANDROID) |
base::DIR_ANDROID_APP_DATA; |
#elif defined(OS_POSIX) |
@@ -128,11 +98,63 @@ const int IsolateHolder::kV8SnapshotBasePathKey = |
#endif // OS_ANDROID |
#endif // !OS_MACOSX |
-const char IsolateHolder::kNativesFileName[] = "natives_blob.bin"; |
-const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin"; |
+const char V8Initializer::kNativesFileName[] = "natives_blob.bin"; |
+const char V8Initializer::kSnapshotFileName[] = "snapshot_blob.bin"; |
// static |
-bool IsolateHolder::LoadV8Snapshot() { |
+void V8Initializer::Initialize(ScriptMode mode, |
+ v8::ArrayBuffer::Allocator* allocator) { |
+ CHECK(allocator); |
+ static bool v8_is_initialized = false; |
+ if (v8_is_initialized) |
+ return; |
+ v8::V8::InitializePlatform(V8Platform::Get()); |
+ v8::V8::SetArrayBufferAllocator(allocator); |
+ g_array_buffer_allocator = allocator; |
+ if (mode == gin::V8Initializer::kStrictMode) { |
+ static const char use_strict[] = "--use_strict"; |
+ v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); |
+ } |
+ |
+ v8::StartupData natives; |
+ natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
+ natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
+ v8::V8::SetNativesDataBlob(&natives); |
+ |
+ v8::StartupData snapshot; |
+ snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
+ snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
+ v8::V8::SetSnapshotDataBlob(&snapshot); |
+ |
+ v8::V8::SetEntropySource(&GenerateEntropy); |
+ v8::V8::Initialize(); |
+ |
+ v8_is_initialized = true; |
+} |
+ |
+// static |
+v8::ArrayBuffer::Allocator* V8Initializer::GetArrayBufferAllocator() { |
+ return g_array_buffer_allocator; |
rmcilroy
2015/03/30 10:29:22
let's keep the g_array_buffer_allocator in Isolate
|
+} |
+ |
+// static |
+void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, |
+ int* natives_size_out, |
+ const char** snapshot_data_out, |
+ int* snapshot_size_out) { |
+ if (!g_mapped_natives || !g_mapped_snapshot) { |
+ *natives_data_out = *snapshot_data_out = NULL; |
+ *natives_size_out = *snapshot_size_out = 0; |
+ return; |
+ } |
+ *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
+ *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
+ *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
+ *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
+} |
+ |
+// static |
+bool V8Initializer::LoadV8Snapshot() { |
if (g_mapped_natives && g_mapped_snapshot) |
return true; |
@@ -143,20 +165,22 @@ bool IsolateHolder::LoadV8Snapshot() { |
base::FilePath natives_path = data_path.AppendASCII(kNativesFileName); |
base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName); |
-#else // !defined(OS_MACOSX) |
+#else // !defined(OS_MACOSX) |
base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
base::SysUTF8ToCFStringRef(kNativesFileName)); |
- base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( |
- natives_file_name); |
+ base::FilePath natives_path = |
+ base::mac::PathForFrameworkBundleResource(natives_file_name); |
base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( |
base::SysUTF8ToCFStringRef(kSnapshotFileName)); |
- base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( |
- snapshot_file_name); |
+ base::FilePath snapshot_path = |
+ base::mac::PathForFrameworkBundleResource(snapshot_file_name); |
DCHECK(!natives_path.empty()); |
DCHECK(!snapshot_path.empty()); |
#endif // !defined(OS_MACOSX) |
- if (!MapV8Files(&natives_path, &snapshot_path)) |
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
+ if (!MapV8Files(base::File(natives_path, flags), |
+ base::File(snapshot_path, flags))) |
return false; |
#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
@@ -167,13 +191,13 @@ bool IsolateHolder::LoadV8Snapshot() { |
#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
} |
-//static |
-bool IsolateHolder::LoadV8SnapshotFd(int natives_fd, |
- int64 natives_offset, |
- int64 natives_size, |
- int snapshot_fd, |
- int64 snapshot_offset, |
- int64 snapshot_size) { |
+// static |
+bool V8Initializer::LoadV8SnapshotFromFile(base::PlatformFile natives_pf, |
+ int64 natives_offset, |
+ int64 natives_size, |
+ base::PlatformFile snapshot_pf, |
+ int64 snapshot_offset, |
+ int64 snapshot_size) { |
if (g_mapped_natives && g_mapped_snapshot) |
return true; |
@@ -191,110 +215,10 @@ bool IsolateHolder::LoadV8SnapshotFd(int natives_fd, |
base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); |
} |
- return MapV8Files( |
- NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region); |
-} |
-#endif // V8_USE_EXTERNAL_STARTUP_DATA |
- |
-//static |
-void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, |
- int* natives_size_out, |
- const char** snapshot_data_out, |
- int* snapshot_size_out) { |
- if (!g_mapped_natives || !g_mapped_snapshot) { |
- *natives_data_out = *snapshot_data_out = NULL; |
- *natives_size_out = *snapshot_size_out = 0; |
- return; |
- } |
- *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
- *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
- *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
- *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
-} |
- |
-IsolateHolder::IsolateHolder() { |
- CHECK(g_array_buffer_allocator) |
- << "You need to invoke gin::IsolateHolder::Initialize first"; |
- v8::Isolate::CreateParams params; |
- params.entry_hook = DebugImpl::GetFunctionEntryHook(); |
- params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); |
- params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
- base::SysInfo::AmountOfVirtualMemory(), |
- base::SysInfo::NumberOfProcessors()); |
- isolate_ = v8::Isolate::New(params); |
- isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator)); |
-#if defined(OS_WIN) |
- { |
- void* code_range; |
- size_t size; |
- isolate_->GetCodeRange(&code_range, &size); |
- Debug::CodeRangeCreatedCallback callback = |
- DebugImpl::GetCodeRangeCreatedCallback(); |
- if (code_range && size && callback) |
- callback(code_range, size); |
- } |
-#endif |
+ return MapV8Files(base::File(natives_pf), base::File(snapshot_pf), |
+ natives_region, snapshot_region); |
} |
-IsolateHolder::~IsolateHolder() { |
- if (task_observer_.get()) |
- base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
-#if defined(OS_WIN) |
- { |
- void* code_range; |
- size_t size; |
- isolate_->GetCodeRange(&code_range, &size); |
- Debug::CodeRangeDeletedCallback callback = |
- DebugImpl::GetCodeRangeDeletedCallback(); |
- if (code_range && callback) |
- callback(code_range); |
- } |
-#endif |
- isolate_data_.reset(); |
- isolate_->Dispose(); |
- isolate_ = NULL; |
-} |
- |
-// static |
-void IsolateHolder::Initialize(ScriptMode mode, |
- v8::ArrayBuffer::Allocator* allocator) { |
- CHECK(allocator); |
- static bool v8_is_initialized = false; |
- if (v8_is_initialized) |
- return; |
- v8::V8::InitializePlatform(V8Platform::Get()); |
- v8::V8::SetArrayBufferAllocator(allocator); |
- g_array_buffer_allocator = allocator; |
- if (mode == gin::IsolateHolder::kStrictMode) { |
- static const char use_strict[] = "--use_strict"; |
- v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); |
- } |
- v8::V8::SetEntropySource(&GenerateEntropy); |
-#if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
- v8::StartupData natives; |
- natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
- natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
- v8::V8::SetNativesDataBlob(&natives); |
- |
- v8::StartupData snapshot; |
- snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
- snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
- v8::V8::SetSnapshotDataBlob(&snapshot); |
#endif // V8_USE_EXTERNAL_STARTUP_DATA |
- v8::V8::Initialize(); |
- v8_is_initialized = true; |
-} |
- |
-void IsolateHolder::AddRunMicrotasksObserver() { |
- DCHECK(!task_observer_.get()); |
- task_observer_.reset(new RunMicrotasksObserver(isolate_));; |
- base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
-} |
- |
-void IsolateHolder::RemoveRunMicrotasksObserver() { |
- DCHECK(task_observer_.get()); |
- base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
- task_observer_.reset(); |
-} |
} // namespace gin |