Message from discussion
Handle to Persistent for ClearWeak
Received: by 10.58.187.17 with SMTP id fo17mr6189519vec.37.1347370493499;
Tue, 11 Sep 2012 06:34:53 -0700 (PDT)
X-BeenThere: v8-users@googlegroups.com
Received: by 10.221.0.70 with SMTP id nl6ls701577vcb.5.gmail; Tue, 11 Sep 2012
06:34:52 -0700 (PDT)
Received: by 10.52.30.133 with SMTP id s5mr2919158vdh.10.1347370492140;
Tue, 11 Sep 2012 06:34:52 -0700 (PDT)
Date: Tue, 11 Sep 2012 06:34:51 -0700 (PDT)
From: Andrey <strikex...@gmail.com>
To: v8-users@googlegroups.com
Message-Id: <afc0f383-afc9-4970-8c4a-d4c945eeaa79@googlegroups.com>
Subject: Handle to Persistent for ClearWeak
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_1512_3475120.1347370491627"
------=_Part_1512_3475120.1347370491627
Content-Type: multipart/alternative;
boundary="----=_Part_1513_21631762.1347370491627"
------=_Part_1513_21631762.1347370491627
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
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?
------=_Part_1513_21631762.1347370491627
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<div>Hello, All.</div><div>Why in this code sample does not work IsWeak and=
ClearWeak?</div><div><br></div><div>void WeakCallback( v8::Persistent<v=
8::Value> object, void* parameter)</div><div>{</div><div> in=
t* obj =3D static_cast< int* >( parameter );</div><div> d=
elete obj;</div><div> object.ClearWeak();</div><div> &nbs=
p;object.Dispose();</div><div>}</div><div><br></div><div>v8::Handle<v8::=
Object> obj()</div><div>{</div><div> HandleScope hs;</div><d=
iv> v8::Local<v8::Object> obj =3D v8::Object::New();</div=
><div> </div><div> //I want that my object has a re=
ference to native pointer</div><div> v8::Local<v8::ObjectTem=
plate> native_tmpl =3D v8::ObjectTemplate::New();</div><div>  =
;native_tmpl->SetInternalFieldCount( 1 );</div><div> v8::Per=
sistent<v8::Object> native =3D v8::Persistent<v8::Object>::New(=
native_tmpl->NewInstance() );</div><div> int* ptr_i =3D new=
int( 1 );</div><div> native->SetPointerInInternalField( 0, =
ptr_i );</div><div> native.MakeWeak( ptr_i, WeakCallback );</di=
v><div> obj->Set( v8::String::New( "native" ), native );</di=
v><div> return hs.Close( obj );</div><div>}</div><div><br></div=
><div>//code</div><div>v8::Handle<v8::Value> native =3D obj()->Get=
( v8::String::New( "native" ) );</div><div>v8::Persistent<v8::Object>=
persistent =3D v8::Persistent<v8::Object>( native->ToObject() );<=
/div><div>bool b =3D persistent.IsWeak();//return false</div><div>persisten=
t.ClearWeak();//doesn't work, because WeakCallback has been called by gc</d=
iv><div><br></div><div>ClearWeak is necessary not to cause WeakCallback aft=
er std::move for native C++ pointer.</div><div>What am I doing wrong?</div>
------=_Part_1513_21631762.1347370491627--
------=_Part_1512_3475120.1347370491627--