Skip to content

Commit 5c22f42

Browse files
committed
Merge examples to simple.
1 parent 4f397c7 commit 5c22f42

File tree

12 files changed

+98
-249
lines changed

12 files changed

+98
-249
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ members = [
88

99
# internal
1010
"examples/simple",
11-
# "examples/class",
1211
]

examples/class/Cargo.toml

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/class/Makefile.toml

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/class/build.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/class/src/lib.rs

Lines changed: 0 additions & 160 deletions
This file was deleted.

examples/class/tests/confirm_compiled.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/simple/src/lib.rs

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
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};
3-
use phper::sys::{zend_ini_entry_def, zend_module_entry, zend_register_ini_entries, zend_unregister_ini_entries, OnUpdateBool};
4-
use phper::sys::{OnUpdateString};
3+
use phper::sys::{zend_ini_entry_def, zend_module_entry, zend_register_ini_entries, zend_unregister_ini_entries, OnUpdateBool, phper_zval_string};
4+
use phper::sys::{OnUpdateString, zend_class_entry, zend_register_internal_class, zend_declare_property_string, ZEND_ACC_PUBLIC, zend_type, zend_read_property};
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};
@@ -12,13 +12,15 @@ use phper::{
1212
php_rinit_function, php_rshutdown_function,
1313
};
1414
use phper::{php_minfo, php_minfo_function, php_rinit, php_rshutdown, zend_get_module};
15-
use std::ffi::CStr;
15+
use std::ffi::{CStr, CString};
1616
use std::mem;
1717
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};
21+
use phper::sys::{php_info_print_table_start, php_info_print_table_row, php_info_print_table_end, phper_init_class_entry};
22+
23+
static mut MY_CLASS_CE: *mut zend_class_entry = null_mut();
2224

2325
static SIMPLE_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
2426
static SIMPLE_TEXT: ModuleGlobals<*const c_char> = ModuleGlobals::new(null());
@@ -32,7 +34,12 @@ static INI_ENTRIES: IniEntryDefs<3> = IniEntryDefs::new([
3234
#[php_minit_function]
3335
fn m_init_simple(type_: c_int, module_number: c_int) -> bool {
3436
unsafe {
35-
zend_register_ini_entries(INI_ENTRIES.get(), module_number);
37+
zend_register_ini_entries(INI_ENTRIES.as_ptr(), module_number);
38+
}
39+
unsafe {
40+
let mut my_class_ce = phper_init_class_entry(c_str_ptr!("MyClass"), MY_CLASS_METHODS.as_ptr());
41+
MY_CLASS_CE = zend_register_internal_class(&mut my_class_ce);
42+
zend_declare_property_string(MY_CLASS_CE, c_str_ptr!("foo"), 3, c_str_ptr!("bar"), ZEND_ACC_PUBLIC as c_int);
3643
}
3744
true
3845
}
@@ -59,8 +66,8 @@ fn r_shutdown_simple(type_: c_int, module_number: c_int) -> bool {
5966
fn m_info_simple(zend_module: *mut ::phper::sys::zend_module_entry) {
6067
unsafe {
6168
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());
69+
php_info_print_table_row(2, c_str_ptr!("simple.enable"), format!("{}\0", *SIMPLE_ENABLE.as_ptr()).as_ptr());
70+
php_info_print_table_row(2, c_str_ptr!("simple.text"), format!("{}\0", CStr::from_ptr((*SIMPLE_TEXT.as_ptr())).to_str().unwrap()).as_ptr());
6471
php_info_print_table_end();
6572
}
6673
}
@@ -85,7 +92,7 @@ pub fn test_simple(execute_data: ExecuteData) -> impl SetVal {
8592
return None;
8693
}
8794

88-
let s = CStr::from_ptr((*SIMPLE_TEXT.get())).to_str().unwrap();
95+
let s = CStr::from_ptr((*SIMPLE_TEXT.as_ptr())).to_str().unwrap();
8996
println!("simple.text: '{}'", s);
9097

9198
Some(format!(
@@ -123,10 +130,70 @@ static FUNCTION_ENTRIES: FunctionEntries<2> = FunctionEntries::new([
123130
function_entry_end(),
124131
]);
125132

133+
134+
static ARG_INFO_MY_CLASS_FOO: InternalArgInfos<2> = InternalArgInfos::new([
135+
zend_internal_arg_info {
136+
name: 2 as *const _,
137+
type_: 0,
138+
pass_by_reference: 0,
139+
is_variadic: 0,
140+
},
141+
zend_internal_arg_info {
142+
name: c_str_ptr!("prefix"),
143+
type_: 0,
144+
pass_by_reference: 0,
145+
is_variadic: 0,
146+
},
147+
]);
148+
149+
static MY_CLASS_METHODS: FunctionEntries<2> = FunctionEntries::new([
150+
zend_function_entry {
151+
fname: c_str_ptr!("foo"),
152+
handler: Some(php_fn!(my_class_foo)),
153+
arg_info: ARG_INFO_MY_CLASS_FOO.get(),
154+
num_args: 1,
155+
flags: 0,
156+
},
157+
unsafe { transmute([0u8; size_of::<zend_function_entry>()]) },
158+
]);
159+
160+
161+
#[php_function]
162+
pub fn my_class_foo(execute_data: ExecuteData) -> impl SetVal {
163+
let mut prefix: *const c_char = null_mut();
164+
let mut prefix_len = 0;
165+
166+
unsafe {
167+
if zend_parse_parameters(
168+
execute_data.num_args() as c_int,
169+
c_str_ptr!("s"),
170+
&mut prefix,
171+
&mut prefix_len,
172+
) != ZEND_RESULT_CODE_SUCCESS
173+
{
174+
return None;
175+
}
176+
177+
let prefix = CStr::from_ptr(prefix).to_str().unwrap();
178+
179+
let this = if execute_data.get_type() == phper::sys::IS_OBJECT as zend_type {
180+
execute_data.get_this()
181+
} else {
182+
null_mut()
183+
};
184+
185+
let foo = zend_read_property(MY_CLASS_CE, this, c_str_ptr!("foo"), 3, 1, null_mut());
186+
let foo = Val::from_raw(foo);
187+
let foo = foo.as_c_str().unwrap().to_str().unwrap();
188+
Some(format!("{}{}", prefix, foo))
189+
}
190+
}
191+
192+
126193
static MODULE_ENTRY: ModuleEntry = ModuleEntry::new(create_zend_module_entry(
127194
c_str_ptr!(env!("CARGO_PKG_NAME")),
128195
c_str_ptr!(env!("CARGO_PKG_VERSION")),
129-
FUNCTION_ENTRIES.get(),
196+
FUNCTION_ENTRIES.as_ptr(),
130197
Some(php_minit!(m_init_simple)),
131198
Some(php_mshutdown!(m_shutdown_simple)),
132199
Some(php_rinit!(r_init_simple)),

examples/simple/tests/confirm_compiled.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66

77
var_dump(get_extension_funcs('simple'));
88
var_dump(test_simple("aaa", "bbb"));
9+
var_dump((new MyClass())->foo("foo-"));

phper-macros/src/inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub(crate) fn zend_get_module(_attr: TokenStream, input: TokenStream) -> TokenSt
127127
#body
128128
}
129129
let internal: fn() -> &'static ::phper::zend::modules::ModuleEntry = internal;
130-
internal().get()
130+
internal().as_ptr()
131131
}
132132
};
133133

0 commit comments

Comments
 (0)