Convert the rest of the functions in core.cc to use CreateFunctionTemplate.
[chromium-blink-merge.git] / gin / function_template.h.pump
blob5812c80d9c9ce3ecb3185f7bcbeb1411d415ce78
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 $var MAX_ARITY = 4
10 // Copyright 2013 The Chromium Authors. All rights reserved.
11 // Use of this source code is governed by a BSD-style license that can be
12 // found in the LICENSE file.
14 #include "base/callback.h"
15 #include "base/logging.h"
16 #include "gin/arguments.h"
17 #include "gin/converter.h"
18 #include "gin/public/gin_embedders.h"
19 #include "gin/public/wrapper_info.h"
20 #include "gin/wrappable.h"
22 #include "v8/include/v8.h"
24 namespace gin {
26 class PerIsolateData;
28 namespace internal {
30 template<typename T>
31 struct RemoveConstRef {
32   typedef T Type;
34 template<typename T>
35 struct RemoveConstRef<const T&> {
36   typedef T Type;
40 // CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
41 // CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to 
42 // DispatchToCallback, where it is invoked.
44 // v8::FunctionTemplate only supports passing void* as data so how do we know
45 // when to delete the base::Callback? That's where CallbackHolderBase comes in.
46 // It inherits from Wrappable, which delete itself when both (a) the refcount
47 // via base::RefCounted has dropped to zero, and (b) there are no more
48 // JavaScript references in V8.
49 class CallbackHolderBase : public Wrappable {
50  public:
51   virtual WrapperInfo* GetWrapperInfo() OVERRIDE;
52   static WrapperInfo kWrapperInfo;
53  protected:
54   virtual ~CallbackHolderBase() {}
57 template<typename Sig>
58 class CallbackHolder : public CallbackHolderBase {
59  public:
60   CallbackHolder(const base::Callback<Sig>& callback)
61       : callback(callback) {}
62   base::Callback<Sig> callback;
63  private:
64   virtual ~CallbackHolder() {}
68 // This set of templates invokes a base::Callback, converts the return type to a
69 // JavaScript value, and returns that value to script via the provided
70 // gin::Arguments object.
72 // In C++, you can declare the function foo(void), but you can't pass a void
73 // expression to foo. As a result, we must specialize the case of Callbacks that
74 // have the void return type.
76 $range ARITY 0..MAX_ARITY
77 $for ARITY [[
78 $var INV_ARITY = MAX_ARITY - ARITY
79 $range ARG 1..INV_ARITY
80 $range VOID INV_ARITY+1..MAX_ARITY
82 $if ARITY == 0 [[
83 template<typename R$for ARG [[, typename P$(ARG) = void]]>
84 struct Invoker {
85 ]] $else [[
86 template<typename R$for ARG [[, typename P$(ARG)]]>
87 struct Invoker<R$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
90   inline static void Go(
91       Arguments* args,
92       const base::Callback<R($for ARG , [[P$(ARG)]])>& callback$for ARG [[, 
93       const P$(ARG)& a$(ARG)]]) {
94     args->Return(callback.Run($for ARG, [[a$(ARG)]]));
95   }
97 template<$for ARG , [[typename P$(ARG)]]>
98 struct Invoker<void$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
99   inline static void Go(
100       Arguments* args,
101       const base::Callback<void($for ARG , [[P$(ARG)]])>& callback$for ARG [[,
102       const P$(ARG)& a$(ARG)]]) {
103     callback.Run($for ARG, [[a$(ARG)]]);
104   }
110 // DispatchToCallback converts all the JavaScript arguments to C++ types and
111 // invokes the base::Callback.
112 $range ARITY 0..MAX_ARITY
113 $for ARITY [[
114 $range ARG 1..ARITY
116 template<typename R$for ARG [[, typename P$(ARG)]]>
117 static void DispatchToCallback(
118     const v8::FunctionCallbackInfo<v8::Value>& info) {
119   Arguments args(info);
120   CallbackHolderBase* holder_base = NULL;
121   CHECK(args.GetData(&holder_base));
123   typedef CallbackHolder<R($for ARG , [[P$(ARG)]])> HolderT;
124   HolderT* holder = static_cast<HolderT*>(holder_base);
126 $if ARITY != 0 [[
129 $for ARG [[  typename RemoveConstRef<P$(ARG)>::Type a$(ARG);
132   if (
133 $for ARG  ||
134       [[!args.GetNext(&a$(ARG))]]) {
135     args.ThrowError();
136     return;
137   }
141   Invoker<R$for ARG [[, P$(ARG)]]>::Go(&args, holder->callback$for ARG [[, a$(ARG)]]);
146 }  // namespace internal
149 // This should be called once per-isolate to initialize the function template
150 // system.
151 void InitFunctionTemplates(PerIsolateData* isolate_data);
154 // This has to be outside the internal namespace because template
155 // specializations must be declared in the same namespace as the original
156 // template.
157 template<>
158 struct Converter<internal::CallbackHolderBase*>
159     : public WrappableConverter<internal::CallbackHolderBase> {};
162 // Creates a v8::FunctionTemplate that will run the provided base::Callback each
163 // time it is called. JavaScript arguments and return values are converted via
164 // gin::Converter.
165 $range ARITY 0..MAX_ARITY
166 $for ARITY [[
167 $range ARG 1..ARITY
169 template<typename R$for ARG [[, typename P$(ARG)]]>
170 v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
171     v8::Isolate* isolate,
172     const base::Callback<R($for ARG , [[P$(ARG)]])> callback) {
173   typedef internal::CallbackHolder<R($for ARG , [[P$(ARG)]])> HolderT;
174   scoped_refptr<HolderT> holder(new HolderT(callback));
175   return v8::FunctionTemplate::New(
176       &internal::DispatchToCallback<R$for ARG [[, P$(ARG)]]>,
177       ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
182 }  // namespace gin