Skip to content

Commit db80c31

Browse files
committed
Add ref traits.
1 parent 528cc51 commit db80c31

File tree

12 files changed

+63
-105
lines changed

12 files changed

+63
-105
lines changed

examples/http-server/src/server.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use hyper::{
1818
Body, Request, Response, Server, StatusCode,
1919
};
2020
use phper::{
21-
alloc::EBox,
21+
alloc::{EBox, RefClone},
2222
classes::{ClassEntry, DynamicClass, StatelessClassEntry, Visibility},
2323
errors::Error::Throw,
2424
functions::Argument,
@@ -75,9 +75,7 @@ pub fn make_server_class() -> DynamicClass<Option<Builder<AddrIncoming>>> {
7575
static HANDLE: AtomicPtr<ZVal> = AtomicPtr::new(null_mut());
7676

7777
let builder = replace(this.as_mut_state(), None).unwrap();
78-
let handle = this
79-
.duplicate_property("onRequestHandle")
80-
.map_err(phper::Error::NotRefCountedType)?;
78+
let handle = EBox::new(this.get_mut_property("onRequestHandle").ref_clone());
8179
HANDLE.store(EBox::into_raw(handle), Ordering::SeqCst);
8280

8381
let make_svc = make_service_fn(move |_conn| async move {

phper-alloc/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod macros;
1616

1717
use phper_sys::*;
1818
use std::{
19+
borrow::Borrow,
1920
convert::TryInto,
2021
mem::{forget, size_of},
2122
ops::{Deref, DerefMut},
@@ -90,3 +91,17 @@ impl<T> Drop for EBox<T> {
9091
}
9192
}
9293
}
94+
95+
/// Duplicate an object without deep copy, but to only add the refcount, for php
96+
/// refcount struct.
97+
pub trait ToRefOwned {
98+
type Owned: Borrow<Self>;
99+
100+
fn to_ref_owned(&mut self) -> Self::Owned;
101+
}
102+
103+
/// Duplicate an object without deep copy, but to only add the refcount, for php
104+
/// refcount struct.
105+
pub trait RefClone {
106+
fn ref_clone(&mut self) -> Self;
107+
}

phper-sys/build.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,6 @@ fn main() {
2121
let includes = execute_command(&[php_config.as_str(), "--includes"]);
2222
let includes = includes.split(' ').collect::<Vec<_>>();
2323

24-
// Generate php const.
25-
26-
let php_bin = execute_command(&[php_config.as_str(), "--php-binary"]);
27-
let php_info = execute_command(&[php_bin.as_str(), "-i"]);
28-
29-
println!(
30-
"cargo:rustc-env=ZEND_MODULE_BUILD_ID={}",
31-
php_info
32-
.lines()
33-
.find_map(|line| {
34-
if line.starts_with("Zend Extension Build") {
35-
Some(
36-
line.chars()
37-
.skip_while(|c| *c != 'A')
38-
.collect::<String>()
39-
.trim()
40-
.to_owned(),
41-
)
42-
} else {
43-
None
44-
}
45-
})
46-
.expect("Can't found the field `Zend Extension Build`")
47-
);
48-
49-
println!(
50-
"cargo:rustc-env=PHP_MODULE_BUILD_ID={}",
51-
php_info
52-
.lines()
53-
.find_map(|line| {
54-
if line.starts_with("PHP Extension Build") {
55-
Some(
56-
line.chars()
57-
.skip_while(|c| *c != 'A')
58-
.collect::<String>()
59-
.trim()
60-
.to_owned(),
61-
)
62-
} else {
63-
None
64-
}
65-
})
66-
.expect("Can't found the field `PHP Extension Build`")
67-
);
68-
6924
// Generate libphpwrapper.a.
7025

7126
let mut builder = cc::Build::new();

phper-sys/php_wrapper.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,11 @@ zend_string *phper_z_str_p(const zval *zv) {
289289
zend_resource *phper_z_res_p(const zval *zv) {
290290
return Z_RES_P(zv);
291291
}
292+
293+
zend_string *phper_zend_string_copy(zend_string *s) {
294+
return zend_string_copy(s);
295+
}
296+
297+
const char *phper_get_zend_module_build_id() {
298+
return ZEND_MODULE_BUILD_ID;
299+
}

phper-sys/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,3 @@
2020
use std::os::raw::c_char;
2121

2222
include!(concat!(env!("OUT_DIR"), "/php_bindings.rs"));
23-
24-
pub const PHP_MODULE_BUILD_ID: *const c_char =
25-
concat!(env!("PHP_MODULE_BUILD_ID"), "\0").as_ptr().cast();
26-
pub const ZEND_MODULE_BUILD_ID: *const c_char =
27-
concat!(env!("ZEND_MODULE_BUILD_ID"), "\0").as_ptr().cast();

phper/build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ use phper_sys::*;
1212

1313
fn main() {
1414
phper_build::register_configures();
15-
assert_eq!(
16-
PHP_DEBUG, 0,
17-
"PHPER not support DEBUG mode now (php built with `--enable-debug`)."
18-
);
1915
assert_eq!(
2016
USING_ZTS, 0,
2117
"PHPER not support ZTS mode now (php built with `--enable-maintainer-zts` or \

phper/src/arrays.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::{
1717
values::ZVal,
1818
};
1919
use derive_more::From;
20+
use phper_alloc::ToRefOwned;
2021
use std::{
2122
borrow::Borrow,
2223
convert::TryInto,
@@ -188,6 +189,14 @@ impl ToOwned for ZArr {
188189
}
189190
}
190191

192+
impl ToRefOwned for ZArr {
193+
type Owned = ZArray;
194+
195+
fn to_ref_owned(&mut self) -> Self::Owned {
196+
todo!()
197+
}
198+
}
199+
191200
/// Wrapper of [crate::sys::zend_array].
192201
#[repr(transparent)]
193202
pub struct ZArray {

phper/src/modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl Module {
198198
type_: 0,
199199
handle: null_mut(),
200200
module_number: 0,
201-
build_id: PHP_MODULE_BUILD_ID,
201+
build_id: phper_get_zend_module_build_id(),
202202
});
203203

204204
let entry = Box::into_raw(entry);

phper/src/objects.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,8 @@ impl<T: 'static> Object<T> {
8787
self.get_mut_property(name)
8888
}
8989

90-
pub fn duplicate_property(
91-
&mut self, name: impl AsRef<str>,
92-
) -> Result<EBox<ZVal>, NotRefCountedTypeError> {
93-
self.get_mut_property(name).duplicate()
94-
}
95-
9690
#[allow(clippy::useless_conversion)]
97-
fn get_mut_property(&mut self, name: impl AsRef<str>) -> &mut ZVal {
91+
pub fn get_mut_property(&mut self, name: impl AsRef<str>) -> &mut ZVal {
9892
let name = name.as_ref();
9993

10094
let prop = unsafe {

phper/src/strings.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Apis relate to [crate::sys::zend_string].
1212
1313
use crate::{alloc::EBox, sys::*};
14+
use phper_alloc::ToRefOwned;
1415
use std::{
1516
borrow::Borrow,
1617
convert::TryInto,
@@ -84,6 +85,17 @@ impl ToOwned for ZStr {
8485
}
8586
}
8687

88+
impl ToRefOwned for ZStr {
89+
type Owned = ZString;
90+
91+
fn to_ref_owned(&mut self) -> Self::Owned {
92+
unsafe {
93+
let ptr = phper_zend_string_copy(self.as_mut_ptr());
94+
ZString::from_raw(ptr)
95+
}
96+
}
97+
}
98+
8799
/// Like String, CString for [crate::sys::zend_string].
88100
pub struct ZString {
89101
inner: *mut ZStr,

0 commit comments

Comments
 (0)