On Tue, Oct 9, 2018 at 4:40 PM Mike Moening <mike.moen...@gmail.com> wrote:

> The example was helpful.   Encapsulating the Persistent in the callacks
> private data object seems to be the way to roll.
>
>
>> // kParameter will pass a void* parameter back to the callback,
>> kInternalFields
>> // will pass the first two internal fields back to the callback,
>> kFinalizer
>> // will pass a void* parameter back, but is invoked before the object is
>> // actually collected, so it can be resurrected. In the last case, it is
>> not
>> // possible to request a second pass callback.
>> enum class WeakCallbackType { kParameter, kInternalFields, kFinalizer };
>
>
> Does that help?
>
> Sorry, but no not really.
> Resurrection?  Sounds like a zombie movie (I probably don't need this)
> *kInternalFields - will pass the first two internal fields back to the
> callback*
>

This would be useful in a Node world, there is a utiility class ObjectWrap
which uses 1 internal field to store the user's class reference (the C++
object to be wrapped in a V8/JS reference).  It could use kInternalFields
type instead of duplicating the value to be passed as a parameter.  (Just a
possible application of that)


>
> How? Why?
>
> There has to be something better that explains the callbacks better.
>

I did this originally to track array buffer content, so I can release the
backing buffer when the typed array evaporates (is garbage collected).
I generalized to object mostly later, but then re-added 'strings' for short
lived strings associated with address/connection objects.

---
struct arrayBufferHolder  {
void *buffer; // resources associated with object
Persistent<Object> o;
/*
    // other types that this might hold.... add deallocation handlers in
callback
Persistent<String> s;
Persistent<ArrayBuffer> ab;
*/
};
typedef struct arrayBufferHolder ARRAY_BUFFER_HOLDER, *PARRAY_BUFFER_HOLDER;


/* GetHolder() returns a holder from a pool; or allocates a new one... it
reuses previous holders*/



                                        /* ... buf and len are the backing
buffer and length of that buffer */
Local<Object> arrayBuffer = ArrayBuffer::New( isolate, buf, len );
PARRAY_BUFFER_HOLDER holder = GetHolder();
holder->o.Reset( isolate, arrayBuffer );
holder->o.SetWeak<ARRAY_BUFFER_HOLDER>( holder, releaseBuffer,
WeakCallbackType::kParameter );
holder->buffer = buf;



/* The callback to release associated resources when `arrayBuffer` is
collcted */

void releaseBuffer( const WeakCallbackInfo<ARRAY_BUFFER_HOLDER> &info ) {
PARRAY_BUFFER_HOLDER holder = info.GetParameter();

if( !holder->o.IsEmpty() ) { // in case this is one of 3 types to release.
holder->o.ClearWeak();
holder->o.Reset();
}
Deallocate( void*, holder->buffer ); // your resource deallocation here
DropHolder( holder );  // returns holder to pool for later use. (delete
holder;?)
}

---




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

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