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 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 8 | #include <stddef.h> |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 9 | #include <utility> |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 10 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 11 | #include "base/callback.h" |
Hans Wennborg | c8b134b | 2020-06-19 21:15:39 | [diff] [blame] | 12 | #include "base/check.h" |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 13 | #include "base/macros.h" |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 14 | #include "base/strings/strcat.h" |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 15 | #include "gin/arguments.h" |
| 16 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 17 | #include "gin/gin_export.h" |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 18 | #include "v8/include/v8.h" |
| 19 | |
| 20 | namespace gin { |
| 21 | |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 22 | struct InvokerOptions { |
| 23 | bool holder_is_first_argument = false; |
| 24 | const char* holder_type = nullptr; // Null if unknown or not applicable. |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 25 | }; |
| 26 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 27 | namespace internal { |
| 28 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 29 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 30 | struct CallbackParamTraits { |
| 31 | typedef T LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 32 | }; |
| 33 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 34 | struct CallbackParamTraits<const T&> { |
| 35 | typedef T LocalType; |
| 36 | }; |
| 37 | template<typename T> |
| 38 | struct CallbackParamTraits<const T*> { |
| 39 | typedef T* LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 40 | }; |
| 41 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 42 | // CallbackHolder and CallbackHolderBase are used to pass a base::Callback from |
| 43 | // CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 44 | // DispatchToCallback, where it is invoked. |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 45 | |
| 46 | // This simple base class is used so that we can share a single object template |
| 47 | // among every CallbackHolder instance. |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 48 | class GIN_EXPORT CallbackHolderBase { |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 49 | public: |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 50 | v8::Local<v8::External> GetHandle(v8::Isolate* isolate); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 51 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 52 | protected: |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 53 | explicit CallbackHolderBase(v8::Isolate* isolate); |
| 54 | virtual ~CallbackHolderBase(); |
| 55 | |
| 56 | private: |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 57 | static void FirstWeakCallback( |
| 58 | const v8::WeakCallbackInfo<CallbackHolderBase>& data); |
| 59 | static void SecondWeakCallback( |
| 60 | const v8::WeakCallbackInfo<CallbackHolderBase>& data); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 61 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 62 | v8::Global<v8::External> v8_ref_; |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 65 | }; |
| 66 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 67 | template<typename Sig> |
| 68 | class CallbackHolder : public CallbackHolderBase { |
| 69 | public: |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 70 | CallbackHolder(v8::Isolate* isolate, |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 71 | base::RepeatingCallback<Sig> callback, |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 72 | InvokerOptions invoker_options) |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 73 | : CallbackHolderBase(isolate), |
| 74 | callback(std::move(callback)), |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 75 | invoker_options(std::move(invoker_options)) {} |
| 76 | |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 77 | base::RepeatingCallback<Sig> callback; |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 78 | InvokerOptions invoker_options; |
| 79 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 80 | private: |
Takuto Ikuta | 171abf6 | 2018-04-27 16:29:29 | [diff] [blame] | 81 | ~CallbackHolder() override {} |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(CallbackHolder); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 84 | }; |
| 85 | |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 86 | template <typename T> |
| 87 | bool GetNextArgument(Arguments* args, |
| 88 | const InvokerOptions& invoker_options, |
| 89 | bool is_first, |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 90 | T* result) { |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 91 | if (is_first && invoker_options.holder_is_first_argument) { |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 92 | return args->GetHolder(result); |
| 93 | } else { |
| 94 | return args->GetNext(result); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // For advanced use cases, we allow callers to request the unparsed Arguments |
| 99 | // object and poke around in it directly. |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 100 | inline bool GetNextArgument(Arguments* args, |
| 101 | const InvokerOptions& invoker_options, |
| 102 | bool is_first, |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 103 | Arguments* result) { |
| 104 | *result = *args; |
| 105 | return true; |
| 106 | } |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 107 | inline bool GetNextArgument(Arguments* args, |
| 108 | const InvokerOptions& invoker_options, |
| 109 | bool is_first, |
[email protected] | 5b971af | 2014-01-06 22:20:54 | [diff] [blame] | 110 | Arguments** result) { |
| 111 | *result = args; |
| 112 | return true; |
| 113 | } |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 114 | |
[email protected] | 2491f14 | 2013-12-21 17:54:37 | [diff] [blame] | 115 | // It's common for clients to just need the isolate, so we make that easy. |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 116 | inline bool GetNextArgument(Arguments* args, |
| 117 | const InvokerOptions& invoker_options, |
| 118 | bool is_first, |
| 119 | v8::Isolate** result) { |
[email protected] | 2491f14 | 2013-12-21 17:54:37 | [diff] [blame] | 120 | *result = args->isolate(); |
| 121 | return true; |
| 122 | } |
| 123 | |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 124 | // Throws an error indicating conversion failure. |
| 125 | GIN_EXPORT void ThrowConversionError(Arguments* args, |
| 126 | const InvokerOptions& invoker_options, |
| 127 | size_t index); |
| 128 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 129 | // Class template for extracting and storing single argument for callback |
| 130 | // at position |index|. |
| 131 | template <size_t index, typename ArgType> |
| 132 | struct ArgumentHolder { |
| 133 | using ArgLocalType = typename CallbackParamTraits<ArgType>::LocalType; |
| 134 | |
| 135 | ArgLocalType value; |
| 136 | bool ok; |
| 137 | |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 138 | ArgumentHolder(Arguments* args, const InvokerOptions& invoker_options) |
| 139 | : ok(GetNextArgument(args, invoker_options, index == 0, &value)) { |
| 140 | if (!ok) |
| 141 | ThrowConversionError(args, invoker_options, index); |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
| 145 | // Class template for converting arguments from JavaScript to C++ and running |
| 146 | // the callback with them. |
| 147 | template <typename IndicesType, typename... ArgTypes> |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 148 | class Invoker; |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 149 | |
| 150 | template <size_t... indices, typename... ArgTypes> |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 151 | class Invoker<std::index_sequence<indices...>, ArgTypes...> |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 152 | : public ArgumentHolder<indices, ArgTypes>... { |
| 153 | public: |
| 154 | // Invoker<> inherits from ArgumentHolder<> for each argument. |
| 155 | // C++ has always been strict about the class initialization order, |
| 156 | // so it is guaranteed ArgumentHolders will be initialized (and thus, will |
| 157 | // extract arguments from Arguments) in the right order. |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 158 | Invoker(Arguments* args, const InvokerOptions& invoker_options) |
| 159 | : ArgumentHolder<indices, ArgTypes>(args, invoker_options)..., |
| 160 | args_(args) {} |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 161 | |
| 162 | bool IsOK() { |
| 163 | return And(ArgumentHolder<indices, ArgTypes>::ok...); |
| 164 | } |
| 165 | |
| 166 | template <typename ReturnType> |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 167 | void DispatchToCallback( |
| 168 | base::RepeatingCallback<ReturnType(ArgTypes...)> callback) { |
Jeremy Apthorp | 789ac3b | 2020-04-01 01:06:45 | [diff] [blame] | 169 | args_->Return( |
| 170 | callback.Run(std::move(ArgumentHolder<indices, ArgTypes>::value)...)); |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // In C++, you can declare the function foo(void), but you can't pass a void |
| 174 | // expression to foo. As a result, we must specialize the case of Callbacks |
| 175 | // that have the void return type. |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 176 | void DispatchToCallback(base::RepeatingCallback<void(ArgTypes...)> callback) { |
Jeremy Apthorp | 789ac3b | 2020-04-01 01:06:45 | [diff] [blame] | 177 | callback.Run(std::move(ArgumentHolder<indices, ArgTypes>::value)...); |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private: |
| 181 | static bool And() { return true; } |
| 182 | template <typename... T> |
| 183 | static bool And(bool arg1, T... args) { |
| 184 | return arg1 && And(args...); |
| 185 | } |
| 186 | |
| 187 | Arguments* args_; |
| 188 | }; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 189 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 190 | // DispatchToCallback converts all the JavaScript arguments to C++ types and |
| 191 | // invokes the base::Callback. |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 192 | template <typename Sig> |
| 193 | struct Dispatcher {}; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 194 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 195 | template <typename ReturnType, typename... ArgTypes> |
| 196 | struct Dispatcher<ReturnType(ArgTypes...)> { |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 197 | static void DispatchToCallbackImpl(Arguments* args) { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 198 | v8::Local<v8::External> v8_holder; |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 199 | CHECK(args->GetData(&v8_holder)); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 200 | CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>( |
| 201 | v8_holder->Value()); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 202 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 203 | typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 204 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 205 | |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 206 | using Indices = std::index_sequence_for<ArgTypes...>; |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 207 | Invoker<Indices, ArgTypes...> invoker(args, holder->invoker_options); |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 208 | if (invoker.IsOK()) |
| 209 | invoker.DispatchToCallback(holder->callback); |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 210 | } |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 211 | |
| 212 | static void DispatchToCallback( |
| 213 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 214 | Arguments args(info); |
| 215 | DispatchToCallbackImpl(&args); |
| 216 | } |
| 217 | |
| 218 | static void DispatchToCallbackForProperty( |
| 219 | v8::Local<v8::Name>, |
| 220 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 221 | Arguments args(info); |
| 222 | DispatchToCallbackImpl(&args); |
| 223 | } |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 224 | }; |
| 225 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 226 | } // namespace internal |
| 227 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 228 | // CreateFunctionTemplate creates a v8::FunctionTemplate that will create |
| 229 | // JavaScript functions that execute a provided C++ function or base::Callback. |
| 230 | // JavaScript arguments are automatically converted via gin::Converter, as is |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 231 | // the return value of the C++ function, if any. |invoker_options| contains |
| 232 | // additional parameters. If it contains a holder_type, it will be used to |
| 233 | // provide a useful conversion error if the holder is the first argument. If not |
| 234 | // provided, a generic invocation error will be used. |
mnaganov | 098ba6f | 2015-03-03 16:34:48 | [diff] [blame] | 235 | // |
| 236 | // NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own |
| 237 | // internal reasons, thus it is generally a good idea to cache the template |
| 238 | // returned by this function. Otherwise, repeated method invocations from JS |
| 239 | // will create substantial memory leaks. See http://crbug.com/463487. |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 240 | template <typename Sig> |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 241 | v8::Local<v8::FunctionTemplate> CreateFunctionTemplate( |
tzik | c21a0dc | 2017-11-14 08:23:44 | [diff] [blame] | 242 | v8::Isolate* isolate, |
| 243 | base::RepeatingCallback<Sig> callback, |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 244 | InvokerOptions invoker_options = {}) { |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 245 | typedef internal::CallbackHolder<Sig> HolderT; |
Devlin Cronin | e9db984 | 2018-04-09 17:51:05 | [diff] [blame] | 246 | HolderT* holder = |
| 247 | new HolderT(isolate, std::move(callback), std::move(invoker_options)); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 248 | |
jochen | 596fd5e | 2016-07-06 12:29:50 | [diff] [blame] | 249 | v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New( |
| 250 | isolate, &internal::Dispatcher<Sig>::DispatchToCallback, |
| 251 | ConvertToV8<v8::Local<v8::External>>(isolate, |
| 252 | holder->GetHandle(isolate))); |
| 253 | tmpl->RemovePrototype(); |
| 254 | return tmpl; |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 255 | } |
| 256 | |
Jeremy Roman | 6a1242b | 2019-02-04 17:51:57 | [diff] [blame] | 257 | // CreateDataPropertyCallback creates a v8::AccessorNameGetterCallback and |
| 258 | // corresponding data value that will hold and execute the provided |
| 259 | // base::RepeatingCallback, using automatic conversions similar to |
| 260 | // |CreateFunctionTemplate|. |
| 261 | // |
| 262 | // It is expected that these will be passed to v8::Template::SetLazyDataProperty |
| 263 | // or another similar function. |
| 264 | template <typename Sig> |
| 265 | std::pair<v8::AccessorNameGetterCallback, v8::Local<v8::Value>> |
| 266 | CreateDataPropertyCallback(v8::Isolate* isolate, |
| 267 | base::RepeatingCallback<Sig> callback, |
| 268 | InvokerOptions invoker_options = {}) { |
| 269 | typedef internal::CallbackHolder<Sig> HolderT; |
| 270 | HolderT* holder = |
| 271 | new HolderT(isolate, std::move(callback), std::move(invoker_options)); |
| 272 | return {&internal::Dispatcher<Sig>::DispatchToCallbackForProperty, |
| 273 | ConvertToV8<v8::Local<v8::External>>(isolate, |
| 274 | holder->GetHandle(isolate))}; |
| 275 | } |
| 276 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 277 | } // namespace gin |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 278 | |
| 279 | #endif // GIN_FUNCTION_TEMPLATE_H_ |