1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
// This file was GENERATED by command:
// pump.py function_template.h.pump
// DO NOT EDIT BY HAND!!!
#ifndef GIN_FUNCTION_TEMPLATE_H_
#define GIN_FUNCTION_TEMPLATE_H_
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/callback.h"
#include "base/logging.h"
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/public/gin_embedders.h"
#include "gin/public/wrapper_info.h"
#include "gin/wrappable.h"
#include "v8/include/v8.h"
namespace gin {
class PerIsolateData;
namespace internal {
template<typename T>
struct RemoveConstRef {
typedef T Type;
};
template<typename T>
struct RemoveConstRef<const T&> {
typedef T Type;
};
// CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
// CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to
// DispatchToCallback, where it is invoked.
//
// v8::FunctionTemplate only supports passing void* as data so how do we know
// when to delete the base::Callback? That's where CallbackHolderBase comes in.
// It inherits from Wrappable, which delete itself when both (a) the refcount
// via base::RefCounted has dropped to zero, and (b) there are no more
// JavaScript references in V8.
class CallbackHolderBase : public Wrappable {
public:
virtual WrapperInfo* GetWrapperInfo() OVERRIDE;
static WrapperInfo kWrapperInfo;
protected:
virtual ~CallbackHolderBase() {}
};
template<typename Sig>
class CallbackHolder : public CallbackHolderBase {
public:
CallbackHolder(const base::Callback<Sig>& callback)
: callback(callback) {}
base::Callback<Sig> callback;
private:
virtual ~CallbackHolder() {}
};
// This set of templates invokes a base::Callback, converts the return type to a
// JavaScript value, and returns that value to script via the provided
// gin::Arguments object.
//
// In C++, you can declare the function foo(void), but you can't pass a void
// expression to foo. As a result, we must specialize the case of Callbacks that
// have the void return type.
template<typename R, typename P1 = void, typename P2 = void,
typename P3 = void, typename P4 = void>
struct Invoker {
inline static void Go(
Arguments* args,
const base::Callback<R(P1, P2, P3, P4)>& callback,
const P1& a1,
const P2& a2,
const P3& a3,
const P4& a4) {
args->Return(callback.Run(a1, a2, a3, a4));
}
};
template<typename P1, typename P2, typename P3, typename P4>
struct Invoker<void, P1, P2, P3, P4> {
inline static void Go(
Arguments* args,
const base::Callback<void(P1, P2, P3, P4)>& callback,
const P1& a1,
const P2& a2,
const P3& a3,
const P4& a4) {
callback.Run(a1, a2, a3, a4);
}
};
template<typename R, typename P1, typename P2, typename P3>
struct Invoker<R, P1, P2, P3, void> {
inline static void Go(
Arguments* args,
const base::Callback<R(P1, P2, P3)>& callback,
const P1& a1,
const P2& a2,
const P3& a3) {
args->Return(callback.Run(a1, a2, a3));
}
};
template<typename P1, typename P2, typename P3>
struct Invoker<void, P1, P2, P3, void> {
inline static void Go(
Arguments* args,
const base::Callback<void(P1, P2, P3)>& callback,
const P1& a1,
const P2& a2,
const P3& a3) {
callback.Run(a1, a2, a3);
}
};
template<typename R, typename P1, typename P2>
struct Invoker<R, P1, P2, void, void> {
inline static void Go(
Arguments* args,
const base::Callback<R(P1, P2)>& callback,
const P1& a1,
const P2& a2) {
args->Return(callback.Run(a1, a2));
}
};
template<typename P1, typename P2>
struct Invoker<void, P1, P2, void, void> {
inline static void Go(
Arguments* args,
const base::Callback<void(P1, P2)>& callback,
const P1& a1,
const P2& a2) {
callback.Run(a1, a2);
}
};
template<typename R, typename P1>
struct Invoker<R, P1, void, void, void> {
inline static void Go(
Arguments* args,
const base::Callback<R(P1)>& callback,
const P1& a1) {
args->Return(callback.Run(a1));
}
};
template<typename P1>
struct Invoker<void, P1, void, void, void> {
inline static void Go(
Arguments* args,
const base::Callback<void(P1)>& callback,
const P1& a1) {
callback.Run(a1);
}
};
template<typename R>
struct Invoker<R, void, void, void, void> {
inline static void Go(
Arguments* args,
const base::Callback<R()>& callback) {
args->Return(callback.Run());
}
};
template<>
struct Invoker<void, void, void, void, void> {
inline static void Go(
Arguments* args,
const base::Callback<void()>& callback) {
callback.Run();
}
};
// DispatchToCallback converts all the JavaScript arguments to C++ types and
// invokes the base::Callback.
template<typename R>
static void DispatchToCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Arguments args(info);
CallbackHolderBase* holder_base = NULL;
CHECK(args.GetData(&holder_base));
typedef CallbackHolder<R()> HolderT;
HolderT* holder = static_cast<HolderT*>(holder_base);
Invoker<R>::Go(&args, holder->callback);
}
template<typename R, typename P1>
static void DispatchToCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Arguments args(info);
CallbackHolderBase* holder_base = NULL;
CHECK(args.GetData(&holder_base));
typedef CallbackHolder<R(P1)> HolderT;
HolderT* holder = static_cast<HolderT*>(holder_base);
typename RemoveConstRef<P1>::Type a1;
if (!args.GetNext(&a1)) {
args.ThrowError();
return;
}
Invoker<R, P1>::Go(&args, holder->callback, a1);
}
template<typename R, typename P1, typename P2>
static void DispatchToCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Arguments args(info);
CallbackHolderBase* holder_base = NULL;
CHECK(args.GetData(&holder_base));
typedef CallbackHolder<R(P1, P2)> HolderT;
HolderT* holder = static_cast<HolderT*>(holder_base);
typename RemoveConstRef<P1>::Type a1;
typename RemoveConstRef<P2>::Type a2;
if (!args.GetNext(&a1) ||
!args.GetNext(&a2)) {
args.ThrowError();
return;
}
Invoker<R, P1, P2>::Go(&args, holder->callback, a1, a2);
}
template<typename R, typename P1, typename P2, typename P3>
static void DispatchToCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Arguments args(info);
CallbackHolderBase* holder_base = NULL;
CHECK(args.GetData(&holder_base));
typedef CallbackHolder<R(P1, P2, P3)> HolderT;
HolderT* holder = static_cast<HolderT*>(holder_base);
typename RemoveConstRef<P1>::Type a1;
typename RemoveConstRef<P2>::Type a2;
typename RemoveConstRef<P3>::Type a3;
if (!args.GetNext(&a1) ||
!args.GetNext(&a2) ||
!args.GetNext(&a3)) {
args.ThrowError();
return;
}
Invoker<R, P1, P2, P3>::Go(&args, holder->callback, a1, a2, a3);
}
template<typename R, typename P1, typename P2, typename P3, typename P4>
static void DispatchToCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Arguments args(info);
CallbackHolderBase* holder_base = NULL;
CHECK(args.GetData(&holder_base));
typedef CallbackHolder<R(P1, P2, P3, P4)> HolderT;
HolderT* holder = static_cast<HolderT*>(holder_base);
typename RemoveConstRef<P1>::Type a1;
typename RemoveConstRef<P2>::Type a2;
typename RemoveConstRef<P3>::Type a3;
typename RemoveConstRef<P4>::Type a4;
if (!args.GetNext(&a1) ||
!args.GetNext(&a2) ||
!args.GetNext(&a3) ||
!args.GetNext(&a4)) {
args.ThrowError();
return;
}
Invoker<R, P1, P2, P3, P4>::Go(&args, holder->callback, a1, a2, a3, a4);
}
} // namespace internal
// This should be called once per-isolate to initialize the function template
// system.
void InitFunctionTemplates(PerIsolateData* isolate_data);
// This has to be outside the internal namespace because template
// specializations must be declared in the same namespace as the original
// template.
template<>
struct Converter<internal::CallbackHolderBase*>
: public WrappableConverter<internal::CallbackHolderBase> {};
// Creates a v8::FunctionTemplate that will run the provided base::Callback each
// time it is called. JavaScript arguments and return values are converted via
// gin::Converter.
template<typename R>
v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
v8::Isolate* isolate,
const base::Callback<R()> callback) {
typedef internal::CallbackHolder<R()> HolderT;
scoped_refptr<HolderT> holder(new HolderT(callback));
return v8::FunctionTemplate::New(
&internal::DispatchToCallback<R>,
ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
}
template<typename R, typename P1>
v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
v8::Isolate* isolate,
const base::Callback<R(P1)> callback) {
typedef internal::CallbackHolder<R(P1)> HolderT;
scoped_refptr<HolderT> holder(new HolderT(callback));
return v8::FunctionTemplate::New(
&internal::DispatchToCallback<R, P1>,
ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
}
template<typename R, typename P1, typename P2>
v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
v8::Isolate* isolate,
const base::Callback<R(P1, P2)> callback) {
typedef internal::CallbackHolder<R(P1, P2)> HolderT;
scoped_refptr<HolderT> holder(new HolderT(callback));
return v8::FunctionTemplate::New(
&internal::DispatchToCallback<R, P1, P2>,
ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
}
template<typename R, typename P1, typename P2, typename P3>
v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
v8::Isolate* isolate,
const base::Callback<R(P1, P2, P3)> callback) {
typedef internal::CallbackHolder<R(P1, P2, P3)> HolderT;
scoped_refptr<HolderT> holder(new HolderT(callback));
return v8::FunctionTemplate::New(
&internal::DispatchToCallback<R, P1, P2, P3>,
ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
}
template<typename R, typename P1, typename P2, typename P3, typename P4>
v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
v8::Isolate* isolate,
const base::Callback<R(P1, P2, P3, P4)> callback) {
typedef internal::CallbackHolder<R(P1, P2, P3, P4)> HolderT;
scoped_refptr<HolderT> holder(new HolderT(callback));
return v8::FunctionTemplate::New(
&internal::DispatchToCallback<R, P1, P2, P3, P4>,
ConvertToV8<internal::CallbackHolderBase*>(isolate, holder.get()));
}
} // namespace gin
#endif // GIN_FUNCTION_TEMPLATE_H_
|