diff options
Diffstat (limited to 'gin/function_template.h')
-rw-r--r-- | gin/function_template.h | 120 |
1 files changed, 113 insertions, 7 deletions
diff --git a/gin/function_template.h b/gin/function_template.h index 9c5b154..7cf84f8 100644 --- a/gin/function_template.h +++ b/gin/function_template.h @@ -1,3 +1,9 @@ +// This file was GENERATED by command: +// pump.py function_template.h.pump +// DO NOT EDIT BY HAND!!! + + + // 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. @@ -16,6 +22,8 @@ namespace gin { class PerIsolateData; +namespace internal { + template<typename T> struct RemoveConstRef { typedef T Type; @@ -25,6 +33,18 @@ struct RemoveConstRef<const T&> { typedef T Type; }; +} // namespace internal + + +// CallbackHolder and CallbackHolderBase are used to pass a base::Callback from +// CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to +// DispatchCallback, 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: static void EnsureRegistered(PerIsolateData* isolate_data); @@ -48,9 +68,61 @@ class CallbackHolder : public CallbackHolderBase { virtual ~CallbackHolder() {} }; -// TODO(aa): Generate the overloads below with pump. +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<void()> HolderT; + HolderT* holder = static_cast<HolderT*>(holder_base); -template<typename P1, typename P2> + holder->callback.Run(); +} + +template<typename R> +v8::Local<v8::FunctionTemplate> CreateFunctionTempate( + v8::Isolate* isolate, + const base::Callback<R()> callback) { + typedef CallbackHolder<void()> HolderT; + scoped_refptr<HolderT> holder(new HolderT(callback)); + return v8::FunctionTemplate::New( + &DispatchToCallback<R>, + ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); +} + +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<void(P1)> HolderT; + HolderT* holder = static_cast<HolderT*>(holder_base); + + typename internal::RemoveConstRef<P1>::Type a1; + if (!args.GetNext(&a1)) { + args.ThrowError(); + return; + } + + holder->callback.Run(a1); +} + +template<typename R, typename P1> +v8::Local<v8::FunctionTemplate> CreateFunctionTempate( + v8::Isolate* isolate, + const base::Callback<R(P1)> callback) { + typedef CallbackHolder<void(P1)> HolderT; + scoped_refptr<HolderT> holder(new HolderT(callback)); + return v8::FunctionTemplate::New( + &DispatchToCallback<R, P1>, + ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); +} + +template<typename R, typename P1, typename P2> static void DispatchToCallback( const v8::FunctionCallbackInfo<v8::Value>& info) { Arguments args(info); @@ -60,8 +132,8 @@ static void DispatchToCallback( typedef CallbackHolder<void(P1, P2)> HolderT; HolderT* holder = static_cast<HolderT*>(holder_base); - typename RemoveConstRef<P1>::Type a1; - typename RemoveConstRef<P2>::Type a2; + typename internal::RemoveConstRef<P1>::Type a1; + typename internal::RemoveConstRef<P2>::Type a2; if (!args.GetNext(&a1) || !args.GetNext(&a2)) { args.ThrowError(); @@ -71,14 +143,48 @@ static void DispatchToCallback( holder->callback.Run(a1, a2); } -template<typename P1, typename P2> +template<typename R, typename P1, typename P2> v8::Local<v8::FunctionTemplate> CreateFunctionTempate( v8::Isolate* isolate, - const base::Callback<void(P1, P2)>& callback) { + const base::Callback<R(P1, P2)> callback) { typedef CallbackHolder<void(P1, P2)> HolderT; scoped_refptr<HolderT> holder(new HolderT(callback)); return v8::FunctionTemplate::New( - &DispatchToCallback<P1, P2>, + &DispatchToCallback<R, P1, P2>, + ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); +} + +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<void(P1, P2, P3)> HolderT; + HolderT* holder = static_cast<HolderT*>(holder_base); + + typename internal::RemoveConstRef<P1>::Type a1; + typename internal::RemoveConstRef<P2>::Type a2; + typename internal::RemoveConstRef<P3>::Type a3; + if (!args.GetNext(&a1) || + !args.GetNext(&a2) || + !args.GetNext(&a3)) { + args.ThrowError(); + return; + } + + holder->callback.Run(a1, a2, a3); +} + +template<typename R, typename P1, typename P2, typename P3> +v8::Local<v8::FunctionTemplate> CreateFunctionTempate( + v8::Isolate* isolate, + const base::Callback<R(P1, P2, P3)> callback) { + typedef CallbackHolder<void(P1, P2, P3)> HolderT; + scoped_refptr<HolderT> holder(new HolderT(callback)); + return v8::FunctionTemplate::New( + &DispatchToCallback<R, P1, P2, P3>, ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); } |