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..1e8b120cd96d87e469ce48bcab950bff1683c2ed |
--- /dev/null |
+++ b/gin/function_template_util.h |
@@ -0,0 +1,85 @@ |
+// 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/per_isolate_data.h" |
+#include "gin/public/gin_embedders.h" |
+#include "gin/public/wrapper_info.h" |
+#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
|
+ |
+#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 { |
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
|
+ 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) { |
abarth-chromium
2013/11/22 09:17:13
DispatchV8Call -> DispatchToCallback ?
Aaron Boodman
2013/11/22 18:01:01
Done.
|
+ 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; |
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
|
+ if (!args.GetNext(&a1) || |
+ !args.GetNext(&a2)) { |
+ args.ThrowError(); |
+ return; |
+ } |
+ |
+ 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; |
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.
|
+ return v8::FunctionTemplate::New( |
+ &DispatchV8Call<P1, P2>, |
+ ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); |
+} |
+ |
+} // namespace gin |