Skip to content

Commit e2c28de

Browse files
committed
Add ini-related macros.
1 parent 084ec7d commit e2c28de

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ Then create a PHP extension project from the [template](https://github.com/jmjoy
2020
cargo generate --git https://github.com/jmjoy/phper-ext-skel.git
2121
```
2222

23+
## Notice
24+
25+
Now the library don't support `ZTS`, the template in use `thread_local` instead.
26+

phper-macros/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use proc_macro2::{Ident, Span};
66
use quote::quote;
77
use syn::punctuated::Punctuated;
88
use syn::token::Comma;
9-
use syn::{parse_macro_input, parse_str, FnArg, ItemFn};
9+
use syn::{parse_macro_input, parse_str, FnArg, ItemFn, NestedMeta, AttributeArgs, Meta};
1010

1111
#[proc_macro_attribute]
1212
pub fn php_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
@@ -50,6 +50,13 @@ pub fn php_minit_function(_attr: TokenStream, input: TokenStream) -> TokenStream
5050
#[no_mangle]
5151
#(#attrs)*
5252
pub extern "C" fn #name(#inputs) -> ::std::os::raw::c_int {
53+
unsafe {
54+
::phper_sys::zend_register_ini_entries(
55+
INI_ENTRIES.with(|i| i.as_ptr() as *const ::phper_sys::zend_ini_entry_def),
56+
module_number
57+
);
58+
}
59+
5360
let f = |#inner_inputs| #ret {
5461
#body
5562
};
@@ -80,6 +87,10 @@ pub fn php_mshutdown_function(_attr: TokenStream, input: TokenStream) -> TokenSt
8087
#[no_mangle]
8188
#(#attrs)*
8289
pub extern "C" fn #name(#inputs) -> ::std::os::raw::c_int {
90+
unsafe {
91+
::phper_sys::zend_unregister_ini_entries(module_number);
92+
}
93+
8394
let f = |#inner_inputs| #ret {
8495
#body
8596
};
@@ -180,6 +191,11 @@ pub fn php_minfo_function(_attr: TokenStream, input: TokenStream) -> TokenStream
180191
result.into()
181192
}
182193

194+
//#[proc_macro_attribute]
195+
//pub fn php_ini(_attr: TokenStream, input: TokenStream) -> TokenStream {
196+
// input
197+
//}
198+
183199
fn internal_function_parameters(inputs: &mut Punctuated<FnArg, Comma>) {
184200
inputs.push(parse_str("execute_data: *mut ::phper_sys::zend_execute_data").unwrap());
185201
inputs.push(parse_str("return_value: *mut ::phper_sys::zval").unwrap());

phper/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,14 @@ extern crate phper_macros;
22

33
pub mod alloc;
44
pub mod context;
5+
mod macros;
56

67
pub use phper_macros::*;
8+
use std::cell::Cell;
9+
use phper_sys::zend_ini_entry_def;
10+
11+
pub type IniEntries = Vec<zend_ini_entry_def>;
12+
13+
pub struct NotThreadSafe<T>(pub T);
14+
15+
unsafe impl<T> Sync for NotThreadSafe<T> {}

phper/src/macros.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[macro_export]
2+
macro_rules! c_str_ptr {
3+
($lit: expr) => {
4+
::std::concat!($lit, "\0").as_ptr() as *const ::std::os::raw::c_char
5+
};
6+
}
7+
8+
#[macro_export]
9+
macro_rules! php_ini {
10+
($($x:expr),*) => {
11+
vec![
12+
$($x,)*
13+
::phper_sys::zend_ini_end!()
14+
]
15+
};
16+
($($x:expr,)*) => {
17+
$crate::php_ini![$($x),*]
18+
};
19+
}
20+
21+
#[macro_export]
22+
macro_rules! std_php_ini_entry {
23+
($name:expr,$default:expr,$modifiable:expr,$on_modify:expr,$arg2:expr) => {
24+
{
25+
let name: &'static str = ::std::concat!($name, "\0");
26+
let name_len = name.len() - 1;
27+
28+
let value: &'static str = ::std::concat!($default, "\0");
29+
let value_len = value.len() - 1;
30+
31+
let arg2 = $arg2.with(|i| i.as_ptr() as *mut c_void);
32+
33+
::phper_sys::zend_ini_entry_def {
34+
name: name.as_ptr() as *const c_char,
35+
on_modify: Some($on_modify),
36+
mh_arg1: ::std::ptr::null_mut(),
37+
mh_arg2: arg2,
38+
mh_arg3: ::std::ptr::null_mut(),
39+
value: value.as_ptr() as *const c_char,
40+
displayer: None,
41+
modifiable: $modifiable as c_int,
42+
name_length: name_len as c_uint,
43+
value_length: value_len as c_uint,
44+
}
45+
}
46+
};
47+
}
48+

0 commit comments

Comments
 (0)