1 $$ This is a pump file for generating file templates. Pump is a python
2 $$ script that is part of the Google Test suite of utilities. Description
5 $$ http://code.google.com/p/googletest/wiki/PumpManual
8 #ifndef GIN_FUNCTION_TEMPLATE_H_
9 #define GIN_FUNCTION_TEMPLATE_H_
13 // Copyright 2013 The Chromium Authors. All rights reserved.
14 // Use of this source code is governed by a BSD-style license that can be
15 // found in the LICENSE file.
17 #include "base/callback.h"
18 #include "base/logging.h"
19 #include "gin/arguments.h"
20 #include "gin/converter.h"
21 #include "gin/gin_export.h"
22 #include "gin/handle.h"
23 #include "gin/public/gin_embedders.h"
24 #include "gin/public/wrapper_info.h"
25 #include "gin/wrappable.h"
27 #include "v8/include/v8.h"
33 enum CreateFunctionTemplateFlags {
34 HolderIsFirstArgument = 1 << 0,
40 struct CallbackParamTraits {
44 struct CallbackParamTraits<const T&> {
48 struct CallbackParamTraits<const T*> {
53 // CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
54 // CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to
55 // DispatchToCallback, where it is invoked.
57 // v8::FunctionTemplate only supports passing void* as data so how do we know
58 // when to delete the base::Callback? That's where CallbackHolderBase comes in.
59 // It inherits from Wrappable, which delete itself when both (a) the refcount
60 // via base::RefCounted has dropped to zero, and (b) there are no more
61 // JavaScript references in V8.
63 // This simple base class is used so that we can share a single object template
64 // among every CallbackHolder instance.
65 class GIN_EXPORT CallbackHolderBase : public Wrappable<CallbackHolderBase> {
67 static WrapperInfo kWrapperInfo;
69 virtual ~CallbackHolderBase() {}
72 template<typename Sig>
73 class CallbackHolder : public CallbackHolderBase {
75 CallbackHolder(const base::Callback<Sig>& callback, int flags)
76 : callback(callback), flags(flags) {}
77 base::Callback<Sig> callback;
80 virtual ~CallbackHolder() {}
84 // This set of templates invokes a base::Callback, converts the return type to a
85 // JavaScript value, and returns that value to script via the provided
86 // gin::Arguments object.
88 // In C++, you can declare the function foo(void), but you can't pass a void
89 // expression to foo. As a result, we must specialize the case of Callbacks that
90 // have the void return type.
92 $range ARITY 0..MAX_ARITY
94 $var INV_ARITY = MAX_ARITY - ARITY
95 $range ARG 1..INV_ARITY
96 $range VOID INV_ARITY+1..MAX_ARITY
99 template<typename R$for ARG [[, typename P$(ARG) = void]]>
102 template<typename R$for ARG [[, typename P$(ARG)]]>
103 struct Invoker<R$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
106 inline static void Go(
108 const base::Callback<R($for ARG , [[P$(ARG)]])>& callback$for ARG [[,
109 const P$(ARG)& a$(ARG)]]) {
110 args->Return(callback.Run($for ARG, [[a$(ARG)]]));
113 template<$for ARG , [[typename P$(ARG)]]>
114 struct Invoker<void$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
115 inline static void Go(
117 const base::Callback<void($for ARG , [[P$(ARG)]])>& callback$for ARG [[,
118 const P$(ARG)& a$(ARG)]]) {
119 callback.Run($for ARG, [[a$(ARG)]]);
127 bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
129 if (is_first && (create_flags & HolderIsFirstArgument) != 0) {
130 return args->GetHolder(result);
132 return args->GetNext(result);
136 // For advanced use cases, we allow callers to request the unparsed Arguments
137 // object and poke around in it directly.
138 inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
143 inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
144 Arguments** result) {
149 // It's common for clients to just need the isolate, so we make that easy.
150 inline bool GetNextArgument(Arguments* args, int create_flags,
151 bool is_first, v8::Isolate** result) {
152 *result = args->isolate();
157 // DispatchToCallback converts all the JavaScript arguments to C++ types and
158 // invokes the base::Callback.
159 template<typename Sig>
163 $range ARITY 0..MAX_ARITY
167 template<typename R$for ARG [[, typename P$(ARG)]]>
168 struct Dispatcher<R($for ARG , [[P$(ARG)]])> {
169 static void DispatchToCallback(
170 const v8::FunctionCallbackInfo<v8::Value>& info) {
171 Arguments args(info);
172 CallbackHolderBase* holder_base = NULL;
173 CHECK(args.GetData(&holder_base));
175 typedef CallbackHolder<R($for ARG , [[P$(ARG)]])> HolderT;
176 HolderT* holder = static_cast<HolderT*>(holder_base);
181 $for ARG [[ typename CallbackParamTraits<P$(ARG)>::LocalType a$(ARG);
185 [[!GetNextArgument(&args, holder->flags, $if ARG == 1 [[true]] $else [[false]], &a$(ARG))]]) {
192 Invoker<R$for ARG [[, P$(ARG)]]>::Go(&args, holder->callback$for ARG [[, a$(ARG)]]);
198 } // namespace internal
201 // CreateFunctionTemplate creates a v8::FunctionTemplate that will create
202 // JavaScript functions that execute a provided C++ function or base::Callback.
203 // JavaScript arguments are automatically converted via gin::Converter, as is
204 // the return value of the C++ function, if any.
205 template<typename Sig>
206 v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
207 v8::Isolate* isolate, const base::Callback<Sig> callback,
208 int callback_flags = 0) {
209 typedef internal::CallbackHolder<Sig> HolderT;
210 gin::Handle<HolderT> holder = CreateHandle(
211 isolate, new HolderT(callback, callback_flags));
212 return v8::FunctionTemplate::New(
214 &internal::Dispatcher<Sig>::DispatchToCallback,
215 ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
220 #endif // GIN_FUNCTION_TEMPLATE_H_