That seems to work better (no SetInternalField error):

            Handle<Object> proto = Handle<Object>::Cast(fn-
>Get(String::New("prototype")));
            proto->SetInternalField(0, External::New(data));

I'm still not sure about how to access the function and it's prototype
through the args object in the callback.

The problem with the Set callback data is that it requires the data to
be available a little earlier than I wanted.  It seems like I should
be able to use internal data fields for functions in the same way that
I do for objects.

erik


On Mar 6, 10:29 pm, Matthias Ernst <[email protected]> wrote:
> On Wed, Mar 7, 2012 at 3:16 AM, cruisercoder <[email protected]> wrote:
> > How do I get internal data for function callbacks?   The approach I'm
> > currently taking is to use the function's prototype object where I can
> > declare an internal field and then set it at context time.   However,
> > I get an error about the internal field count when I retrieve the
> > prototype and try to set data (code and error shown below).
>
> > I'm also not sure about how to unpack the data in the handler.
>
> > erik
>
> > v8::Handle<v8::Value> myHandler(const v8::Arguments& args) {
> >    nd::unused(args);
> >    using namespace v8;
> >    return Undefined();
> > }
>
> > using namespace v8;
> > HandleScope handle_scope;
>
> > v8::Persistent<v8::ObjectTemplate> global_template =
> > Persistent<ObjectTemplate>::New(ObjectTemplate::New());
>
> > Handle<FunctionTemplate> tmpl = FunctionTemplate::New(myHandler);
>
> > Handle<ObjectTemplate> proto_t = tmpl->PrototypeTemplate();
> > proto_t->SetInternalFieldCount(1);
>
> > global_template->Set(String::New("myFunction"),tmpl);
>
> > Persistent<Context> context = Context::New(0, global_template);
>
> > Context::Scope context_scope(context);
>
> > Handle<Function> fn = tmpl->GetFunction();
> > Handle<Object> proto = Handle<Object>::Cast(fn->GetPrototype());
>
> GetPrototype() is a method of Object and returns object's __proto__
> (which I guess is "Function.prototype" in this case). You want
> fn.Get(String::New("prototype")).
>
> Not quite sure what you're trying to achieve, but you can also
> register callback data in FunctionTemplate::New (the "data" parameter)
> which will be passed into your callback as Arguments::Data().
>
> Matthias
>
>
>
>
>
>
>
>
>
> > void *data(0);
> > proto->SetInternalField(0, External::New(data));
>
> > #
> > # Fatal error in v8::Object::SetInternalField()
> > # Writing internal field out of bounds
> > #
>
> > --
> > v8-users mailing list
> > [email protected]
> >http://groups.google.com/group/v8-users

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

Reply via email to