1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GIN_FUNCTION_TEMPLATE_H_
6 #define GIN_FUNCTION_TEMPLATE_H_
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "gin/arguments.h"
11 #include "gin/converter.h"
12 #include "gin/gin_export.h"
13 #include "v8/include/v8.h"
19 enum CreateFunctionTemplateFlags
{
20 HolderIsFirstArgument
= 1 << 0,
26 struct CallbackParamTraits
{
30 struct CallbackParamTraits
<const T
&> {
34 struct CallbackParamTraits
<const T
*> {
39 // CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
40 // CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to
41 // DispatchToCallback, where it is invoked.
43 // This simple base class is used so that we can share a single object template
44 // among every CallbackHolder instance.
45 class GIN_EXPORT CallbackHolderBase
{
47 v8::Handle
<v8::External
> GetHandle(v8::Isolate
* isolate
);
50 explicit CallbackHolderBase(v8::Isolate
* isolate
);
51 virtual ~CallbackHolderBase();
54 static void WeakCallback(
55 const v8::WeakCallbackData
<v8::External
, CallbackHolderBase
>& data
);
57 v8::Persistent
<v8::External
> v8_ref_
;
59 DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase
);
62 template<typename Sig
>
63 class CallbackHolder
: public CallbackHolderBase
{
65 CallbackHolder(v8::Isolate
* isolate
,
66 const base::Callback
<Sig
>& callback
,
68 : CallbackHolderBase(isolate
), callback(callback
), flags(flags
) {}
69 base::Callback
<Sig
> callback
;
72 virtual ~CallbackHolder() {}
74 DISALLOW_COPY_AND_ASSIGN(CallbackHolder
);
78 bool GetNextArgument(Arguments
* args
, int create_flags
, bool is_first
,
80 if (is_first
&& (create_flags
& HolderIsFirstArgument
) != 0) {
81 return args
->GetHolder(result
);
83 return args
->GetNext(result
);
87 // For advanced use cases, we allow callers to request the unparsed Arguments
88 // object and poke around in it directly.
89 inline bool GetNextArgument(Arguments
* args
, int create_flags
, bool is_first
,
94 inline bool GetNextArgument(Arguments
* args
, int create_flags
, bool is_first
,
100 // It's common for clients to just need the isolate, so we make that easy.
101 inline bool GetNextArgument(Arguments
* args
, int create_flags
,
102 bool is_first
, v8::Isolate
** result
) {
103 *result
= args
->isolate();
107 // Classes for generating and storing an argument pack of integer indices
108 // (based on well-known "indices trick", see: http://goo.gl/bKKojn):
109 template <size_t... indices
>
110 struct IndicesHolder
{};
112 template <size_t requested_index
, size_t... indices
>
113 struct IndicesGenerator
{
114 using type
= typename IndicesGenerator
<requested_index
- 1,
118 template <size_t... indices
>
119 struct IndicesGenerator
<0, indices
...> {
120 using type
= IndicesHolder
<indices
...>;
123 // Class template for extracting and storing single argument for callback
124 // at position |index|.
125 template <size_t index
, typename ArgType
>
126 struct ArgumentHolder
{
127 using ArgLocalType
= typename CallbackParamTraits
<ArgType
>::LocalType
;
132 ArgumentHolder(Arguments
* args
, int create_flags
)
133 : ok(GetNextArgument(args
, create_flags
, index
== 0, &value
)) {
139 // Class template for converting arguments from JavaScript to C++ and running
140 // the callback with them.
141 template <typename IndicesType
, typename
... ArgTypes
>
144 template <size_t... indices
, typename
... ArgTypes
>
145 class Invoker
<IndicesHolder
<indices
...>, ArgTypes
...>
146 : public ArgumentHolder
<indices
, ArgTypes
>... {
148 // Invoker<> inherits from ArgumentHolder<> for each argument.
149 // C++ has always been strict about the class initialization order,
150 // so it is guaranteed ArgumentHolders will be initialized (and thus, will
151 // extract arguments from Arguments) in the right order.
152 Invoker(Arguments
* args
, int create_flags
)
153 : ArgumentHolder
<indices
, ArgTypes
>(args
, create_flags
)..., args_(args
) {
154 // GCC thinks that create_flags is going unused, even though the
155 // expansion above clearly makes use of it. Per jyasskin@, casting
156 // to void is the commonly accepted way to convince the compiler
157 // that you're actually using a parameter/varible.
162 return And(ArgumentHolder
<indices
, ArgTypes
>::ok
...);
165 template <typename ReturnType
>
166 void DispatchToCallback(base::Callback
<ReturnType(ArgTypes
...)> callback
) {
167 args_
->Return(callback
.Run(ArgumentHolder
<indices
, ArgTypes
>::value
...));
170 // In C++, you can declare the function foo(void), but you can't pass a void
171 // expression to foo. As a result, we must specialize the case of Callbacks
172 // that have the void return type.
173 void DispatchToCallback(base::Callback
<void(ArgTypes
...)> callback
) {
174 callback
.Run(ArgumentHolder
<indices
, ArgTypes
>::value
...);
178 static bool And() { return true; }
179 template <typename
... T
>
180 static bool And(bool arg1
, T
... args
) {
181 return arg1
&& And(args
...);
187 // DispatchToCallback converts all the JavaScript arguments to C++ types and
188 // invokes the base::Callback.
189 template <typename Sig
>
190 struct Dispatcher
{};
192 template <typename ReturnType
, typename
... ArgTypes
>
193 struct Dispatcher
<ReturnType(ArgTypes
...)> {
194 static void DispatchToCallback(
195 const v8::FunctionCallbackInfo
<v8::Value
>& info
) {
196 Arguments
args(info
);
197 v8::Handle
<v8::External
> v8_holder
;
198 CHECK(args
.GetData(&v8_holder
));
199 CallbackHolderBase
* holder_base
= reinterpret_cast<CallbackHolderBase
*>(
202 typedef CallbackHolder
<ReturnType(ArgTypes
...)> HolderT
;
203 HolderT
* holder
= static_cast<HolderT
*>(holder_base
);
205 using Indices
= typename IndicesGenerator
<sizeof...(ArgTypes
)>::type
;
206 Invoker
<Indices
, ArgTypes
...> invoker(&args
, holder
->flags
);
208 invoker
.DispatchToCallback(holder
->callback
);
212 } // namespace internal
215 // CreateFunctionTemplate creates a v8::FunctionTemplate that will create
216 // JavaScript functions that execute a provided C++ function or base::Callback.
217 // JavaScript arguments are automatically converted via gin::Converter, as is
218 // the return value of the C++ function, if any.
219 template<typename Sig
>
220 v8::Local
<v8::FunctionTemplate
> CreateFunctionTemplate(
221 v8::Isolate
* isolate
, const base::Callback
<Sig
> callback
,
222 int callback_flags
= 0) {
223 typedef internal::CallbackHolder
<Sig
> HolderT
;
224 HolderT
* holder
= new HolderT(isolate
, callback
, callback_flags
);
226 return v8::FunctionTemplate::New(
228 &internal::Dispatcher
<Sig
>::DispatchToCallback
,
229 ConvertToV8
<v8::Handle
<v8::External
> >(isolate
,
230 holder
->GetHandle(isolate
)));
233 // CreateFunctionHandler installs a CallAsFunction handler on the given
234 // object template that forwards to a provided C++ function or base::Callback.
235 template<typename Sig
>
236 void CreateFunctionHandler(v8::Isolate
* isolate
,
237 v8::Local
<v8::ObjectTemplate
> tmpl
,
238 const base::Callback
<Sig
> callback
,
239 int callback_flags
= 0) {
240 typedef internal::CallbackHolder
<Sig
> HolderT
;
241 HolderT
* holder
= new HolderT(isolate
, callback
, callback_flags
);
242 tmpl
->SetCallAsFunctionHandler(&internal::Dispatcher
<Sig
>::DispatchToCallback
,
243 ConvertToV8
<v8::Handle
<v8::External
> >(
244 isolate
, holder
->GetHandle(isolate
)));
249 #endif // GIN_FUNCTION_TEMPLATE_H_