@@ -59,11 +59,9 @@ static uint32_t count(const Php::Object &object)
59
59
*/
60
60
static void callback (const v8::FunctionCallbackInfo<v8::Value> &info)
61
61
{
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
63
63
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 ));
67
65
68
66
// an array to hold all the arguments
69
67
Php::Array arguments;
@@ -96,7 +94,7 @@ static void indexed_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info)
96
94
{
97
95
// create a local handle, so properties "fall out of scope" and retrieve the original object
98
96
v8::HandleScope scope (isolate ());
99
- Handle <Php::Object> handle (info.Data ( ));
97
+ Handle <Php::Object> handle (info.Holder ()-> GetInternalField ( 0 ));
100
98
101
99
// create a new array to store all the properties
102
100
v8::Local<v8::Array> properties (v8::Array::New (isolate ()));
@@ -128,7 +126,7 @@ static void named_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info)
128
126
{
129
127
// create a local handle, so properties "fall out of scope" and retrieve the original object
130
128
v8::HandleScope scope (isolate ());
131
- Handle <Php::Object> handle (info.Data ( ));
129
+ Handle <Php::Object> handle (info.Holder ()-> GetInternalField ( 0 ));
132
130
133
131
// create a new array to store all the properties
134
132
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
161
159
{
162
160
// create a local handle, so properties "fall out of scope" and retrieve the original object
163
161
v8::HandleScope scope (isolate ());
164
- Handle <Php::Object> handle (info.Data ( ));
162
+ Handle <Php::Object> handle (info.Holder ()-> GetInternalField ( 0 ));
165
163
166
164
// check if we have an item at the requested offset
167
165
if (handle->call (" offsetExists" , static_cast <int64_t >(index )))
@@ -188,7 +186,7 @@ static void getter(v8::Local<v8::String> property, const v8::PropertyCallbackInf
188
186
v8::HandleScope scope (isolate ());
189
187
190
188
// 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 ));
192
190
v8::String::Utf8Value name (property);
193
191
194
192
/* *
@@ -261,8 +259,9 @@ static void getter(v8::Local<v8::String> property, const v8::PropertyCallbackInf
261
259
*/
262
260
static void setter (uint32_t index, v8::Local<v8::Value> input, const v8::PropertyCallbackInfo<v8::Value>& info)
263
261
{
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 ));
266
265
267
266
// store the property inside the object
268
267
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
277
276
*/
278
277
static void setter (v8::Local<v8::String> property, v8::Local<v8::Value> input, const v8::PropertyCallbackInfo<v8::Value>& info)
279
278
{
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 ));
282
282
v8::String::Utf8Value name (property);
283
283
284
284
// 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
301
301
* @param object The object to wrap
302
302
*/
303
303
Object::Object (Php::Object object) :
304
- _template (v8::ObjectTemplate::New())
304
+ _template (v8::ObjectTemplate::New()),
305
+ _object (object)
305
306
{
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
+
306
312
// if the object can be invoked as a function, we register the callback
307
313
if (object.isCallable ()) _template->SetCallAsFunctionHandler (callback, Handle <Php::Value>(object));
308
314
309
315
// 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);
311
317
312
318
// if the object implements the ArrayAccess interface, we should also set the indexed property handler
313
- if (object.instanceOf (" ArrayAccess" )) _template->SetIndexedPropertyHandler (getter, setter, nullptr , nullptr , indexed_enumerator, Handle <Php::Object>(object) );
319
+ if (object.instanceOf (" ArrayAccess" )) _template->SetIndexedPropertyHandler (getter, setter, nullptr , nullptr , indexed_enumerator);
314
320
}
315
321
316
322
/* *
@@ -322,7 +328,13 @@ Object::Object(Php::Object object) :
322
328
Object::operator v8::Local<v8::Value> ()
323
329
{
324
330
// 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;
326
338
}
327
339
328
340
/* *
0 commit comments