Hi, I'm using a C++ lambda to represent the callback state for a Javascript 
callback and wanted to solicit some feedback on this approach.  I thought 
this would be an elegant way to set up the C++ binding.  Here is some 
example code from the Javascript side:

var i = new Thing();
i.callback = function (arg1, arg2) {
  // do stuff
};

i.callback will be called back whenever an interesting event happens to the 
"i" object in C++ world.  

Here is C++ binding code to capture the object *i*, the *function* on it, 
and the *context*.   (My application is single-threaded.)  Null tests etc. 
edited out.

void ThingCallbackSetter (Local <String> property, Local <Value> value, 
const PropertyCallbackInfo<void> &info)
{ Isolate* i = Isolate::GetCurrent();
  HandleScope scope (i);
  Handle<Object> o = info . This (); 
  Thing* thing = //unwrap the C++ object

  //  Make persistent versions of object, function, and context
  Persistent <v8::Object,   CopyablePersistentTraits<Object> >   obj (i, o);
  Persistent <v8::Function, CopyablePersistentTraits<Function> > fn (i, 
Handle<v8::Function>::Cast (value));
  Persistent <v8::Context,   CopyablePersistentTraits<Context> > ctxt (i, 
i->GetCurrentContext());

  thing -> AwaitCallback ([obj, fn, ctxt] (Type1 * a0, Type1 * a1, Type3 * 
a2)
    { Isolate* i = Isolate::GetCurrent();
      HandleScope _ (i);

      //  Make local versions of persistents
      Handle <Context> ct = Local <Context>::New (i, ctxt);
      Context::Scope context_scope (ct);
      Handle <Function> local_fn = Local <Function>::New (i, fn);
      Handle <Object> local_obj  = Local <Object>::New (i, obj);
      
      Handle <Value> argv[ 2 ] = {to_js (a1), to_js (a2)};
      local_fn -> Call (local_obj, 2, argv);
    }
  );    
}

This works, but I feel the code is more gnarly than I would like, and I 
might be making outright mistakes here.  Any ideas on how to streamline it?

thanks,
Brandon



-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
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 v8-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to