diff options
author | jochen <jochen@chromium.org> | 2014-09-10 16:47:31 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-11 00:01:37 +0000 |
commit | 2f43f2c92879b36d1262d02fba4e32890912b0b5 (patch) | |
tree | 49da9698dcedc41856e97072fa694feb375c50cc /gin | |
parent | 2ab903f7671ca80c798088c9d41ae5597257b450 (diff) | |
download | chromium_src-2f43f2c92879b36d1262d02fba4e32890912b0b5.zip chromium_src-2f43f2c92879b36d1262d02fba4e32890912b0b5.tar.gz chromium_src-2f43f2c92879b36d1262d02fba4e32890912b0b5.tar.bz2 |
Refactor IsolateHolder to be able to always create the isolate
Currently, blink creates its own isolates. We want to always use an
IsolateHolder to create the isolates. To be able to do this, I introduce
an Initialize method that setups V8. The new IsolateHolder ctor now
doesn't take any parameters but just creates new isolates according to
the configuration.
All non-blink gin users are cut over to the new API
BUG=none
R=abarth@chromium.org,andrewhayden@chromium.org,eroman@chromium.org
Review URL: https://codereview.chromium.org/553903003
Cr-Commit-Position: refs/heads/master@{#294262}
Diffstat (limited to 'gin')
-rw-r--r-- | gin/array_buffer.h | 2 | ||||
-rw-r--r-- | gin/isolate_holder.cc | 37 | ||||
-rw-r--r-- | gin/public/isolate_holder.h | 8 | ||||
-rw-r--r-- | gin/shell/gin_main.cc | 5 | ||||
-rw-r--r-- | gin/shell_runner_unittest.cc | 5 | ||||
-rw-r--r-- | gin/test/file_runner.cc | 5 | ||||
-rw-r--r-- | gin/test/v8_test.cc | 5 |
7 files changed, 45 insertions, 22 deletions
diff --git a/gin/array_buffer.h b/gin/array_buffer.h index 858cbf1..a07a472 100644 --- a/gin/array_buffer.h +++ b/gin/array_buffer.h @@ -20,7 +20,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { virtual void* AllocateUninitialized(size_t length) OVERRIDE; virtual void Free(void* data, size_t length) OVERRIDE; - static ArrayBufferAllocator* SharedInstance(); + GIN_EXPORT static ArrayBufferAllocator* SharedInstance(); }; class GIN_EXPORT ArrayBuffer { diff --git a/gin/isolate_holder.cc b/gin/isolate_holder.cc index d0ebbb8..8655c4b 100644 --- a/gin/isolate_holder.cc +++ b/gin/isolate_holder.cc @@ -24,8 +24,7 @@ bool GenerateEntropy(unsigned char* buffer, size_t amount) { return true; } -void EnsureV8Initialized(gin::IsolateHolder::ScriptMode mode, - bool gin_managed) { +void EnsureV8Initialized(bool gin_managed) { static bool v8_is_initialized = false; static bool v8_is_gin_managed = false; if (v8_is_initialized) { @@ -34,24 +33,13 @@ void EnsureV8Initialized(gin::IsolateHolder::ScriptMode mode, } v8_is_initialized = true; v8_is_gin_managed = gin_managed; - if (!gin_managed) - return; - - v8::V8::InitializePlatform(V8Platform::Get()); - v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance()); - if (mode == gin::IsolateHolder::kStrictMode) { - static const char v8_flags[] = "--use_strict"; - v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); - } - v8::V8::SetEntropySource(&GenerateEntropy); - v8::V8::Initialize(); } } // namespace -IsolateHolder::IsolateHolder(ScriptMode mode) +IsolateHolder::IsolateHolder() : isolate_owner_(true) { - EnsureV8Initialized(mode, true); + EnsureV8Initialized(true); isolate_ = v8::Isolate::New(); v8::ResourceConstraints constraints; constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), @@ -64,7 +52,7 @@ IsolateHolder::IsolateHolder(ScriptMode mode) IsolateHolder::IsolateHolder(v8::Isolate* isolate, v8::ArrayBuffer::Allocator* allocator) : isolate_owner_(false), isolate_(isolate) { - EnsureV8Initialized(kNonStrictMode, false); + EnsureV8Initialized(false); Init(allocator); } @@ -74,6 +62,23 @@ IsolateHolder::~IsolateHolder() { isolate_->Dispose(); } +// static +void IsolateHolder::Initialize(ScriptMode mode, + v8::ArrayBuffer::Allocator* allocator) { + static bool v8_is_initialized = false; + if (v8_is_initialized) + return; + v8::V8::InitializePlatform(V8Platform::Get()); + v8::V8::SetArrayBufferAllocator(allocator); + if (mode == gin::IsolateHolder::kStrictMode) { + static const char v8_flags[] = "--use_strict"; + v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); + } + v8::V8::SetEntropySource(&GenerateEntropy); + v8::V8::Initialize(); + v8_is_initialized = true; +} + void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) { v8::Isolate::Scope isolate_scope(isolate_); v8::HandleScope handle_scope(isolate_); diff --git a/gin/public/isolate_holder.h b/gin/public/isolate_holder.h index ee696f6..af01782 100644 --- a/gin/public/isolate_holder.h +++ b/gin/public/isolate_holder.h @@ -32,11 +32,17 @@ class GIN_EXPORT IsolateHolder { kStrictMode }; - explicit IsolateHolder(ScriptMode mode); + IsolateHolder(); + // Deprecated. IsolateHolder(v8::Isolate* isolate, v8::ArrayBuffer::Allocator* allocator); ~IsolateHolder(); + // Should be invoked once before creating IsolateHolder instances to + // initialize V8 and Gin. + static void Initialize(ScriptMode mode, + v8::ArrayBuffer::Allocator* allocator); + v8::Isolate* isolate() { return isolate_; } private: diff --git a/gin/shell/gin_main.cc b/gin/shell/gin_main.cc index c8e3784..aa9f2b5 100644 --- a/gin/shell/gin_main.cc +++ b/gin/shell/gin_main.cc @@ -8,6 +8,7 @@ #include "base/files/file_util.h" #include "base/i18n/icu_util.h" #include "base/message_loop/message_loop.h" +#include "gin/array_buffer.h" #include "gin/modules/console.h" #include "gin/modules/module_runner_delegate.h" #include "gin/public/isolate_holder.h" @@ -61,7 +62,9 @@ int main(int argc, char** argv) { CommandLine::Init(argc, argv); base::i18n::InitializeICU(); - gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); + gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, + gin::ArrayBufferAllocator::SharedInstance()); + gin::IsolateHolder instance; base::MessageLoop message_loop; diff --git a/gin/shell_runner_unittest.cc b/gin/shell_runner_unittest.cc index 95403ec..07ab678 100644 --- a/gin/shell_runner_unittest.cc +++ b/gin/shell_runner_unittest.cc @@ -5,6 +5,7 @@ #include "gin/shell_runner.h" #include "base/compiler_specific.h" +#include "gin/array_buffer.h" #include "gin/converter.h" #include "gin/public/isolate_holder.h" #include "testing/gtest/include/gtest/gtest.h" @@ -19,7 +20,9 @@ namespace gin { TEST(RunnerTest, Run) { std::string source = "this.result = 'PASS';\n"; - gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); + gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, + gin::ArrayBufferAllocator::SharedInstance()); + gin::IsolateHolder instance; ShellRunnerDelegate delegate; Isolate* isolate = instance.isolate(); diff --git a/gin/test/file_runner.cc b/gin/test/file_runner.cc index a387289..83228d6 100644 --- a/gin/test/file_runner.cc +++ b/gin/test/file_runner.cc @@ -7,6 +7,7 @@ #include "base/files/file_util.h" #include "base/message_loop/message_loop.h" #include "base/path_service.h" +#include "gin/array_buffer.h" #include "gin/converter.h" #include "gin/modules/console.h" #include "gin/modules/module_registry.h" @@ -57,7 +58,9 @@ void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate, base::MessageLoop message_loop; - gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); + gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, + gin::ArrayBufferAllocator::SharedInstance()); + gin::IsolateHolder instance; gin::ShellRunner runner(delegate, instance.isolate()); { gin::Runner::Scope scope(&runner); diff --git a/gin/test/v8_test.cc b/gin/test/v8_test.cc index c27154b..cb6d573 100644 --- a/gin/test/v8_test.cc +++ b/gin/test/v8_test.cc @@ -4,6 +4,7 @@ #include "gin/test/v8_test.h" +#include "gin/array_buffer.h" #include "gin/public/isolate_holder.h" using v8::Context; @@ -19,7 +20,9 @@ V8Test::~V8Test() { } void V8Test::SetUp() { - instance_.reset(new gin::IsolateHolder(gin::IsolateHolder::kStrictMode)); + gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, + gin::ArrayBufferAllocator::SharedInstance()); + instance_.reset(new gin::IsolateHolder); instance_->isolate()->Enter(); HandleScope handle_scope(instance_->isolate()); context_.Reset(instance_->isolate(), Context::New(instance_->isolate())); |