[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 1 | // This file was GENERATED by command: |
| 2 | // pump.py function_template.h.pump |
| 3 | // DO NOT EDIT BY HAND!!! |
| 4 | |
| 5 | |
| 6 | |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 7 | #ifndef GIN_FUNCTION_TEMPLATE_H_ |
| 8 | #define GIN_FUNCTION_TEMPLATE_H_ |
| 9 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 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. |
| 13 | |
| 14 | #include "base/callback.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "gin/arguments.h" |
| 17 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 18 | #include "gin/gin_export.h" |
[email protected] | d379b0c | 2013-12-05 05:48:01 | [diff] [blame] | 19 | #include "gin/handle.h" |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 20 | #include "gin/public/gin_embedders.h" |
| 21 | #include "gin/public/wrapper_info.h" |
| 22 | #include "gin/wrappable.h" |
| 23 | |
| 24 | #include "v8/include/v8.h" |
| 25 | |
| 26 | namespace gin { |
| 27 | |
| 28 | class PerIsolateData; |
| 29 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 30 | enum CreateFunctionTemplateFlags { |
| 31 | HolderIsFirstArgument = 1 << 0, |
| 32 | }; |
| 33 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 34 | namespace internal { |
| 35 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 36 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 37 | struct CallbackParamTraits { |
| 38 | typedef T LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 39 | }; |
| 40 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 41 | struct CallbackParamTraits<const T&> { |
| 42 | typedef T LocalType; |
| 43 | }; |
| 44 | template<typename T> |
| 45 | struct CallbackParamTraits<const T*> { |
| 46 | typedef T* LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 47 | }; |
| 48 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 49 | |
| 50 | // CallbackHolder and CallbackHolderBase are used to pass a base::Callback from |
| 51 | // CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 52 | // DispatchToCallback, where it is invoked. |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 53 | // |
| 54 | // v8::FunctionTemplate only supports passing void* as data so how do we know |
| 55 | // when to delete the base::Callback? That's where CallbackHolderBase comes in. |
| 56 | // It inherits from Wrappable, which delete itself when both (a) the refcount |
| 57 | // via base::RefCounted has dropped to zero, and (b) there are no more |
| 58 | // JavaScript references in V8. |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 59 | |
| 60 | // This simple base class is used so that we can share a single object template |
| 61 | // among every CallbackHolder instance. |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 62 | class GIN_EXPORT CallbackHolderBase : public Wrappable<CallbackHolderBase> { |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 63 | public: |
| 64 | static WrapperInfo kWrapperInfo; |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 65 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 66 | protected: |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 67 | virtual ~CallbackHolderBase() {} |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 68 | }; |
| 69 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 70 | template<typename Sig> |
| 71 | class CallbackHolder : public CallbackHolderBase { |
| 72 | public: |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 73 | CallbackHolder(const base::Callback<Sig>& callback, int flags) |
| 74 | : callback(callback), flags(flags) {} |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 75 | base::Callback<Sig> callback; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 76 | int flags; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 77 | private: |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 78 | virtual ~CallbackHolder() {} |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 79 | }; |
| 80 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 81 | |
| 82 | // This set of templates invokes a base::Callback, converts the return type to a |
| 83 | // JavaScript value, and returns that value to script via the provided |
| 84 | // gin::Arguments object. |
| 85 | // |
| 86 | // In C++, you can declare the function foo(void), but you can't pass a void |
| 87 | // expression to foo. As a result, we must specialize the case of Callbacks that |
| 88 | // have the void return type. |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 89 | template<typename R, typename P1 = void, typename P2 = void, |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 90 | typename P3 = void, typename P4 = void, typename P5 = void, |
| 91 | typename P6 = void> |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 92 | struct Invoker { |
| 93 | inline static void Go( |
| 94 | Arguments* args, |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 95 | const base::Callback<R(P1, P2, P3, P4, P5, P6)>& callback, |
| 96 | const P1& a1, |
| 97 | const P2& a2, |
| 98 | const P3& a3, |
| 99 | const P4& a4, |
| 100 | const P5& a5, |
| 101 | const P6& a6) { |
| 102 | args->Return(callback.Run(a1, a2, a3, a4, a5, a6)); |
| 103 | } |
| 104 | }; |
| 105 | template<typename P1, typename P2, typename P3, typename P4, typename P5, |
| 106 | typename P6> |
| 107 | struct Invoker<void, P1, P2, P3, P4, P5, P6> { |
| 108 | inline static void Go( |
| 109 | Arguments* args, |
| 110 | const base::Callback<void(P1, P2, P3, P4, P5, P6)>& callback, |
| 111 | const P1& a1, |
| 112 | const P2& a2, |
| 113 | const P3& a3, |
| 114 | const P4& a4, |
| 115 | const P5& a5, |
| 116 | const P6& a6) { |
| 117 | callback.Run(a1, a2, a3, a4, a5, a6); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | template<typename R, typename P1, typename P2, typename P3, typename P4, |
| 122 | typename P5> |
| 123 | struct Invoker<R, P1, P2, P3, P4, P5, void> { |
| 124 | inline static void Go( |
| 125 | Arguments* args, |
| 126 | const base::Callback<R(P1, P2, P3, P4, P5)>& callback, |
| 127 | const P1& a1, |
| 128 | const P2& a2, |
| 129 | const P3& a3, |
| 130 | const P4& a4, |
| 131 | const P5& a5) { |
| 132 | args->Return(callback.Run(a1, a2, a3, a4, a5)); |
| 133 | } |
| 134 | }; |
| 135 | template<typename P1, typename P2, typename P3, typename P4, typename P5> |
| 136 | struct Invoker<void, P1, P2, P3, P4, P5, void> { |
| 137 | inline static void Go( |
| 138 | Arguments* args, |
| 139 | const base::Callback<void(P1, P2, P3, P4, P5)>& callback, |
| 140 | const P1& a1, |
| 141 | const P2& a2, |
| 142 | const P3& a3, |
| 143 | const P4& a4, |
| 144 | const P5& a5) { |
| 145 | callback.Run(a1, a2, a3, a4, a5); |
| 146 | } |
| 147 | }; |
| 148 | |
| 149 | template<typename R, typename P1, typename P2, typename P3, typename P4> |
| 150 | struct Invoker<R, P1, P2, P3, P4, void, void> { |
| 151 | inline static void Go( |
| 152 | Arguments* args, |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 153 | const base::Callback<R(P1, P2, P3, P4)>& callback, |
| 154 | const P1& a1, |
| 155 | const P2& a2, |
| 156 | const P3& a3, |
| 157 | const P4& a4) { |
| 158 | args->Return(callback.Run(a1, a2, a3, a4)); |
| 159 | } |
| 160 | }; |
| 161 | template<typename P1, typename P2, typename P3, typename P4> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 162 | struct Invoker<void, P1, P2, P3, P4, void, void> { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 163 | inline static void Go( |
| 164 | Arguments* args, |
| 165 | const base::Callback<void(P1, P2, P3, P4)>& callback, |
| 166 | const P1& a1, |
| 167 | const P2& a2, |
| 168 | const P3& a3, |
| 169 | const P4& a4) { |
| 170 | callback.Run(a1, a2, a3, a4); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | template<typename R, typename P1, typename P2, typename P3> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 175 | struct Invoker<R, P1, P2, P3, void, void, void> { |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 176 | inline static void Go( |
| 177 | Arguments* args, |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 178 | const base::Callback<R(P1, P2, P3)>& callback, |
| 179 | const P1& a1, |
| 180 | const P2& a2, |
| 181 | const P3& a3) { |
| 182 | args->Return(callback.Run(a1, a2, a3)); |
| 183 | } |
| 184 | }; |
| 185 | template<typename P1, typename P2, typename P3> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 186 | struct Invoker<void, P1, P2, P3, void, void, void> { |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 187 | inline static void Go( |
| 188 | Arguments* args, |
| 189 | const base::Callback<void(P1, P2, P3)>& callback, |
| 190 | const P1& a1, |
| 191 | const P2& a2, |
| 192 | const P3& a3) { |
| 193 | callback.Run(a1, a2, a3); |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | template<typename R, typename P1, typename P2> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 198 | struct Invoker<R, P1, P2, void, void, void, void> { |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 199 | inline static void Go( |
| 200 | Arguments* args, |
| 201 | const base::Callback<R(P1, P2)>& callback, |
| 202 | const P1& a1, |
| 203 | const P2& a2) { |
| 204 | args->Return(callback.Run(a1, a2)); |
| 205 | } |
| 206 | }; |
| 207 | template<typename P1, typename P2> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 208 | struct Invoker<void, P1, P2, void, void, void, void> { |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 209 | inline static void Go( |
| 210 | Arguments* args, |
| 211 | const base::Callback<void(P1, P2)>& callback, |
| 212 | const P1& a1, |
| 213 | const P2& a2) { |
| 214 | callback.Run(a1, a2); |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | template<typename R, typename P1> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 219 | struct Invoker<R, P1, void, void, void, void, void> { |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 220 | inline static void Go( |
| 221 | Arguments* args, |
| 222 | const base::Callback<R(P1)>& callback, |
| 223 | const P1& a1) { |
| 224 | args->Return(callback.Run(a1)); |
| 225 | } |
| 226 | }; |
| 227 | template<typename P1> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 228 | struct Invoker<void, P1, void, void, void, void, void> { |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 229 | inline static void Go( |
| 230 | Arguments* args, |
| 231 | const base::Callback<void(P1)>& callback, |
| 232 | const P1& a1) { |
| 233 | callback.Run(a1); |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | template<typename R> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 238 | struct Invoker<R, void, void, void, void, void, void> { |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 239 | inline static void Go( |
| 240 | Arguments* args, |
| 241 | const base::Callback<R()>& callback) { |
| 242 | args->Return(callback.Run()); |
| 243 | } |
| 244 | }; |
| 245 | template<> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 246 | struct Invoker<void, void, void, void, void, void, void> { |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 247 | inline static void Go( |
| 248 | Arguments* args, |
| 249 | const base::Callback<void()>& callback) { |
| 250 | callback.Run(); |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 255 | template<typename T> |
| 256 | bool GetNextArgument(Arguments* args, int create_flags, bool is_first, |
| 257 | T* result) { |
| 258 | if (is_first && (create_flags & HolderIsFirstArgument) != 0) { |
| 259 | return args->GetHolder(result); |
| 260 | } else { |
| 261 | return args->GetNext(result); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // For advanced use cases, we allow callers to request the unparsed Arguments |
| 266 | // object and poke around in it directly. |
| 267 | inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first, |
| 268 | Arguments* result) { |
| 269 | *result = *args; |
| 270 | return true; |
| 271 | } |
| 272 | |
[email protected] | 2491f14 | 2013-12-21 17:54:37 | [diff] [blame^] | 273 | // It's common for clients to just need the isolate, so we make that easy. |
| 274 | inline bool GetNextArgument(Arguments* args, int create_flags, |
| 275 | bool is_first, v8::Isolate** result) { |
| 276 | *result = args->isolate(); |
| 277 | return true; |
| 278 | } |
| 279 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 280 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 281 | // DispatchToCallback converts all the JavaScript arguments to C++ types and |
| 282 | // invokes the base::Callback. |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 283 | template<typename Sig> |
| 284 | struct Dispatcher { |
| 285 | }; |
| 286 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 287 | template<typename R> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 288 | struct Dispatcher<R()> { |
| 289 | static void DispatchToCallback( |
| 290 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 291 | Arguments args(info); |
| 292 | CallbackHolderBase* holder_base = NULL; |
| 293 | CHECK(args.GetData(&holder_base)); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 294 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 295 | typedef CallbackHolder<R()> HolderT; |
| 296 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 297 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 298 | Invoker<R>::Go(&args, holder->callback); |
| 299 | } |
| 300 | }; |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 301 | |
| 302 | template<typename R, typename P1> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 303 | struct Dispatcher<R(P1)> { |
| 304 | static void DispatchToCallback( |
| 305 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 306 | Arguments args(info); |
| 307 | CallbackHolderBase* holder_base = NULL; |
| 308 | CHECK(args.GetData(&holder_base)); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 309 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 310 | typedef CallbackHolder<R(P1)> HolderT; |
| 311 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 312 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 313 | typename CallbackParamTraits<P1>::LocalType a1; |
| 314 | if (!GetNextArgument(&args, holder->flags, true, &a1)) { |
| 315 | args.ThrowError(); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | Invoker<R, P1>::Go(&args, holder->callback, a1); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 320 | } |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 321 | }; |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 322 | |
| 323 | template<typename R, typename P1, typename P2> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 324 | struct Dispatcher<R(P1, P2)> { |
| 325 | static void DispatchToCallback( |
| 326 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 327 | Arguments args(info); |
| 328 | CallbackHolderBase* holder_base = NULL; |
| 329 | CHECK(args.GetData(&holder_base)); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 330 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 331 | typedef CallbackHolder<R(P1, P2)> HolderT; |
| 332 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 333 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 334 | typename CallbackParamTraits<P1>::LocalType a1; |
| 335 | typename CallbackParamTraits<P2>::LocalType a2; |
| 336 | if (!GetNextArgument(&args, holder->flags, true, &a1) || |
| 337 | !GetNextArgument(&args, holder->flags, false, &a2)) { |
| 338 | args.ThrowError(); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | Invoker<R, P1, P2>::Go(&args, holder->callback, a1, a2); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 343 | } |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 344 | }; |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 345 | |
| 346 | template<typename R, typename P1, typename P2, typename P3> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 347 | struct Dispatcher<R(P1, P2, P3)> { |
| 348 | static void DispatchToCallback( |
| 349 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 350 | Arguments args(info); |
| 351 | CallbackHolderBase* holder_base = NULL; |
| 352 | CHECK(args.GetData(&holder_base)); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 353 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 354 | typedef CallbackHolder<R(P1, P2, P3)> HolderT; |
| 355 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 356 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 357 | typename CallbackParamTraits<P1>::LocalType a1; |
| 358 | typename CallbackParamTraits<P2>::LocalType a2; |
| 359 | typename CallbackParamTraits<P3>::LocalType a3; |
| 360 | if (!GetNextArgument(&args, holder->flags, true, &a1) || |
| 361 | !GetNextArgument(&args, holder->flags, false, &a2) || |
| 362 | !GetNextArgument(&args, holder->flags, false, &a3)) { |
| 363 | args.ThrowError(); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | Invoker<R, P1, P2, P3>::Go(&args, holder->callback, a1, a2, a3); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 368 | } |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 369 | }; |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 370 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 371 | template<typename R, typename P1, typename P2, typename P3, typename P4> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 372 | struct Dispatcher<R(P1, P2, P3, P4)> { |
| 373 | static void DispatchToCallback( |
| 374 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 375 | Arguments args(info); |
| 376 | CallbackHolderBase* holder_base = NULL; |
| 377 | CHECK(args.GetData(&holder_base)); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 378 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 379 | typedef CallbackHolder<R(P1, P2, P3, P4)> HolderT; |
| 380 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 381 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 382 | typename CallbackParamTraits<P1>::LocalType a1; |
| 383 | typename CallbackParamTraits<P2>::LocalType a2; |
| 384 | typename CallbackParamTraits<P3>::LocalType a3; |
| 385 | typename CallbackParamTraits<P4>::LocalType a4; |
| 386 | if (!GetNextArgument(&args, holder->flags, true, &a1) || |
| 387 | !GetNextArgument(&args, holder->flags, false, &a2) || |
| 388 | !GetNextArgument(&args, holder->flags, false, &a3) || |
| 389 | !GetNextArgument(&args, holder->flags, false, &a4)) { |
| 390 | args.ThrowError(); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | Invoker<R, P1, P2, P3, P4>::Go(&args, holder->callback, a1, a2, a3, a4); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 395 | } |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 396 | }; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 397 | |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 398 | template<typename R, typename P1, typename P2, typename P3, typename P4, |
| 399 | typename P5> |
| 400 | struct Dispatcher<R(P1, P2, P3, P4, P5)> { |
| 401 | static void DispatchToCallback( |
| 402 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 403 | Arguments args(info); |
| 404 | CallbackHolderBase* holder_base = NULL; |
| 405 | CHECK(args.GetData(&holder_base)); |
| 406 | |
| 407 | typedef CallbackHolder<R(P1, P2, P3, P4, P5)> HolderT; |
| 408 | HolderT* holder = static_cast<HolderT*>(holder_base); |
| 409 | |
| 410 | typename CallbackParamTraits<P1>::LocalType a1; |
| 411 | typename CallbackParamTraits<P2>::LocalType a2; |
| 412 | typename CallbackParamTraits<P3>::LocalType a3; |
| 413 | typename CallbackParamTraits<P4>::LocalType a4; |
| 414 | typename CallbackParamTraits<P5>::LocalType a5; |
| 415 | if (!GetNextArgument(&args, holder->flags, true, &a1) || |
| 416 | !GetNextArgument(&args, holder->flags, false, &a2) || |
| 417 | !GetNextArgument(&args, holder->flags, false, &a3) || |
| 418 | !GetNextArgument(&args, holder->flags, false, &a4) || |
| 419 | !GetNextArgument(&args, holder->flags, false, &a5)) { |
| 420 | args.ThrowError(); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | Invoker<R, P1, P2, P3, P4, P5>::Go(&args, holder->callback, a1, a2, a3, a4, |
| 425 | a5); |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | template<typename R, typename P1, typename P2, typename P3, typename P4, |
| 430 | typename P5, typename P6> |
| 431 | struct Dispatcher<R(P1, P2, P3, P4, P5, P6)> { |
| 432 | static void DispatchToCallback( |
| 433 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 434 | Arguments args(info); |
| 435 | CallbackHolderBase* holder_base = NULL; |
| 436 | CHECK(args.GetData(&holder_base)); |
| 437 | |
| 438 | typedef CallbackHolder<R(P1, P2, P3, P4, P5, P6)> HolderT; |
| 439 | HolderT* holder = static_cast<HolderT*>(holder_base); |
| 440 | |
| 441 | typename CallbackParamTraits<P1>::LocalType a1; |
| 442 | typename CallbackParamTraits<P2>::LocalType a2; |
| 443 | typename CallbackParamTraits<P3>::LocalType a3; |
| 444 | typename CallbackParamTraits<P4>::LocalType a4; |
| 445 | typename CallbackParamTraits<P5>::LocalType a5; |
| 446 | typename CallbackParamTraits<P6>::LocalType a6; |
| 447 | if (!GetNextArgument(&args, holder->flags, true, &a1) || |
| 448 | !GetNextArgument(&args, holder->flags, false, &a2) || |
| 449 | !GetNextArgument(&args, holder->flags, false, &a3) || |
| 450 | !GetNextArgument(&args, holder->flags, false, &a4) || |
| 451 | !GetNextArgument(&args, holder->flags, false, &a5) || |
| 452 | !GetNextArgument(&args, holder->flags, false, &a6)) { |
| 453 | args.ThrowError(); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | Invoker<R, P1, P2, P3, P4, P5, P6>::Go(&args, holder->callback, a1, a2, a3, |
| 458 | a4, a5, a6); |
| 459 | } |
| 460 | }; |
| 461 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 462 | } // namespace internal |
| 463 | |
| 464 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 465 | // CreateFunctionTemplate creates a v8::FunctionTemplate that will create |
| 466 | // JavaScript functions that execute a provided C++ function or base::Callback. |
| 467 | // JavaScript arguments are automatically converted via gin::Converter, as is |
| 468 | // the return value of the C++ function, if any. |
| 469 | template<typename Sig> |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 470 | v8::Local<v8::FunctionTemplate> CreateFunctionTemplate( |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 471 | v8::Isolate* isolate, const base::Callback<Sig> callback, |
| 472 | int callback_flags = 0) { |
| 473 | typedef internal::CallbackHolder<Sig> HolderT; |
| 474 | gin::Handle<HolderT> holder = CreateHandle( |
| 475 | isolate, new HolderT(callback, callback_flags)); |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 476 | return v8::FunctionTemplate::New( |
[email protected] | 505ffde | 2013-12-19 15:47:13 | [diff] [blame] | 477 | isolate, |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 478 | &internal::Dispatcher<Sig>::DispatchToCallback, |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 479 | ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get())); |
| 480 | } |
| 481 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 482 | } // namespace gin |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 483 | |
| 484 | #endif // GIN_FUNCTION_TEMPLATE_H_ |