diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-19 19:25:12 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-19 19:25:12 +0000 |
commit | 1b93c237f36e291242da6b03550f2c4a9f1c6d1c (patch) | |
tree | 91bbdda0b6425fc5a2b191ef0777619f5cce8f19 /gin | |
parent | 41494f74e8f9dac770129841b73b5251b96b6fe0 (diff) | |
download | chromium_src-1b93c237f36e291242da6b03550f2c4a9f1c6d1c.zip chromium_src-1b93c237f36e291242da6b03550f2c4a9f1c6d1c.tar.gz chromium_src-1b93c237f36e291242da6b03550f2c4a9f1c6d1c.tar.bz2 |
Introduce a Gin class instead of using global functions to control gin
The Gin class holds and controls a v8::Isolate. The isolate is not
entered by default, i.e. before you can use gin for a given Gin
instance, you need to enter the isolate first, e.g. by using a
v8::Isolate::Scope.
This has the advantage that we don't rely on the deprecate default
isolate, and also support having multiple isolates in one process.
BUG=317398
R=abarth@chromium.org
TEST=gin_unittests and mojo_js_bindings_unittests pass
Review URL: https://codereview.chromium.org/76353002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236029 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin')
-rw-r--r-- | gin/converter_unittest.cc | 15 | ||||
-rw-r--r-- | gin/gin.cc | 55 | ||||
-rw-r--r-- | gin/gin.gyp | 5 | ||||
-rw-r--r-- | gin/gin.h | 31 | ||||
-rw-r--r-- | gin/initialize.cc | 39 | ||||
-rw-r--r-- | gin/initialize.h | 14 | ||||
-rw-r--r-- | gin/runner.cc | 4 | ||||
-rw-r--r-- | gin/runner.h | 1 | ||||
-rw-r--r-- | gin/runner_unittest.cc | 5 | ||||
-rw-r--r-- | gin/shell/gin_main.cc | 9 | ||||
-rw-r--r-- | gin/test/file_runner.cc | 22 | ||||
-rw-r--r-- | gin/test/run_all_unittests.cc | 4 | ||||
-rw-r--r-- | gin/test/v8_test.cc | 22 | ||||
-rw-r--r-- | gin/test/v8_test.h | 9 |
14 files changed, 147 insertions, 88 deletions
diff --git a/gin/converter_unittest.cc b/gin/converter_unittest.cc index b98a6da..ade9dff 100644 --- a/gin/converter_unittest.cc +++ b/gin/converter_unittest.cc @@ -9,6 +9,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" +#include "gin/gin.h" #include "gin/test/v8_test.h" #include "testing/gtest/include/gtest/gtest.h" #include "v8/include/v8.h" @@ -30,11 +31,11 @@ namespace gin { typedef V8Test ConverterTest; TEST_F(ConverterTest, Bool) { - HandleScope handle_scope(isolate_); + HandleScope handle_scope(instance_->isolate()); - EXPECT_TRUE(Converter<bool>::ToV8(isolate_, true)->StrictEquals( + EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals( Boolean::New(true))); - EXPECT_TRUE(Converter<bool>::ToV8(isolate_, false)->StrictEquals( + EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals( Boolean::New(false))); struct { @@ -66,11 +67,11 @@ TEST_F(ConverterTest, Bool) { } TEST_F(ConverterTest, Int32) { - HandleScope handle_scope(isolate_); + HandleScope handle_scope(instance_->isolate()); int test_data_to[] = {-1, 0, 1}; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) { - EXPECT_TRUE(Converter<int32_t>::ToV8(isolate_, + EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i])->StrictEquals(Integer::New(test_data_to[i]))); } @@ -104,7 +105,7 @@ TEST_F(ConverterTest, Int32) { } TEST_F(ConverterTest, Vector) { - HandleScope handle_scope(isolate_); + HandleScope handle_scope(instance_->isolate()); std::vector<int> expected; expected.push_back(-1); @@ -112,7 +113,7 @@ TEST_F(ConverterTest, Vector) { expected.push_back(1); Handle<Array> js_array = Handle<Array>::Cast( - Converter<std::vector<int> >::ToV8(isolate_, expected)); + Converter<std::vector<int> >::ToV8(instance_->isolate(), expected)); ASSERT_FALSE(js_array.IsEmpty()); EXPECT_EQ(3u, js_array->Length()); for (size_t i = 0; i < expected.size(); ++i) { diff --git a/gin/gin.cc b/gin/gin.cc new file mode 100644 index 0000000..28b8d2b --- /dev/null +++ b/gin/gin.cc @@ -0,0 +1,55 @@ +// 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. + +#include "gin/gin.h" + +#include <stdlib.h> +#include <string.h> + +#include "base/rand_util.h" +#include "base/sys_info.h" +#include "gin/array_buffer.h" +#include "gin/per_isolate_data.h" + +namespace gin { + +namespace { + +bool GenerateEntropy(unsigned char* buffer, size_t amount) { + base::RandBytes(buffer, amount); + return true; +} + + +void EnsureV8Initialized() { + static bool v8_is_initialized = false; + if (v8_is_initialized) + return; + v8_is_initialized = true; + + v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance()); + static const char v8_flags[] = "--use_strict --harmony"; + v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); + v8::V8::SetEntropySource(&GenerateEntropy); + v8::V8::Initialize(); +} + +} // namespace + +Gin::Gin() { + EnsureV8Initialized(); + 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_); +} + +Gin::~Gin() { + isolate_->Dispose(); +} + +} // namespace gin diff --git a/gin/gin.gyp b/gin/gin.gyp index 7546cfb..ee55822 100644 --- a/gin/gin.gyp +++ b/gin/gin.gyp @@ -37,8 +37,8 @@ 'converter.h', 'dictionary.cc', 'dictionary.h', - 'initialize.cc', - 'initialize.h', + 'gin.cc', + 'gin.h', 'per_context_data.cc', 'per_context_data.h', 'per_isolate_data.cc', @@ -55,6 +55,7 @@ 'target_name': 'gin_shell', 'type': 'executable', 'dependencies': [ + '../base/base.gyp:base_i18n', 'gin', ], 'sources': [ diff --git a/gin/gin.h b/gin/gin.h new file mode 100644 index 0000000..0a5c9ac --- /dev/null +++ b/gin/gin.h @@ -0,0 +1,31 @@ +// 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/initialize.cc b/gin/initialize.cc deleted file mode 100644 index d95470f..0000000 --- a/gin/initialize.cc +++ /dev/null @@ -1,39 +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. - -#include "gin/initialize.h" - -#include <stdlib.h> -#include <string.h> - -#include "gin/array_buffer.h" -#include "gin/per_isolate_data.h" - -namespace gin { - -namespace { - -bool GenerateEntropy(unsigned char* buffer, size_t amount) { - // TODO(abarth): Gin needs a source of entropy. - return false; -} - -const char kFlags[] = "--use_strict --harmony"; - -} - -void Initialize() { - v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance()); - v8::V8::InitializeICU(); - v8::V8::SetFlagsFromString(kFlags, - static_cast<uint32_t>(arraysize(kFlags)) - 1); - v8::V8::SetEntropySource(&GenerateEntropy); - v8::V8::Initialize(); - - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::HandleScope handle_scope(isolate); - new PerIsolateData(isolate); -} - -} // namespace gin diff --git a/gin/initialize.h b/gin/initialize.h deleted file mode 100644 index 4ebcf8d..0000000 --- a/gin/initialize.h +++ /dev/null @@ -1,14 +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_INITIALIZE_H_ -#define GIN_INITIALIZE_H_ - -namespace gin { - -void Initialize(); - -} // namespace gin - -#endif // GIN_INITIALIZE_H_ diff --git a/gin/runner.cc b/gin/runner.cc index 46671c5..f45d6a0 100644 --- a/gin/runner.cc +++ b/gin/runner.cc @@ -42,6 +42,7 @@ Runner::Runner(RunnerDelegate* delegate, Isolate* isolate) : ContextHolder(isolate), delegate_(delegate), weak_factory_(this) { + v8::Isolate::Scope isolate_scope(isolate); HandleScope handle_scope(isolate); SetContext(Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this))); @@ -66,7 +67,8 @@ void Runner::Run(v8::Handle<Script> script) { } Runner::Scope::Scope(Runner* runner) - : handle_scope_(runner->isolate()), + : isolate_scope_(runner->isolate()), + handle_scope_(runner->isolate()), scope_(runner->context()) { } diff --git a/gin/runner.h b/gin/runner.h index fa41259..e664c3d 100644 --- a/gin/runner.h +++ b/gin/runner.h @@ -50,6 +50,7 @@ class Runner : public ContextHolder { ~Scope(); private: + v8::Isolate::Scope isolate_scope_; v8::HandleScope handle_scope_; v8::Context::Scope scope_; diff --git a/gin/runner_unittest.cc b/gin/runner_unittest.cc index adcebed..3ff7703 100644 --- a/gin/runner_unittest.cc +++ b/gin/runner_unittest.cc @@ -6,6 +6,7 @@ #include "base/compiler_specific.h" #include "gin/converter.h" +#include "gin/gin.h" #include "testing/gtest/include/gtest/gtest.h" using v8::Isolate; @@ -18,8 +19,10 @@ namespace gin { TEST(RunnerTest, Run) { std::string source = "this.result = 'PASS';\n"; + gin::Gin instance; + RunnerDelegate delegate; - Isolate* isolate = Isolate::GetCurrent(); + Isolate* isolate = instance.isolate(); Runner runner(&delegate, isolate); Runner::Scope scope(&runner); runner.Run(source); diff --git a/gin/shell/gin_main.cc b/gin/shell/gin_main.cc index ca984f7..e1d95bb 100644 --- a/gin/shell/gin_main.cc +++ b/gin/shell/gin_main.cc @@ -6,8 +6,9 @@ #include "base/bind.h" #include "base/command_line.h" #include "base/file_util.h" +#include "base/i18n/icu_util.h" #include "base/message_loop/message_loop.h" -#include "gin/initialize.h" +#include "gin/gin.h" #include "gin/modules/console.h" #include "gin/modules/module_runner_delegate.h" #include "gin/test/file_runner.h" @@ -58,12 +59,14 @@ class ShellRunnerDelegate : public ModuleRunnerDelegate { int main(int argc, char** argv) { base::AtExitManager at_exit; CommandLine::Init(argc, argv); - gin::Initialize(); + base::i18n::InitializeICU(); + + gin::Gin instance; base::MessageLoop message_loop; gin::ShellRunnerDelegate delegate; - gin::Runner runner(&delegate, v8::Isolate::GetCurrent()); + gin::Runner runner(&delegate, instance.isolate()); CommandLine::StringVector args = CommandLine::ForCurrentProcess()->GetArgs(); for (CommandLine::StringVector::const_iterator it = args.begin(); diff --git a/gin/test/file_runner.cc b/gin/test/file_runner.cc index dd0ca66..e6b4256 100644 --- a/gin/test/file_runner.cc +++ b/gin/test/file_runner.cc @@ -8,6 +8,7 @@ #include "base/message_loop/message_loop.h" #include "base/path_service.h" #include "gin/converter.h" +#include "gin/gin.h" #include "gin/modules/module_registry.h" #include "gin/test/gtest.h" #include "gin/try_catch.h" @@ -46,17 +47,20 @@ void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate) { base::MessageLoop message_loop; - gin::Runner runner(delegate, v8::Isolate::GetCurrent()); - gin::Runner::Scope scope(&runner); - runner.Run(source); + gin::Gin instance; + gin::Runner runner(delegate, instance.isolate()); + { + gin::Runner::Scope scope(&runner); + runner.Run(source); - message_loop.RunUntilIdle(); + message_loop.RunUntilIdle(); - v8::Handle<v8::Value> result = runner.context()->Global()->Get( - StringToSymbol(runner.isolate(), "result")); - std::string result_string; - ASSERT_TRUE(ConvertFromV8(result, &result_string)); - EXPECT_EQ("PASS", result_string); + v8::Handle<v8::Value> result = runner.context()->Global()->Get( + StringToSymbol(runner.isolate(), "result")); + std::string result_string; + ASSERT_TRUE(ConvertFromV8(result, &result_string)); + EXPECT_EQ("PASS", result_string); + } } } // namespace gin diff --git a/gin/test/run_all_unittests.cc b/gin/test/run_all_unittests.cc index c8644ed..cb1c96c 100644 --- a/gin/test/run_all_unittests.cc +++ b/gin/test/run_all_unittests.cc @@ -5,15 +5,13 @@ #include "base/basictypes.h" #include "base/bind.h" #include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" #include "base/test/launcher/unit_test_launcher.h" #include "base/test/test_suite.h" -#include "gin/initialize.h" int main(int argc, char** argv) { base::TestSuite test_suite(argc, argv); - gin::Initialize(); - return base::LaunchUnitTests( argc, argv, base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); diff --git a/gin/test/v8_test.cc b/gin/test/v8_test.cc index b2c1d5d..b09930e 100644 --- a/gin/test/v8_test.cc +++ b/gin/test/v8_test.cc @@ -4,10 +4,11 @@ #include "gin/test/v8_test.h" +#include "gin/gin.h" + using v8::Context; using v8::Local; using v8::HandleScope; -using v8::Isolate; namespace gin { @@ -18,16 +19,21 @@ V8Test::~V8Test() { } void V8Test::SetUp() { - isolate_ = Isolate::GetCurrent(); - HandleScope handle_scope(isolate_); - context_.Reset(isolate_, Context::New(isolate_)); - Local<Context>::New(isolate_, context_)->Enter(); + instance_.reset(new gin::Gin); + instance_->isolate()->Enter(); + HandleScope handle_scope(instance_->isolate()); + context_.Reset(instance_->isolate(), Context::New(instance_->isolate())); + Local<Context>::New(instance_->isolate(), context_)->Enter(); } void V8Test::TearDown() { - HandleScope handle_scope(isolate_); - Local<Context>::New(isolate_, context_)->Exit(); - context_.Reset(); + { + HandleScope handle_scope(instance_->isolate()); + Local<Context>::New(instance_->isolate(), context_)->Exit(); + context_.Reset(); + } + instance_->isolate()->Exit(); + instance_.reset(); } } // namespace gin diff --git a/gin/test/v8_test.h b/gin/test/v8_test.h index 2b46f24..e7f93a6 100644 --- a/gin/test/v8_test.h +++ b/gin/test/v8_test.h @@ -5,12 +5,16 @@ #ifndef GIN_TEST_V8_TEST_H_ #define GIN_TEST_V8_TEST_H_ +#include "base/basictypes.h" #include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" #include "testing/gtest/include/gtest/gtest.h" #include "v8/include/v8.h" namespace gin { +class Gin; + // A base class for tests that use v8. class V8Test : public testing::Test { public: @@ -21,8 +25,11 @@ class V8Test : public testing::Test { virtual void TearDown() OVERRIDE; protected: - v8::Isolate* isolate_; + scoped_ptr<Gin> instance_; v8::Persistent<v8::Context> context_; + + private: + DISALLOW_COPY_AND_ASSIGN(V8Test); }; } // namespace gin |