OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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. | |
4 | |
5 #include "base/callback.h" | |
6 #include "base/logging.h" | |
7 #include "gin/arguments.h" | |
8 #include "gin/converter.h" | |
9 #include "gin/per_isolate_data.h" | |
10 #include "gin/public/gin_embedders.h" | |
11 #include "gin/public/wrapper_info.h" | |
12 #include "gin/wrappable.h" | |
abarth-chromium
2013/11/22 09:17:13
You've got a bunch more headers than you need here
Aaron Boodman
2013/11/22 18:01:01
It's Google (and Chrome) style: https://code.googl
| |
13 | |
14 #include "v8/include/v8.h" | |
15 | |
16 namespace gin { | |
17 | |
18 template<typename T> | |
19 struct RemoveConstRef { | |
20 typedef T Type; | |
21 }; | |
22 template<typename T> | |
23 struct RemoveConstRef<const T&> { | |
24 typedef T Type; | |
25 }; | |
26 | |
27 class CallbackHolderBase : public Wrappable { | |
28 public: | |
29 static void Register(v8::Isolate* isolate); | |
30 virtual WrapperInfo* GetWrapperInfo() OVERRIDE; | |
31 static WrapperInfo kWrapperInfo; | |
32 protected: | |
33 virtual ~CallbackHolderBase() {} | |
34 }; | |
35 | |
36 template<> | |
37 struct Converter<CallbackHolderBase*> | |
38 : public WrappableConverter<CallbackHolderBase> {}; | |
39 | |
40 template<typename Sig> | |
41 class CallbackHolder : public CallbackHolderBase { | |
abarth-chromium
2013/11/22 09:17:13
Should we move CallbackHolder and CallbackHolderBa
Aaron Boodman
2013/11/22 18:01:01
C++ template esoterica #23: template specializatio
| |
42 public: | |
43 CallbackHolder() {} | |
44 base::Callback<Sig> callback; | |
45 private: | |
46 virtual ~CallbackHolder() {} | |
47 }; | |
48 | |
49 // TODO(aa): Generate the overloads below with pump. | |
50 | |
51 template<typename P1, typename P2> | |
52 static void DispatchV8Call(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
abarth-chromium
2013/11/22 09:17:13
DispatchV8Call -> DispatchToCallback ?
Aaron Boodman
2013/11/22 18:01:01
Done.
| |
53 Arguments args(info); | |
54 CallbackHolderBase* holder_base = NULL; | |
55 CHECK(args.GetData(&holder_base)); | |
56 | |
57 typedef CallbackHolder<void(P1, P2)> HolderT; | |
58 HolderT* holder = static_cast<HolderT*>(holder_base); | |
59 | |
60 typedef typename RemoveConstRef<P1>::Type P1Storage; | |
61 typedef typename RemoveConstRef<P2>::Type P2Storage; | |
62 P1Storage a1; | |
63 P2Storage a2; | |
abarth-chromium
2013/11/22 09:17:13
You can just write:
typename RemoveConstRef<P1>::
Aaron Boodman
2013/11/22 18:01:01
Fair enough. I think I was using these types in mo
| |
64 if (!args.GetNext(&a1) || | |
65 !args.GetNext(&a2)) { | |
66 args.ThrowError(); | |
67 return; | |
68 } | |
69 | |
70 holder->callback.Run(a1, a2); | |
71 } | |
72 | |
73 template<typename P1, typename P2> | |
74 v8::Local<v8::FunctionTemplate> CreateFunctionTempate( | |
75 v8::Isolate* isolate, | |
76 const base::Callback<void(P1, P2)>& callback) { | |
77 typedef CallbackHolder<void(P1, P2)> HolderT; | |
78 scoped_refptr<HolderT> holder(new HolderT()); | |
79 holder->callback = callback; | |
abarth-chromium
2013/11/22 09:17:13
It might be slightly prettier to pass the callback
Aaron Boodman
2013/11/22 18:01:01
Done.
| |
80 return v8::FunctionTemplate::New( | |
81 &DispatchV8Call<P1, P2>, | |
82 ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); | |
83 } | |
84 | |
85 } // namespace gin | |
OLD | NEW |