More TSan v2 suppressions:
[chromium-blink-merge.git] / gin / function_template.h.pump
blobb074bdefd5df05c4598a38c5f360ddccdc88e691
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
3 $$ can be found here:
4 $$
5 $$ http://code.google.com/p/googletest/wiki/PumpManual
6 $$
8 #ifndef GIN_FUNCTION_TEMPLATE_H_
9 #define GIN_FUNCTION_TEMPLATE_H_
11 $var MAX_ARITY = 6
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"
29 namespace gin {
31 class PerIsolateData;
33 enum CreateFunctionTemplateFlags {
34   HolderIsFirstArgument = 1 << 0,
37 namespace internal {
39 template<typename T>
40 struct CallbackParamTraits {
41   typedef T LocalType;
43 template<typename T>
44 struct CallbackParamTraits<const T&> {
45   typedef T LocalType;
47 template<typename T>
48 struct CallbackParamTraits<const T*> {
49   typedef T* LocalType;
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> {
66  public:
67   static WrapperInfo kWrapperInfo;
68  protected:
69   virtual ~CallbackHolderBase() {}
72 template<typename Sig>
73 class CallbackHolder : public CallbackHolderBase {
74  public:
75   CallbackHolder(const base::Callback<Sig>& callback, int flags)
76       : callback(callback), flags(flags) {}
77   base::Callback<Sig> callback;
78   int flags;
79  private:
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
93 $for ARITY [[
94 $var INV_ARITY = MAX_ARITY - ARITY
95 $range ARG 1..INV_ARITY
96 $range VOID INV_ARITY+1..MAX_ARITY
98 $if ARITY == 0 [[
99 template<typename R$for ARG [[, typename P$(ARG) = void]]>
100 struct Invoker {
101 ]] $else [[
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(
107       Arguments* args,
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)]]));
111   }
113 template<$for ARG , [[typename P$(ARG)]]>
114 struct Invoker<void$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
115   inline static void Go(
116       Arguments* args,
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)]]);
120   }
126 template<typename T>
127 bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
128                      T* result) {
129   if (is_first && (create_flags & HolderIsFirstArgument) != 0) {
130     return args->GetHolder(result);
131   } else {
132     return args->GetNext(result);
133   }
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,
139                             Arguments* result) {
140   *result = *args;
141   return true;
143 inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
144                             Arguments** result) {
145   *result = args;
146   return true;
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();
153   return true;
157 // DispatchToCallback converts all the JavaScript arguments to C++ types and
158 // invokes the base::Callback.
159 template<typename Sig>
160 struct Dispatcher {
163 $range ARITY 0..MAX_ARITY
164 $for ARITY [[
165 $range ARG 1..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);
178 $if ARITY != 0 [[
181 $for ARG [[    typename CallbackParamTraits<P$(ARG)>::LocalType a$(ARG);
184     if ($for ARG  ||
185         [[!GetNextArgument(&args, holder->flags, $if ARG == 1 [[true]] $else [[false]], &a$(ARG))]]) {
186       args.ThrowError();
187       return;
188     }
192     Invoker<R$for ARG [[, P$(ARG)]]>::Go(&args, holder->callback$for ARG [[, a$(ARG)]]);
193   }
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(
213       isolate,
214       &internal::Dispatcher<Sig>::DispatchToCallback,
215       ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
218 }  // namespace gin
220 #endif  // GIN_FUNCTION_TEMPLATE_H_