blob: 61b2064ba01e35c8db8701fd60f8bbf7d10da03c [file] [log] [blame]
kolczyk735c49b62014-10-24 13:06:041// 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]37dacfa2013-11-26 03:31:044
[email protected]b520e132013-11-29 03:21:485#ifndef GIN_FUNCTION_TEMPLATE_H_
6#define GIN_FUNCTION_TEMPLATE_H_
7
avi90e658dd2015-12-21 07:16:198#include <stddef.h>
Jeremy Roman6a1242b2019-02-04 17:51:579#include <utility>
avi90e658dd2015-12-21 07:16:1910
[email protected]314cde12013-11-23 20:26:5111#include "base/callback.h"
Hans Wennborgc8b134b2020-06-19 21:15:3912#include "base/check.h"
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
Devlin Cronine9db9842018-04-09 17:51:0514#include "base/strings/strcat.h"
[email protected]314cde12013-11-23 20:26:5115#include "gin/arguments.h"
16#include "gin/converter.h"
[email protected]48c21632013-12-12 21:32:3417#include "gin/gin_export.h"
Dan Elphick05acd602021-08-30 15:22:0718#include "v8/include/v8-external.h"
19#include "v8/include/v8-forward.h"
20#include "v8/include/v8-persistent-handle.h"
21#include "v8/include/v8-template.h"
[email protected]314cde12013-11-23 20:26:5122
23namespace gin {
24
Devlin Cronine9db9842018-04-09 17:51:0525struct InvokerOptions {
26 bool holder_is_first_argument = false;
27 const char* holder_type = nullptr; // Null if unknown or not applicable.
[email protected]bf3dd3c2013-12-06 06:55:2528};
29
[email protected]37dacfa2013-11-26 03:31:0430namespace internal {
31
[email protected]314cde12013-11-23 20:26:5132template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2533struct CallbackParamTraits {
34 typedef T LocalType;
[email protected]314cde12013-11-23 20:26:5135};
36template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2537struct CallbackParamTraits<const T&> {
38 typedef T LocalType;
39};
40template<typename T>
41struct CallbackParamTraits<const T*> {
42 typedef T* LocalType;
[email protected]314cde12013-11-23 20:26:5143};
44
Colin Blundellea615d422021-05-12 09:35:4145// CallbackHolder and CallbackHolderBase are used to pass a
46// base::RepeatingCallback from CreateFunctionTemplate through v8 (via
47// v8::FunctionTemplate) to DispatchToCallback, where it is invoked.
[email protected]6fe56102013-12-08 07:10:5848
49// This simple base class is used so that we can share a single object template
50// among every CallbackHolder instance.
[email protected]bf0142902014-02-11 15:06:1251class GIN_EXPORT CallbackHolderBase {
[email protected]b4acaf82013-12-12 09:40:5052 public:
Daniel Hosseinian68c0798d2021-04-16 08:16:0753 CallbackHolderBase(const CallbackHolderBase&) = delete;
54 CallbackHolderBase& operator=(const CallbackHolderBase&) = delete;
55
deepak.sfaaa1b62015-04-30 07:30:4856 v8::Local<v8::External> GetHandle(v8::Isolate* isolate);
[email protected]bf0142902014-02-11 15:06:1257
[email protected]314cde12013-11-23 20:26:5158 protected:
[email protected]bf0142902014-02-11 15:06:1259 explicit CallbackHolderBase(v8::Isolate* isolate);
60 virtual ~CallbackHolderBase();
61
62 private:
dcarney99ade9082015-04-22 09:55:4263 static void FirstWeakCallback(
64 const v8::WeakCallbackInfo<CallbackHolderBase>& data);
65 static void SecondWeakCallback(
66 const v8::WeakCallbackInfo<CallbackHolderBase>& data);
[email protected]bf0142902014-02-11 15:06:1267
dcarney99ade9082015-04-22 09:55:4268 v8::Global<v8::External> v8_ref_;
[email protected]314cde12013-11-23 20:26:5169};
70
[email protected]314cde12013-11-23 20:26:5171template<typename Sig>
72class CallbackHolder : public CallbackHolderBase {
73 public:
[email protected]bf0142902014-02-11 15:06:1274 CallbackHolder(v8::Isolate* isolate,
tzikc21a0dc2017-11-14 08:23:4475 base::RepeatingCallback<Sig> callback,
Devlin Cronine9db9842018-04-09 17:51:0576 InvokerOptions invoker_options)
tzikc21a0dc2017-11-14 08:23:4477 : CallbackHolderBase(isolate),
78 callback(std::move(callback)),
Devlin Cronine9db9842018-04-09 17:51:0579 invoker_options(std::move(invoker_options)) {}
Daniel Hosseinian68c0798d2021-04-16 08:16:0780 CallbackHolder(const CallbackHolder&) = delete;
81 CallbackHolder& operator=(const CallbackHolder&) = delete;
Devlin Cronine9db9842018-04-09 17:51:0582
tzikc21a0dc2017-11-14 08:23:4483 base::RepeatingCallback<Sig> callback;
Devlin Cronine9db9842018-04-09 17:51:0584 InvokerOptions invoker_options;
85
[email protected]314cde12013-11-23 20:26:5186 private:
Daniel Hosseinian68c0798d2021-04-16 08:16:0787 ~CallbackHolder() override = default;
[email protected]314cde12013-11-23 20:26:5188};
89
Devlin Cronine9db9842018-04-09 17:51:0590template <typename T>
91bool GetNextArgument(Arguments* args,
92 const InvokerOptions& invoker_options,
93 bool is_first,
[email protected]bf3dd3c2013-12-06 06:55:2594 T* result) {
Devlin Cronine9db9842018-04-09 17:51:0595 if (is_first && invoker_options.holder_is_first_argument) {
[email protected]bf3dd3c2013-12-06 06:55:2596 return args->GetHolder(result);
97 } else {
98 return args->GetNext(result);
99 }
100}
101
102// For advanced use cases, we allow callers to request the unparsed Arguments
103// object and poke around in it directly.
Devlin Cronine9db9842018-04-09 17:51:05104inline bool GetNextArgument(Arguments* args,
105 const InvokerOptions& invoker_options,
106 bool is_first,
[email protected]bf3dd3c2013-12-06 06:55:25107 Arguments* result) {
108 *result = *args;
109 return true;
110}
Devlin Cronine9db9842018-04-09 17:51:05111inline bool GetNextArgument(Arguments* args,
112 const InvokerOptions& invoker_options,
113 bool is_first,
[email protected]5b971af2014-01-06 22:20:54114 Arguments** result) {
115 *result = args;
116 return true;
117}
[email protected]bf3dd3c2013-12-06 06:55:25118
[email protected]2491f142013-12-21 17:54:37119// It's common for clients to just need the isolate, so we make that easy.
Devlin Cronine9db9842018-04-09 17:51:05120inline bool GetNextArgument(Arguments* args,
121 const InvokerOptions& invoker_options,
122 bool is_first,
123 v8::Isolate** result) {
[email protected]2491f142013-12-21 17:54:37124 *result = args->isolate();
125 return true;
126}
127
Devlin Cronine9db9842018-04-09 17:51:05128// Throws an error indicating conversion failure.
129GIN_EXPORT void ThrowConversionError(Arguments* args,
130 const InvokerOptions& invoker_options,
131 size_t index);
132
kolczyk735c49b62014-10-24 13:06:04133// Class template for extracting and storing single argument for callback
134// at position |index|.
135template <size_t index, typename ArgType>
136struct ArgumentHolder {
137 using ArgLocalType = typename CallbackParamTraits<ArgType>::LocalType;
138
139 ArgLocalType value;
140 bool ok;
141
Devlin Cronine9db9842018-04-09 17:51:05142 ArgumentHolder(Arguments* args, const InvokerOptions& invoker_options)
143 : ok(GetNextArgument(args, invoker_options, index == 0, &value)) {
144 if (!ok)
145 ThrowConversionError(args, invoker_options, index);
kolczyk735c49b62014-10-24 13:06:04146 }
147};
148
149// Class template for converting arguments from JavaScript to C++ and running
150// the callback with them.
151template <typename IndicesType, typename... ArgTypes>
tzikc21a0dc2017-11-14 08:23:44152class Invoker;
kolczyk735c49b62014-10-24 13:06:04153
154template <size_t... indices, typename... ArgTypes>
tzikc21a0dc2017-11-14 08:23:44155class Invoker<std::index_sequence<indices...>, ArgTypes...>
kolczyk735c49b62014-10-24 13:06:04156 : public ArgumentHolder<indices, ArgTypes>... {
157 public:
158 // Invoker<> inherits from ArgumentHolder<> for each argument.
159 // C++ has always been strict about the class initialization order,
160 // so it is guaranteed ArgumentHolders will be initialized (and thus, will
161 // extract arguments from Arguments) in the right order.
Devlin Cronine9db9842018-04-09 17:51:05162 Invoker(Arguments* args, const InvokerOptions& invoker_options)
163 : ArgumentHolder<indices, ArgTypes>(args, invoker_options)...,
164 args_(args) {}
kolczyk735c49b62014-10-24 13:06:04165
166 bool IsOK() {
167 return And(ArgumentHolder<indices, ArgTypes>::ok...);
168 }
169
170 template <typename ReturnType>
tzikc21a0dc2017-11-14 08:23:44171 void DispatchToCallback(
172 base::RepeatingCallback<ReturnType(ArgTypes...)> callback) {
Jeremy Apthorp789ac3b2020-04-01 01:06:45173 args_->Return(
174 callback.Run(std::move(ArgumentHolder<indices, ArgTypes>::value)...));
kolczyk735c49b62014-10-24 13:06:04175 }
176
177 // In C++, you can declare the function foo(void), but you can't pass a void
178 // expression to foo. As a result, we must specialize the case of Callbacks
179 // that have the void return type.
tzikc21a0dc2017-11-14 08:23:44180 void DispatchToCallback(base::RepeatingCallback<void(ArgTypes...)> callback) {
Jeremy Apthorp789ac3b2020-04-01 01:06:45181 callback.Run(std::move(ArgumentHolder<indices, ArgTypes>::value)...);
kolczyk735c49b62014-10-24 13:06:04182 }
183
184 private:
185 static bool And() { return true; }
186 template <typename... T>
187 static bool And(bool arg1, T... args) {
188 return arg1 && And(args...);
189 }
190
Keishi Hattori0e45c022021-11-27 09:25:52191 raw_ptr<Arguments> args_;
kolczyk735c49b62014-10-24 13:06:04192};
[email protected]bf3dd3c2013-12-06 06:55:25193
[email protected]81f8b91b2013-11-26 21:02:51194// DispatchToCallback converts all the JavaScript arguments to C++ types and
Colin Blundellea615d422021-05-12 09:35:41195// invokes the base::RepeatingCallback.
kolczyk735c49b62014-10-24 13:06:04196template <typename Sig>
197struct Dispatcher {};
[email protected]bf3dd3c2013-12-06 06:55:25198
kolczyk735c49b62014-10-24 13:06:04199template <typename ReturnType, typename... ArgTypes>
200struct Dispatcher<ReturnType(ArgTypes...)> {
Jeremy Roman6a1242b2019-02-04 17:51:57201 static void DispatchToCallbackImpl(Arguments* args) {
deepak.sfaaa1b62015-04-30 07:30:48202 v8::Local<v8::External> v8_holder;
Jeremy Roman6a1242b2019-02-04 17:51:57203 CHECK(args->GetData(&v8_holder));
[email protected]bf0142902014-02-11 15:06:12204 CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>(
205 v8_holder->Value());
[email protected]314cde12013-11-23 20:26:51206
kolczyk735c49b62014-10-24 13:06:04207 typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT;
[email protected]bf3dd3c2013-12-06 06:55:25208 HolderT* holder = static_cast<HolderT*>(holder_base);
[email protected]37dacfa2013-11-26 03:31:04209
tzikc21a0dc2017-11-14 08:23:44210 using Indices = std::index_sequence_for<ArgTypes...>;
Jeremy Roman6a1242b2019-02-04 17:51:57211 Invoker<Indices, ArgTypes...> invoker(args, holder->invoker_options);
kolczyk735c49b62014-10-24 13:06:04212 if (invoker.IsOK())
213 invoker.DispatchToCallback(holder->callback);
[email protected]d73341d12013-12-21 00:48:46214 }
Jeremy Roman6a1242b2019-02-04 17:51:57215
216 static void DispatchToCallback(
217 const v8::FunctionCallbackInfo<v8::Value>& info) {
218 Arguments args(info);
219 DispatchToCallbackImpl(&args);
220 }
221
222 static void DispatchToCallbackForProperty(
223 v8::Local<v8::Name>,
224 const v8::PropertyCallbackInfo<v8::Value>& info) {
225 Arguments args(info);
226 DispatchToCallbackImpl(&args);
227 }
[email protected]d73341d12013-12-21 00:48:46228};
229
[email protected]81f8b91b2013-11-26 21:02:51230} // namespace internal
231
[email protected]bf3dd3c2013-12-06 06:55:25232// CreateFunctionTemplate creates a v8::FunctionTemplate that will create
Colin Blundellea615d422021-05-12 09:35:41233// JavaScript functions that execute a provided C++ function or
234// base::RepeatingCallback. JavaScript arguments are automatically converted via
235// gin::Converter, as is the return value of the C++ function, if any.
236// |invoker_options| contains additional parameters. If it contains a
237// holder_type, it will be used to provide a useful conversion error if the
238// holder is the first argument. If not provided, a generic invocation error
239// will be used.
mnaganov098ba6f2015-03-03 16:34:48240//
241// NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own
242// internal reasons, thus it is generally a good idea to cache the template
243// returned by this function. Otherwise, repeated method invocations from JS
244// will create substantial memory leaks. See http://crbug.com/463487.
tzikc21a0dc2017-11-14 08:23:44245template <typename Sig>
[email protected]81f8b91b2013-11-26 21:02:51246v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
tzikc21a0dc2017-11-14 08:23:44247 v8::Isolate* isolate,
248 base::RepeatingCallback<Sig> callback,
Devlin Cronine9db9842018-04-09 17:51:05249 InvokerOptions invoker_options = {}) {
[email protected]bf3dd3c2013-12-06 06:55:25250 typedef internal::CallbackHolder<Sig> HolderT;
Devlin Cronine9db9842018-04-09 17:51:05251 HolderT* holder =
252 new HolderT(isolate, std::move(callback), std::move(invoker_options));
[email protected]bf0142902014-02-11 15:06:12253
jochen596fd5e2016-07-06 12:29:50254 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
255 isolate, &internal::Dispatcher<Sig>::DispatchToCallback,
Sathya Gunasekaran77d2ce82021-01-08 16:41:13256 ConvertToV8<v8::Local<v8::External>>(isolate, holder->GetHandle(isolate)),
257 v8::Local<v8::Signature>(), 0, v8::ConstructorBehavior::kThrow);
jochen596fd5e2016-07-06 12:29:50258 return tmpl;
[email protected]7618ebbb2013-11-27 03:38:26259}
260
Jeremy Roman6a1242b2019-02-04 17:51:57261// CreateDataPropertyCallback creates a v8::AccessorNameGetterCallback and
262// corresponding data value that will hold and execute the provided
263// base::RepeatingCallback, using automatic conversions similar to
264// |CreateFunctionTemplate|.
265//
266// It is expected that these will be passed to v8::Template::SetLazyDataProperty
267// or another similar function.
268template <typename Sig>
269std::pair<v8::AccessorNameGetterCallback, v8::Local<v8::Value>>
270CreateDataPropertyCallback(v8::Isolate* isolate,
271 base::RepeatingCallback<Sig> callback,
272 InvokerOptions invoker_options = {}) {
273 typedef internal::CallbackHolder<Sig> HolderT;
274 HolderT* holder =
275 new HolderT(isolate, std::move(callback), std::move(invoker_options));
276 return {&internal::Dispatcher<Sig>::DispatchToCallbackForProperty,
277 ConvertToV8<v8::Local<v8::External>>(isolate,
278 holder->GetHandle(isolate))};
279}
280
[email protected]314cde12013-11-23 20:26:51281} // namespace gin
[email protected]b520e132013-11-29 03:21:48282
283#endif // GIN_FUNCTION_TEMPLATE_H_