Add callback converter.
This commit is contained in:
parent
5d6921ff2a
commit
1387d3969e
6 changed files with 453 additions and 1 deletions
291
native_mate/callback.h
Normal file
291
native_mate/callback.h
Normal file
|
@ -0,0 +1,291 @@
|
|||
// This file was GENERATED by command:
|
||||
// pump.py callback.h.pump
|
||||
// DO NOT EDIT BY HAND!!!
|
||||
|
||||
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "native_mate/function_template.h"
|
||||
#include "native_mate/locker.h"
|
||||
#include "native_mate/scoped_persistent.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
namespace internal {
|
||||
|
||||
typedef scoped_refptr<RefCountedPersistent<v8::Function> > SafeV8Function;
|
||||
|
||||
// Helper to convert type to V8 with storage type (const T& to T).
|
||||
template<typename T>
|
||||
v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, T a) {
|
||||
return Converter<typename base::internal::CallbackParamTraits<T>::StorageType>
|
||||
::ToV8(isolate, a);
|
||||
}
|
||||
|
||||
// This set of templates invokes a V8::Function by converting the C++ types.
|
||||
template<typename Sig>
|
||||
struct V8FunctionInvoker;
|
||||
|
||||
template<typename R>
|
||||
struct V8FunctionInvoker<R()> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, 0, NULL));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct V8FunctionInvoker<void()> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
holder->Call(holder, 0, NULL);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1>
|
||||
struct V8FunctionInvoker<R(P1)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1>
|
||||
struct V8FunctionInvoker<void(P1)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2>
|
||||
struct V8FunctionInvoker<R(P1, P2)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2>
|
||||
struct V8FunctionInvoker<void(P1, P2)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2, typename P3>
|
||||
struct V8FunctionInvoker<R(P1, P2, P3)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2, typename P3>
|
||||
struct V8FunctionInvoker<void(P1, P2, P3)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2, typename P3, typename P4>
|
||||
struct V8FunctionInvoker<R(P1, P2, P3, P4)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2, typename P3, typename P4>
|
||||
struct V8FunctionInvoker<void(P1, P2, P3, P4)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2, typename P3, typename P4,
|
||||
typename P5>
|
||||
struct V8FunctionInvoker<R(P1, P2, P3, P4, P5)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4, P5 a5) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
ConvertToV8(isolate, a5),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2, typename P3, typename P4, typename P5>
|
||||
struct V8FunctionInvoker<void(P1, P2, P3, P4, P5)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4, P5 a5) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
ConvertToV8(isolate, a5),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2, typename P3, typename P4,
|
||||
typename P5, typename P6>
|
||||
struct V8FunctionInvoker<R(P1, P2, P3, P4, P5, P6)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4, P5 a5, P6 a6) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
ConvertToV8(isolate, a5),
|
||||
ConvertToV8(isolate, a6),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
||||
typename P6>
|
||||
struct V8FunctionInvoker<void(P1, P2, P3, P4, P5, P6)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4, P5 a5, P6 a6) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
ConvertToV8(isolate, a5),
|
||||
ConvertToV8(isolate, a6),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
template<typename Sig>
|
||||
struct Converter<base::Callback<Sig> > {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const base::Callback<Sig>& val) {
|
||||
return CreateFunctionTemplate(isolate, val)->GetFunction();
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::Callback<Sig>* out) {
|
||||
if (!val->IsFunction())
|
||||
return false;
|
||||
|
||||
internal::SafeV8Function function(
|
||||
new RefCountedPersistent<v8::Function>(val));
|
||||
*out = base::Bind(&internal::V8FunctionInvoker<Sig>::Go, isolate, function);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
111
native_mate/callback.h.pump
Normal file
111
native_mate/callback.h.pump
Normal file
|
@ -0,0 +1,111 @@
|
|||
$$ This is a pump file for generating file templates. Pump is a python
|
||||
$$ script that is part of the Google Test suite of utilities. Description
|
||||
$$ can be found here:
|
||||
$$
|
||||
$$ http://code.google.com/p/googletest/wiki/PumpManual
|
||||
$$
|
||||
$var MAX_ARITY = 6
|
||||
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "native_mate/function_template.h"
|
||||
#include "native_mate/locker.h"
|
||||
#include "native_mate/scoped_persistent.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
namespace internal {
|
||||
|
||||
typedef scoped_refptr<RefCountedPersistent<v8::Function> > SafeV8Function;
|
||||
|
||||
// Helper to convert type to V8 with storage type (const T& to T).
|
||||
template<typename T>
|
||||
v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, T a) {
|
||||
return Converter<typename base::internal::CallbackParamTraits<T>::StorageType>
|
||||
::ToV8(isolate, a);
|
||||
}
|
||||
|
||||
// This set of templates invokes a V8::Function by converting the C++ types.
|
||||
template<typename Sig>
|
||||
struct V8FunctionInvoker;
|
||||
|
||||
$range ARITY 0..MAX_ARITY
|
||||
$for ARITY [[
|
||||
$range ARG 1..ARITY
|
||||
|
||||
template<typename R$for ARG [[, typename P$(ARG)]]>
|
||||
struct V8FunctionInvoker<R($for ARG , [[P$(ARG)]])> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function$for ARG [[, P$(ARG) a$(ARG)]]) {
|
||||
R ret;
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
|
||||
$if ARITY == 0 [[
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, 0, NULL));
|
||||
]] $else [[
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
$for ARG [[
|
||||
|
||||
ConvertToV8(isolate, a$(ARG)),
|
||||
]]
|
||||
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
]]
|
||||
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<$for ARG , [[typename P$(ARG)]]>
|
||||
struct V8FunctionInvoker<void($for ARG , [[P$(ARG)]])> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function$for ARG [[, P$(ARG) a$(ARG)]]) {
|
||||
Locker locker(isolate);
|
||||
MATE_HANDLE_SCOPE(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
|
||||
$if ARITY == 0 [[
|
||||
holder->Call(holder, 0, NULL);
|
||||
]] $else [[
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
$for ARG [[
|
||||
|
||||
ConvertToV8(isolate, a$(ARG)),
|
||||
]]
|
||||
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
]]
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
]]
|
||||
|
||||
} // namespace internal
|
||||
|
||||
template<typename Sig>
|
||||
struct Converter<base::Callback<Sig> > {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const base::Callback<Sig>& val) {
|
||||
return CreateFunctionTemplate(isolate, val)->GetFunction();
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::Callback<Sig>* out) {
|
||||
if (!val->IsFunction())
|
||||
return false;
|
||||
|
||||
internal::SafeV8Function function(
|
||||
new RefCountedPersistent<v8::Function>(val));
|
||||
*out = base::Bind(&internal::V8FunctionInvoker<Sig>::Go, isolate, function);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
17
native_mate/locker.cc
Normal file
17
native_mate/locker.cc
Normal file
|
@ -0,0 +1,17 @@
|
|||
// 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.chromium file.
|
||||
|
||||
#include "native_mate/locker.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
Locker::Locker(v8::Isolate* isolate) {
|
||||
if (v8::Locker::IsActive())
|
||||
locker_.reset(new v8::Locker(isolate));
|
||||
}
|
||||
|
||||
Locker::~Locker() {
|
||||
}
|
||||
|
||||
} // namespace mate
|
30
native_mate/locker.h
Normal file
30
native_mate/locker.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
// 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.chromium file.
|
||||
|
||||
#ifndef NATIVE_MATE_LOCKER_H_
|
||||
#define NATIVE_MATE_LOCKER_H_
|
||||
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
// Only lock when lockers are used in current thread.
|
||||
class Locker {
|
||||
public:
|
||||
explicit Locker(v8::Isolate* isolate);
|
||||
~Locker();
|
||||
|
||||
private:
|
||||
void* operator new(size_t size);
|
||||
void operator delete(void*, size_t);
|
||||
|
||||
scoped_ptr<v8::Locker> locker_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Locker);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // NATIVE_MATE_LOCKER_H_
|
|
@ -56,7 +56,7 @@ class ScopedPersistent {
|
|||
v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
|
||||
if (handle_.IsEmpty())
|
||||
return v8::Local<T>();
|
||||
return v8::Local<T>::New(isolate, handle_);
|
||||
return MATE_PERSISTENT_TO_LOCAL(T, isolate, handle_);
|
||||
}
|
||||
|
||||
template<typename P, typename C>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
'native_mate_files': [
|
||||
'native_mate/arguments.cc',
|
||||
'native_mate/arguments.h',
|
||||
'native_mate/callback.h',
|
||||
'native_mate/compat.h',
|
||||
'native_mate/constructor.h',
|
||||
'native_mate/converter.cc',
|
||||
|
@ -12,6 +13,8 @@
|
|||
'native_mate/function_template.cc',
|
||||
'native_mate/function_template.h',
|
||||
'native_mate/handle.h',
|
||||
'native_mate/locker.cc',
|
||||
'native_mate/locker.h',
|
||||
'native_mate/object_template_builder.cc',
|
||||
'native_mate/object_template_builder.h',
|
||||
'native_mate/scoped_persistent.h',
|
||||
|
|
Loading…
Reference in a new issue