blob: 1cbe78ccd4841501c66db95259be1e3492cc28b7 [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>
9
[email protected]314cde12013-11-23 20:26:5110#include "base/callback.h"
11#include "base/logging.h"
avi90e658dd2015-12-21 07:16:1912#include "base/macros.h"
[email protected]314cde12013-11-23 20:26:5113#include "gin/arguments.h"
14#include "gin/converter.h"
[email protected]48c21632013-12-12 21:32:3415#include "gin/gin_export.h"
[email protected]314cde12013-11-23 20:26:5116#include "v8/include/v8.h"
17
18namespace gin {
19
[email protected]bf3dd3c2013-12-06 06:55:2520enum CreateFunctionTemplateFlags {
21 HolderIsFirstArgument = 1 << 0,
22};
23
[email protected]37dacfa2013-11-26 03:31:0424namespace internal {
25
[email protected]314cde12013-11-23 20:26:5126template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2527struct CallbackParamTraits {
28 typedef T LocalType;
[email protected]314cde12013-11-23 20:26:5129};
30template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2531struct CallbackParamTraits<const T&> {
32 typedef T LocalType;
33};
34template<typename T>
35struct CallbackParamTraits<const T*> {
36 typedef T* LocalType;
[email protected]314cde12013-11-23 20:26:5137};
38
[email protected]37dacfa2013-11-26 03:31:0439// CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
40// CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to
[email protected]81f8b91b2013-11-26 21:02:5141// DispatchToCallback, where it is invoked.
[email protected]6fe56102013-12-08 07:10:5842
43// This simple base class is used so that we can share a single object template
44// among every CallbackHolder instance.
[email protected]bf0142902014-02-11 15:06:1245class GIN_EXPORT CallbackHolderBase {
[email protected]b4acaf82013-12-12 09:40:5046 public:
deepak.sfaaa1b62015-04-30 07:30:4847 v8::Local<v8::External> GetHandle(v8::Isolate* isolate);
[email protected]bf0142902014-02-11 15:06:1248
[email protected]314cde12013-11-23 20:26:5149 protected:
[email protected]bf0142902014-02-11 15:06:1250 explicit CallbackHolderBase(v8::Isolate* isolate);
51 virtual ~CallbackHolderBase();
52
53 private:
dcarney99ade9082015-04-22 09:55:4254 static void FirstWeakCallback(
55 const v8::WeakCallbackInfo<CallbackHolderBase>& data);
56 static void SecondWeakCallback(
57 const v8::WeakCallbackInfo<CallbackHolderBase>& data);
[email protected]bf0142902014-02-11 15:06:1258
dcarney99ade9082015-04-22 09:55:4259 v8::Global<v8::External> v8_ref_;
[email protected]bf0142902014-02-11 15:06:1260
61 DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase);
[email protected]314cde12013-11-23 20:26:5162};
63
[email protected]314cde12013-11-23 20:26:5164template<typename Sig>
65class CallbackHolder : public CallbackHolderBase {
66 public:
[email protected]bf0142902014-02-11 15:06:1267 CallbackHolder(v8::Isolate* isolate,
tzikc21a0dc2017-11-14 08:23:4468 base::RepeatingCallback<Sig> callback,
[email protected]bf0142902014-02-11 15:06:1269 int flags)
tzikc21a0dc2017-11-14 08:23:4470 : CallbackHolderBase(isolate),
71 callback(std::move(callback)),
72 flags(flags) {}
73 base::RepeatingCallback<Sig> callback;
[email protected]bf3dd3c2013-12-06 06:55:2574 int flags;
[email protected]314cde12013-11-23 20:26:5175 private:
[email protected]695af7f52013-12-11 19:41:0176 virtual ~CallbackHolder() {}
[email protected]bf0142902014-02-11 15:06:1277
78 DISALLOW_COPY_AND_ASSIGN(CallbackHolder);
[email protected]314cde12013-11-23 20:26:5179};
80
[email protected]bf3dd3c2013-12-06 06:55:2581template<typename T>
82bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
83 T* result) {
84 if (is_first && (create_flags & HolderIsFirstArgument) != 0) {
85 return args->GetHolder(result);
86 } else {
87 return args->GetNext(result);
88 }
89}
90
91// For advanced use cases, we allow callers to request the unparsed Arguments
92// object and poke around in it directly.
93inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
94 Arguments* result) {
95 *result = *args;
96 return true;
97}
[email protected]5b971af2014-01-06 22:20:5498inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
99 Arguments** result) {
100 *result = args;
101 return true;
102}
[email protected]bf3dd3c2013-12-06 06:55:25103
[email protected]2491f142013-12-21 17:54:37104// It's common for clients to just need the isolate, so we make that easy.
105inline bool GetNextArgument(Arguments* args, int create_flags,
106 bool is_first, v8::Isolate** result) {
107 *result = args->isolate();
108 return true;
109}
110
kolczyk735c49b62014-10-24 13:06:04111// Class template for extracting and storing single argument for callback
112// at position |index|.
113template <size_t index, typename ArgType>
114struct ArgumentHolder {
115 using ArgLocalType = typename CallbackParamTraits<ArgType>::LocalType;
116
117 ArgLocalType value;
118 bool ok;
119
120 ArgumentHolder(Arguments* args, int create_flags)
121 : ok(GetNextArgument(args, create_flags, index == 0, &value)) {
eseidel2584930f2014-12-15 22:06:44122 if (!ok) {
123 // Ideally we would include the expected c++ type in the error
124 // message which we can access via typeid(ArgType).name()
125 // however we compile with no-rtti, which disables typeid.
kolczyk735c49b62014-10-24 13:06:04126 args->ThrowError();
eseidel2584930f2014-12-15 22:06:44127 }
kolczyk735c49b62014-10-24 13:06:04128 }
129};
130
131// Class template for converting arguments from JavaScript to C++ and running
132// the callback with them.
133template <typename IndicesType, typename... ArgTypes>
tzikc21a0dc2017-11-14 08:23:44134class Invoker;
kolczyk735c49b62014-10-24 13:06:04135
136template <size_t... indices, typename... ArgTypes>
tzikc21a0dc2017-11-14 08:23:44137class Invoker<std::index_sequence<indices...>, ArgTypes...>
kolczyk735c49b62014-10-24 13:06:04138 : public ArgumentHolder<indices, ArgTypes>... {
139 public:
140 // Invoker<> inherits from ArgumentHolder<> for each argument.
141 // C++ has always been strict about the class initialization order,
142 // so it is guaranteed ArgumentHolders will be initialized (and thus, will
143 // extract arguments from Arguments) in the right order.
144 Invoker(Arguments* args, int create_flags)
cmasonea34136a2014-11-01 00:10:47145 : ArgumentHolder<indices, ArgTypes>(args, create_flags)..., args_(args) {
cmasonea34136a2014-11-01 00:10:47146 }
kolczyk735c49b62014-10-24 13:06:04147
148 bool IsOK() {
149 return And(ArgumentHolder<indices, ArgTypes>::ok...);
150 }
151
152 template <typename ReturnType>
tzikc21a0dc2017-11-14 08:23:44153 void DispatchToCallback(
154 base::RepeatingCallback<ReturnType(ArgTypes...)> callback) {
kolczyk735c49b62014-10-24 13:06:04155 args_->Return(callback.Run(ArgumentHolder<indices, ArgTypes>::value...));
156 }
157
158 // In C++, you can declare the function foo(void), but you can't pass a void
159 // expression to foo. As a result, we must specialize the case of Callbacks
160 // that have the void return type.
tzikc21a0dc2017-11-14 08:23:44161 void DispatchToCallback(base::RepeatingCallback<void(ArgTypes...)> callback) {
kolczyk735c49b62014-10-24 13:06:04162 callback.Run(ArgumentHolder<indices, ArgTypes>::value...);
163 }
164
165 private:
166 static bool And() { return true; }
167 template <typename... T>
168 static bool And(bool arg1, T... args) {
169 return arg1 && And(args...);
170 }
171
172 Arguments* args_;
173};
[email protected]bf3dd3c2013-12-06 06:55:25174
[email protected]81f8b91b2013-11-26 21:02:51175// DispatchToCallback converts all the JavaScript arguments to C++ types and
176// invokes the base::Callback.
kolczyk735c49b62014-10-24 13:06:04177template <typename Sig>
178struct Dispatcher {};
[email protected]bf3dd3c2013-12-06 06:55:25179
kolczyk735c49b62014-10-24 13:06:04180template <typename ReturnType, typename... ArgTypes>
181struct Dispatcher<ReturnType(ArgTypes...)> {
[email protected]bf3dd3c2013-12-06 06:55:25182 static void DispatchToCallback(
183 const v8::FunctionCallbackInfo<v8::Value>& info) {
184 Arguments args(info);
deepak.sfaaa1b62015-04-30 07:30:48185 v8::Local<v8::External> v8_holder;
[email protected]bf0142902014-02-11 15:06:12186 CHECK(args.GetData(&v8_holder));
187 CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>(
188 v8_holder->Value());
[email protected]314cde12013-11-23 20:26:51189
kolczyk735c49b62014-10-24 13:06:04190 typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT;
[email protected]bf3dd3c2013-12-06 06:55:25191 HolderT* holder = static_cast<HolderT*>(holder_base);
[email protected]37dacfa2013-11-26 03:31:04192
tzikc21a0dc2017-11-14 08:23:44193 using Indices = std::index_sequence_for<ArgTypes...>;
kolczyk735c49b62014-10-24 13:06:04194 Invoker<Indices, ArgTypes...> invoker(&args, holder->flags);
195 if (invoker.IsOK())
196 invoker.DispatchToCallback(holder->callback);
[email protected]d73341d12013-12-21 00:48:46197 }
198};
199
[email protected]81f8b91b2013-11-26 21:02:51200} // namespace internal
201
202
[email protected]bf3dd3c2013-12-06 06:55:25203// CreateFunctionTemplate creates a v8::FunctionTemplate that will create
204// JavaScript functions that execute a provided C++ function or base::Callback.
205// JavaScript arguments are automatically converted via gin::Converter, as is
206// the return value of the C++ function, if any.
mnaganov098ba6f2015-03-03 16:34:48207//
208// NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own
209// internal reasons, thus it is generally a good idea to cache the template
210// returned by this function. Otherwise, repeated method invocations from JS
211// will create substantial memory leaks. See http://crbug.com/463487.
tzikc21a0dc2017-11-14 08:23:44212template <typename Sig>
[email protected]81f8b91b2013-11-26 21:02:51213v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
tzikc21a0dc2017-11-14 08:23:44214 v8::Isolate* isolate,
215 base::RepeatingCallback<Sig> callback,
[email protected]bf3dd3c2013-12-06 06:55:25216 int callback_flags = 0) {
217 typedef internal::CallbackHolder<Sig> HolderT;
tzikc21a0dc2017-11-14 08:23:44218 HolderT* holder = new HolderT(isolate, std::move(callback), callback_flags);
[email protected]bf0142902014-02-11 15:06:12219
jochen596fd5e2016-07-06 12:29:50220 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
221 isolate, &internal::Dispatcher<Sig>::DispatchToCallback,
222 ConvertToV8<v8::Local<v8::External>>(isolate,
223 holder->GetHandle(isolate)));
224 tmpl->RemovePrototype();
225 return tmpl;
[email protected]7618ebbb2013-11-27 03:38:26226}
227
[email protected]777183d02014-03-10 12:55:19228// CreateFunctionHandler installs a CallAsFunction handler on the given
229// object template that forwards to a provided C++ function or base::Callback.
tzikc21a0dc2017-11-14 08:23:44230template <typename Sig>
[email protected]777183d02014-03-10 12:55:19231void CreateFunctionHandler(v8::Isolate* isolate,
232 v8::Local<v8::ObjectTemplate> tmpl,
tzikc21a0dc2017-11-14 08:23:44233 base::RepeatingCallback<Sig> callback,
[email protected]777183d02014-03-10 12:55:19234 int callback_flags = 0) {
235 typedef internal::CallbackHolder<Sig> HolderT;
tzikc21a0dc2017-11-14 08:23:44236 HolderT* holder = new HolderT(isolate, std::move(callback), callback_flags);
[email protected]777183d02014-03-10 12:55:19237 tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback,
deepak.sfaaa1b62015-04-30 07:30:48238 ConvertToV8<v8::Local<v8::External> >(
[email protected]777183d02014-03-10 12:55:19239 isolate, holder->GetHandle(isolate)));
240}
241
[email protected]314cde12013-11-23 20:26:51242} // namespace gin
[email protected]b520e132013-11-29 03:21:48243
244#endif // GIN_FUNCTION_TEMPLATE_H_