diff options
-rw-r--r-- | gin/converter_unittest.cc | 2 | ||||
-rw-r--r-- | gin/gin.gyp | 4 | ||||
-rw-r--r-- | gin/gin.h | 31 | ||||
-rw-r--r-- | gin/isolate_holder.cc (renamed from gin/gin.cc) | 33 | ||||
-rw-r--r-- | gin/per_isolate_data.cc | 6 | ||||
-rw-r--r-- | gin/public/isolate_holder.h | 44 | ||||
-rw-r--r-- | gin/runner_unittest.cc | 4 | ||||
-rw-r--r-- | gin/shell/gin_main.cc | 4 | ||||
-rw-r--r-- | gin/test/file_runner.cc | 4 | ||||
-rw-r--r-- | gin/test/v8_test.cc | 4 | ||||
-rw-r--r-- | gin/test/v8_test.h | 4 | ||||
-rw-r--r-- | gin/wrappable_unittest.cc | 2 | ||||
-rw-r--r-- | mojo/apps/js/main.cc | 4 |
13 files changed, 89 insertions, 57 deletions
diff --git a/gin/converter_unittest.cc b/gin/converter_unittest.cc index ade9dff..0a6bbba 100644 --- a/gin/converter_unittest.cc +++ b/gin/converter_unittest.cc @@ -9,7 +9,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" -#include "gin/gin.h" +#include "gin/public/isolate_holder.h" #include "gin/test/v8_test.h" #include "testing/gtest/include/gtest/gtest.h" #include "v8/include/v8.h" diff --git a/gin/gin.gyp b/gin/gin.gyp index 4b08dfc..715db90 100644 --- a/gin/gin.gyp +++ b/gin/gin.gyp @@ -29,8 +29,7 @@ 'converter.h', 'dictionary.cc', 'dictionary.h', - 'gin.cc', - 'gin.h', + 'isolate_holder.cc', 'modules/console.cc', 'modules/console.h', 'modules/file_module_provider.cc', @@ -44,6 +43,7 @@ 'per_isolate_data.cc', 'per_isolate_data.h', 'public/gin_embedders.h', + 'public/isolate_holder.h', 'public/wrapper_info.h', 'runner.cc', 'runner.h', diff --git a/gin/gin.h b/gin/gin.h deleted file mode 100644 index 0a5c9ac..0000000 --- a/gin/gin.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2013 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. - -#ifndef GIN_GIN_H_ -#define GIN_GIN_H_ - -#include "base/basictypes.h" - -namespace v8 { -class Isolate; -} - -namespace gin { - -class Gin { - public: - Gin(); - ~Gin(); - - v8::Isolate* isolate() { return isolate_; } - - private: - v8::Isolate* isolate_; - - DISALLOW_COPY_AND_ASSIGN(Gin); -}; - -} // namespace gin - -#endif // GIN_INITIALIZE_H_ diff --git a/gin/gin.cc b/gin/isolate_holder.cc index 28b8d2b..3c2bf9a 100644 --- a/gin/gin.cc +++ b/gin/isolate_holder.cc @@ -2,11 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "gin/gin.h" +#include "gin/public/isolate_holder.h" #include <stdlib.h> #include <string.h> +#include "base/logging.h" #include "base/rand_util.h" #include "base/sys_info.h" #include "gin/array_buffer.h" @@ -22,11 +23,15 @@ bool GenerateEntropy(unsigned char* buffer, size_t amount) { } -void EnsureV8Initialized() { +void EnsureV8Initialized(bool gin_managed) { static bool v8_is_initialized = false; - if (v8_is_initialized) + static bool v8_is_gin_managed = false; + if (v8_is_initialized) { + CHECK_EQ(v8_is_gin_managed, gin_managed); return; + } v8_is_initialized = true; + v8_is_gin_managed = gin_managed; v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance()); static const char v8_flags[] = "--use_strict --harmony"; @@ -37,19 +42,31 @@ void EnsureV8Initialized() { } // namespace -Gin::Gin() { - EnsureV8Initialized(); +IsolateHolder::IsolateHolder() + : isolate_owner_(true) { + EnsureV8Initialized(true); isolate_ = v8::Isolate::New(); v8::ResourceConstraints constraints; constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory()); v8::SetResourceConstraints(isolate_, &constraints); v8::Isolate::Scope isolate_scope(isolate_); v8::HandleScope handle_scope(isolate_); - new PerIsolateData(isolate_); + isolate_data_.reset(new PerIsolateData(isolate_)); } -Gin::~Gin() { - isolate_->Dispose(); +IsolateHolder::IsolateHolder(v8::Isolate* isolate) + : isolate_owner_(false), + isolate_(isolate) { + EnsureV8Initialized(false); + v8::Isolate::Scope isolate_scope(isolate_); + v8::HandleScope handle_scope(isolate_); + isolate_data_.reset(new PerIsolateData(isolate_)); +} + +IsolateHolder::~IsolateHolder() { + isolate_data_.reset(); + if (isolate_owner_) + isolate_->Dispose(); } } // namespace gin diff --git a/gin/per_isolate_data.cc b/gin/per_isolate_data.cc index b18a89a..844f1e1 100644 --- a/gin/per_isolate_data.cc +++ b/gin/per_isolate_data.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "gin/per_isolate_data.h" +#include "gin/public/gin_embedders.h" using v8::Eternal; using v8::Handle; @@ -16,14 +17,15 @@ namespace gin { PerIsolateData::PerIsolateData(Isolate* isolate) : isolate_(isolate) { - isolate_->SetData(this); + isolate_->SetData(kEmbedderNativeGin, this); } PerIsolateData::~PerIsolateData() { + isolate_->SetData(kEmbedderNativeGin, NULL); } PerIsolateData* PerIsolateData::From(Isolate* isolate) { - return static_cast<PerIsolateData*>(isolate->GetData()); + return static_cast<PerIsolateData*>(isolate->GetData(kEmbedderNativeGin)); } void PerIsolateData::SetObjectTemplate(WrapperInfo* info, diff --git a/gin/public/isolate_holder.h b/gin/public/isolate_holder.h new file mode 100644 index 0000000..15755a4 --- /dev/null +++ b/gin/public/isolate_holder.h @@ -0,0 +1,44 @@ +// Copyright 2013 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. + +#ifndef GIN_PUBLIC_ISOLATE_HOLDER_H_ +#define GIN_PUBLIC_ISOLATE_HOLDER_H_ + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" + +namespace v8 { +class Isolate; +} + +namespace gin { + +class PerIsolateData; + +class IsolateHolder { + public: + // You can use gin in two modes: either gin manages V8, or the gin-embedder + // manages gin. If gin manages V8, use the IsolateHolder constructor without + // parameters, otherwise, the gin-embedder needs to create v8::Isolates and + // pass them to IsolateHolder. + // + // It is not possible to mix the two. + IsolateHolder(); + explicit IsolateHolder(v8::Isolate* isolate); + + ~IsolateHolder(); + + v8::Isolate* isolate() { return isolate_; } + + private: + bool isolate_owner_; + v8::Isolate* isolate_; + scoped_ptr<PerIsolateData> isolate_data_; + + DISALLOW_COPY_AND_ASSIGN(IsolateHolder); +}; + +} // namespace gin + +#endif // GIN_PUBLIC_ISOLATE_HOLDER_H_ diff --git a/gin/runner_unittest.cc b/gin/runner_unittest.cc index 3ff7703..9c3c30e 100644 --- a/gin/runner_unittest.cc +++ b/gin/runner_unittest.cc @@ -6,7 +6,7 @@ #include "base/compiler_specific.h" #include "gin/converter.h" -#include "gin/gin.h" +#include "gin/public/isolate_holder.h" #include "testing/gtest/include/gtest/gtest.h" using v8::Isolate; @@ -19,7 +19,7 @@ namespace gin { TEST(RunnerTest, Run) { std::string source = "this.result = 'PASS';\n"; - gin::Gin instance; + gin::IsolateHolder instance; RunnerDelegate delegate; Isolate* isolate = instance.isolate(); diff --git a/gin/shell/gin_main.cc b/gin/shell/gin_main.cc index 4e18d71..9d081a3 100644 --- a/gin/shell/gin_main.cc +++ b/gin/shell/gin_main.cc @@ -8,9 +8,9 @@ #include "base/file_util.h" #include "base/i18n/icu_util.h" #include "base/message_loop/message_loop.h" -#include "gin/gin.h" #include "gin/modules/console.h" #include "gin/modules/module_runner_delegate.h" +#include "gin/public/isolate_holder.h" #include "gin/test/file_runner.h" #include "gin/try_catch.h" @@ -61,7 +61,7 @@ int main(int argc, char** argv) { CommandLine::Init(argc, argv); base::i18n::InitializeICU(); - gin::Gin instance; + gin::IsolateHolder instance; base::MessageLoop message_loop; diff --git a/gin/test/file_runner.cc b/gin/test/file_runner.cc index ff5ac5d..0c60c4b 100644 --- a/gin/test/file_runner.cc +++ b/gin/test/file_runner.cc @@ -8,9 +8,9 @@ #include "base/message_loop/message_loop.h" #include "base/path_service.h" #include "gin/converter.h" -#include "gin/gin.h" #include "gin/modules/console.h" #include "gin/modules/module_registry.h" +#include "gin/public/isolate_holder.h" #include "gin/test/gtest.h" #include "gin/try_catch.h" #include "testing/gtest/include/gtest/gtest.h" @@ -51,7 +51,7 @@ void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate) { base::MessageLoop message_loop; - gin::Gin instance; + gin::IsolateHolder instance; gin::Runner runner(delegate, instance.isolate()); { gin::Runner::Scope scope(&runner); diff --git a/gin/test/v8_test.cc b/gin/test/v8_test.cc index b09930e..95f4e74 100644 --- a/gin/test/v8_test.cc +++ b/gin/test/v8_test.cc @@ -4,7 +4,7 @@ #include "gin/test/v8_test.h" -#include "gin/gin.h" +#include "gin/public/isolate_holder.h" using v8::Context; using v8::Local; @@ -19,7 +19,7 @@ V8Test::~V8Test() { } void V8Test::SetUp() { - instance_.reset(new gin::Gin); + instance_.reset(new gin::IsolateHolder); instance_->isolate()->Enter(); HandleScope handle_scope(instance_->isolate()); context_.Reset(instance_->isolate(), Context::New(instance_->isolate())); diff --git a/gin/test/v8_test.h b/gin/test/v8_test.h index e7f93a6..6ad52d6 100644 --- a/gin/test/v8_test.h +++ b/gin/test/v8_test.h @@ -13,7 +13,7 @@ namespace gin { -class Gin; +class IsolateHolder; // A base class for tests that use v8. class V8Test : public testing::Test { @@ -25,7 +25,7 @@ class V8Test : public testing::Test { virtual void TearDown() OVERRIDE; protected: - scoped_ptr<Gin> instance_; + scoped_ptr<IsolateHolder> instance_; v8::Persistent<v8::Context> context_; private: diff --git a/gin/wrappable_unittest.cc b/gin/wrappable_unittest.cc index af48769..7699fd1 100644 --- a/gin/wrappable_unittest.cc +++ b/gin/wrappable_unittest.cc @@ -4,8 +4,8 @@ #include "base/logging.h" #include "gin/arguments.h" -#include "gin/gin.h" #include "gin/per_isolate_data.h" +#include "gin/public/isolate_holder.h" #include "gin/test/v8_test.h" #include "gin/wrappable.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/mojo/apps/js/main.cc b/mojo/apps/js/main.cc index 322c216..1ad084b 100644 --- a/mojo/apps/js/main.cc +++ b/mojo/apps/js/main.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "gin/gin.h" +#include "gin/public/isolate_holder.h" #include "mojo/public/system/core_cpp.h" #include "mojo/public/system/macros.h" @@ -17,7 +17,7 @@ #endif extern "C" MOJO_APPS_JS_EXPORT MojoResult CDECL MojoMain(MojoHandle pipe) { - gin::Gin instance; + gin::IsolateHolder instance; // TODO(abarth): Load JS off the network and execute it. return MOJO_RESULT_OK; } |