From 1b93c237f36e291242da6b03550f2c4a9f1c6d1c Mon Sep 17 00:00:00 2001 From: "jochen@chromium.org" Date: Tue, 19 Nov 2013 19:25:12 +0000 Subject: 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 --- gin/converter_unittest.cc | 15 ++++++------ gin/gin.cc | 55 +++++++++++++++++++++++++++++++++++++++++++ gin/gin.gyp | 5 ++-- gin/gin.h | 31 ++++++++++++++++++++++++ gin/initialize.cc | 39 ------------------------------ gin/initialize.h | 14 ----------- gin/runner.cc | 4 +++- gin/runner.h | 1 + gin/runner_unittest.cc | 5 +++- gin/shell/gin_main.cc | 9 ++++--- gin/test/file_runner.cc | 22 ++++++++++------- gin/test/run_all_unittests.cc | 4 +--- gin/test/v8_test.cc | 22 ++++++++++------- gin/test/v8_test.h | 9 ++++++- 14 files changed, 147 insertions(+), 88 deletions(-) create mode 100644 gin/gin.cc create mode 100644 gin/gin.h delete mode 100644 gin/initialize.cc delete mode 100644 gin/initialize.h (limited to 'gin') 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::ToV8(isolate_, true)->StrictEquals( + EXPECT_TRUE(Converter::ToV8(instance_->isolate(), true)->StrictEquals( Boolean::New(true))); - EXPECT_TRUE(Converter::ToV8(isolate_, false)->StrictEquals( + EXPECT_TRUE(Converter::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::ToV8(isolate_, + EXPECT_TRUE(Converter::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 expected; expected.push_back(-1); @@ -112,7 +113,7 @@ TEST_F(ConverterTest, Vector) { expected.push_back(1); Handle js_array = Handle::Cast( - Converter >::ToV8(isolate_, expected)); + Converter >::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 +#include + +#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 -#include - -#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(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