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
)...,
157 return And(ArgumentHolder
<indices
, ArgTypes
>::ok
...);
160 template <typename ReturnType
>
161 void DispatchToCallback(base::Callback
<ReturnType(ArgTypes
...)> callback
) {
162 args_
->Return(callback
.Run(ArgumentHolder
<indices
, ArgTypes
>::value
...));
165 // In C++, you can declare the function foo(void), but you can't pass a void
166 // expression to foo. As a result, we must specialize the case of Callbacks
167 // that have the void return type.
168 void DispatchToCallback(base::Callback
<void(ArgTypes
...)> callback
) {
169 callback
.Run(ArgumentHolder
<indices
, ArgTypes
>::value
...);
173 static bool And() { return true; }
174 template <typename
... T
>
175 static bool And(bool arg1
, T
... args
) {
176 return arg1
&& And(args
...);
182 // DispatchToCallback converts all the JavaScript arguments to C++ types and
183 // invokes the base::Callback.
184 template <typename Sig
>
185 struct Dispatcher
{};
187 template <typename ReturnType
, typename
... ArgTypes
>
188 struct Dispatcher
<ReturnType(ArgTypes
...)> {
189 static void DispatchToCallback(
190 const v8::FunctionCallbackInfo
<v8::Value
>& info
) {
191 Arguments
args(info
);
192 v8::Handle
<v8::External
> v8_holder
;
193 CHECK(args
.GetData(&v8_holder
));
194 CallbackHolderBase
* holder_base
= reinterpret_cast<CallbackHolderBase
*>(
197 typedef CallbackHolder
<ReturnType(ArgTypes
...)> HolderT
;
198 HolderT
* holder
= static_cast<HolderT
*>(holder_base
);
200 using Indices
= typename IndicesGenerator
<sizeof...(ArgTypes
)>::type
;
201 Invoker
<Indices
, ArgTypes
...> invoker(&args
, holder
->flags
);
203 invoker
.DispatchToCallback(holder
->callback
);
207 } // namespace internal
210 // CreateFunctionTemplate creates a v8::FunctionTemplate that will create
211 // JavaScript functions that execute a provided C++ function or base::Callback.
212 // JavaScript arguments are automatically converted via gin::Converter, as is
213 // the return value of the C++ function, if any.
214 template<typename Sig
>
215 v8::Local
<v8::FunctionTemplate
> CreateFunctionTemplate(
216 v8::Isolate
* isolate
, const base::Callback
<Sig
> callback
,
217 int callback_flags
= 0) {
218 typedef internal::CallbackHolder
<Sig
> HolderT
;
219 HolderT
* holder
= new HolderT(isolate
, callback
, callback_flags
);
221 return v8::FunctionTemplate::New(
223 &internal::Dispatcher
<Sig
>::DispatchToCallback
,
224 ConvertToV8
<v8::Handle
<v8::External
> >(isolate
,
225 holder
->GetHandle(isolate
)));
228 // CreateFunctionHandler installs a CallAsFunction handler on the given
229 // object template that forwards to a provided C++ function or base::Callback.
230 template<typename Sig
>
231 void CreateFunctionHandler(v8::Isolate
* isolate
,
232 v8::Local
<v8::ObjectTemplate
> tmpl
,
233 const base::Callback
<Sig
> callback
,
234 int callback_flags
= 0) {
235 typedef internal::CallbackHolder
<Sig
> HolderT
;
236 HolderT
* holder
= new HolderT(isolate
, callback
, callback_flags
);
237 tmpl
->SetCallAsFunctionHandler(&internal::Dispatcher
<Sig
>::DispatchToCallback
,
238 ConvertToV8
<v8::Handle
<v8::External
> >(
239 isolate
, holder
->GetHandle(isolate
)));
244 #endif // GIN_FUNCTION_TEMPLATE_H_