Here's the code I use for this that seems to work (though suggestions 
welcome):

template<class CALLBACK_FUNCTION>
void global_set_weak(v8::Isolate * isolate, const v8::Local<v8::Object> & 
javascript_object, CALLBACK_FUNCTION function)
{
    struct SetWeakCallbackData{
        SetWeakCallbackData(CALLBACK_FUNCTION function, v8::Isolate * isolate, 
const v8::Local<v8::Object> & javascript_object) :
            function(function) {
                this->global.Reset(isolate, javascript_object);
        }
        CALLBACK_FUNCTION function;
        v8::Global<v8::Object> global;
    };

    auto callback_data = new SetWeakCallbackData(function, isolate, 
javascript_object);

    // set the callback on the javascript_object to be called when it's garbage 
collected
    callback_data->global.template SetWeak<SetWeakCallbackData>(callback_data,
        [](const v8::WeakCallbackInfo<SetWeakCallbackData> & data) {
            SetWeakCallbackData * callback_data = data.GetParameter();
            callback_data->function();
            callback_data->global.Reset();
            delete callback_data;
        }, v8::WeakCallbackType::kParameter);
}


On Sunday, October 2, 2016 at 12:58:35 AM UTC-7, Ben Noordhuis wrote:
>
> On Sun, Oct 2, 2016 at 1:54 AM, <[email protected] <javascript:>> wrote: 
> > In this example, the Persistent is allocated on the stack. If I want the 
> callback to keep working after the Persistent goes out of scope, would I 
> have to allocate it on the heap? Would I then have to delete the Persistent 
> in the callback? 
>
> Yes to both questions. 
>

-- 
-- 
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.

Reply via email to