Skip to content

Commit ac15b5e

Browse files
committed
Design module args api.
1 parent 7cd96a7 commit ac15b5e

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

examples/simple/src/lib.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use phper::sys::{OnUpdateString, zend_class_entry, zend_register_internal_class,
55
use phper::zend::api::{FunctionEntries, ModuleGlobals, function_entry_end};
66
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, create_zend_module_entry};
8+
use phper::zend::modules::{ModuleEntry, create_zend_module_entry, ModuleArgs};
99
use phper::zend::types::{ExecuteData, Val, SetVal, Value, ClassEntry};
1010
use phper::{
1111
php_function, php_minit, php_minit_function, php_mshutdown, php_mshutdown_function,
@@ -32,40 +32,35 @@ static INI_ENTRIES: IniEntryDefs<3> = IniEntryDefs::new([
3232
]);
3333

3434
#[php_minit_function]
35-
fn m_init_simple(type_: c_int, module_number: c_int) -> bool {
36-
unsafe {
37-
zend_register_ini_entries(INI_ENTRIES.as_ptr(), module_number);
38-
39-
MY_CLASS_CE.init(c_str_ptr!("MyClass"), &MY_CLASS_METHODS);
40-
MY_CLASS_CE.declare_property("foo", 3, ZEND_ACC_PUBLIC);
41-
}
35+
fn m_init_simple(args: ModuleArgs) -> bool {
36+
args.register_ini_entries(&INI_ENTRIES);
37+
MY_CLASS_CE.init(c_str_ptr!("MyClass"), &MY_CLASS_METHODS);
38+
MY_CLASS_CE.declare_property("foo", 3, ZEND_ACC_PUBLIC);
4239
true
4340
}
4441

4542
#[php_mshutdown_function]
46-
fn m_shutdown_simple(type_: c_int, module_number: c_int) -> bool {
47-
unsafe {
48-
zend_unregister_ini_entries(module_number);
49-
}
43+
fn m_shutdown_simple(args: ModuleArgs) -> bool {
44+
args.unregister_ini_entries();
5045
true
5146
}
5247

5348
#[php_rinit_function]
54-
fn r_init_simple(type_: c_int, module_number: c_int) -> bool {
49+
fn r_init_simple(args: ModuleArgs) -> bool {
5550
true
5651
}
5752

5853
#[php_rshutdown_function]
59-
fn r_shutdown_simple(type_: c_int, module_number: c_int) -> bool {
54+
fn r_shutdown_simple(args: ModuleArgs) -> bool {
6055
true
6156
}
6257

6358
#[php_minfo_function]
6459
fn m_info_simple(zend_module: *mut ::phper::sys::zend_module_entry) {
6560
unsafe {
6661
php_info_print_table_start();
67-
php_info_print_table_row(2, if SIMPLE_ENABLE.get() { c_str_ptr!("1") } else { c_str_ptr!("0") });
68-
php_info_print_table_row(2, c_str_ptr!("simple.text"), SIMPLE_TEXT.as_ptr());
62+
php_info_print_table_row(2, c_str_ptr!("simple.enable"), if SIMPLE_ENABLE.get() { c_str_ptr!("1") } else { c_str_ptr!("0") });
63+
php_info_print_table_row(2, c_str_ptr!("simple.text"), SIMPLE_TEXT.get());
6964
php_info_print_table_end();
7065
}
7166
}

phper-macros/src/inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ pub(crate) fn hook_fn(input: TokenStream, prefix: impl ToString) -> TokenStream
3030
#body
3131
}
3232

33-
let internal: fn(::std::os::raw::c_int, ::std::os::raw::c_int) -> bool = internal;
33+
let internal: fn(::phper::zend::modules::ModuleArgs) -> bool = internal;
3434

35-
if internal(type_, module_number) {
35+
if internal(::phper::zend::modules::ModuleArgs::new(type_, module_number)) {
3636
::phper::sys::ZEND_RESULT_CODE_SUCCESS
3737
} else {
3838
::phper::sys::ZEND_RESULT_CODE_FAILURE

phper/src/zend/modules.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use crate::sys::USING_ZTS;
88
use crate::sys::PHP_MODULE_BUILD_ID;
99
use crate::sys::zend_function_entry;
1010
use std::ptr::{null, null_mut};
11+
use crate::zend::ini::IniEntryDefs;
12+
use crate::sys::zend_register_ini_entries;
13+
use crate::sys::zend_unregister_ini_entries;
1114

1215
pub const fn create_zend_module_entry(
1316
name: *const c_char,
@@ -67,3 +70,30 @@ impl ModuleEntry {
6770
}
6871

6972
unsafe impl Sync for ModuleEntry {}
73+
74+
pub struct ModuleArgs {
75+
type_: c_int,
76+
module_number: c_int,
77+
}
78+
79+
impl ModuleArgs {
80+
pub const fn new(type_: c_int, module_number: c_int) -> Self {
81+
Self {
82+
type_,
83+
module_number
84+
}
85+
}
86+
87+
pub fn register_ini_entries<const N: usize>(&self, ini_entries: &IniEntryDefs<N>) {
88+
unsafe {
89+
zend_register_ini_entries(ini_entries.as_ptr(), self.module_number);
90+
}
91+
}
92+
93+
pub fn unregister_ini_entries(&self) {
94+
unsafe {
95+
zend_unregister_ini_entries(self.module_number);
96+
}
97+
}
98+
}
99+

0 commit comments

Comments
 (0)