Skip to content

Commit

Permalink
Introduce a Gin class instead of using global functions to control gin
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jochen@chromium.org committed Nov 19, 2013
1 parent 41494f7 commit 1b93c23
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 92 deletions.
15 changes: 8 additions & 7 deletions gin/converter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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])));
}

Expand Down Expand Up @@ -104,15 +105,15 @@ 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);
expected.push_back(0);
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) {
Expand Down
55 changes: 55 additions & 0 deletions gin/gin.cc
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions gin/gin.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -55,6 +55,7 @@
'target_name': 'gin_shell',
'type': 'executable',
'dependencies': [
'../base/base.gyp:base_i18n',
'gin',
],
'sources': [
Expand Down
31 changes: 31 additions & 0 deletions gin/gin.h
Original file line number Diff line number Diff line change
@@ -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_
39 changes: 0 additions & 39 deletions gin/initialize.cc

This file was deleted.

14 changes: 0 additions & 14 deletions gin/initialize.h

This file was deleted.

4 changes: 3 additions & 1 deletion gin/runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand All @@ -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()) {
}

Expand Down
1 change: 1 addition & 0 deletions gin/runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Runner : public ContextHolder {
~Scope();

private:
v8::Isolate::Scope isolate_scope_;
v8::HandleScope handle_scope_;
v8::Context::Scope scope_;

Expand Down
5 changes: 4 additions & 1 deletion gin/runner_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions gin/shell/gin_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 13 additions & 9 deletions gin/test/file_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
4 changes: 1 addition & 3 deletions gin/test/run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
22 changes: 14 additions & 8 deletions gin/test/v8_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Loading

0 comments on commit 1b93c23

Please sign in to comment.