Skip to content

Commit cbfa3af

Browse files
committed
Refactor create zend_module_entry api.
1 parent 2100e54 commit cbfa3af

File tree

4 files changed

+149
-64
lines changed

4 files changed

+149
-64
lines changed

examples/hello-class/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use phper::{
1010
api::{FunctionEntries, ModuleGlobals},
1111
compile::{create_zend_arg_info, MultiInternalArgInfo, Visibility},
1212
ini::IniEntries,
13-
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
13+
modules::{ModuleArgs, ModuleEntry, ModuleEntryBuilder},
1414
types::{ClassEntry, ExecuteData, SetVal, Value},
1515
},
1616
zend_get_module,
@@ -130,18 +130,17 @@ static FUNCTION_ENTRIES: FunctionEntries<1> = FunctionEntries::new([zend_functio
130130
flags: 0,
131131
}]);
132132

133-
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(create_zend_module_entry(
133+
static MODULE_ENTRY: ModuleEntry = ModuleEntryBuilder::new(
134134
c_str_ptr!(env!("CARGO_PKG_NAME")),
135135
c_str_ptr!(env!("CARGO_PKG_VERSION")),
136-
FUNCTION_ENTRIES.as_ptr(),
137-
Some(php_minit!(module_init)),
138-
Some(php_mshutdown!(module_shutdown)),
139-
Some(php_rinit!(request_init)),
140-
Some(php_rshutdown!(request_shutdown)),
141-
Some(php_minfo!(module_info)),
142-
None,
143-
None,
144-
));
136+
)
137+
.functions(FUNCTION_ENTRIES.as_ptr())
138+
.module_startup_func(php_minit!(module_init))
139+
.module_shutdown_func(php_mshutdown!(module_shutdown))
140+
.request_startup_func(php_rinit!(request_init))
141+
.request_shutdown_func(php_rshutdown!(request_shutdown))
142+
.info_func(php_minfo!(module_info))
143+
.build();
145144

146145
#[zend_get_module]
147146
pub fn get_module() -> &'static ModuleEntry {

examples/hello/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use phper::{
1010
api::{FunctionEntries, ModuleGlobals},
1111
compile::{create_zend_arg_info, MultiInternalArgInfo},
1212
ini::IniEntries,
13-
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
13+
modules::{ModuleArgs, ModuleEntry, ModuleEntryBuilder},
1414
types::{ExecuteData, SetVal},
1515
},
1616
zend_get_module,
@@ -83,18 +83,17 @@ static FUNCTION_ENTRIES: FunctionEntries<1> = FunctionEntries::new([zend_functio
8383
flags: 0,
8484
}]);
8585

86-
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(create_zend_module_entry(
86+
static MODULE_ENTRY: ModuleEntry = ModuleEntryBuilder::new(
8787
c_str_ptr!(env!("CARGO_PKG_NAME")),
8888
c_str_ptr!(env!("CARGO_PKG_VERSION")),
89-
FUNCTION_ENTRIES.as_ptr(),
90-
Some(php_minit!(module_init)),
91-
Some(php_mshutdown!(module_shutdown)),
92-
Some(php_rinit!(request_init)),
93-
Some(php_rshutdown!(request_shutdown)),
94-
Some(php_minfo!(module_info)),
95-
None,
96-
None,
97-
));
89+
)
90+
.functions(FUNCTION_ENTRIES.as_ptr())
91+
.module_startup_func(php_minit!(module_init))
92+
.module_shutdown_func(php_mshutdown!(module_shutdown))
93+
.request_startup_func(php_rinit!(request_init))
94+
.request_shutdown_func(php_rshutdown!(request_shutdown))
95+
.info_func(php_minfo!(module_info))
96+
.build();
9897

9998
#[zend_get_module]
10099
pub fn get_module() -> &'static ModuleEntry {

examples/mini-curl/src/lib.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ use phper::{
1111
compile::{create_zend_arg_info, MultiInternalArgInfo, Visibility},
1212
errors::Level,
1313
ini::{create_ini_entry, IniEntries},
14-
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
14+
modules::{ModuleArgs, ModuleEntry, ModuleEntryBuilder},
1515
types::{ClassEntry, ExecuteData, ReturnValue, SetVal, Value},
1616
},
1717
zend_get_module,
1818
};
19-
use std::ptr::null;
2019

2120
static MINI_CURL_CE: ClassEntry = ClassEntry::new();
2221

@@ -145,18 +144,16 @@ pub fn mini_curl_destruct(execute_data: &mut ExecuteData) -> impl SetVal {
145144
ReturnValue::Null
146145
}
147146

148-
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(create_zend_module_entry(
147+
static MODULE_ENTRY: ModuleEntry = ModuleEntryBuilder::new(
149148
c_str_ptr!(env!("CARGO_PKG_NAME")),
150149
c_str_ptr!(env!("CARGO_PKG_VERSION")),
151-
null(),
152-
Some(php_minit!(module_init)),
153-
Some(php_mshutdown!(module_shutdown)),
154-
Some(php_rinit!(request_init)),
155-
Some(php_rshutdown!(request_shutdown)),
156-
Some(php_minfo!(module_info)),
157-
None,
158-
None,
159-
));
150+
)
151+
.module_startup_func(php_minit!(module_init))
152+
.module_shutdown_func(php_mshutdown!(module_shutdown))
153+
.request_startup_func(php_rinit!(request_init))
154+
.request_shutdown_func(php_rshutdown!(request_shutdown))
155+
.info_func(php_minfo!(module_info))
156+
.build();
160157

161158
#[zend_get_module]
162159
pub fn get_module() -> &'static ModuleEntry {

phper/src/zend/modules.rs

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
ptr::{null, null_mut},
1414
};
1515

16-
pub const fn create_zend_module_entry(
16+
pub struct ModuleEntryBuilder {
1717
name: *const c_char,
1818
version: *const c_char,
1919
functions: *const zend_function_entry,
@@ -24,35 +24,125 @@ pub const fn create_zend_module_entry(
2424
info_func: Option<unsafe extern "C" fn(*mut zend_module_entry)>,
2525
globals_ctor: Option<unsafe extern "C" fn(global: *mut c_void)>,
2626
globals_dtor: Option<unsafe extern "C" fn(global: *mut c_void)>,
27-
) -> zend_module_entry {
28-
zend_module_entry {
29-
size: size_of::<zend_module_entry>() as c_ushort,
30-
zend_api: ZEND_MODULE_API_NO as c_uint,
31-
zend_debug: ZEND_DEBUG as c_uchar,
32-
zts: USING_ZTS as c_uchar,
33-
ini_entry: null(),
34-
deps: null(),
35-
name,
36-
functions,
37-
module_startup_func,
38-
module_shutdown_func,
39-
request_startup_func,
40-
request_shutdown_func,
41-
info_func,
42-
version,
43-
globals_size: 0usize,
44-
#[cfg(phper_zts)]
45-
globals_id_ptr: std::ptr::null_mut(),
46-
#[cfg(not(phper_zts))]
47-
globals_ptr: std::ptr::null_mut(),
48-
globals_ctor,
49-
globals_dtor,
50-
post_deactivate_func: None,
51-
module_started: 0,
52-
type_: 0,
53-
handle: null_mut(),
54-
module_number: 0,
55-
build_id: PHP_MODULE_BUILD_ID,
27+
}
28+
29+
impl ModuleEntryBuilder {
30+
pub const fn new(name: *const c_char, version: *const c_char) -> Self {
31+
Self {
32+
name,
33+
version,
34+
functions: null(),
35+
module_startup_func: None,
36+
module_shutdown_func: None,
37+
request_startup_func: None,
38+
request_shutdown_func: None,
39+
info_func: None,
40+
globals_ctor: None,
41+
globals_dtor: None,
42+
}
43+
}
44+
45+
pub const fn functions(self, functions: *const zend_function_entry) -> Self {
46+
Self { functions, ..self }
47+
}
48+
49+
pub const fn module_startup_func(
50+
self,
51+
module_startup_func: unsafe extern "C" fn(c_int, c_int) -> c_int,
52+
) -> Self {
53+
Self {
54+
module_startup_func: Some(module_startup_func),
55+
..self
56+
}
57+
}
58+
59+
pub const fn module_shutdown_func(
60+
self,
61+
module_shutdown_func: unsafe extern "C" fn(c_int, c_int) -> c_int,
62+
) -> Self {
63+
Self {
64+
module_shutdown_func: Some(module_shutdown_func),
65+
..self
66+
}
67+
}
68+
69+
pub const fn request_startup_func(
70+
self,
71+
request_startup_func: unsafe extern "C" fn(c_int, c_int) -> c_int,
72+
) -> Self {
73+
Self {
74+
request_startup_func: Some(request_startup_func),
75+
..self
76+
}
77+
}
78+
79+
pub const fn request_shutdown_func(
80+
self,
81+
request_shutdown_func: unsafe extern "C" fn(c_int, c_int) -> c_int,
82+
) -> Self {
83+
Self {
84+
request_shutdown_func: Some(request_shutdown_func),
85+
..self
86+
}
87+
}
88+
89+
pub const fn info_func(self, info_func: unsafe extern "C" fn(*mut zend_module_entry)) -> Self {
90+
Self {
91+
info_func: Some(info_func),
92+
..self
93+
}
94+
}
95+
96+
pub const fn globals_ctor(
97+
self,
98+
globals_ctor: unsafe extern "C" fn(global: *mut c_void),
99+
) -> Self {
100+
Self {
101+
globals_ctor: Some(globals_ctor),
102+
..self
103+
}
104+
}
105+
106+
pub const fn globals_dtor(
107+
self,
108+
globals_dtor: unsafe extern "C" fn(global: *mut c_void),
109+
) -> Self {
110+
Self {
111+
globals_dtor: Some(globals_dtor),
112+
..self
113+
}
114+
}
115+
116+
pub const fn build(self) -> ModuleEntry {
117+
ModuleEntry::new(zend_module_entry {
118+
size: size_of::<zend_module_entry>() as c_ushort,
119+
zend_api: ZEND_MODULE_API_NO as c_uint,
120+
zend_debug: ZEND_DEBUG as c_uchar,
121+
zts: USING_ZTS as c_uchar,
122+
ini_entry: null(),
123+
deps: null(),
124+
name: self.name,
125+
functions: self.functions,
126+
module_startup_func: self.module_startup_func,
127+
module_shutdown_func: self.module_shutdown_func,
128+
request_startup_func: self.request_startup_func,
129+
request_shutdown_func: self.request_shutdown_func,
130+
info_func: self.info_func,
131+
version: self.version,
132+
globals_size: 0usize,
133+
#[cfg(phper_zts)]
134+
globals_id_ptr: std::ptr::null_mut(),
135+
#[cfg(not(phper_zts))]
136+
globals_ptr: std::ptr::null_mut(),
137+
globals_ctor: self.globals_ctor,
138+
globals_dtor: self.globals_dtor,
139+
post_deactivate_func: None,
140+
module_started: 0,
141+
type_: 0,
142+
handle: null_mut(),
143+
module_number: 0,
144+
build_id: PHP_MODULE_BUILD_ID,
145+
})
56146
}
57147
}
58148

0 commit comments

Comments
 (0)