Update OWNERS file for src/tools/auto-bisect.
[chromium-blink-merge.git] / gin / function_template.h
blob955ff53c284d416b8ffa33e2011635a342476c2c
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"
15 namespace gin {
17 class PerIsolateData;
19 enum CreateFunctionTemplateFlags {
20 HolderIsFirstArgument = 1 << 0,
23 namespace internal {
25 template<typename T>
26 struct CallbackParamTraits {
27 typedef T LocalType;
29 template<typename T>
30 struct CallbackParamTraits<const T&> {
31 typedef T LocalType;
33 template<typename T>
34 struct CallbackParamTraits<const T*> {
35 typedef T* LocalType;
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 {
46 public:
47 v8::Handle<v8::External> GetHandle(v8::Isolate* isolate);
49 protected:
50 explicit CallbackHolderBase(v8::Isolate* isolate);
51 virtual ~CallbackHolderBase();
53 private:
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 {
64 public:
65 CallbackHolder(v8::Isolate* isolate,
66 const base::Callback<Sig>& callback,
67 int flags)
68 : CallbackHolderBase(isolate), callback(callback), flags(flags) {}
69 base::Callback<Sig> callback;
70 int flags;
71 private:
72 virtual ~CallbackHolder() {}
74 DISALLOW_COPY_AND_ASSIGN(CallbackHolder);
77 template<typename T>
78 bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
79 T* result) {
80 if (is_first && (create_flags & HolderIsFirstArgument) != 0) {
81 return args->GetHolder(result);
82 } else {
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,
90 Arguments* result) {
91 *result = *args;
92 return true;
94 inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
95 Arguments** result) {
96 *result = args;
97 return true;
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();
104 return true;
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,
115 requested_index - 1,
116 indices...>::type;
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;
129 ArgLocalType value;
130 bool ok;
132 ArgumentHolder(Arguments* args, int create_flags)
133 : ok(GetNextArgument(args, create_flags, index == 0, &value)) {
134 if (!ok)
135 args->ThrowError();
139 // Class template for converting arguments from JavaScript to C++ and running
140 // the callback with them.
141 template <typename IndicesType, typename... ArgTypes>
142 class Invoker {};
144 template <size_t... indices, typename... ArgTypes>
145 class Invoker<IndicesHolder<indices...>, ArgTypes...>
146 : public ArgumentHolder<indices, ArgTypes>... {
147 public:
148 // Invoker<> inherits from ArgumentHolder<> for each argument.
149 // C++ has always been strict about the class initialization order,
150 // so it is guaranteed ArgumentHolders will be initialized (and thus, will
151 // extract arguments from Arguments) in the right order.
152 Invoker(Arguments* args, int create_flags)
153 : ArgumentHolder<indices, ArgTypes>(args, create_flags)...,
154 args_(args) {}
156 bool IsOK() {
157 return And(ArgumentHolder<indices, ArgTypes>::ok...);
160 template <typename ReturnType>
161 void DispatchToCallback(base::Callback<ReturnType(ArgTypes...)> callback) {
162 args_->Return(callback.Run(ArgumentHolder<indices, ArgTypes>::value...));
165 // In C++, you can declare the function foo(void), but you can't pass a void
166 // expression to foo. As a result, we must specialize the case of Callbacks
167 // that have the void return type.
168 void DispatchToCallback(base::Callback<void(ArgTypes...)> callback) {
169 callback.Run(ArgumentHolder<indices, ArgTypes>::value...);
172 private:
173 static bool And() { return true; }
174 template <typename... T>
175 static bool And(bool arg1, T... args) {
176 return arg1 && And(args...);
179 Arguments* args_;
182 // DispatchToCallback converts all the JavaScript arguments to C++ types and
183 // invokes the base::Callback.
184 template <typename Sig>
185 struct Dispatcher {};
187 template <typename ReturnType, typename... ArgTypes>
188 struct Dispatcher<ReturnType(ArgTypes...)> {
189 static void DispatchToCallback(
190 const v8::FunctionCallbackInfo<v8::Value>& info) {
191 Arguments args(info);
192 v8::Handle<v8::External> v8_holder;
193 CHECK(args.GetData(&v8_holder));
194 CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>(
195 v8_holder->Value());
197 typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT;
198 HolderT* holder = static_cast<HolderT*>(holder_base);
200 using Indices = typename IndicesGenerator<sizeof...(ArgTypes)>::type;
201 Invoker<Indices, ArgTypes...> invoker(&args, holder->flags);
202 if (invoker.IsOK())
203 invoker.DispatchToCallback(holder->callback);
207 } // namespace internal
210 // CreateFunctionTemplate creates a v8::FunctionTemplate that will create
211 // JavaScript functions that execute a provided C++ function or base::Callback.
212 // JavaScript arguments are automatically converted via gin::Converter, as is
213 // the return value of the C++ function, if any.
214 template<typename Sig>
215 v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
216 v8::Isolate* isolate, const base::Callback<Sig> callback,
217 int callback_flags = 0) {
218 typedef internal::CallbackHolder<Sig> HolderT;
219 HolderT* holder = new HolderT(isolate, callback, callback_flags);
221 return v8::FunctionTemplate::New(
222 isolate,
223 &internal::Dispatcher<Sig>::DispatchToCallback,
224 ConvertToV8<v8::Handle<v8::External> >(isolate,
225 holder->GetHandle(isolate)));
228 // CreateFunctionHandler installs a CallAsFunction handler on the given
229 // object template that forwards to a provided C++ function or base::Callback.
230 template<typename Sig>
231 void CreateFunctionHandler(v8::Isolate* isolate,
232 v8::Local<v8::ObjectTemplate> tmpl,
233 const base::Callback<Sig> callback,
234 int callback_flags = 0) {
235 typedef internal::CallbackHolder<Sig> HolderT;
236 HolderT* holder = new HolderT(isolate, callback, callback_flags);
237 tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback,
238 ConvertToV8<v8::Handle<v8::External> >(
239 isolate, holder->GetHandle(isolate)));
242 } // namespace gin
244 #endif // GIN_FUNCTION_TEMPLATE_H_