@@ -189,9 +189,9 @@ ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
189
189
return object ;
190
190
}
191
191
192
- ZEND_API void ZEND_FASTCALL zend_objects_clone_members (zend_object * new_object , zend_object * old_object )
192
+ ZEND_API void ZEND_FASTCALL zend_objects_clone_members_with (zend_object * new_object , zend_object * old_object , const zend_class_entry * scope , const HashTable * properties )
193
193
{
194
- bool has_clone_method = old_object -> ce -> clone != NULL ;
194
+ bool might_update_properties = old_object -> ce -> clone != NULL || zend_hash_num_elements ( properties ) > 0 ;
195
195
196
196
if (old_object -> ce -> default_properties_count ) {
197
197
zval * src = old_object -> properties_table ;
@@ -202,7 +202,7 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
202
202
i_zval_ptr_dtor (dst );
203
203
ZVAL_COPY_VALUE_PROP (dst , src );
204
204
zval_add_ref (dst );
205
- if (has_clone_method ) {
205
+ if (might_update_properties ) {
206
206
/* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
207
207
Z_PROP_FLAG_P (dst ) |= IS_PROP_REINITABLE ;
208
208
}
@@ -217,7 +217,7 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
217
217
src ++ ;
218
218
dst ++ ;
219
219
} while (src != end );
220
- } else if (old_object -> properties && !has_clone_method ) {
220
+ } else if (old_object -> properties && !might_update_properties ) {
221
221
/* fast copy */
222
222
if (EXPECTED (old_object -> handlers == & std_object_handlers )) {
223
223
if (EXPECTED (!(GC_FLAGS (old_object -> properties ) & IS_ARRAY_IMMUTABLE ))) {
@@ -251,7 +251,7 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
251
251
ZVAL_COPY_VALUE (& new_prop , prop );
252
252
zval_add_ref (& new_prop );
253
253
}
254
- if (has_clone_method ) {
254
+ if (might_update_properties ) {
255
255
/* Unconditionally add the IS_PROP_REINITABLE flag to avoid a potential cache miss of property_info */
256
256
Z_PROP_FLAG_P (& new_prop ) |= IS_PROP_REINITABLE ;
257
257
}
@@ -263,9 +263,31 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
263
263
} ZEND_HASH_FOREACH_END ();
264
264
}
265
265
266
- if (has_clone_method ) {
266
+ if (might_update_properties ) {
267
267
GC_ADDREF (new_object );
268
- zend_call_known_instance_method_with_0_params (new_object -> ce -> clone , new_object , NULL );
268
+ if (old_object -> ce -> clone ) {
269
+ zend_call_known_instance_method_with_0_params (new_object -> ce -> clone , new_object , NULL );
270
+ }
271
+
272
+ if (EXPECTED (!EG (exception )) && zend_hash_num_elements (properties ) > 0 ) {
273
+ zend_ulong num_key ;
274
+ zend_string * key ;
275
+ zval * val ;
276
+ ZEND_HASH_FOREACH_KEY_VAL (properties , num_key , key , val ) {
277
+ if (UNEXPECTED (key == NULL )) {
278
+ key = zend_long_to_str (num_key );
279
+ zend_update_property_ex (scope , new_object , key , val );
280
+ zend_string_release_ex (key , false);
281
+ } else {
282
+ zend_update_property_ex (scope , new_object , key , val );
283
+ }
284
+
285
+ if (UNEXPECTED (EG (exception ))) {
286
+ break ;
287
+ }
288
+ } ZEND_HASH_FOREACH_END ();
289
+ }
290
+
269
291
270
292
if (ZEND_CLASS_HAS_READONLY_PROPS (new_object -> ce )) {
271
293
for (uint32_t i = 0 ; i < new_object -> ce -> default_properties_count ; i ++ ) {
@@ -279,12 +301,34 @@ ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object,
279
301
}
280
302
}
281
303
282
- ZEND_API zend_object * zend_objects_clone_obj (zend_object * old_object )
304
+ ZEND_API void ZEND_FASTCALL zend_objects_clone_members (zend_object * new_object , zend_object * old_object )
305
+ {
306
+ ZEND_ASSERT (old_object -> ce == new_object -> ce );
307
+
308
+ zend_objects_clone_members_with (new_object , old_object , old_object -> ce , & zend_empty_array );
309
+ }
310
+
311
+ ZEND_API zend_object * zend_objects_clone_obj_with (zend_object * old_object , const zend_class_entry * scope , const HashTable * properties )
283
312
{
284
313
zend_object * new_object ;
285
314
315
+ /* Compatibility with code that only overrides clone_obj. */
316
+ if (UNEXPECTED (old_object -> handlers -> clone_obj != zend_objects_clone_obj )) {
317
+ if (!old_object -> handlers -> clone_obj ) {
318
+ zend_throw_error (NULL , "Trying to clone an uncloneable object of class %s" , ZSTR_VAL (old_object -> ce -> name ));
319
+ return NULL ;
320
+ }
321
+
322
+ if (zend_hash_num_elements (properties ) > 0 ) {
323
+ zend_throw_error (NULL , "Cloning objects of class %s with updated properties is not supported" , ZSTR_VAL (old_object -> ce -> name ));
324
+ return NULL ;
325
+ }
326
+
327
+ return old_object -> handlers -> clone_obj (old_object );
328
+ }
329
+
286
330
if (UNEXPECTED (zend_object_is_lazy (old_object ))) {
287
- return zend_lazy_object_clone (old_object );
331
+ return zend_lazy_object_clone (old_object , scope , properties );
288
332
}
289
333
290
334
/* assume that create isn't overwritten, so when clone depends on the
@@ -301,7 +345,12 @@ ZEND_API zend_object *zend_objects_clone_obj(zend_object *old_object)
301
345
} while (p != end );
302
346
}
303
347
304
- zend_objects_clone_members (new_object , old_object );
348
+ zend_objects_clone_members_with (new_object , old_object , scope , properties );
305
349
306
350
return new_object ;
307
351
}
352
+
353
+ ZEND_API zend_object * zend_objects_clone_obj (zend_object * old_object )
354
+ {
355
+ return zend_objects_clone_obj_with (old_object , old_object -> ce , & zend_empty_array );
356
+ }
0 commit comments