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
)) {
135 // Ideally we would include the expected c++ type in the error
136 // message which we can access via typeid(ArgType).name()
137 // however we compile with no-rtti, which disables typeid.
143 // Class template for converting arguments from JavaScript to C++ and running
144 // the callback with them.
145 template <typename IndicesType
, typename
... ArgTypes
>
148 template <size_t... indices
, typename
... ArgTypes
>
149 class Invoker
<IndicesHolder
<indices
...>, ArgTypes
...>
150 : public ArgumentHolder
<indices
, ArgTypes
>... {
152 // Invoker<> inherits from ArgumentHolder<> for each argument.
153 // C++ has always been strict about the class initialization order,
154 // so it is guaranteed ArgumentHolders will be initialized (and thus, will
155 // extract arguments from Arguments) in the right order.
156 Invoker(Arguments
* args
, int create_flags
)
157 : ArgumentHolder
<indices
, ArgTypes
>(args
, create_flags
)..., args_(args
) {
158 // GCC thinks that create_flags is going unused, even though the
159 // expansion above clearly makes use of it. Per jyasskin@, casting
160 // to void is the commonly accepted way to convince the compiler
161 // that you're actually using a parameter/varible.
166 return And(ArgumentHolder
<indices
, ArgTypes
>::ok
...);
169 template <typename ReturnType
>
170 void DispatchToCallback(base::Callback
<ReturnType(ArgTypes
...)> callback
) {
171 args_
->Return(callback
.Run(ArgumentHolder
<indices
, ArgTypes
>::value
...));
174 // In C++, you can declare the function foo(void), but you can't pass a void
175 // expression to foo. As a result, we must specialize the case of Callbacks
176 // that have the void return type.
177 void DispatchToCallback(base::Callback
<void(ArgTypes
...)> callback
) {
178 callback
.Run(ArgumentHolder
<indices
, ArgTypes
>::value
...);
182 static bool And() { return true; }
183 template <typename
... T
>
184 static bool And(bool arg1
, T
... args
) {
185 return arg1
&& And(args
...);
191 // DispatchToCallback converts all the JavaScript arguments to C++ types and
192 // invokes the base::Callback.
193 template <typename Sig
>
194 struct Dispatcher
{};
196 template <typename ReturnType
, typename
... ArgTypes
>
197 struct Dispatcher
<ReturnType(ArgTypes
...)> {
198 static void DispatchToCallback(
199 const v8::FunctionCallbackInfo
<v8::Value
>& info
) {
200 Arguments
args(info
);
201 v8::Handle
<v8::External
> v8_holder
;
202 CHECK(args
.GetData(&v8_holder
));
203 CallbackHolderBase
* holder_base
= reinterpret_cast<CallbackHolderBase
*>(
206 typedef CallbackHolder
<ReturnType(ArgTypes
...)> HolderT
;
207 HolderT
* holder
= static_cast<HolderT
*>(holder_base
);
209 using Indices
= typename IndicesGenerator
<sizeof...(ArgTypes
)>::type
;
210 Invoker
<Indices
, ArgTypes
...> invoker(&args
, holder
->flags
);
212 invoker
.DispatchToCallback(holder
->callback
);
216 } // namespace internal
219 // CreateFunctionTemplate creates a v8::FunctionTemplate that will create
220 // JavaScript functions that execute a provided C++ function or base::Callback.
221 // JavaScript arguments are automatically converted via gin::Converter, as is
222 // the return value of the C++ function, if any.
224 // NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own
225 // internal reasons, thus it is generally a good idea to cache the template
226 // returned by this function. Otherwise, repeated method invocations from JS
227 // will create substantial memory leaks. See http://crbug.com/463487.
228 template<typename Sig
>
229 v8::Local
<v8::FunctionTemplate
> CreateFunctionTemplate(
230 v8::Isolate
* isolate
, const base::Callback
<Sig
> callback
,
231 int callback_flags
= 0) {
232 typedef internal::CallbackHolder
<Sig
> HolderT
;
233 HolderT
* holder
= new HolderT(isolate
, callback
, callback_flags
);
235 return v8::FunctionTemplate::New(
237 &internal::Dispatcher
<Sig
>::DispatchToCallback
,
238 ConvertToV8
<v8::Handle
<v8::External
> >(isolate
,
239 holder
->GetHandle(isolate
)));
242 // CreateFunctionHandler installs a CallAsFunction handler on the given
243 // object template that forwards to a provided C++ function or base::Callback.
244 template<typename Sig
>
245 void CreateFunctionHandler(v8::Isolate
* isolate
,
246 v8::Local
<v8::ObjectTemplate
> tmpl
,
247 const base::Callback
<Sig
> callback
,
248 int callback_flags
= 0) {
249 typedef internal::CallbackHolder
<Sig
> HolderT
;
250 HolderT
* holder
= new HolderT(isolate
, callback
, callback_flags
);
251 tmpl
->SetCallAsFunctionHandler(&internal::Dispatcher
<Sig
>::DispatchToCallback
,
252 ConvertToV8
<v8::Handle
<v8::External
> >(
253 isolate
, holder
->GetHandle(isolate
)));
258 #endif // GIN_FUNCTION_TEMPLATE_H_