Skip to content

Commit 3aecfdb

Browse files
committed
Merge pull request #1035
2 parents c6b92f3 + b31745f commit 3aecfdb

26 files changed

+761
-4
lines changed

src/BSON/Binary.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,32 @@ static phongo_create_object_retval php_phongo_binary_create_object(zend_class_en
367367
#endif
368368
} /* }}} */
369369

370+
static phongo_create_object_retval php_phongo_binary_clone_object(zval* object TSRMLS_DC) /* {{{ */
371+
{
372+
php_phongo_binary_t* intern;
373+
php_phongo_binary_t* new_intern;
374+
phongo_create_object_retval new_object;
375+
376+
intern = Z_BINARY_OBJ_P(object);
377+
new_object = php_phongo_binary_create_object(Z_OBJCE_P(object) TSRMLS_CC);
378+
379+
#if PHP_VERSION_ID >= 70000
380+
new_intern = Z_OBJ_BINARY(new_object);
381+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
382+
#else
383+
{
384+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
385+
386+
new_intern = (php_phongo_binary_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
387+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
388+
}
389+
#endif
390+
391+
php_phongo_binary_init(new_intern, intern->data, intern->data_len, intern->type TSRMLS_CC);
392+
393+
return new_object;
394+
} /* }}} */
395+
370396
static int php_phongo_binary_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
371397
{
372398
php_phongo_binary_t *intern1, *intern2;
@@ -462,6 +488,7 @@ void php_phongo_binary_init_ce(INIT_FUNC_ARGS) /* {{{ */
462488
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, zend_ce_serializable);
463489

464490
memcpy(&php_phongo_handler_binary, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
491+
php_phongo_handler_binary.clone_obj = php_phongo_binary_clone_object;
465492
php_phongo_handler_binary.compare_objects = php_phongo_binary_compare_objects;
466493
php_phongo_handler_binary.get_debug_info = php_phongo_binary_get_debug_info;
467494
php_phongo_handler_binary.get_gc = php_phongo_binary_get_gc;

src/BSON/DBPointer.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,32 @@ phongo_create_object_retval php_phongo_dbpointer_create_object(zend_class_entry*
287287
#endif
288288
} /* }}} */
289289

290+
static phongo_create_object_retval php_phongo_dbpointer_clone_object(zval* object TSRMLS_DC) /* {{{ */
291+
{
292+
php_phongo_dbpointer_t* intern;
293+
php_phongo_dbpointer_t* new_intern;
294+
phongo_create_object_retval new_object;
295+
296+
intern = Z_DBPOINTER_OBJ_P(object);
297+
new_object = php_phongo_dbpointer_create_object(Z_OBJCE_P(object) TSRMLS_CC);
298+
299+
#if PHP_VERSION_ID >= 70000
300+
new_intern = Z_OBJ_DBPOINTER(new_object);
301+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
302+
#else
303+
{
304+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
305+
306+
new_intern = (php_phongo_dbpointer_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
307+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
308+
}
309+
#endif
310+
311+
php_phongo_dbpointer_init(new_intern, intern->ref, intern->ref_len, intern->id, 24 TSRMLS_CC);
312+
313+
return new_object;
314+
} /* }}} */
315+
290316
static int php_phongo_dbpointer_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
291317
{
292318
php_phongo_dbpointer_t *intern1, *intern2;
@@ -376,6 +402,7 @@ void php_phongo_dbpointer_init_ce(INIT_FUNC_ARGS) /* {{{ */
376402
zend_class_implements(php_phongo_dbpointer_ce TSRMLS_CC, 1, zend_ce_serializable);
377403

378404
memcpy(&php_phongo_handler_dbpointer, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
405+
php_phongo_handler_dbpointer.clone_obj = php_phongo_dbpointer_clone_object;
379406
php_phongo_handler_dbpointer.compare_objects = php_phongo_dbpointer_compare_objects;
380407
php_phongo_handler_dbpointer.get_debug_info = php_phongo_dbpointer_get_debug_info;
381408
php_phongo_handler_dbpointer.get_gc = php_phongo_dbpointer_get_gc;

src/BSON/Decimal128.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,34 @@ static phongo_create_object_retval php_phongo_decimal128_create_object(zend_clas
300300
#endif
301301
} /* }}} */
302302

303+
static phongo_create_object_retval php_phongo_decimal128_clone_object(zval* object TSRMLS_DC) /* {{{ */
304+
{
305+
php_phongo_decimal128_t* intern;
306+
php_phongo_decimal128_t* new_intern;
307+
phongo_create_object_retval new_object;
308+
309+
intern = Z_DECIMAL128_OBJ_P(object);
310+
new_object = php_phongo_decimal128_create_object(Z_OBJCE_P(object) TSRMLS_CC);
311+
312+
#if PHP_VERSION_ID >= 70000
313+
new_intern = Z_OBJ_DECIMAL128(new_object);
314+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
315+
#else
316+
{
317+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
318+
319+
new_intern = (php_phongo_decimal128_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
320+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
321+
}
322+
#endif
323+
324+
// Use memcpy to copy bson value to avoid converting to string and back
325+
memcpy(&new_intern->decimal, &intern->decimal, sizeof(bson_decimal128_t));
326+
new_intern->initialized = true;
327+
328+
return new_object;
329+
} /* }}} */
330+
303331
static HashTable* php_phongo_decimal128_get_gc(zval* object, phongo_get_gc_table table, int* n TSRMLS_DC) /* {{{ */
304332
{
305333
*table = NULL;
@@ -371,6 +399,7 @@ void php_phongo_decimal128_init_ce(INIT_FUNC_ARGS) /* {{{ */
371399
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, zend_ce_serializable);
372400

373401
memcpy(&php_phongo_handler_decimal128, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
402+
php_phongo_handler_decimal128.clone_obj = php_phongo_decimal128_clone_object;
374403
php_phongo_handler_decimal128.get_debug_info = php_phongo_decimal128_get_debug_info;
375404
php_phongo_handler_decimal128.get_gc = php_phongo_decimal128_get_gc;
376405
php_phongo_handler_decimal128.get_properties = php_phongo_decimal128_get_properties;

src/BSON/Int64.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,32 @@ phongo_create_object_retval php_phongo_int64_create_object(zend_class_entry* cla
271271
#endif
272272
} /* }}} */
273273

274+
static phongo_create_object_retval php_phongo_int64_clone_object(zval* object TSRMLS_DC) /* {{{ */
275+
{
276+
php_phongo_int64_t* intern;
277+
php_phongo_int64_t* new_intern;
278+
phongo_create_object_retval new_object;
279+
280+
intern = Z_INT64_OBJ_P(object);
281+
new_object = php_phongo_int64_create_object(Z_OBJCE_P(object) TSRMLS_CC);
282+
283+
#if PHP_VERSION_ID >= 70000
284+
new_intern = Z_OBJ_INT64(new_object);
285+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
286+
#else
287+
{
288+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
289+
290+
new_intern = (php_phongo_int64_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
291+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
292+
}
293+
#endif
294+
295+
php_phongo_int64_init(new_intern, intern->integer);
296+
297+
return new_object;
298+
} /* }}} */
299+
274300
static int php_phongo_int64_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
275301
{
276302
php_phongo_int64_t *intern1, *intern2;
@@ -356,6 +382,7 @@ void php_phongo_int64_init_ce(INIT_FUNC_ARGS) /* {{{ */
356382
zend_class_implements(php_phongo_int64_ce TSRMLS_CC, 1, zend_ce_serializable);
357383

358384
memcpy(&php_phongo_handler_int64, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
385+
php_phongo_handler_int64.clone_obj = php_phongo_int64_clone_object;
359386
php_phongo_handler_int64.compare_objects = php_phongo_int64_compare_objects;
360387
php_phongo_handler_int64.get_debug_info = php_phongo_int64_get_debug_info;
361388
php_phongo_handler_int64.get_gc = php_phongo_int64_get_gc;

src/BSON/Javascript.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,33 @@ phongo_create_object_retval php_phongo_javascript_create_object(zend_class_entry
420420
#endif
421421
} /* }}} */
422422

423+
static phongo_create_object_retval php_phongo_javascript_clone_object(zval* object TSRMLS_DC) /* {{{ */
424+
{
425+
php_phongo_javascript_t* intern;
426+
php_phongo_javascript_t* new_intern;
427+
phongo_create_object_retval new_object;
428+
429+
intern = Z_JAVASCRIPT_OBJ_P(object);
430+
new_object = php_phongo_javascript_create_object(Z_OBJCE_P(object) TSRMLS_CC);
431+
432+
#if PHP_VERSION_ID >= 70000
433+
new_intern = Z_OBJ_JAVASCRIPT(new_object);
434+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
435+
#else
436+
{
437+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
438+
439+
new_intern = (php_phongo_javascript_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
440+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
441+
}
442+
#endif
443+
444+
php_phongo_javascript_init(new_intern, intern->code, intern->code_len, NULL TSRMLS_CC);
445+
new_intern->scope = bson_copy(intern->scope);
446+
447+
return new_object;
448+
} /* }}} */
449+
423450
static int php_phongo_javascript_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
424451
{
425452
php_phongo_javascript_t *intern1, *intern2;
@@ -545,6 +572,7 @@ void php_phongo_javascript_init_ce(INIT_FUNC_ARGS) /* {{{ */
545572
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, zend_ce_serializable);
546573

547574
memcpy(&php_phongo_handler_javascript, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
575+
php_phongo_handler_javascript.clone_obj = php_phongo_javascript_clone_object;
548576
php_phongo_handler_javascript.compare_objects = php_phongo_javascript_compare_objects;
549577
php_phongo_handler_javascript.get_debug_info = php_phongo_javascript_get_debug_info;
550578
php_phongo_handler_javascript.get_gc = php_phongo_javascript_get_gc;

src/BSON/MaxKey.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ void php_phongo_maxkey_init_ce(INIT_FUNC_ARGS) /* {{{ */
157157
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, zend_ce_serializable);
158158

159159
memcpy(&php_phongo_handler_maxkey, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
160+
/* Re-assign default handler previously removed in php_phongo.c */
161+
php_phongo_handler_maxkey.clone_obj = zend_objects_clone_obj;
160162
#if PHP_VERSION_ID >= 70000
161163
php_phongo_handler_maxkey.free_obj = php_phongo_maxkey_free_object;
162164
php_phongo_handler_maxkey.offset = XtOffsetOf(php_phongo_maxkey_t, std);

src/BSON/MinKey.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ void php_phongo_minkey_init_ce(INIT_FUNC_ARGS) /* {{{ */
158158
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, zend_ce_serializable);
159159

160160
memcpy(&php_phongo_handler_minkey, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
161+
/* Re-assign default handler previously removed in php_phongo.c */
162+
php_phongo_handler_minkey.clone_obj = zend_objects_clone_obj;
161163
#if PHP_VERSION_ID >= 70000
162164
php_phongo_handler_minkey.free_obj = php_phongo_minkey_free_object;
163165
php_phongo_handler_minkey.offset = XtOffsetOf(php_phongo_minkey_t, std);

src/BSON/ObjectId.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include "phongo_compat.h"
3131
#include "php_phongo.h"
3232

33+
#define PHONGO_OID_SIZE sizeof(((php_phongo_objectid_t *)0)->oid)
34+
#define PHONGO_OID_LEN (PHONGO_OID_SIZE - 1)
35+
3336
zend_class_entry* php_phongo_objectid_ce;
3437

3538
/* Initialize the object with a generated value and return whether it was
@@ -163,7 +166,7 @@ static PHP_METHOD(ObjectId, __toString)
163166
return;
164167
}
165168

166-
PHONGO_RETURN_STRINGL(intern->oid, 24);
169+
PHONGO_RETURN_STRINGL(intern->oid, PHONGO_OID_LEN);
167170
} /* }}} */
168171

169172
/* {{{ proto array MongoDB\BSON\ObjectId::jsonSerialize()
@@ -179,7 +182,7 @@ static PHP_METHOD(ObjectId, jsonSerialize)
179182
intern = Z_OBJECTID_OBJ_P(getThis());
180183

181184
array_init_size(return_value, 1);
182-
ADD_ASSOC_STRINGL(return_value, "$oid", intern->oid, 24);
185+
ADD_ASSOC_STRINGL(return_value, "$oid", intern->oid, PHONGO_OID_LEN);
183186
} /* }}} */
184187

185188
/* {{{ proto string MongoDB\BSON\ObjectId::serialize()
@@ -199,11 +202,11 @@ static PHP_METHOD(ObjectId, serialize)
199202

200203
#if PHP_VERSION_ID >= 70000
201204
array_init_size(&retval, 1);
202-
ADD_ASSOC_STRINGL(&retval, "oid", intern->oid, 24);
205+
ADD_ASSOC_STRINGL(&retval, "oid", intern->oid, PHONGO_OID_LEN);
203206
#else
204207
ALLOC_INIT_ZVAL(retval);
205208
array_init_size(retval, 1);
206-
ADD_ASSOC_STRINGL(retval, "oid", intern->oid, 24);
209+
ADD_ASSOC_STRINGL(retval, "oid", intern->oid, PHONGO_OID_LEN);
207210
#endif
208211

209212
PHP_VAR_SERIALIZE_INIT(var_hash);
@@ -336,6 +339,34 @@ static phongo_create_object_retval php_phongo_objectid_create_object(zend_class_
336339
#endif
337340
} /* }}} */
338341

342+
static phongo_create_object_retval php_phongo_objectid_clone_object(zval* object TSRMLS_DC) /* {{{ */
343+
{
344+
php_phongo_objectid_t* intern;
345+
php_phongo_objectid_t* new_intern;
346+
phongo_create_object_retval new_object;
347+
348+
intern = Z_OBJECTID_OBJ_P(object);
349+
new_object = php_phongo_objectid_create_object(Z_OBJCE_P(object) TSRMLS_CC);
350+
351+
#if PHP_VERSION_ID >= 70000
352+
new_intern = Z_OBJ_OBJECTID(new_object);
353+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
354+
#else
355+
{
356+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
357+
358+
new_intern = (php_phongo_objectid_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
359+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
360+
}
361+
#endif
362+
363+
// Use memcpy to copy bson value to avoid converting to string and back
364+
memcpy(&new_intern->oid, &intern->oid, PHONGO_OID_SIZE);
365+
new_intern->initialized = true;
366+
367+
return new_object;
368+
} /* }}} */
369+
339370
static int php_phongo_objectid_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
340371
{
341372
php_phongo_objectid_t* intern1;
@@ -415,6 +446,7 @@ void php_phongo_objectid_init_ce(INIT_FUNC_ARGS) /* {{{ */
415446
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, zend_ce_serializable);
416447

417448
memcpy(&php_phongo_handler_objectid, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
449+
php_phongo_handler_objectid.clone_obj = php_phongo_objectid_clone_object;
418450
php_phongo_handler_objectid.compare_objects = php_phongo_objectid_compare_objects;
419451
php_phongo_handler_objectid.get_debug_info = php_phongo_objectid_get_debug_info;
420452
php_phongo_handler_objectid.get_gc = php_phongo_objectid_get_gc;

src/BSON/Regex.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,32 @@ static phongo_create_object_retval php_phongo_regex_create_object(zend_class_ent
371371
#endif
372372
} /* }}} */
373373

374+
static phongo_create_object_retval php_phongo_regex_clone_object(zval* object TSRMLS_DC) /* {{{ */
375+
{
376+
php_phongo_regex_t* intern;
377+
php_phongo_regex_t* new_intern;
378+
phongo_create_object_retval new_object;
379+
380+
intern = Z_REGEX_OBJ_P(object);
381+
new_object = php_phongo_regex_create_object(Z_OBJCE_P(object) TSRMLS_CC);
382+
383+
#if PHP_VERSION_ID >= 70000
384+
new_intern = Z_OBJ_REGEX(new_object);
385+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
386+
#else
387+
{
388+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
389+
390+
new_intern = (php_phongo_regex_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
391+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
392+
}
393+
#endif
394+
395+
php_phongo_regex_init(new_intern, intern->pattern, intern->pattern_len, intern->flags, intern->flags_len TSRMLS_CC);
396+
397+
return new_object;
398+
} /* }}} */
399+
374400
static int php_phongo_regex_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
375401
{
376402
php_phongo_regex_t *intern1, *intern2;
@@ -464,6 +490,7 @@ void php_phongo_regex_init_ce(INIT_FUNC_ARGS) /* {{{ */
464490
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
465491

466492
memcpy(&php_phongo_handler_regex, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
493+
php_phongo_handler_regex.clone_obj = php_phongo_regex_clone_object;
467494
php_phongo_handler_regex.compare_objects = php_phongo_regex_compare_objects;
468495
php_phongo_handler_regex.get_debug_info = php_phongo_regex_get_debug_info;
469496
php_phongo_handler_regex.get_gc = php_phongo_regex_get_gc;

src/BSON/Symbol.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,32 @@ phongo_create_object_retval php_phongo_symbol_create_object(zend_class_entry* cl
249249
#endif
250250
} /* }}} */
251251

252+
static phongo_create_object_retval php_phongo_symbol_clone_object(zval* object TSRMLS_DC) /* {{{ */
253+
{
254+
php_phongo_symbol_t* intern;
255+
php_phongo_symbol_t* new_intern;
256+
phongo_create_object_retval new_object;
257+
258+
intern = Z_SYMBOL_OBJ_P(object);
259+
new_object = php_phongo_symbol_create_object(Z_OBJCE_P(object) TSRMLS_CC);
260+
261+
#if PHP_VERSION_ID >= 70000
262+
new_intern = Z_OBJ_SYMBOL(new_object);
263+
zend_objects_clone_members(&new_intern->std, &intern->std TSRMLS_CC);
264+
#else
265+
{
266+
zend_object_handle handle = Z_OBJ_HANDLE_P(object);
267+
268+
new_intern = (php_phongo_symbol_t*) zend_object_store_get_object_by_handle(new_object.handle TSRMLS_CC);
269+
zend_objects_clone_members(&new_intern->std, new_object, &intern->std, handle TSRMLS_CC);
270+
}
271+
#endif
272+
273+
php_phongo_symbol_init(new_intern, intern->symbol, intern->symbol_len TSRMLS_CC);
274+
275+
return new_object;
276+
} /* }}} */
277+
252278
static int php_phongo_symbol_compare_objects(zval* o1, zval* o2 TSRMLS_DC) /* {{{ */
253279
{
254280
php_phongo_symbol_t *intern1, *intern2;
@@ -326,6 +352,7 @@ void php_phongo_symbol_init_ce(INIT_FUNC_ARGS) /* {{{ */
326352
zend_class_implements(php_phongo_symbol_ce TSRMLS_CC, 1, zend_ce_serializable);
327353

328354
memcpy(&php_phongo_handler_symbol, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
355+
php_phongo_handler_symbol.clone_obj = php_phongo_symbol_clone_object;
329356
php_phongo_handler_symbol.compare_objects = php_phongo_symbol_compare_objects;
330357
php_phongo_handler_symbol.get_debug_info = php_phongo_symbol_get_debug_info;
331358
php_phongo_handler_symbol.get_gc = php_phongo_symbol_get_gc;

0 commit comments

Comments
 (0)