Skip to content

Commit ef3e17c

Browse files
committed
Design ini api.
1 parent 2ea2065 commit ef3e17c

File tree

11 files changed

+129
-23
lines changed

11 files changed

+129
-23
lines changed

examples/simple/src/lib.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#![feature(allocator_api)]
2+
#![feature(const_raw_ptr_deref)]
23

34
use phper::{c_str_ptr, php_fn, ebox};
45
use phper::sys::{ZEND_RESULT_CODE_SUCCESS, zend_parse_parameters, zend_internal_arg_info, zend_function_entry, PHP_INI_SYSTEM};
5-
use phper::sys::{zend_ini_entry_def, zend_module_entry, zend_register_ini_entries, zend_unregister_ini_entries};
6-
use phper::zend::api::FunctionEntries;
6+
use phper::sys::{zend_ini_entry_def, zend_module_entry, zend_register_ini_entries, zend_unregister_ini_entries, OnUpdateBool};
7+
use phper::sys::{OnUpdateString};
8+
use phper::zend::api::{FunctionEntries, ModuleGlobals};
79
use phper::zend::compile::InternalArgInfos;
8-
use phper::zend::ini::IniEntryDefs;
10+
use phper::zend::ini::{IniEntryDefs, ini_entry_def_end};
911
use phper::zend::modules::ModuleEntry;
1012
use phper::zend::types::{ExecuteData, Val, SetVal, Value};
1113
use phper::{
@@ -18,21 +20,15 @@ use std::mem;
1820
use std::mem::{size_of, transmute};
1921
use std::os::raw::{c_char, c_int, c_uchar, c_uint, c_ushort};
2022
use std::ptr::{null, null_mut};
23+
use phper::zend::exceptions::MyException;
2124

22-
static INI_ENTRIES: IniEntryDefs<2> = IniEntryDefs::new([
23-
zend_ini_entry_def {
24-
name: c_str_ptr!("simple.enable"),
25-
on_modify: None,
26-
mh_arg1: null_mut(),
27-
mh_arg2: null_mut(),
28-
mh_arg3: null_mut(),
29-
value: c_str_ptr!("1"),
30-
displayer: None,
31-
modifiable: PHP_INI_SYSTEM as c_int,
32-
name_length: 0,
33-
value_length: 0,
34-
},
35-
unsafe { transmute([0u8; size_of::<zend_ini_entry_def>()]) },
25+
static SIMPLE_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
26+
static SIMPLE_TEXT: ModuleGlobals<*const c_char> = ModuleGlobals::new(null());
27+
28+
static INI_ENTRIES: IniEntryDefs<3> = IniEntryDefs::new([
29+
SIMPLE_ENABLE.create_ini_entry_def("simple.enable", "1", Some(OnUpdateBool), PHP_INI_SYSTEM),
30+
SIMPLE_TEXT.create_ini_entry_def("simple.text", "", Some(OnUpdateString), PHP_INI_SYSTEM),
31+
ini_entry_def_end(),
3632
]);
3733

3834
#[php_minit_function]
@@ -82,10 +78,13 @@ pub fn test_simple(execute_data: ExecuteData) -> impl SetVal {
8278
&mut b_len,
8379
) != ZEND_RESULT_CODE_SUCCESS
8480
{
85-
return Value::Null;
81+
return None;
8682
}
8783

88-
Value::String(format!(
84+
let s = CStr::from_ptr((*SIMPLE_TEXT.get())).to_str().unwrap();
85+
println!("simple.text: '{}'", s);
86+
87+
Some(format!(
8988
"(a . b) = {}{}",
9089
CStr::from_ptr(a).to_str().unwrap(),
9190
CStr::from_ptr(b).to_str().unwrap(),

phper-macros/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,3 @@ pub fn php_rshutdown_function(_attr: TokenStream, input: TokenStream) -> TokenSt
9999
pub fn php_minfo_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
100100
info_fn(input, "zm_info_")
101101
}
102-

phper-macros/src/utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use proc_macro::TokenStream;
22
use quote::quote;
3-
use syn::{parse_macro_input, Expr};
3+
use syn::{parse_macro_input, Expr, Fields};
4+
use syn::parse::Nothing;
5+
use syn::ItemStruct;
6+
use proc_macro2::{TokenTree, Group};
47

58
pub(crate) fn c_str(input: TokenStream) -> TokenStream {
69
let input = parse_macro_input!(input as Expr);

phper-sys/php_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <php.h>
55
#include <php_ini.h>
66
#include <ext/standard/info.h>
7+
#include <zend_exceptions.h>
78

89
zend_string *zend_string_init_(const char *str, size_t len, int persistent);
910
zend_string *zend_new_interned_string_(zend_string *str);

phper/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![feature(min_const_generics)]
2+
#![feature(const_fn_fn_ptr_basics)]
3+
#![feature(const_fn_transmute)]
24

35
/*!
46
A library that allows us to write PHP extensions using pure Rust and using safe Rust whenever possible.

phper/src/zend/api.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
1-
use crate::sys::zend_function_entry;
1+
use crate::sys::{zend_function_entry, zend_ini_entry_def};
22
use std::cell::UnsafeCell;
3+
use std::os::raw::{c_char, c_int, c_void};
4+
use crate::zend::ini::Mh;
5+
use std::ptr::null_mut;
6+
7+
#[repr(C)]
8+
pub struct ModuleGlobals<T: 'static> {
9+
inner: UnsafeCell<T>,
10+
}
11+
12+
impl<T: 'static> ModuleGlobals<T> {
13+
pub const fn new(inner: T) -> Self {
14+
Self { inner: UnsafeCell::new(inner) }
15+
}
16+
17+
pub const fn get(&self) -> *mut T {
18+
self.inner.get()
19+
}
20+
21+
pub const fn create_ini_entry_def(&'static self, name: &str, default_value: &str, on_modify: Option<Mh>, modifiable: u32) -> zend_ini_entry_def {
22+
zend_ini_entry_def {
23+
name: name.as_ptr().cast(),
24+
on_modify,
25+
mh_arg1: 0 as *mut _,
26+
mh_arg2: self.get().cast(),
27+
mh_arg3: null_mut(),
28+
value: default_value.as_ptr().cast(),
29+
displayer: None,
30+
modifiable: modifiable as c_int,
31+
name_length: name.len() as u32,
32+
value_length: default_value.len() as u32,
33+
}
34+
}
35+
}
36+
37+
unsafe impl<T: 'static> Sync for ModuleGlobals<T> {}
338

439
pub struct FunctionEntries<const N: usize> {
540
inner: UnsafeCell<[zend_function_entry; N]>,

phper/src/zend/exceptions.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub trait Throwable {
2+
3+
}
4+
5+
pub struct MyException;
6+
7+
impl Throwable for MyException {}

phper/src/zend/ini.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
use crate::sys::zend_ini_entry_def;
22
use std::cell::UnsafeCell;
3+
use std::os::raw::{c_int, c_void};
4+
use crate::sys::{zend_ini_entry, zend_string};
5+
use std::mem::{transmute, size_of};
6+
7+
pub type Mh = unsafe extern "C" fn(*mut zend_ini_entry, *mut zend_string, *mut c_void, *mut c_void, *mut c_void, c_int) -> c_int;
8+
9+
pub const fn ini_entry_def_end() -> zend_ini_entry_def {
10+
unsafe { transmute([0u8; size_of::<zend_ini_entry_def>()]) }
11+
}
312

413
pub struct IniEntryDefs<const N: usize> {
514
inner: UnsafeCell<[zend_ini_entry_def; N]>,
@@ -17,3 +26,19 @@ impl<const N: usize> IniEntryDefs<N> {
1726
}
1827

1928
unsafe impl<const N: usize> Sync for IniEntryDefs<N> {}
29+
30+
struct Entry {
31+
a: &'static str,
32+
b: &'static str,
33+
}
34+
35+
struct Entry2 {
36+
a: &'static str,
37+
b: &'static str,
38+
}
39+
40+
const fn entry(e: Entry) -> Entry2 {
41+
let a = e.a;
42+
let b = e.b;
43+
Entry2 { a, b }
44+
}

phper/src/zend/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub mod api;
22
pub mod compile;
3+
pub mod exceptions;
34
pub mod ini;
45
pub mod modules;
6+
pub mod portability;
57
pub mod types;

phper/src/zend/portability.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)