Skip to content

Commit

Permalink
Refactor IsolateHolder to be able to always create the isolate
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
jeisinger authored and Commit bot committed Sep 11, 2014
1 parent 2ab903f commit 2f43f2c
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gin/array_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 21 additions & 16 deletions gin/isolate_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(),
Expand All @@ -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);
}

Expand All @@ -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_);
Expand Down
8 changes: 7 additions & 1 deletion gin/public/isolate_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion gin/shell/gin_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion gin/shell_runner_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion gin/test/file_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion gin/test/v8_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "gin/test/v8_test.h"

#include "gin/array_buffer.h"
#include "gin/public/isolate_holder.h"

using v8::Context;
Expand All @@ -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()));
Expand Down
5 changes: 4 additions & 1 deletion mojo/apps/js/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "base/message_loop/message_loop.h"
#include "gin/array_buffer.h"
#include "gin/public/isolate_holder.h"
#include "mojo/apps/js/mojo_runner_delegate.h"
#include "mojo/public/cpp/system/core.h"
Expand All @@ -24,7 +25,9 @@ namespace apps {
void Start(MojoHandle pipe, const std::string& module) {
base::MessageLoop loop;

gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode);
gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
gin::ArrayBufferAllocator::SharedInstance());
gin::IsolateHolder instance;
MojoRunnerDelegate delegate;
gin::ShellRunner runner(&delegate, instance.isolate());
delegate.Start(&runner, pipe, module);
Expand Down
5 changes: 4 additions & 1 deletion mojo/apps/js/test/js_to_cpp_unittest.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/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "gin/array_buffer.h"
#include "gin/public/isolate_holder.h"
#include "mojo/apps/js/mojo_runner_delegate.h"
#include "mojo/apps/js/test/js_to_cpp.mojom.h"
Expand Down Expand Up @@ -398,7 +399,9 @@ class JsToCppTest : public testing::Test {

cpp_side->set_js_side(js_side.get());

gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode);
gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
gin::ArrayBufferAllocator::SharedInstance());
gin::IsolateHolder instance;
apps::MojoRunnerDelegate delegate;
gin::ShellRunner runner(&delegate, instance.isolate());
delegate.Start(&runner, pipe.handle1.release().value(), test);
Expand Down
2 changes: 1 addition & 1 deletion net/DEPS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include_rules = [
"+crypto",
"+gin/public",
"+gin",
"+jni",
"+third_party/apple_apsl",
"+third_party/libevent",
Expand Down
6 changes: 4 additions & 2 deletions net/proxy/proxy_resolver_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "gin/array_buffer.h"
#include "gin/public/isolate_holder.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
Expand Down Expand Up @@ -768,8 +769,9 @@ int ProxyResolverV8::SetPacScript(
void ProxyResolverV8::EnsureIsolateCreated() {
if (g_proxy_resolver_isolate_)
return;
g_proxy_resolver_isolate_ =
new gin::IsolateHolder(gin::IsolateHolder::kNonStrictMode);
gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode,
gin::ArrayBufferAllocator::SharedInstance());
g_proxy_resolver_isolate_ = new gin::IsolateHolder;
ANNOTATE_LEAKING_OBJECT_PTR(g_proxy_resolver_isolate_);
}

Expand Down

0 comments on commit 2f43f2c

Please sign in to comment.