Commit 2f43f2c9 authored by jochen's avatar jochen Committed by Commit bot

Refactor IsolateHolder to be able to always create the isolate

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}
parent 2ab903f7
......@@ -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 {
......
......@@ -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) {
......@@ -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(),
......@@ -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);
}
......@@ -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_);
......
......@@ -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:
......
......@@ -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"
......@@ -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;
......
......@@ -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"
......@@ -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();
......
......@@ -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"
......@@ -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);
......
......@@ -4,6 +4,7 @@
#include "gin/test/v8_test.h"
#include "gin/array_buffer.h"
#include "gin/public/isolate_holder.h"
using v8::Context;
......@@ -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()));
......
......@@ -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"
......@@ -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);
......
......@@ -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"
......@@ -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);
......
include_rules = [
"+crypto",
"+gin/public",
"+gin",
"+jni",
"+third_party/apple_apsl",
"+third_party/libevent",
......
......@@ -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"
......@@ -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_);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment