Skip to content

Commit

Permalink
[gin] Add a v8::Platform implementation to allow for v8 posting tasks
Browse files Browse the repository at this point in the history
This currently has no effect unless concurrent sweeping and job based
sweeping is turned on in v8.

BUG=v8:3015
R=abarth@chromium.org,dcarney@chromium.org

Review URL: https://codereview.chromium.org/225413004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@261872 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jochen@chromium.org committed Apr 4, 2014
1 parent 4df54e8 commit b64e521
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build/common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,9 @@
# Compile d8 for the host toolset.
'v8_toolset_for_d8': 'host',

# Compiles v8 without its platform library.
'v8_use_default_platform': 0,

# Use the chromium skia by default.
'use_system_skia%': '0',

Expand Down
4 changes: 4 additions & 0 deletions gin/gin.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
'type': '<(component)',
'dependencies': [
'../base/base.gyp:base',
'../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations',
'../v8/tools/gyp/v8.gyp:v8',

],
'export_dependent_settings': [
'../base/base.gyp:base',
Expand Down Expand Up @@ -58,13 +60,15 @@
'public/context_holder.h',
'public/gin_embedders.h',
'public/isolate_holder.h',
'public/v8_platform.h',
'public/wrapper_info.h',
'runner.cc',
'runner.h',
'shell_runner.cc',
'shell_runner.h',
'try_catch.cc',
'try_catch.h',
'v8_platform.cc',
'wrappable.cc',
'wrappable.h',
'wrapper_info.cc',
Expand Down
2 changes: 2 additions & 0 deletions gin/isolate_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "gin/array_buffer.h"
#include "gin/function_template.h"
#include "gin/per_isolate_data.h"
#include "gin/public/v8_platform.h"

namespace gin {

Expand All @@ -36,6 +37,7 @@ void EnsureV8Initialized(bool gin_managed) {
if (!gin_managed)
return;

v8::V8::InitializePlatform(V8Platform::Get());
v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance());
static const char v8_flags[] = "--use_strict --harmony";
v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
Expand Down
5 changes: 4 additions & 1 deletion gin/per_isolate_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "gin/per_isolate_data.h"
#include "gin/public/gin_embedders.h"

Expand All @@ -18,7 +19,9 @@ namespace gin {

PerIsolateData::PerIsolateData(Isolate* isolate,
ArrayBuffer::Allocator* allocator)
: isolate_(isolate), allocator_(allocator) {
: isolate_(isolate),
allocator_(allocator),
message_loop_proxy_(base::MessageLoopProxy::current()) {
isolate_->SetData(kEmbedderNativeGin, this);
}

Expand Down
9 changes: 9 additions & 0 deletions gin/per_isolate_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
#include <map>

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "gin/gin_export.h"
#include "gin/public/wrapper_info.h"
#include "v8/include/v8.h"

namespace base {
class MessageLoopProxy;
}

namespace gin {

class IndexedPropertyInterceptor;
Expand Down Expand Up @@ -60,6 +65,9 @@ class GIN_EXPORT PerIsolateData {

v8::Isolate* isolate() { return isolate_; }
v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
base::MessageLoopProxy* message_loop_proxy() {
return message_loop_proxy_.get();
}

private:
typedef std::map<
Expand All @@ -79,6 +87,7 @@ class GIN_EXPORT PerIsolateData {
FunctionTemplateMap function_templates_;
IndexedPropertyInterceptorMap indexed_interceptors_;
NamedPropertyInterceptorMap named_interceptors_;
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;

DISALLOW_COPY_AND_ASSIGN(PerIsolateData);
};
Expand Down
38 changes: 38 additions & 0 deletions gin/public/v8_platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2014 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_PUBLIC_V8_PLATFORM_H_
#define GIN_PUBLIC_V8_PLATFORM_H_

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/lazy_instance.h"
#include "gin/gin_export.h"
#include "v8/include/v8-platform.h"

namespace gin {

// A v8::Platform implementation to use with gin.
class GIN_EXPORT V8Platform : public NON_EXPORTED_BASE(v8::Platform) {
public:
static V8Platform* Get();

// v8::Platform implementation.
virtual void CallOnBackgroundThread(
v8::Task* task,
v8::Platform::ExpectedRuntime expected_runtime) OVERRIDE;
virtual void CallOnForegroundThread(v8::Isolate* isolate,
v8::Task* task) OVERRIDE;
private:
friend struct base::DefaultLazyInstanceTraits<V8Platform>;

V8Platform();
virtual ~V8Platform();

DISALLOW_COPY_AND_ASSIGN(V8Platform);
};

} // namespace gin

#endif // GIN_PUBLIC_V8_PLATFORM_H_
42 changes: 42 additions & 0 deletions gin/v8_platform.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2014 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/public/v8_platform.h"

#include "base/bind.h"
#include "base/location.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/threading/worker_pool.h"
#include "gin/per_isolate_data.h"

namespace gin {

namespace {

base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER;

} // namespace

// static
V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); }

V8Platform::V8Platform() {}

V8Platform::~V8Platform() {}

void V8Platform::CallOnBackgroundThread(
v8::Task* task,
v8::Platform::ExpectedRuntime expected_runtime) {
base::WorkerPool::PostTask(
FROM_HERE,
base::Bind(&v8::Task::Run, base::Owned(task)),
expected_runtime == v8::Platform::kLongRunningTask);
}

void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) {
PerIsolateData::From(isolate)->message_loop_proxy()->PostTask(
FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task)));
}

} // namespace gin

0 comments on commit b64e521

Please sign in to comment.