Skip to content

Commit 2ea2065

Browse files
committed
Add zval types.
1 parent da0c91d commit 2ea2065

File tree

7 files changed

+100
-23
lines changed

7 files changed

+100
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ members = [
88

99
# internal
1010
"examples/simple",
11-
"examples/class",
11+
# "examples/class",
1212
]

examples/simple/src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use phper::zend::api::FunctionEntries;
77
use phper::zend::compile::InternalArgInfos;
88
use phper::zend::ini::IniEntryDefs;
99
use phper::zend::modules::ModuleEntry;
10-
use phper::zend::types::{ExecuteData, Val};
10+
use phper::zend::types::{ExecuteData, Val, SetVal, Value};
1111
use phper::{
1212
php_function, php_minit, php_minit_function, php_mshutdown, php_mshutdown_function,
1313
php_rinit_function, php_rshutdown_function,
@@ -66,12 +66,7 @@ fn m_info_simple(zend_module: *mut ::phper::sys::zend_module_entry) {
6666
}
6767

6868
#[php_function]
69-
pub fn test_simple(execute_data: ExecuteData, return_value: Val) {
70-
println!(
71-
"zif_test_simple success, num args: {}",
72-
execute_data.num_args()
73-
);
74-
69+
pub fn test_simple(execute_data: ExecuteData) -> impl SetVal {
7570
let mut a: *const c_char = null_mut();
7671
let mut a_len = 0;
7772
let mut b: *const c_char = null_mut();
@@ -87,14 +82,14 @@ pub fn test_simple(execute_data: ExecuteData, return_value: Val) {
8782
&mut b_len,
8883
) != ZEND_RESULT_CODE_SUCCESS
8984
{
90-
return;
85+
return Value::Null;
9186
}
9287

93-
println!(
94-
"echo param, a: {:?}, b: {:?}",
95-
CStr::from_ptr(a).to_str(),
96-
CStr::from_ptr(b).to_str()
97-
);
88+
Value::String(format!(
89+
"(a . b) = {}{}",
90+
CStr::from_ptr(a).to_str().unwrap(),
91+
CStr::from_ptr(b).to_str().unwrap(),
92+
))
9893
}
9994
}
10095

examples/simple/tests/confirm_compiled.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
ini_set("display_startup_errors", "On");
55
error_reporting(E_ALL);
66

7-
$functions = get_extension_funcs('simple');
8-
var_dump($functions);
7+
var_dump(get_extension_funcs('simple'));
98
var_dump(test_simple("aaa", "bbb"));

phper-macros/src/inner.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,9 @@ pub(crate) fn php_function(_attr: TokenStream, input: TokenStream) -> TokenStrea
9292
fn internal(#inputs) #ret {
9393
#body
9494
}
95-
let internal: fn(::phper::zend::types::ExecuteData, ::phper::zend::types::Val) = internal;
96-
internal(
97-
::phper::zend::types::ExecuteData::from_raw(execute_data),
98-
::phper::zend::types::Val::from_raw(return_value),
99-
);
95+
// let internal: fn(::phper::zend::types::ExecuteData) -> impl ::phper::zend::types::SetVal = internal;
96+
let value = internal(::phper::zend::types::ExecuteData::from_raw(execute_data));
97+
::phper::zend::types::SetVal::set_val(value, &mut ::phper::zend::types::Val::from_raw(return_value));
10098
}
10199
};
102100

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ void phper_zval_string(zval *return_value, const char *s) {
2020

2121
zend_uchar phper_zval_get_type(const zval* pz) {
2222
return zval_get_type(pz);
23+
}
24+
25+
void phper_zval_stringl(zval *return_value, const char *s, size_t len) {
26+
ZVAL_STRINGL(return_value, s, len);
2327
}

phper-sys/php_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ zend_string *zend_new_interned_string_(zend_string *str);
1010
zend_class_entry phper_init_class_entry(const char *class_name, const zend_function_entry *functions);
1111
void phper_zval_string(zval *return_value, const char *s);
1212
zend_uchar phper_zval_get_type(const zval* pz);
13+
void phper_zval_stringl(zval *return_value, const char *s, size_t len);
1314

1415
#endif //PHPER_PHP_WRAPPER_H

phper/src/zend/types.rs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::sys::{zend_execute_data, zval, zend_type, phper_zval_get_type, IS_STRING};
1+
use crate::sys::{zend_execute_data, zval, zend_type, phper_zval_get_type, IS_STRING, IS_NULL, IS_TRUE, IS_FALSE, phper_zval_stringl};
22
use std::ffi::CStr;
3+
use std::borrow::Cow;
34

45
pub struct ExecuteData {
56
raw: *mut zend_execute_data,
@@ -32,14 +33,31 @@ impl ExecuteData {
3233
}
3334
}
3435

36+
#[repr(u32)]
37+
pub enum ValType {
38+
UNDEF = crate::sys::IS_UNDEF,
39+
NULL = crate::sys::IS_NULL,
40+
FALSE = crate::sys::IS_FALSE,
41+
TRUE = crate::sys::IS_TRUE,
42+
LONG = crate::sys::IS_LONG,
43+
DOUBLE = crate::sys::IS_DOUBLE,
44+
STRING = crate::sys::IS_STRING,
45+
ARRAY = crate::sys::IS_ARRAY,
46+
OBJECT = crate::sys::IS_OBJECT,
47+
RESOURCE = crate::sys::IS_RESOURCE,
48+
REFERENCE = crate::sys::IS_REFERENCE,
49+
}
50+
3551
pub struct Val {
3652
raw: *mut zval,
3753
}
3854

3955
impl Val {
4056
#[inline]
4157
pub fn from_raw(val: *mut zval) -> Self {
42-
Self { raw: val }
58+
Self {
59+
raw: val,
60+
}
4361
}
4462

4563
pub fn get(&self) -> *mut zval {
@@ -55,4 +73,66 @@ impl Val {
5573
}
5674
}
5775
}
76+
77+
unsafe fn type_info(&mut self) -> &mut u32 {
78+
&mut (*self.raw).u1.type_info
79+
}
5880
}
81+
82+
pub trait SetVal {
83+
fn set_val(self, val: &mut Val);
84+
}
85+
86+
impl SetVal for () {
87+
fn set_val(self, val: &mut Val) {
88+
unsafe {
89+
*val.type_info() = IS_NULL;
90+
}
91+
}
92+
}
93+
94+
impl SetVal for bool {
95+
fn set_val(self, val: &mut Val) {
96+
unsafe {
97+
*val.type_info() = if self {
98+
IS_TRUE
99+
} else {
100+
IS_FALSE
101+
};
102+
}
103+
}
104+
}
105+
106+
impl SetVal for &str {
107+
fn set_val(self, val: &mut Val) {
108+
unsafe {
109+
phper_zval_stringl(val.raw, self.as_ptr().cast(), self.len());
110+
}
111+
}
112+
}
113+
114+
impl SetVal for String {
115+
fn set_val(self, val: &mut Val) {
116+
unsafe {
117+
phper_zval_stringl(val.raw, self.as_ptr().cast(), self.len());
118+
}
119+
}
120+
}
121+
122+
pub enum Value<'a> {
123+
Null,
124+
Bool(bool),
125+
Str(&'a str),
126+
String(String),
127+
}
128+
129+
impl SetVal for Value<'_> {
130+
fn set_val(self, val: &mut Val) {
131+
match self {
132+
Value::Null => ().set_val(val),
133+
Value::Bool(b) => b.set_val(val),
134+
Value::Str(s) => s.set_val(val),
135+
Value::String(s) => s.set_val(val),
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)