On Oct 22, 2:13 am, Stephan Beal <[email protected]> wrote:
> http://code.google.com/p/v8-juice/wiki/BindingFunctions

Hello, *.*!

i've expanded the above-mentioned functionality to allow us to bind
near-arbitrary functions as _overloads_ of JS functions. We can now,
using a simple templates mechanism, create a single
v8::InvocationCallback function which will dispatch to a different
native function depending on the argument count (it's also possible to
add a wildcard case which is called if no others match). Note that it
is impossible, without a large amount of template infrastructure, to
automatically dispatch based on the JS-based types of the arguments.

Assume we have:

bool BoundNative::overload();
int BoundNative::overload(int i);
int BoundNative_overload( int i );
double BoundNative_overload( int i, double d ); // non-member!
v8::Handle<v8::Value> BoundNative_overload( v8::Arguments const &
argv );

We can now bound various versions of those by generating a
v8::InvocationCallback:

v8::InvocationCallback IC =
 convert::OverloadInvocables< convert::TypeList<
 
convert::InvocableMemFunc0<BoundNative,bool,&BoundNative::overload>,
      convert::InvocableFunction1<int,int,BoundNative_overload>,
 
convert::InvocableFunction2<double,int,double,BoundNative_overload>,
      convert::InvocableCallback<-1, BoundNative_overload> // any
other argument count
    > >::Invocable

myObject->Set( String::New("overloaded"),
               FunctionTemplate::New( IC )->GetFunction() );


And in JS:

myobj.overload(); // calls: bool BoundNative::overload()
myobj.overload(13); // calls: int BoundNative_overload(int)
myobj.overload(13, 17.7); // calls: double BoundNative_overload
(int,double)
myobj.overload(1,2,3,4); // calls: Handle<Value> BoundNative_overload
(Arguments)


The code to support this is in the v8-juice source tree (see above
link) but is header-only and independent of the core library (well, in
some ways these bits _are_ the core library). Thus it's easy to
extract and use in arbitrary v8 client code (contact me if you'd like
to do this and i'll describe what you need). (We've got similar
support for binding overloaded C++ constructors, but that requires
significantly more code from the v8-juice tree to support.)


Happy hacking!

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to