Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: gin/function_template_util.h

Issue 76923003: First cut at gin::Bind() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: k Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/arguments.h ('k') | gin/function_template_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_forward.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"
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 {
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) {
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;
64 if (!args.GetNext(&a1) ||
65 !args.GetNext(&a2)) {
66 return args.ThrowError();
jochen (gone - plz use gerrit) 2013/11/22 08:24:55 the function is void
67 }
68
69 holder->callback.Run(a1, a2);
70 }
71
72 template<typename P1, typename P2>
73 v8::Local<v8::FunctionTemplate> CreateFunctionTempate(
74 v8::Isolate* isolate,
75 const base::Callback<void(P1, P2)>& callback) {
76 typedef CallbackHolder<void(P1, P2)> HolderT;
77 scoped_refptr<HolderT> holder(new HolderT());
78 holder->callback = callback;
jochen (gone - plz use gerrit) 2013/11/22 08:24:55 where is holder deleted again?
Aaron Boodman 2013/11/22 08:28:00 The Wrappable base class deletes it during GC. (Wr
jochen (gone - plz use gerrit) 2013/11/22 08:29:09 ah, makes sense
79 return v8::FunctionTemplate::New(
80 &DispatchV8Call<P1, P2>,
81 ConvertToV8<CallbackHolderBase*>(isolate, holder.get()));
82 }
83
84 } // namespace gin
OLDNEW
« no previous file with comments | « gin/arguments.h ('k') | gin/function_template_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698