Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
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&lt;v=
8::Value&gt; object, void* parameter)</div><div>{</div><div>&nbsp; &nbsp;in=
t* obj =3D static_cast&lt; int* &gt;( parameter );</div><div>&nbsp; &nbsp;d=
elete obj;</div><div>&nbsp; &nbsp;object.ClearWeak();</div><div>&nbsp; &nbs=
p;object.Dispose();</div><div>}</div><div><br></div><div>v8::Handle&lt;v8::=
Object&gt; obj()</div><div>{</div><div>&nbsp; &nbsp;HandleScope hs;</div><d=
iv>&nbsp; &nbsp;v8::Local&lt;v8::Object&gt; obj =3D v8::Object::New();</div=
><div>&nbsp; &nbsp;</div><div>&nbsp; &nbsp;//I want that my object has a re=
ference to native pointer</div><div>&nbsp; &nbsp;v8::Local&lt;v8::ObjectTem=
plate&gt; native_tmpl =3D v8::ObjectTemplate::New();</div><div>&nbsp; &nbsp=
;native_tmpl-&gt;SetInternalFieldCount( 1 );</div><div>&nbsp; &nbsp;v8::Per=
sistent&lt;v8::Object&gt; native =3D v8::Persistent&lt;v8::Object&gt;::New(=
 native_tmpl-&gt;NewInstance() );</div><div>&nbsp; &nbsp;int* ptr_i =3D new=
 int( 1 );</div><div>&nbsp; &nbsp;native-&gt;SetPointerInInternalField( 0, =
ptr_i );</div><div>&nbsp; &nbsp;native.MakeWeak( ptr_i, WeakCallback );</di=
v><div>&nbsp; &nbsp;obj-&gt;Set( v8::String::New( "native" ), native );</di=
v><div>&nbsp; &nbsp;return hs.Close( obj );</div><div>}</div><div><br></div=
><div>//code</div><div>v8::Handle&lt;v8::Value&gt; native =3D obj()-&gt;Get=
( v8::String::New( "native" ) );</div><div>v8::Persistent&lt;v8::Object&gt;=
 persistent =3D v8::Persistent&lt;v8::Object&gt;( native-&gt;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--