Skip to content

Commit cbf1b62

Browse files
authored
Adjust module api. (#99)
1 parent af6d07a commit cbf1b62

File tree

10 files changed

+145
-137
lines changed

10 files changed

+145
-137
lines changed

examples/complex/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use phper::{
1313
classes::{ClassEntity, Visibility},
1414
functions::Argument,
1515
ini::{ini_get, Policy},
16-
modules::{Module, ModuleContext},
16+
modules::Module,
1717
objects::StateObj,
1818
php_get_module,
1919
values::ZVal,
@@ -50,10 +50,10 @@ pub fn get_module() -> Module {
5050
);
5151

5252
// register hook functions
53-
module.on_module_init(|_: ModuleContext| true);
54-
module.on_module_shutdown(|_| true);
55-
module.on_request_init(|_| true);
56-
module.on_request_shutdown(|_| true);
53+
module.on_module_init(|| true);
54+
module.on_module_shutdown(|| true);
55+
module.on_request_init(|| true);
56+
module.on_request_shutdown(|| true);
5757

5858
// register functions
5959
module

examples/http-server/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ pub fn get_module() -> Module {
3737
let rt = Arc::new(rt);
3838
let rt_ = rt.clone();
3939

40-
module.on_module_init(move |_| {
40+
module.on_module_init(move || {
4141
let guard = rt_.enter();
4242
forget(guard);
4343
true
4444
});
45-
module.on_module_shutdown(move |_| {
45+
module.on_module_shutdown(move || {
4646
drop(rt);
4747
true
4848
});

phper-doc/doc/_06_module/_01_hooks/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn get_module() -> Module {
3535
env!("CARGO_PKG_AUTHORS"),
3636
);
3737
38-
module.on_module_init(|_| {
38+
module.on_module_init(|| {
3939
// Do somethings in `MINIT` stage.
4040
true
4141
});

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,7 @@ zend_internal_arg_info phper_zend_arg_info(bool pass_by_ref, const char *name) {
433433
info[0].name = name;
434434
return info[0];
435435
}
436+
437+
void *phper_z_ptr_p(const zval *zv) {
438+
return Z_PTR_P(zv);
439+
}

phper/src/classes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl ClassEntry {
125125
/// let std_class = ClassEntry::from_globals("stdClass").unwrap();
126126
/// let _obj = std_class.new_object([]).unwrap();
127127
/// ```
128-
pub fn from_globals<'a>(class_name: impl AsRef<str>) -> crate::Result<&'a Self> {
128+
pub fn from_globals(class_name: impl AsRef<str>) -> crate::Result<&'static Self> {
129129
let name = class_name.as_ref();
130130
let ptr: *mut Self = find_global_class_entry_ptr(name).cast();
131131
unsafe {
@@ -332,7 +332,7 @@ impl<T: 'static> ClassEntity<T> {
332332
}
333333

334334
#[allow(clippy::useless_conversion)]
335-
pub(crate) unsafe fn init(&mut self) -> *mut zend_class_entry {
335+
pub(crate) unsafe fn init(&self) -> *mut zend_class_entry {
336336
let parent: *mut zend_class_entry = self
337337
.parent
338338
.as_ref()

phper/src/constants.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Apis relate to [crate::sys::zend_constant].
1212
13-
use crate::{modules::ModuleContext, sys::*, types::Scalar};
13+
use crate::{sys::*, types::Scalar};
1414
use std::ffi::{c_char, c_int};
1515

1616
pub(crate) struct Constant {
@@ -26,22 +26,33 @@ impl Constant {
2626
}
2727
}
2828

29-
pub(crate) fn register(&self, module_context: &ModuleContext) {
29+
pub(crate) fn register(&self, module_number: c_int) {
3030
let name_ptr = self.name.as_ptr() as *const c_char;
3131
let name_len = self.name.len();
3232
let flags = (CONST_PERSISTENT | CONST_CS) as c_int;
33-
let num = module_context.module_number;
3433

3534
unsafe {
3635
match &self.value {
37-
Scalar::Null => zend_register_null_constant(name_ptr, name_len, flags, num),
38-
Scalar::Bool(b) => {
39-
zend_register_bool_constant(name_ptr, name_len, *b as zend_bool, flags, num)
36+
Scalar::Null => {
37+
zend_register_null_constant(name_ptr, name_len, flags, module_number)
4038
}
41-
Scalar::I64(i) => {
42-
zend_register_long_constant(name_ptr, name_len, *i as zend_long, flags, num)
39+
Scalar::Bool(b) => zend_register_bool_constant(
40+
name_ptr,
41+
name_len,
42+
*b as zend_bool,
43+
flags,
44+
module_number,
45+
),
46+
Scalar::I64(i) => zend_register_long_constant(
47+
name_ptr,
48+
name_len,
49+
*i as zend_long,
50+
flags,
51+
module_number,
52+
),
53+
Scalar::F64(f) => {
54+
zend_register_double_constant(name_ptr, name_len, *f, flags, module_number)
4355
}
44-
Scalar::F64(f) => zend_register_double_constant(name_ptr, name_len, *f, flags, num),
4556
Scalar::String(s) => {
4657
let s_ptr = s.as_ptr() as *mut u8;
4758
zend_register_stringl_constant(
@@ -50,7 +61,7 @@ impl Constant {
5061
s_ptr.cast(),
5162
s.len(),
5263
flags,
53-
num,
64+
module_number,
5465
)
5566
}
5667
Scalar::Bytes(s) => {
@@ -61,7 +72,7 @@ impl Constant {
6172
s_ptr.cast(),
6273
s.len(),
6374
flags,
64-
num,
75+
module_number,
6576
)
6677
}
6778
}

phper/src/ini.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
1313
use crate::sys::*;
1414
use std::{
15-
ffi::CStr,
16-
mem::{zeroed, ManuallyDrop},
15+
ffi::{c_int, CStr},
16+
mem::zeroed,
1717
os::raw::c_char,
1818
ptr::null_mut,
1919
str,
@@ -155,7 +155,7 @@ impl IniEntity {
155155
}
156156

157157
#[inline]
158-
pub(crate) fn entry(&mut self) -> zend_ini_entry_def {
158+
pub(crate) fn entry(&self) -> zend_ini_entry_def {
159159
create_ini_entry_ex(&self.name, &self.default_value, self.policy as u32)
160160
}
161161
}
@@ -194,16 +194,27 @@ fn create_ini_entry_ex(name: &str, default_value: &str, modifiable: u32) -> zend
194194
}
195195
}
196196

197-
pub(crate) unsafe fn entries(ini_entries: Vec<IniEntity>) -> *const zend_ini_entry_def {
197+
unsafe fn entries(ini_entries: &[IniEntity]) -> *const zend_ini_entry_def {
198198
let mut entries = Vec::with_capacity(ini_entries.len() + 1);
199199

200-
ini_entries.into_iter().for_each(|entity| {
200+
ini_entries.iter().for_each(|entity| {
201201
// Ini entity will exist throughout the whole application life cycle.
202-
let mut entity = ManuallyDrop::new(entity);
203202
entries.push(entity.entry());
204203
});
205204

206205
entries.push(zeroed::<zend_ini_entry_def>());
207206

208207
Box::into_raw(entries.into_boxed_slice()).cast()
209208
}
209+
210+
pub(crate) fn register(ini_entries: &[IniEntity], module_number: c_int) {
211+
unsafe {
212+
zend_register_ini_entries(entries(ini_entries), module_number);
213+
}
214+
}
215+
216+
pub(crate) fn unregister(module_number: c_int) {
217+
unsafe {
218+
zend_unregister_ini_entries(module_number);
219+
}
220+
}

0 commit comments

Comments
 (0)