blob: c0228cb5f83f2a24f8f806dab381c82340fe5455 [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
20class PerIsolateData;
21
[email protected]bf3dd3c2013-12-06 06:55:2522enum CreateFunctionTemplateFlags {
23 HolderIsFirstArgument = 1 << 0,
24};
25
[email protected]37dacfa2013-11-26 03:31:0426namespace internal {
27
[email protected]314cde12013-11-23 20:26:5128template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2529struct CallbackParamTraits {
30 typedef T LocalType;
[email protected]314cde12013-11-23 20:26:5131};
32template<typename T>
[email protected]bf3dd3c2013-12-06 06:55:2533struct CallbackParamTraits<const T&> {
34 typedef T LocalType;
35};
36template<typename T>
37struct CallbackParamTraits<const T*> {
38 typedef T* LocalType;
[email protected]314cde12013-11-23 20:26:5139};
40
[email protected]37dacfa2013-11-26 03:31:0441
42// CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
43// CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to
[email protected]81f8b91b2013-11-26 21:02:5144// DispatchToCallback, where it is invoked.
[email protected]6fe56102013-12-08 07:10:5845
46// This simple base class is used so that we can share a single object template
47// among every CallbackHolder instance.
[email protected]bf0142902014-02-11 15:06:1248class GIN_EXPORT CallbackHolderBase {
[email protected]b4acaf82013-12-12 09:40:5049 public:
deepak.sfaaa1b62015-04-30 07:30:4850 v8::Local<v8::External> GetHandle(v8::Isolate* isolate);
[email protected]bf0142902014-02-11 15:06:1251
[email protected]314cde12013-11-23 20:26:5152 protected:
[email protected]bf0142902014-02-11 15:06:1253 explicit CallbackHolderBase(v8::Isolate* isolate);
54 virtual ~CallbackHolderBase();
55
56 private:
dcarney99ade9082015-04-22 09:55:4257 static void FirstWeakCallback(
58 const v8::WeakCallbackInfo<CallbackHolderBase>& data);
59 static void SecondWeakCallback(
60 const v8::WeakCallbackInfo<CallbackHolderBase>& data);
[email protected]bf0142902014-02-11 15:06:1261
dcarney99ade9082015-04-22 09:55:4262 v8::Global<v8::External> v8_ref_;
[email protected]bf0142902014-02-11 15:06:1263
64 DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase);
[email protected]314cde12013-11-23 20:26:5165};
66
[email protected]314cde12013-11-23 20:26:5167template<typename Sig>
68class CallbackHolder : public CallbackHolderBase {
69 public:
[email protected]bf0142902014-02-11 15:06:1270 CallbackHolder(v8::Isolate* isolate,
71 const base::Callback<Sig>& callback,
72 int flags)
73 : CallbackHolderBase(isolate), callback(callback), flags(flags) {}
[email protected]314cde12013-11-23 20:26:5174 base::Callback<Sig> callback;
[email protected]bf3dd3c2013-12-06 06:55:2575 int flags;
[email protected]314cde12013-11-23 20:26:5176 private:
[email protected]695af7f52013-12-11 19:41:0177 virtual ~CallbackHolder() {}
[email protected]bf0142902014-02-11 15:06:1278
79 DISALLOW_COPY_AND_ASSIGN(CallbackHolder);
[email protected]314cde12013-11-23 20:26:5180};
81
[email protected]bf3dd3c2013-12-06 06:55:2582template<typename T>
83bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
84 T* result) {
85 if (is_first && (create_flags & HolderIsFirstArgument) != 0) {
86 return args->GetHolder(result);
87 } else {
88 return args->GetNext(result);
89 }
90}
91
92// For advanced use cases, we allow callers to request the unparsed Arguments
93// object and poke around in it directly.
94inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
95 Arguments* result) {
96 *result = *args;
97 return true;
98}
[email protected]5b971af2014-01-06 22:20:5499inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
100 Arguments** result) {
101 *result = args;
102 return true;
103}
[email protected]bf3dd3c2013-12-06 06:55:25104
[email protected]2491f142013-12-21 17:54:37105// It's common for clients to just need the isolate, so we make that easy.
106inline bool GetNextArgument(Arguments* args, int create_flags,
107 bool is_first, v8::Isolate** result) {
108 *result = args->isolate();
109 return true;
110}
111
kolczyk735c49b62014-10-24 13:06:04112// Classes for generating and storing an argument pack of integer indices
113// (based on well-known "indices trick", see: http://goo.gl/bKKojn):
114template <size_t... indices>
115struct IndicesHolder {};
116
117template <size_t requested_index, size_t... indices>
118struct IndicesGenerator {
119 using type = typename IndicesGenerator<requested_index - 1,
120 requested_index - 1,
121 indices...>::type;
122};
123template <size_t... indices>
124struct IndicesGenerator<0, indices...> {
125 using type = IndicesHolder<indices...>;
126};
127
128// Class template for extracting and storing single argument for callback
129// at position |index|.
130template <size_t index, typename ArgType>
131struct ArgumentHolder {
132 using ArgLocalType = typename CallbackParamTraits<ArgType>::LocalType;
133
134 ArgLocalType value;
135 bool ok;
136
137 ArgumentHolder(Arguments* args, int create_flags)
138 : ok(GetNextArgument(args, create_flags, index == 0, &value)) {
eseidel2584930f2014-12-15 22:06:44139 if (!ok) {
140 // Ideally we would include the expected c++ type in the error
141 // message which we can access via typeid(ArgType).name()
142 // however we compile with no-rtti, which disables typeid.
kolczyk735c49b62014-10-24 13:06:04143 args->ThrowError();
eseidel2584930f2014-12-15 22:06:44144 }
kolczyk735c49b62014-10-24 13:06:04145 }
146};
147
148// Class template for converting arguments from JavaScript to C++ and running
149// the callback with them.
150template <typename IndicesType, typename... ArgTypes>
151class Invoker {};
152
153template <size_t... indices, typename... ArgTypes>
154class Invoker<IndicesHolder<indices...>, ArgTypes...>
155 : public ArgumentHolder<indices, ArgTypes>... {
156 public:
157 // Invoker<> inherits from ArgumentHolder<> for each argument.
158 // C++ has always been strict about the class initialization order,
159 // so it is guaranteed ArgumentHolders will be initialized (and thus, will
160 // extract arguments from Arguments) in the right order.
161 Invoker(Arguments* args, int create_flags)
cmasonea34136a2014-11-01 00:10:47162 : ArgumentHolder<indices, ArgTypes>(args, create_flags)..., args_(args) {
163 // GCC thinks that create_flags is going unused, even though the
164 // expansion above clearly makes use of it. Per jyasskin@, casting
165 // to void is the commonly accepted way to convince the compiler
166 // that you're actually using a parameter/varible.
167 (void)create_flags;
168 }
kolczyk735c49b62014-10-24 13:06:04169
170 bool IsOK() {
171 return And(ArgumentHolder<indices, ArgTypes>::ok...);
172 }
173
174 template <typename ReturnType>
175 void DispatchToCallback(base::Callback<ReturnType(ArgTypes...)> callback) {
176 args_->Return(callback.Run(ArgumentHolder<indices, ArgTypes>::value...));
177 }
178
179 // In C++, you can declare the function foo(void), but you can't pass a void
180 // expression to foo. As a result, we must specialize the case of Callbacks
181 // that have the void return type.
182 void DispatchToCallback(base::Callback<void(ArgTypes...)> callback) {
183 callback.Run(ArgumentHolder<indices, ArgTypes>::value...);
184 }
185
186 private:
187 static bool And() { return true; }
188 template <typename... T>
189 static bool And(bool arg1, T... args) {
190 return arg1 && And(args...);
191 }
192
193 Arguments* args_;
194};
[email protected]bf3dd3c2013-12-06 06:55:25195
[email protected]81f8b91b2013-11-26 21:02:51196// DispatchToCallback converts all the JavaScript arguments to C++ types and
197// invokes the base::Callback.
kolczyk735c49b62014-10-24 13:06:04198template <typename Sig>
199struct Dispatcher {};
[email protected]bf3dd3c2013-12-06 06:55:25200
kolczyk735c49b62014-10-24 13:06:04201template <typename ReturnType, typename... ArgTypes>
202struct Dispatcher<ReturnType(ArgTypes...)> {
[email protected]bf3dd3c2013-12-06 06:55:25203 static void DispatchToCallback(
204 const v8::FunctionCallbackInfo<v8::Value>& info) {
205 Arguments args(info);
deepak.sfaaa1b62015-04-30 07:30:48206 v8::Local<v8::External> v8_holder;
[email protected]bf0142902014-02-11 15:06:12207 CHECK(args.GetData(&v8_holder));
208 CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>(
209 v8_holder->Value());
[email protected]314cde12013-11-23 20:26:51210
kolczyk735c49b62014-10-24 13:06:04211 typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT;
[email protected]bf3dd3c2013-12-06 06:55:25212 HolderT* holder = static_cast<HolderT*>(holder_base);
[email protected]37dacfa2013-11-26 03:31:04213
kolczyk735c49b62014-10-24 13:06:04214 using Indices = typename IndicesGenerator<sizeof...(ArgTypes)>::type;
215 Invoker<Indices, ArgTypes...> invoker(&args, holder->flags);
216 if (invoker.IsOK())
217 invoker.DispatchToCallback(holder->callback);
[email protected]d73341d12013-12-21 00:48:46218 }
219};
220
[email protected]81f8b91b2013-11-26 21:02:51221} // namespace internal
222
223
[email protected]bf3dd3c2013-12-06 06:55:25224// CreateFunctionTemplate creates a v8::FunctionTemplate that will create
225// JavaScript functions that execute a provided C++ function or base::Callback.
226// JavaScript arguments are automatically converted via gin::Converter, as is
227// the return value of the C++ function, if any.
mnaganov098ba6f2015-03-03 16:34:48228//
229// NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own
230// internal reasons, thus it is generally a good idea to cache the template
231// returned by this function. Otherwise, repeated method invocations from JS
232// will create substantial memory leaks. See http://crbug.com/463487.
[email protected]bf3dd3c2013-12-06 06:55:25233template<typename Sig>
[email protected]81f8b91b2013-11-26 21:02:51234v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
[email protected]bf3dd3c2013-12-06 06:55:25235 v8::Isolate* isolate, const base::Callback<Sig> callback,
236 int callback_flags = 0) {
237 typedef internal::CallbackHolder<Sig> HolderT;
[email protected]bf0142902014-02-11 15:06:12238 HolderT* holder = new HolderT(isolate, callback, callback_flags);
239
[email protected]81f8b91b2013-11-26 21:02:51240 return v8::FunctionTemplate::New(
[email protected]505ffde2013-12-19 15:47:13241 isolate,
[email protected]bf3dd3c2013-12-06 06:55:25242 &internal::Dispatcher<Sig>::DispatchToCallback,
deepak.sfaaa1b62015-04-30 07:30:48243 ConvertToV8<v8::Local<v8::External> >(isolate,
[email protected]bf0142902014-02-11 15:06:12244 holder->GetHandle(isolate)));
[email protected]7618ebbb2013-11-27 03:38:26245}
246
[email protected]777183d02014-03-10 12:55:19247// CreateFunctionHandler installs a CallAsFunction handler on the given
248// object template that forwards to a provided C++ function or base::Callback.
249template<typename Sig>
250void CreateFunctionHandler(v8::Isolate* isolate,
251 v8::Local<v8::ObjectTemplate> tmpl,
252 const base::Callback<Sig> callback,
253 int callback_flags = 0) {
254 typedef internal::CallbackHolder<Sig> HolderT;
255 HolderT* holder = new HolderT(isolate, callback, callback_flags);
256 tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback,
deepak.sfaaa1b62015-04-30 07:30:48257 ConvertToV8<v8::Local<v8::External> >(
[email protected]777183d02014-03-10 12:55:19258 isolate, holder->GetHandle(isolate)));
259}
260
[email protected]314cde12013-11-23 20:26:51261} // namespace gin
[email protected]b520e132013-11-29 03:21:48262
263#endif // GIN_FUNCTION_TEMPLATE_H_