mirror of
https://github.com/icewind1991/ivory.git
synced 2026-06-03 18:54:07 +02:00
Added support to set functions for module
This commit is contained in:
parent
872ca624fb
commit
3098a609fd
2 changed files with 95 additions and 3 deletions
|
|
@ -6,11 +6,12 @@ extern crate php;
|
|||
|
||||
use libc::*;
|
||||
use php::*;
|
||||
use zend::*;
|
||||
use php::info::*;
|
||||
|
||||
#[link_args = "-Wl,-undefined,dynamic_lookup"]
|
||||
extern {
|
||||
|
||||
pub fn php_printf(format: *const c_char , ...) -> size_t;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
|
@ -30,6 +31,12 @@ pub extern fn php_module_info() {
|
|||
print_table_end();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn helloworld(data: &ExecuteData, retval: &Value) {
|
||||
unsafe {
|
||||
php_printf(c_str!("Hello world, Rust!"))
|
||||
};
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn get_module() -> *mut zend::Module {
|
||||
|
|
@ -41,5 +48,18 @@ pub extern fn get_module() -> *mut zend::Module {
|
|||
|
||||
entry.set_info_func(php_module_info);
|
||||
|
||||
let args = Box::new([
|
||||
ArgInfo::new(1 as *const c_char, 0, 0, 0),
|
||||
ArgInfo::new(c_str!("name"), 0, 0, 0),
|
||||
]);
|
||||
|
||||
let funcs = Box::new([
|
||||
Function::new(c_str!("helloworld"), helloworld),
|
||||
Function::new_with_args(c_str!("helloworld2"), helloworld, args),
|
||||
Function::end(),
|
||||
]);
|
||||
|
||||
entry.set_functions(funcs);
|
||||
|
||||
Box::into_raw(entry)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,79 @@ type InfoFunc = extern fn () ;
|
|||
type GlobalsCtorFunc = extern fn (global: *const c_void) -> c_void;
|
||||
type GlobalsDtorFunc = extern fn (global: *const c_void) -> c_void;
|
||||
type PostDeactivateFunc = extern fn () -> c_int;
|
||||
type HandlerFunc = extern fn (execute_data: &ExecuteData, retval: &Value);
|
||||
|
||||
|
||||
|
||||
pub struct ExecuteData {}
|
||||
pub struct Value {}
|
||||
pub struct ModuleDep {}
|
||||
pub struct Function {}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct ArgInfo {
|
||||
name: *const c_char,
|
||||
class_name: *const c_char,
|
||||
type_hint: c_uchar,
|
||||
pass_by_reference: c_uchar,
|
||||
allow_null: c_uchar,
|
||||
is_variadic: c_uchar,
|
||||
}
|
||||
|
||||
impl ArgInfo {
|
||||
pub fn new(name: *const c_char, allow_null: c_uchar, is_variadic: c_uchar, by_reference: c_uchar) -> ArgInfo {
|
||||
ArgInfo {
|
||||
name: name,
|
||||
class_name: std::ptr::null(),
|
||||
type_hint: 0,
|
||||
pass_by_reference: by_reference,
|
||||
allow_null: allow_null,
|
||||
is_variadic: is_variadic,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Function {
|
||||
fname: *const c_char,
|
||||
handler: Option<HandlerFunc>,
|
||||
arg_info: *const ArgInfo,
|
||||
num_args: u32,
|
||||
flags: u32,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub fn new(name: *const c_char, handler: HandlerFunc) -> Function {
|
||||
Function {
|
||||
fname: name,
|
||||
handler: Some(handler),
|
||||
arg_info: std::ptr::null(),
|
||||
num_args: 0,
|
||||
flags: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_args(name: *const c_char, handler: HandlerFunc, args: Box<[ArgInfo]>) -> Function {
|
||||
let num_args = args.len() as u32;
|
||||
|
||||
Function {
|
||||
fname: name,
|
||||
handler: Some(handler),
|
||||
arg_info: Box::into_raw(args) as *const ArgInfo,
|
||||
num_args: num_args - 1,
|
||||
flags: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end() -> Function {
|
||||
Function {
|
||||
fname: std::ptr::null(),
|
||||
handler: None,
|
||||
arg_info: std::ptr::null(),
|
||||
num_args: 0,
|
||||
flags: 0,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct INI {}
|
||||
|
||||
#[repr(C)]
|
||||
|
|
@ -84,6 +152,10 @@ impl Module {
|
|||
pub fn set_info_func(&mut self, func: InfoFunc) {
|
||||
self.info_func = Some(func);
|
||||
}
|
||||
|
||||
pub fn set_functions(&mut self, funcs: Box<[Function]>) {
|
||||
self.functions = Box::into_raw(funcs) as *const Function;
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Sync for Module {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue