@@ -59,11 +59,9 @@ static uint32_t count(const Php::Object &object)
5959 */  
6060static  void  callback (const  v8::FunctionCallbackInfo<v8::Value> &info)
6161{
62-     //  create a local handle, so properties "fall out of scope"
62+     //  create a local handle, so properties "fall out of scope" and retrieve a handle to the original object 
6363    v8::HandleScope     scope (isolate ());
64- 
65-     //  retrieve handle to the original object
66-     Handle<Php::Value>  handle (info.Data ());
64+     Handle<Php::Object> handle (info.Holder ()->GetInternalField (0 ));
6765
6866    //  an array to hold all the arguments
6967    Php::Array arguments;
@@ -96,7 +94,7 @@ static void indexed_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info)
9694{
9795    //  create a local handle, so properties "fall out of scope" and retrieve the original object
9896    v8::HandleScope         scope (isolate ());
99-     Handle<Php::Object>     handle (info.Data ( ));
97+     Handle<Php::Object>     handle (info.Holder ()-> GetInternalField ( 0 ));
10098
10199    //  create a new array to store all the properties
102100    v8::Local<v8::Array>    properties (v8::Array::New (isolate ()));
@@ -128,7 +126,7 @@ static void named_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info)
128126{
129127    //  create a local handle, so properties "fall out of scope" and retrieve the original object
130128    v8::HandleScope         scope (isolate ());
131-     Handle<Php::Object>     handle (info.Data ( ));
129+     Handle<Php::Object>     handle (info.Holder ()-> GetInternalField ( 0 ));
132130
133131    //  create a new array to store all the properties
134132    v8::Local<v8::Array>    properties (v8::Array::New (isolate ()));
@@ -161,7 +159,7 @@ static void getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &in
161159{
162160    //  create a local handle, so properties "fall out of scope" and retrieve the original object
163161    v8::HandleScope         scope (isolate ());
164-     Handle<Php::Object>     handle (info.Data ( ));
162+     Handle<Php::Object>     handle (info.Holder ()-> GetInternalField ( 0 ));
165163
166164    //  check if we have an item at the requested offset
167165    if  (handle->call (" offsetExists" static_cast <int64_t >(index)))
@@ -188,7 +186,7 @@ static void getter(v8::Local<v8::String> property, const v8::PropertyCallbackInf
188186    v8::HandleScope         scope (isolate ());
189187
190188    //  retrieve handle to the original object and the property name
191-     Handle<Php::Object>     handle (info.Data ( ));
189+     Handle<Php::Object>     handle (info.Holder ()-> GetInternalField ( 0 ));
192190    v8::String::Utf8Value   name (property);
193191
194192    /* *
@@ -261,8 +259,9 @@ static void getter(v8::Local<v8::String> property, const v8::PropertyCallbackInf
261259 */  
262260static  void  setter (uint32_t  index, v8::Local<v8::Value> input, const  v8::PropertyCallbackInfo<v8::Value>& info)
263261{
264-     //  retrieve handle to the original object
265-     Handle<Php::Object> handle (info.Data ());
262+     //  create a local handle, so properties "fall out of scope" and retrieve the original object
263+     v8::HandleScope         scope (isolate ());
264+     Handle<Php::Object>     handle (info.Holder ()->GetInternalField (0 ));
266265
267266    //  store the property inside the object
268267    handle->call (" offsetSet" static_cast <int64_t >(index), value (input));
@@ -277,8 +276,9 @@ static void setter(uint32_t index, v8::Local<v8::Value> input, const v8::Propert
277276 */  
278277static  void  setter (v8::Local<v8::String> property, v8::Local<v8::Value> input, const  v8::PropertyCallbackInfo<v8::Value>& info)
279278{
280-     //  retrieve handle to the original object and convert the requested property
281-     Handle<Php::Object>     handle (info.Data ());
279+     //  create a local handle, so properties "fall out of scope" and retrieve the original object
280+     v8::HandleScope         scope (isolate ());
281+     Handle<Php::Object>     handle (info.Holder ()->GetInternalField (0 ));
282282    v8::String::Utf8Value   name (property);
283283
284284    //  if the object is not implementing ArrayAccess or has the given property as a member
@@ -301,16 +301,22 @@ static void setter(v8::Local<v8::String> property, v8::Local<v8::Value> input, c
301301 *  @param  object  The object to wrap 
302302 */  
303303Object::Object (Php::Object object) :
304-     _template (v8::ObjectTemplate::New())
304+     _template (v8::ObjectTemplate::New()),
305+     _object (object)
305306{
307+     //  TODO: check whether it saves memory and time to re-use object templates
308+ 
309+     //  reserve space to store the handle to the object as an external reference
310+     _template->SetInternalFieldCount (1 );
311+ 
306312    //  if the object can be invoked as a function, we register the callback
307313    if  (object.isCallable ()) _template->SetCallAsFunctionHandler (callback, Handle<Php::Value>(object));
308314
309315    //  register the property handlers
310-     _template->SetNamedPropertyHandler (getter, setter, nullptr , nullptr , named_enumerator, Handle<Php::Object>(object) );
316+     _template->SetNamedPropertyHandler (getter, setter, nullptr , nullptr , named_enumerator);
311317
312318    //  if the object implements the ArrayAccess interface, we should also set the indexed property handler
313-     if  (object.instanceOf (" ArrayAccess" SetIndexedPropertyHandler (getter, setter, nullptr , nullptr , indexed_enumerator, Handle<Php::Object>(object) );
319+     if  (object.instanceOf (" ArrayAccess" SetIndexedPropertyHandler (getter, setter, nullptr , nullptr , indexed_enumerator);
314320}
315321
316322/* *
@@ -322,7 +328,13 @@ Object::Object(Php::Object object) :
322328Object::operator  v8::Local<v8::Value> ()
323329{
324330    //  create a new object based on the template
325-     return  _template->NewInstance ();
331+     auto  instance = _template->NewInstance ();
332+ 
333+     //  attach the object to the instance
334+     instance->SetInternalField (0 , Handle<Php::Value>(_object));
335+ 
336+     //  return the instance
337+     return  instance;
326338}
327339
328340/* *
0 commit comments