Handle to Persistent for ClearWeak

88 views
Skip to first unread message

Andrey

unread,
Sep 11, 2012, 9:34:51 AM9/11/12
to v8-u...@googlegroups.com
Hello, All.
Why in this code sample does not work IsWeak and ClearWeak?

void WeakCallback( v8::Persistent<v8::Value> object, void* parameter)
{
   int* obj = static_cast< int* >( parameter );
   delete obj;
   object.ClearWeak();
   object.Dispose();
}

v8::Handle<v8::Object> obj()
{
   HandleScope hs;
   v8::Local<v8::Object> obj = v8::Object::New();
   
   //I want that my object has a reference to native pointer
   v8::Local<v8::ObjectTemplate> native_tmpl = v8::ObjectTemplate::New();
   native_tmpl->SetInternalFieldCount( 1 );
   v8::Persistent<v8::Object> native = v8::Persistent<v8::Object>::New( native_tmpl->NewInstance() );
   int* ptr_i = new int( 1 );
   native->SetPointerInInternalField( 0, ptr_i );
   native.MakeWeak( ptr_i, WeakCallback );
   obj->Set( v8::String::New( "native" ), native );
   return hs.Close( obj );
}

//code
v8::Handle<v8::Value> native = obj()->Get( v8::String::New( "native" ) );
v8::Persistent<v8::Object> persistent = v8::Persistent<v8::Object>( native->ToObject() );
bool b = persistent.IsWeak();//return false
persistent.ClearWeak();//doesn't work, because WeakCallback has been called by gc

ClearWeak is necessary not to cause WeakCallback after std::move for native C++ pointer.
What am I doing wrong?
Reply all
Reply to author
Forward
0 new messages