Skip to content

Commit 4f397c7

Browse files
committed
Adjust module.
1 parent 9a026de commit 4f397c7

File tree

4 files changed

+89
-39
lines changed

4 files changed

+89
-39
lines changed

examples/simple/src/lib.rs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use phper::{c_str_ptr, php_fn, ebox};
22
use phper::sys::{ZEND_RESULT_CODE_SUCCESS, zend_parse_parameters, zend_internal_arg_info, zend_function_entry, PHP_INI_SYSTEM};
33
use phper::sys::{zend_ini_entry_def, zend_module_entry, zend_register_ini_entries, zend_unregister_ini_entries, OnUpdateBool};
44
use phper::sys::{OnUpdateString};
5-
use phper::zend::api::{FunctionEntries, ModuleGlobals};
6-
use phper::zend::compile::InternalArgInfos;
5+
use phper::zend::api::{FunctionEntries, ModuleGlobals, function_entry_end};
6+
use phper::zend::compile::{InternalArgInfos, internal_arg_info_begin};
77
use phper::zend::ini::{IniEntryDefs, ini_entry_def_end};
8-
use phper::zend::modules::ModuleEntry;
8+
use phper::zend::modules::{ModuleEntry, create_zend_module_entry};
99
use phper::zend::types::{ExecuteData, Val, SetVal, Value};
1010
use phper::{
1111
php_function, php_minit, php_minit_function, php_mshutdown, php_mshutdown_function,
@@ -18,6 +18,7 @@ use std::mem::{size_of, transmute};
1818
use std::os::raw::{c_char, c_int, c_uchar, c_uint, c_ushort};
1919
use std::ptr::{null, null_mut};
2020
use phper::zend::exceptions::MyException;
21+
use phper::sys::{php_info_print_table_start, php_info_print_table_row, php_info_print_table_end};
2122

2223
static SIMPLE_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
2324
static SIMPLE_TEXT: ModuleGlobals<*const c_char> = ModuleGlobals::new(null());
@@ -56,6 +57,12 @@ fn r_shutdown_simple(type_: c_int, module_number: c_int) -> bool {
5657

5758
#[php_minfo_function]
5859
fn m_info_simple(zend_module: *mut ::phper::sys::zend_module_entry) {
60+
unsafe {
61+
php_info_print_table_start();
62+
php_info_print_table_row(2, c_str_ptr!("simple.enable"), format!("{}\0", *SIMPLE_ENABLE.get()).as_ptr());
63+
php_info_print_table_row(2, c_str_ptr!("simple.text"), format!("{}\0", CStr::from_ptr((*SIMPLE_TEXT.get())).to_str().unwrap()).as_ptr());
64+
php_info_print_table_end();
65+
}
5966
}
6067

6168
#[php_function]
@@ -90,12 +97,7 @@ pub fn test_simple(execute_data: ExecuteData) -> impl SetVal {
9097
}
9198

9299
static ARG_INFO_TEST_SIMPLE: InternalArgInfos<3> = InternalArgInfos::new([
93-
zend_internal_arg_info {
94-
name: 2 as *const _,
95-
type_: 0,
96-
pass_by_reference: 0,
97-
is_variadic: 0,
98-
},
100+
internal_arg_info_begin(2, false),
99101
zend_internal_arg_info {
100102
name: c_str_ptr!("a"),
101103
type_: 0,
@@ -118,38 +120,21 @@ static FUNCTION_ENTRIES: FunctionEntries<2> = FunctionEntries::new([
118120
num_args: 2,
119121
flags: 0,
120122
},
121-
unsafe { transmute([0u8; size_of::<zend_function_entry>()]) },
123+
function_entry_end(),
122124
]);
123125

124-
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(zend_module_entry {
125-
size: size_of::<zend_module_entry>() as c_ushort,
126-
zend_api: phper::sys::ZEND_MODULE_API_NO as c_uint,
127-
zend_debug: phper::sys::ZEND_DEBUG as c_uchar,
128-
zts: phper::sys::USING_ZTS as c_uchar,
129-
ini_entry: std::ptr::null(),
130-
deps: std::ptr::null(),
131-
name: c_str_ptr!(env!("CARGO_PKG_NAME")),
132-
functions: FUNCTION_ENTRIES.get(),
133-
module_startup_func: Some(php_minit!(m_init_simple)),
134-
module_shutdown_func: Some(php_mshutdown!(m_shutdown_simple)),
135-
request_startup_func: Some(php_rinit!(r_init_simple)),
136-
request_shutdown_func: Some(php_rshutdown!(r_shutdown_simple)),
137-
info_func: Some(php_minfo!(m_info_simple)),
138-
version: c_str_ptr!(env!("CARGO_PKG_VERSION")),
139-
globals_size: 0usize,
140-
#[cfg(phper_zts)]
141-
globals_id_ptr: std::ptr::null_mut(),
142-
#[cfg(not(phper_zts))]
143-
globals_ptr: std::ptr::null_mut(),
144-
globals_ctor: None,
145-
globals_dtor: None,
146-
post_deactivate_func: None,
147-
module_started: 0,
148-
type_: 0,
149-
handle: null_mut(),
150-
module_number: 0,
151-
build_id: phper::sys::PHP_MODULE_BUILD_ID,
152-
});
126+
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(create_zend_module_entry(
127+
c_str_ptr!(env!("CARGO_PKG_NAME")),
128+
c_str_ptr!(env!("CARGO_PKG_VERSION")),
129+
FUNCTION_ENTRIES.get(),
130+
Some(php_minit!(m_init_simple)),
131+
Some(php_mshutdown!(m_shutdown_simple)),
132+
Some(php_rinit!(r_init_simple)),
133+
Some(php_rshutdown!(r_shutdown_simple)),
134+
Some(php_minfo!(m_info_simple)),
135+
None,
136+
None,
137+
));
153138

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

phper/src/zend/api.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use std::cell::UnsafeCell;
33
use std::os::raw::{c_char, c_int, c_void};
44
use crate::zend::ini::Mh;
55
use std::ptr::null_mut;
6+
use std::mem::{transmute, size_of};
7+
8+
pub const fn function_entry_end() -> zend_function_entry {
9+
unsafe { transmute([0u8; size_of::<zend_function_entry>()]) }
10+
}
611

712
pub struct ModuleGlobals<T: 'static> {
813
inner: UnsafeCell<T>,

phper/src/zend/compile.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
use crate::sys::zend_internal_arg_info;
22
use std::cell::UnsafeCell;
33

4+
pub const fn internal_arg_info_begin(required_num_args: usize, return_reference: bool) -> zend_internal_arg_info {
5+
zend_internal_arg_info {
6+
name: required_num_args as *const _,
7+
type_: 0,
8+
pass_by_reference: return_reference as _,
9+
is_variadic: 0,
10+
}
11+
}
12+
413
pub struct InternalArgInfos<const N: usize> {
514
inner: UnsafeCell<[zend_internal_arg_info; N]>,
615
}

phper/src/zend/modules.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
11
use crate::sys::zend_module_entry;
22
use std::cell::UnsafeCell;
3+
use std::mem::size_of;
4+
use std::os::raw::{c_ushort, c_uint, c_uchar, c_char, c_int, c_void};
5+
use crate::sys::ZEND_MODULE_API_NO;
6+
use crate::sys::ZEND_DEBUG;
7+
use crate::sys::USING_ZTS;
8+
use crate::sys::PHP_MODULE_BUILD_ID;
9+
use crate::sys::zend_function_entry;
10+
use std::ptr::{null, null_mut};
11+
12+
pub const fn create_zend_module_entry(
13+
name: *const c_char,
14+
version: *const c_char,
15+
functions: *const zend_function_entry,
16+
module_startup_func: Option<unsafe extern "C" fn(c_int, c_int) -> c_int>,
17+
module_shutdown_func: Option<unsafe extern "C" fn(c_int, c_int) -> c_int>,
18+
request_startup_func: Option<unsafe extern "C" fn(c_int, c_int) -> c_int>,
19+
request_shutdown_func: Option<unsafe extern "C" fn(c_int, c_int) -> c_int>,
20+
info_func: Option<unsafe extern "C" fn(*mut zend_module_entry)>,
21+
globals_ctor: Option<unsafe extern "C" fn(global: *mut c_void)>,
22+
globals_dtor: Option<unsafe extern "C" fn(global: *mut c_void)>,
23+
) -> zend_module_entry {
24+
zend_module_entry {
25+
size: size_of::<zend_module_entry>() as c_ushort,
26+
zend_api: ZEND_MODULE_API_NO as c_uint,
27+
zend_debug: ZEND_DEBUG as c_uchar,
28+
zts: USING_ZTS as c_uchar,
29+
ini_entry: null(),
30+
deps: null(),
31+
name,
32+
functions,
33+
module_startup_func,
34+
module_shutdown_func,
35+
request_startup_func,
36+
request_shutdown_func,
37+
info_func,
38+
version,
39+
globals_size: 0usize,
40+
#[cfg(phper_zts)]
41+
globals_id_ptr: std::ptr::null_mut(),
42+
#[cfg(not(phper_zts))]
43+
globals_ptr: std::ptr::null_mut(),
44+
globals_ctor,
45+
globals_dtor,
46+
post_deactivate_func: None,
47+
module_started: 0,
48+
type_: 0,
49+
handle: null_mut(),
50+
module_number: 0,
51+
build_id: PHP_MODULE_BUILD_ID,
52+
}
53+
}
354

455
pub struct ModuleEntry {
556
raw: UnsafeCell<zend_module_entry>,

0 commit comments

Comments
 (0)