kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 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. |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 4 | |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 5 | #ifndef GIN_FUNCTION_TEMPLATE_H_ |
| 6 | #define GIN_FUNCTION_TEMPLATE_H_ |
| 7 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 8 | #include "base/callback.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "gin/arguments.h" |
| 11 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 12 | #include "gin/gin_export.h" |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 13 | #include "v8/include/v8.h" |
| 14 | |
| 15 | namespace gin { |
| 16 | |
| 17 | class PerIsolateData; |
| 18 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 19 | enum CreateFunctionTemplateFlags { |
| 20 | HolderIsFirstArgument = 1 << 0, |
| 21 | }; |
| 22 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 23 | namespace internal { |
| 24 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 25 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 26 | struct CallbackParamTraits { |
| 27 | typedef T LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 28 | }; |
| 29 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 30 | struct CallbackParamTraits<const T&> { |
| 31 | typedef T LocalType; |
| 32 | }; |
| 33 | template<typename T> |
| 34 | struct CallbackParamTraits<const T*> { |
| 35 | typedef T* LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 36 | }; |
| 37 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 38 | |
| 39 | // CallbackHolder and CallbackHolderBase are used to pass a base::Callback from |
| 40 | // CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 41 | // DispatchToCallback, where it is invoked. |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 42 | |
| 43 | // This simple base class is used so that we can share a single object template |
| 44 | // among every CallbackHolder instance. |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 45 | class GIN_EXPORT CallbackHolderBase { |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 46 | public: |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 47 | v8::Handle<v8::External> GetHandle(v8::Isolate* isolate); |
| 48 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 49 | protected: |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 50 | explicit CallbackHolderBase(v8::Isolate* isolate); |
| 51 | virtual ~CallbackHolderBase(); |
| 52 | |
| 53 | private: |
| 54 | static void WeakCallback( |
| 55 | const v8::WeakCallbackData<v8::External, CallbackHolderBase>& data); |
| 56 | |
| 57 | v8::Persistent<v8::External> v8_ref_; |
| 58 | |
| 59 | DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 60 | }; |
| 61 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 62 | template<typename Sig> |
| 63 | class CallbackHolder : public CallbackHolderBase { |
| 64 | public: |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 65 | CallbackHolder(v8::Isolate* isolate, |
| 66 | const base::Callback<Sig>& callback, |
| 67 | int flags) |
| 68 | : CallbackHolderBase(isolate), callback(callback), flags(flags) {} |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 69 | base::Callback<Sig> callback; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 70 | int flags; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 71 | private: |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 72 | virtual ~CallbackHolder() {} |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(CallbackHolder); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 75 | }; |
| 76 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 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); |
| 84 | } |
| 85 | } |
| 86 | |
| 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; |
| 93 | } |
[email protected] | 5b971af | 2014-01-06 22:20:54 | [diff] [blame] | 94 | inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first, |
| 95 | Arguments** result) { |
| 96 | *result = args; |
| 97 | return true; |
| 98 | } |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 99 | |
[email protected] | 2491f14 | 2013-12-21 17:54:37 | [diff] [blame] | 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; |
| 105 | } |
| 106 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 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 {}; |
| 111 | |
| 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; |
| 117 | }; |
| 118 | template <size_t... indices> |
| 119 | struct IndicesGenerator<0, indices...> { |
| 120 | using type = IndicesHolder<indices...>; |
| 121 | }; |
| 122 | |
| 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; |
| 128 | |
| 129 | ArgLocalType value; |
| 130 | bool ok; |
| 131 | |
| 132 | ArgumentHolder(Arguments* args, int create_flags) |
| 133 | : ok(GetNextArgument(args, create_flags, index == 0, &value)) { |
eseidel | 2584930f | 2014-12-15 22:06:44 | [diff] [blame] | 134 | if (!ok) { |
| 135 | // Ideally we would include the expected c++ type in the error |
| 136 | // message which we can access via typeid(ArgType).name() |
| 137 | // however we compile with no-rtti, which disables typeid. |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 138 | args->ThrowError(); |
eseidel | 2584930f | 2014-12-15 22:06:44 | [diff] [blame] | 139 | } |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | |
| 143 | // Class template for converting arguments from JavaScript to C++ and running |
| 144 | // the callback with them. |
| 145 | template <typename IndicesType, typename... ArgTypes> |
| 146 | class Invoker {}; |
| 147 | |
| 148 | template <size_t... indices, typename... ArgTypes> |
| 149 | class Invoker<IndicesHolder<indices...>, ArgTypes...> |
| 150 | : public ArgumentHolder<indices, ArgTypes>... { |
| 151 | public: |
| 152 | // Invoker<> inherits from ArgumentHolder<> for each argument. |
| 153 | // C++ has always been strict about the class initialization order, |
| 154 | // so it is guaranteed ArgumentHolders will be initialized (and thus, will |
| 155 | // extract arguments from Arguments) in the right order. |
| 156 | Invoker(Arguments* args, int create_flags) |
cmasone | a34136a | 2014-11-01 00:10:47 | [diff] [blame] | 157 | : ArgumentHolder<indices, ArgTypes>(args, create_flags)..., args_(args) { |
| 158 | // GCC thinks that create_flags is going unused, even though the |
| 159 | // expansion above clearly makes use of it. Per jyasskin@, casting |
| 160 | // to void is the commonly accepted way to convince the compiler |
| 161 | // that you're actually using a parameter/varible. |
| 162 | (void)create_flags; |
| 163 | } |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 164 | |
| 165 | bool IsOK() { |
| 166 | return And(ArgumentHolder<indices, ArgTypes>::ok...); |
| 167 | } |
| 168 | |
| 169 | template <typename ReturnType> |
| 170 | void DispatchToCallback(base::Callback<ReturnType(ArgTypes...)> callback) { |
| 171 | args_->Return(callback.Run(ArgumentHolder<indices, ArgTypes>::value...)); |
| 172 | } |
| 173 | |
| 174 | // In C++, you can declare the function foo(void), but you can't pass a void |
| 175 | // expression to foo. As a result, we must specialize the case of Callbacks |
| 176 | // that have the void return type. |
| 177 | void DispatchToCallback(base::Callback<void(ArgTypes...)> callback) { |
| 178 | callback.Run(ArgumentHolder<indices, ArgTypes>::value...); |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | static bool And() { return true; } |
| 183 | template <typename... T> |
| 184 | static bool And(bool arg1, T... args) { |
| 185 | return arg1 && And(args...); |
| 186 | } |
| 187 | |
| 188 | Arguments* args_; |
| 189 | }; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 190 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 191 | // DispatchToCallback converts all the JavaScript arguments to C++ types and |
| 192 | // invokes the base::Callback. |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 193 | template <typename Sig> |
| 194 | struct Dispatcher {}; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 195 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 196 | template <typename ReturnType, typename... ArgTypes> |
| 197 | struct Dispatcher<ReturnType(ArgTypes...)> { |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 198 | static void DispatchToCallback( |
| 199 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 200 | Arguments args(info); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 201 | v8::Handle<v8::External> v8_holder; |
| 202 | CHECK(args.GetData(&v8_holder)); |
| 203 | CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>( |
| 204 | v8_holder->Value()); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 205 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 206 | typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 207 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 208 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 209 | using Indices = typename IndicesGenerator<sizeof...(ArgTypes)>::type; |
| 210 | Invoker<Indices, ArgTypes...> invoker(&args, holder->flags); |
| 211 | if (invoker.IsOK()) |
| 212 | invoker.DispatchToCallback(holder->callback); |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 213 | } |
| 214 | }; |
| 215 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 216 | } // namespace internal |
| 217 | |
| 218 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 219 | // CreateFunctionTemplate creates a v8::FunctionTemplate that will create |
| 220 | // JavaScript functions that execute a provided C++ function or base::Callback. |
| 221 | // JavaScript arguments are automatically converted via gin::Converter, as is |
| 222 | // the return value of the C++ function, if any. |
mnaganov | 098ba6f | 2015-03-03 16:34:48 | [diff] [blame^] | 223 | // |
| 224 | // NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own |
| 225 | // internal reasons, thus it is generally a good idea to cache the template |
| 226 | // returned by this function. Otherwise, repeated method invocations from JS |
| 227 | // will create substantial memory leaks. See http://crbug.com/463487. |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 228 | template<typename Sig> |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 229 | v8::Local<v8::FunctionTemplate> CreateFunctionTemplate( |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 230 | v8::Isolate* isolate, const base::Callback<Sig> callback, |
| 231 | int callback_flags = 0) { |
| 232 | typedef internal::CallbackHolder<Sig> HolderT; |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 233 | HolderT* holder = new HolderT(isolate, callback, callback_flags); |
| 234 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 235 | return v8::FunctionTemplate::New( |
[email protected] | 505ffde | 2013-12-19 15:47:13 | [diff] [blame] | 236 | isolate, |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 237 | &internal::Dispatcher<Sig>::DispatchToCallback, |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 238 | ConvertToV8<v8::Handle<v8::External> >(isolate, |
| 239 | holder->GetHandle(isolate))); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 777183d0 | 2014-03-10 12:55:19 | [diff] [blame] | 242 | // CreateFunctionHandler installs a CallAsFunction handler on the given |
| 243 | // object template that forwards to a provided C++ function or base::Callback. |
| 244 | template<typename Sig> |
| 245 | void CreateFunctionHandler(v8::Isolate* isolate, |
| 246 | v8::Local<v8::ObjectTemplate> tmpl, |
| 247 | const base::Callback<Sig> callback, |
| 248 | int callback_flags = 0) { |
| 249 | typedef internal::CallbackHolder<Sig> HolderT; |
| 250 | HolderT* holder = new HolderT(isolate, callback, callback_flags); |
| 251 | tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback, |
| 252 | ConvertToV8<v8::Handle<v8::External> >( |
| 253 | isolate, holder->GetHandle(isolate))); |
| 254 | } |
| 255 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 256 | } // namespace gin |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 257 | |
| 258 | #endif // GIN_FUNCTION_TEMPLATE_H_ |