Skip to content

Commit a09b847

Browse files
committed
Merge branch 'zval_shallow_clone'
2 parents 32e2af0 + f9528f0 commit a09b847

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn main() {
101101
.clang_args(includes.split(' '))
102102
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
103103
.rustfmt_bindings(true)
104-
.no_copy("_zend_value")
104+
.no_copy("_zval_struct")
105105
.no_copy("_zend_string")
106106
.no_copy("_zend_array")
107107
.layout_tests(env::var("EXT_PHP_RS_TEST").is_ok());

src/types/zval.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,38 @@ impl Zval {
500500
{
501501
FromZval::from_zval(self)
502502
}
503+
504+
/// Creates a shallow clone of the [`Zval`].
505+
///
506+
/// This copies the contents of the [`Zval`], and increments the reference
507+
/// counter of the underlying value (if it is reference counted).
508+
///
509+
/// For example, if the zval contains a long, it will simply copy the value.
510+
/// However, if the zval contains an object, the new zval will point to the
511+
/// same object, and the objects reference counter will be incremented.
512+
///
513+
/// # Returns
514+
///
515+
/// The cloned zval.
516+
pub fn shallow_clone(&self) -> Zval {
517+
let mut new = Zval::new();
518+
new.u1 = self.u1;
519+
new.value = self.value;
520+
521+
// SAFETY: `u1` union is only used for easier bitmasking. It is valid to read
522+
// from either of the variants.
523+
//
524+
// SAFETY: If the value if refcounted (`self.u1.type_info & Z_TYPE_FLAGS_MASK`)
525+
// then it is valid to dereference `self.value.counted`.
526+
unsafe {
527+
let flags = ZvalTypeFlags::from_bits_unchecked(self.u1.type_info);
528+
if flags.contains(ZvalTypeFlags::RefCounted) {
529+
(*self.value.counted).gc.refcount += 1;
530+
}
531+
}
532+
533+
new
534+
}
503535
}
504536

505537
impl Debug for Zval {

0 commit comments

Comments
 (0)