Skip to content

Commit b8b12ca

Browse files
committed
Add Resource type.
1 parent 245cfef commit b8b12ca

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,7 @@ bool phper_z_refcounted_p(zval *zval_ptr) {
188188
zval *phper_execute_data_call_arg(zend_execute_data *execute_data, int index) {
189189
return ZEND_CALL_ARG(execute_data, index);
190190
}
191+
192+
int phper_z_res_handle_p(const zval *val) {
193+
return Z_RES_HANDLE_P(val);
194+
}

phper-sys/php_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ bool phper_z_refcounted_p(zval *zval_ptr);
6666

6767
zval *phper_execute_data_call_arg(zend_execute_data *execute_data, int index);
6868

69+
int phper_z_res_handle_p(const zval *val);
70+
6971
#endif //PHPER_PHP_WRAPPER_H

phper/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod ini;
1414
pub mod modules;
1515
pub mod objects;
1616
pub mod output;
17+
pub mod resources;
1718
pub mod strings;
1819
pub mod types;
1920
mod utils;

phper/src/resources.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Apis relate to [crate::sys::zend_resource].
2+
3+
use crate::sys::*;
4+
5+
/// Wrapper of [crate::sys::zend_resource].
6+
#[repr(transparent)]
7+
pub struct Resource {
8+
inner: zend_resource,
9+
}
10+
11+
impl Resource {
12+
/// # Safety
13+
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_resource) -> &'a mut Self {
14+
(ptr as *mut Self).as_mut().expect("ptr should not be null")
15+
}
16+
17+
pub fn handle(&self) -> i64 {
18+
self.inner.handle.into()
19+
}
20+
}

phper/src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ impl Type {
9595
get_base_type_by_raw(self.t) == IS_OBJECT
9696
}
9797

98+
pub const fn is_resource(self) -> bool {
99+
get_base_type_by_raw(self.t) == IS_RESOURCE
100+
}
101+
98102
pub const fn is_indirect(self) -> bool {
99103
self.t == IS_INDIRECT
100104
}

phper/src/values.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
errors::{NotRefCountedTypeError, Throwable, TypeError},
88
functions::{call_internal, ZendFunction},
99
objects::{Object, StatelessObject},
10+
resources::Resource,
1011
strings::ZendString,
1112
sys::*,
1213
types::Type,
@@ -307,6 +308,28 @@ impl Val {
307308
object
308309
}
309310

311+
pub fn as_resource(&self) -> crate::Result<&Resource> {
312+
if self.get_type().is_resource() {
313+
unsafe {
314+
let ptr = self.inner.value.res;
315+
Ok(Resource::from_mut_ptr(ptr))
316+
}
317+
} else {
318+
Err(self.must_be_type_error("resource"))
319+
}
320+
}
321+
322+
pub fn as_mut_resource(&mut self) -> crate::Result<&mut Resource> {
323+
if self.get_type().is_resource() {
324+
unsafe {
325+
let ptr = self.inner.value.res;
326+
Ok(Resource::from_mut_ptr(ptr))
327+
}
328+
} else {
329+
Err(self.must_be_type_error("resource"))
330+
}
331+
}
332+
310333
// TODO Error tip, not only for function arguments, should change.
311334
fn must_be_type_error(&self, expect_type: &str) -> crate::Error {
312335
match self.get_type_name() {

0 commit comments

Comments
 (0)