Skip to content

Commit ec85609

Browse files
committed
Refactor array design.
1 parent 363649a commit ec85609

File tree

6 files changed

+81
-85
lines changed

6 files changed

+81
-85
lines changed

examples/simple/src/lib.rs

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ use phper::{
88
OnUpdateString, PHP_INI_SYSTEM, ZEND_ACC_PUBLIC,
99
},
1010
zend::{
11-
api::{function_entry_end, FunctionEntries, ModuleGlobals},
12-
compile::{internal_arg_info_begin, MultiInternalArgInfo},
13-
ini::{ini_entry_def_end, IniEntryDefs},
11+
api::{FunctionEntries, ModuleGlobals},
12+
compile::MultiInternalArgInfo,
13+
ini::IniEntries,
1414
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
1515
types::{ClassEntry, ExecuteData, SetVal, Val},
1616
},
1717
zend_get_module,
1818
};
1919
use std::{
20-
mem::{size_of, transmute},
2120
os::raw::c_char,
2221
ptr::{null, null_mut},
2322
};
@@ -27,10 +26,9 @@ static MY_CLASS_CE: ClassEntry = ClassEntry::new();
2726
static SIMPLE_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
2827
static SIMPLE_TEXT: ModuleGlobals<*const c_char> = ModuleGlobals::new(null());
2928

30-
static INI_ENTRIES: IniEntryDefs<3> = IniEntryDefs::new([
29+
static INI_ENTRIES: IniEntries<2> = IniEntries::new([
3130
SIMPLE_ENABLE.create_ini_entry_def("simple.enable", "1", Some(OnUpdateBool), PHP_INI_SYSTEM),
3231
SIMPLE_TEXT.create_ini_entry_def("simple.text", "", Some(OnUpdateString), PHP_INI_SYSTEM),
33-
ini_entry_def_end(),
3432
]);
3533

3634
#[php_minit_function]
@@ -96,58 +94,49 @@ pub fn test_simple(execute_data: ExecuteData) -> impl SetVal {
9694
})
9795
}
9896

99-
static ARG_INFO_TEST_SIMPLE: MultiInternalArgInfo<3> = MultiInternalArgInfo::new([
100-
internal_arg_info_begin(2, false),
101-
zend_internal_arg_info {
102-
name: c_str_ptr!("a"),
103-
type_: 0,
104-
pass_by_reference: 0,
105-
is_variadic: 0,
106-
},
107-
zend_internal_arg_info {
108-
name: c_str_ptr!("b"),
109-
type_: 0,
110-
pass_by_reference: 0,
111-
is_variadic: 0,
112-
},
113-
]);
114-
115-
static FUNCTION_ENTRIES: FunctionEntries<2> = FunctionEntries::new([
116-
zend_function_entry {
117-
fname: c_str_ptr!("test_simple"),
118-
handler: Some(php_fn!(test_simple)),
119-
arg_info: ARG_INFO_TEST_SIMPLE.get(),
120-
num_args: 2,
121-
flags: 0,
122-
},
123-
function_entry_end(),
124-
]);
125-
126-
static ARG_INFO_MY_CLASS_FOO: MultiInternalArgInfo<2> = MultiInternalArgInfo::new([
127-
zend_internal_arg_info {
128-
name: 1 as *const _,
129-
type_: 0,
130-
pass_by_reference: 0,
131-
is_variadic: 0,
132-
},
133-
zend_internal_arg_info {
97+
static ARG_INFO_TEST_SIMPLE: MultiInternalArgInfo<2> = MultiInternalArgInfo::new(
98+
[
99+
zend_internal_arg_info {
100+
name: c_str_ptr!("a"),
101+
type_: 0,
102+
pass_by_reference: 0,
103+
is_variadic: 0,
104+
},
105+
zend_internal_arg_info {
106+
name: c_str_ptr!("b"),
107+
type_: 0,
108+
pass_by_reference: 0,
109+
is_variadic: 0,
110+
},
111+
],
112+
false,
113+
);
114+
115+
static FUNCTION_ENTRIES: FunctionEntries<1> = FunctionEntries::new([zend_function_entry {
116+
fname: c_str_ptr!("test_simple"),
117+
handler: Some(php_fn!(test_simple)),
118+
arg_info: ARG_INFO_TEST_SIMPLE.as_ptr(),
119+
num_args: 2,
120+
flags: 0,
121+
}]);
122+
123+
static ARG_INFO_MY_CLASS_FOO: MultiInternalArgInfo<1> = MultiInternalArgInfo::new(
124+
[zend_internal_arg_info {
134125
name: c_str_ptr!("prefix"),
135126
type_: 0,
136127
pass_by_reference: 0,
137128
is_variadic: 0,
138-
},
139-
]);
140-
141-
static MY_CLASS_METHODS: FunctionEntries<2> = FunctionEntries::new([
142-
zend_function_entry {
143-
fname: c_str_ptr!("foo"),
144-
handler: Some(php_fn!(my_class_foo)),
145-
arg_info: ARG_INFO_MY_CLASS_FOO.get(),
146-
num_args: 1,
147-
flags: 0,
148-
},
149-
unsafe { transmute([0u8; size_of::<zend_function_entry>()]) },
150-
]);
129+
}],
130+
false,
131+
);
132+
133+
static MY_CLASS_METHODS: FunctionEntries<1> = FunctionEntries::new([zend_function_entry {
134+
fname: c_str_ptr!("foo"),
135+
handler: Some(php_fn!(my_class_foo)),
136+
arg_info: ARG_INFO_MY_CLASS_FOO.as_ptr(),
137+
num_args: 1,
138+
flags: 0,
139+
}]);
151140

152141
#[php_function]
153142
pub fn my_class_foo(execute_data: ExecuteData) -> impl SetVal {

phper-build/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ pub fn register_configures() {
2121
);
2222
println!(
2323
"cargo:rustc-cfg=phper_php_version=\"{}.{}\"",
24-
PHP_MAJOR_VERSION,
25-
PHP_MINOR_VERSION,
24+
PHP_MAJOR_VERSION, PHP_MINOR_VERSION,
2625
);
2726

2827
if PHP_DEBUG > 0 {

phper/src/zend/api.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
ptr::null_mut,
99
};
1010

11-
pub const fn function_entry_end() -> zend_function_entry {
11+
const fn function_entry_end() -> zend_function_entry {
1212
unsafe { transmute([0u8; size_of::<zend_function_entry>()]) }
1313
}
1414

@@ -67,14 +67,17 @@ impl<T: Copy + 'static> ModuleGlobals<T> {
6767

6868
unsafe impl<T: 'static> Sync for ModuleGlobals<T> {}
6969

70+
#[repr(C)]
71+
struct ZendFunctionEntriesWithEnd<const N: usize>([zend_function_entry; N], zend_function_entry);
72+
7073
pub struct FunctionEntries<const N: usize> {
71-
inner: Cell<[zend_function_entry; N]>,
74+
inner: Cell<ZendFunctionEntriesWithEnd<N>>,
7275
}
7376

7477
impl<const N: usize> FunctionEntries<N> {
7578
pub const fn new(inner: [zend_function_entry; N]) -> Self {
7679
Self {
77-
inner: Cell::new(inner),
80+
inner: Cell::new(ZendFunctionEntriesWithEnd(inner, function_entry_end())),
7881
}
7982
}
8083

phper/src/zend/compile.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
use crate::sys::zend_internal_arg_info;
2-
use std::cell::UnsafeCell;
1+
use crate::sys::{zend_internal_arg_info, zend_uchar};
2+
use std::cell::Cell;
33

4-
pub const fn internal_arg_info_begin(
5-
required_num_args: usize,
6-
return_reference: bool,
7-
) -> zend_internal_arg_info {
8-
zend_internal_arg_info {
9-
name: required_num_args as *const _,
10-
type_: 0,
11-
pass_by_reference: return_reference as _,
12-
is_variadic: 0,
13-
}
14-
}
4+
#[repr(C)]
5+
struct ZendInternalArgInfosWithEnd<const N: usize>(
6+
zend_internal_arg_info,
7+
[zend_internal_arg_info; N],
8+
);
159

1610
pub struct MultiInternalArgInfo<const N: usize> {
17-
inner: UnsafeCell<[zend_internal_arg_info; N]>,
11+
inner: Cell<ZendInternalArgInfosWithEnd<N>>,
1812
}
1913

2014
impl<const N: usize> MultiInternalArgInfo<N> {
21-
pub const fn new(inner: [zend_internal_arg_info; N]) -> Self {
15+
pub const fn new(inner: [zend_internal_arg_info; N], return_reference: bool) -> Self {
2216
Self {
23-
inner: UnsafeCell::new(inner),
17+
inner: Cell::new(ZendInternalArgInfosWithEnd(
18+
zend_internal_arg_info {
19+
name: inner.len() as *const _,
20+
type_: 0,
21+
pass_by_reference: return_reference as zend_uchar,
22+
is_variadic: 0,
23+
},
24+
inner,
25+
)),
2426
}
2527
}
2628

27-
pub const fn get(&self) -> *const zend_internal_arg_info {
28-
self.inner.get().cast()
29+
pub const fn as_ptr(&self) -> *const zend_internal_arg_info {
30+
self.inner.as_ptr().cast()
2931
}
3032
}
3133

phper/src/zend/ini.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ pub type Mh = unsafe extern "C" fn(
1414
c_int,
1515
) -> c_int;
1616

17-
pub const fn ini_entry_def_end() -> zend_ini_entry_def {
17+
const fn ini_entry_def_end() -> zend_ini_entry_def {
1818
unsafe { transmute([0u8; size_of::<zend_ini_entry_def>()]) }
1919
}
2020

21-
pub struct IniEntryDefs<const N: usize> {
22-
inner: Cell<[zend_ini_entry_def; N]>,
21+
#[repr(C)]
22+
struct ZendIniEntriesWithEnd<const N: usize>([zend_ini_entry_def; N], zend_ini_entry_def);
23+
24+
pub struct IniEntries<const N: usize> {
25+
inner: Cell<ZendIniEntriesWithEnd<N>>,
2326
}
2427

25-
impl<const N: usize> IniEntryDefs<N> {
28+
impl<const N: usize> IniEntries<N> {
2629
pub const fn new(inner: [zend_ini_entry_def; N]) -> Self {
2730
Self {
28-
inner: Cell::new(inner),
31+
inner: Cell::new(ZendIniEntriesWithEnd(inner, ini_entry_def_end())),
2932
}
3033
}
3134

@@ -35,4 +38,4 @@ impl<const N: usize> IniEntryDefs<N> {
3538
}
3639
}
3740

38-
unsafe impl<const N: usize> Sync for IniEntryDefs<N> {}
41+
unsafe impl<const N: usize> Sync for IniEntries<N> {}

phper/src/zend/modules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
zend_unregister_ini_entries, PHP_MODULE_BUILD_ID, USING_ZTS, ZEND_DEBUG,
55
ZEND_MODULE_API_NO,
66
},
7-
zend::ini::IniEntryDefs,
7+
zend::ini::IniEntries,
88
};
99
use std::{
1010
cell::Cell,
@@ -92,7 +92,7 @@ impl ModuleArgs {
9292
}
9393
}
9494

95-
pub fn register_ini_entries<const N: usize>(&self, ini_entries: &IniEntryDefs<N>) {
95+
pub fn register_ini_entries<const N: usize>(&self, ini_entries: &IniEntries<N>) {
9696
unsafe {
9797
zend_register_ini_entries(ini_entries.as_ptr(), self.module_number);
9898
}

0 commit comments

Comments
 (0)