Very pseudocode-y:
class Foo {};
function f(Foo * f){}
static void callback(const v8::FunctionCallbackInfo<v8::Value>& args) {
void * internal_field = args[0].GetInternalField(0); // this skips a
bunch of casts and such
}
// create the function called "foo"
ft = FunctionTemplate( callback);
context->Global->Set("foo", ft);
// Create an object backed by a C++ Foo object and call it "o"
o = v8::Object::New();
o.SetInternalField(new Foo());
context->Global->Set("o", o);
then in javascript:
foo(o);
On Thursday, February 4, 2016 at 9:41:30 PM UTC-8, Louis P. Santillan wrote:
>
> First, is the Function callback calling a C++ native function, a JS
> function or another C++ defined Function callback? Providing some
> code might make the issue more obvious.
>
> Second, v8.h has all the JS ("container") types defined. Start there
> when defining variables. You can often use the v8 type when going
> between JS-land and C++-land. Things like:
> ```
> v8::Array arr = Array::New( isolate, len );
> v8::Integer i = Integer::New( isolate, ivalue );
> ```
>
> If you're converting the callback's parameters to a native type:
> ```
> String::Utf8Value str( args[ 0 ] );
> char* ascii = *str; // * is overloaded to return an ASCII C string
> int n = ( int )( args[ 1 ]->Int32Value() );
> ```
>
> Once you have the parameter in the v8 type or C++ type, you can pass
> them appropriately.
>
> If you're hard up for the wrapper approach, then Stephen Beal had an
> extensive template based solution that is probably 18-24 months
> out-of-date (read: extensively out of date) and then there's node.js'
> wrapper object approach.
>
> -L
>
> On Thu, Feb 4, 2016 at 9:09 PM, Zac Hansen <[email protected] <javascript:>>
> wrote:
> > I'm in a FunctionTemplate callback and about to call a function that
> expects
> > parameters of a certain type and I want to make sure that the variable
> > backing the object is of the right type. I know the type the function
> wants
> > (T), but you can't call dynamic_cast<T>(my_void_ptr).
> >
> > Unless I'm missing something (which is entirely possible), it seems like
> it
> > would be nice if v8::External were templated to take a pointer type.
> So I
> > could say v8::External<T>::New(my_t_ptr) and then have
> > my_local_external->Value() return a T* instead of a void *. I'm not a
> > template master, but I think you could make void * the default to not
> have
> > any behavior change in the default case.
> >
> > I think a workaround for this is to make a templated wrapping class that
> > inherits from a non-templated base class.. so I can say
> > dynamic_cast<WrapperClass<T>>((WrapperBaseClass*)void_from_external) ==
> > nullptr and find out that way, but it doesn't seem like that workaround
> > should be necessary.
> >
> > Anyone have a better way to do this (like something already in V8 I
> don't
> > know about) or any thoughts on if v8::External could be made templated?
> > I'd be willing to write the patch, as it seems pretty trivial.
> >
> >
> > Thank you!
> >
> > --Zac
> >
> > --
> > --
> > v8-users mailing list
> > [email protected] <javascript:>
> > http://groups.google.com/group/v8-users
> > ---
> > You received this message because you are subscribed to the Google
> Groups
> > "v8-users" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to [email protected] <javascript:>.
> > For more options, visit https://groups.google.com/d/optout.
>
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.