Index: gin/function_template_util.h |
diff --git a/gin/function_template_util.h b/gin/function_template_util.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0052e8eca4f8d0d1988d083127f8a19e290a4d3c |
--- /dev/null |
+++ b/gin/function_template_util.h |
@@ -0,0 +1,84 @@ |
+// 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_forward.h" |
+#include "base/logging.h" |
+#include "gin/arguments.h" |
+#include "gin/converter.h" |
+#include "gin/per_isolate_data.h" |
+#include "gin/public/gin_embedders.h" |
+#include "gin/public/wrapper_info.h" |
+#include "gin/wrappable.h" |
+ |
+#include "v8/include/v8.h" |
+ |
+namespace gin { |
+ |
+template<typename T> |
+struct RemoveConstRef { |
+ typedef T Type; |
+}; |
+template<typename T> |
+struct RemoveConstRef<const T&> { |
+ typedef T Type; |
+}; |
+ |
+class CallbackHolderBase : public Wrappable { |
+ public: |
+ static void Register(v8::Isolate* isolate); |
+ virtual WrapperInfo* GetWrapperInfo() OVERRIDE; |
+ static WrapperInfo kWrapperInfo; |
+ protected: |
+ virtual ~CallbackHolderBase() {} |
+}; |
+ |
+template<> |
+struct Converter<CallbackHolderBase*> |
+ : public WrappableConverter<CallbackHolderBase> {}; |
+ |
+template<typename Sig> |
+class CallbackHolder : public CallbackHolderBase { |
+ public: |
+ CallbackHolder() {} |
+ base::Callback<Sig> callback; |
+ private: |
+ virtual ~CallbackHolder() {} |
+}; |
+ |
+// TODO(aa): Generate the overloads below with pump. |
+ |
+template<typename P1, typename P2> |
+static void DispatchV8Call(const v8::FunctionCallbackInfo<v8::Value>& info) { |
+ Arguments args(info); |
+ CallbackHolderBase* holder_base = NULL; |
+ CHECK(args.GetData(&holder_base)); |
+ |
+ typedef CallbackHolder<void(P1, P2)> HolderT; |
+ HolderT* holder = static_cast<HolderT*>(holder_base); |
+ |
+ typedef typename RemoveConstRef<P1>::Type P1Storage; |
+ typedef typename RemoveConstRef<P2>::Type P2Storage; |
+ P1Storage a1; |
+ P2Storage a2; |
+ if (!args.GetNext(&a1) || |
+ !args.GetNext(&a2)) { |
+ return args.ThrowError(); |
jochen (gone - plz use gerrit)
2013/11/22 08:24:55
the function is void
|
+ } |
+ |
+ holder->callback.Run(a1, a2); |
+} |
+ |
+template<typename P1, typename P2> |
+v8::Local<v8::FunctionTemplate> CreateFunctionTempate( |
+ v8::Isolate* isolate, |
+ const base::Callback<void(P1, P2)>& callback) { |
+ typedef CallbackHolder<void(P1, P2)> HolderT; |
+ scoped_refptr<HolderT> holder(new HolderT()); |
+ 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
|
+ return v8::FunctionTemplate::New( |
+ &DispatchV8Call<P1, P2>, |
+ ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); |
+} |
+ |
+} // namespace gin |