electron/native_mate/callback.h.pump

134 lines
3.7 KiB
Text
Raw Normal View History

2014-08-10 11:05:25 +00:00
$$ 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;
// 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 V$for ARG [[, typename P$(ARG)]]>
struct V8FunctionInvoker<v8::Local<V>($for ARG , [[P$(ARG)]])> {
static v8::Local<V> Go(v8::Isolate* isolate, SafeV8Function function$for ARG [[, P$(ARG) a$(ARG)]]) {
Locker locker(isolate);
v8::EscapableHandleScope handle_scope(isolate);
v8::Local<v8::Function> holder = function->NewHandle();
2015-05-29 05:41:59 +00:00
v8::Local<v8::Context> context = holder->CreationContext();
v8::Context::Scope context_scope(context);
$if ARITY == 0 [[
v8::Local<v8::Value> val(holder->Call(holder, 0, NULL));
]] $else [[
v8::Local<v8::Value> args[] = {
$for ARG [[
ConvertToV8(isolate, a$(ARG)),
]]
};
v8::Local<v8::Value> val(holder->Call(holder, arraysize(args), args));
]]
return handle_scope.Escape(val);
}
};
2014-08-10 11:05:25 +00:00
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);
2015-05-22 11:11:02 +00:00
v8::Local<v8::Function> holder = function->NewHandle();
2015-05-29 05:41:59 +00:00
v8::Local<v8::Context> context = holder->CreationContext();
v8::Context::Scope context_scope(context);
2014-08-10 11:05:25 +00:00
$if ARITY == 0 [[
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> val(holder->Call(holder, 0, NULL));
2014-08-10 11:05:25 +00:00
]] $else [[
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> args[] = {
2014-08-10 11:05:25 +00:00
$for ARG [[
ConvertToV8(isolate, a$(ARG)),
]]
};
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> val(holder->Call(holder, arraysize(args), args));
2014-08-10 11:05:25 +00:00
]]
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);
2015-05-22 11:11:02 +00:00
v8::Local<v8::Function> holder = function->NewHandle();
2015-05-29 05:41:59 +00:00
v8::Local<v8::Context> context = holder->CreationContext();
v8::Context::Scope context_scope(context);
2014-08-10 11:05:25 +00:00
$if ARITY == 0 [[
holder->Call(holder, 0, NULL);
]] $else [[
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> args[] = {
2014-08-10 11:05:25 +00:00
$for ARG [[
ConvertToV8(isolate, a$(ARG)),
]]
};
holder->Call(holder, arraysize(args), args);
]]
}
};
]]
} // namespace internal
template<typename Sig>
struct Converter<base::Callback<Sig> > {
2015-05-22 11:11:02 +00:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-08-10 11:05:25 +00:00
const base::Callback<Sig>& val) {
return CreateFunctionTemplate(isolate, val)->GetFunction();
}
static bool FromV8(v8::Isolate* isolate,
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> val,
2014-08-10 11:05:25 +00:00
base::Callback<Sig>* out) {
if (!val->IsFunction())
return false;
internal::SafeV8Function function(
2015-02-12 04:55:50 +00:00
new RefCountedPersistent<v8::Function>(isolate, val));
2014-08-10 11:05:25 +00:00
*out = base::Bind(&internal::V8FunctionInvoker<Sig>::Go, isolate, function);
return true;
}
};
} // namespace mate