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
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"
31 struct RemoveConstRef {
35 struct RemoveConstRef<const T&> {
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 {
51 virtual WrapperInfo* GetWrapperInfo() OVERRIDE;
52 static WrapperInfo kWrapperInfo;
54 virtual ~CallbackHolderBase() {}
57 template<typename Sig>
58 class CallbackHolder : public CallbackHolderBase {
60 CallbackHolder(const base::Callback<Sig>& callback)
61 : callback(callback) {}
62 base::Callback<Sig> callback;
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
78 $var INV_ARITY = MAX_ARITY - ARITY
79 $range ARG 1..INV_ARITY
80 $range VOID INV_ARITY+1..MAX_ARITY
83 template<typename R$for ARG [[, typename P$(ARG) = void]]>
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(
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)]]));
97 template<$for ARG , [[typename P$(ARG)]]>
98 struct Invoker<void$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
99 inline static void Go(
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)]]);
110 // DispatchToCallback converts all the JavaScript arguments to C++ types and
111 // invokes the base::Callback.
112 $range ARITY 0..MAX_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);
129 $for ARG [[ typename RemoveConstRef<P$(ARG)>::Type a$(ARG);
134 [[!args.GetNext(&a$(ARG))]]) {
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
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
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
165 $range ARITY 0..MAX_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()));