This commit is contained in:
Robin Appelman 2019-03-18 17:22:32 +01:00
commit 2c6fc98b87
7 changed files with 50 additions and 51 deletions

View file

@ -9,7 +9,7 @@ repository = "https://github.com/rethinkphp/php-rs"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
libc = "0.2.0" libc = "0.2.50"
[lib] [lib]
name = "php" name = "php"

View file

@ -1,45 +1,39 @@
#![allow(unused_variables)] #![allow(unused_variables)]
use libc::*; use libc::*;
use php::*;
use php::zend::*;
use php::info::*; use php::info::*;
use php::zend::*;
use php::*;
extern { extern "C" {
pub fn php_printf(format: *const c_char , ...) -> size_t; pub fn php_printf(format: *const c_char, ...) -> size_t;
} }
#[no_mangle] #[no_mangle]
pub extern fn php_module_startup(type_: c_int, module_number: c_int) -> c_int { pub extern "C" fn php_module_startup(type_: c_int, module_number: c_int) -> c_int {
0 0
} }
#[no_mangle] #[no_mangle]
pub extern fn php_module_shutdown(type_: c_int, module_number: c_int) -> c_int { pub extern "C" fn php_module_shutdown(type_: c_int, module_number: c_int) -> c_int {
0 0
} }
#[no_mangle] #[no_mangle]
pub extern fn php_module_info() { pub extern "C" fn php_module_info() {
print_table_start(); print_table_start();
print_table_row(&["A demo PHP extension written in Rust", "enabled"]); print_table_row(&["A demo PHP extension written in Rust", "enabled"]);
print_table_end(); print_table_end();
} }
#[no_mangle] #[no_mangle]
pub extern fn helloworld(data: &ExecuteData, retval: &Value) { pub extern "C" fn helloworld(data: &ExecuteData, retval: &Value) {
unsafe { unsafe { php_printf(c_str!("Hello world, Rust!")) };
php_printf(c_str!("Hello world, Rust!"))
};
} }
#[no_mangle] #[no_mangle]
pub extern fn get_module() -> *mut zend::Module { pub extern "C" fn get_module() -> *mut zend::Module {
let mut entry = Box::new(zend::Module::new(c_str!("demo"), c_str!("0.1.0-dev")));
let mut entry = Box::new(zend::Module::new(
c_str!("demo"),
c_str!("0.1.0-dev"),
));
entry.set_info_func(php_module_info); entry.set_info_func(php_module_info);

View file

@ -1,7 +1,7 @@
use libc::*; use libc::*;
use std::ffi::CString; use std::ffi::CString;
extern { extern "C" {
pub fn php_info_print_table_start(); pub fn php_info_print_table_start();
pub fn php_info_print_table_row(num_cols: c_int, ...) -> c_void; pub fn php_info_print_table_row(num_cols: c_int, ...) -> c_void;
pub fn php_info_print_table_end(); pub fn php_info_print_table_end();
@ -29,4 +29,3 @@ pub fn print_table_start() {
pub fn print_table_end() { pub fn print_table_end() {
unsafe { php_info_print_table_end() } unsafe { php_info_print_table_end() }
} }

View file

@ -1,11 +1,10 @@
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused_variables)] #![allow(unused_variables)]
extern crate libc; extern crate libc;
#[macro_use] #[macro_use]
pub mod macros; pub mod macros;
pub mod zend;
pub mod info; pub mod info;
pub mod zend;

View file

@ -1,7 +1,6 @@
#[macro_export] #[macro_export]
macro_rules! c_str { macro_rules! c_str {
($s:expr) => { { ($s:expr) => {{
concat!($s, "\0").as_ptr() as *const c_char concat!($s, "\0").as_ptr() as *const c_char
} } }};
} }

View file

@ -1,14 +1,14 @@
use libc::*;
use std; use std;
use std::mem; use std::mem;
use libc::*;
type StartupFunc = extern fn (type_: c_int, module_number: c_int) -> c_int; type StartupFunc = extern "C" fn(type_: c_int, module_number: c_int) -> c_int;
type ShutdownFunc = extern fn (type_: c_int, module_number: c_int) -> c_int; type ShutdownFunc = extern "C" fn(type_: c_int, module_number: c_int) -> c_int;
type InfoFunc = extern fn () ; type InfoFunc = extern "C" fn();
type GlobalsCtorFunc = extern fn (global: *const c_void) -> c_void; type GlobalsCtorFunc = extern "C" fn(global: *const c_void) -> c_void;
type GlobalsDtorFunc = extern fn (global: *const c_void) -> c_void; type GlobalsDtorFunc = extern "C" fn(global: *const c_void) -> c_void;
type PostDeactivateFunc = extern fn () -> c_int; type PostDeactivateFunc = extern "C" fn() -> c_int;
type HandlerFunc = extern fn (execute_data: &ExecuteData, retval: &Value); type HandlerFunc = extern "C" fn(execute_data: &ExecuteData, retval: &Value);
pub struct ExecuteData {} pub struct ExecuteData {}
pub struct Value {} pub struct Value {}
@ -16,16 +16,21 @@ pub struct ModuleDep {}
#[repr(C)] #[repr(C)]
pub struct ArgInfo { pub struct ArgInfo {
name: *const c_char, name: *const c_char,
class_name: *const c_char, class_name: *const c_char,
type_hint: c_uchar, type_hint: c_uchar,
pass_by_reference: c_uchar, pass_by_reference: c_uchar,
allow_null: c_uchar, allow_null: c_uchar,
is_variadic: c_uchar, is_variadic: c_uchar,
} }
impl ArgInfo { impl ArgInfo {
pub fn new(name: *const c_char, allow_null: c_uchar, is_variadic: c_uchar, by_reference: c_uchar) -> ArgInfo { pub fn new(
name: *const c_char,
allow_null: c_uchar,
is_variadic: c_uchar,
by_reference: c_uchar,
) -> ArgInfo {
ArgInfo { ArgInfo {
name: name, name: name,
class_name: std::ptr::null(), class_name: std::ptr::null(),
@ -39,11 +44,11 @@ impl ArgInfo {
#[repr(C)] #[repr(C)]
pub struct Function { pub struct Function {
fname: *const c_char, fname: *const c_char,
handler: Option<HandlerFunc>, handler: Option<HandlerFunc>,
arg_info: *const ArgInfo, arg_info: *const ArgInfo,
num_args: u32, num_args: u32,
flags: u32, flags: u32,
} }
impl Function { impl Function {
@ -57,7 +62,11 @@ impl Function {
} }
} }
pub fn new_with_args(name: *const c_char, handler: HandlerFunc, args: Box<[ArgInfo]>) -> Function { pub fn new_with_args(
name: *const c_char,
handler: HandlerFunc,
args: Box<[ArgInfo]>,
) -> Function {
let num_args = args.len() as u32; let num_args = args.len() as u32;
Function { Function {
@ -78,7 +87,6 @@ impl Function {
flags: 0, flags: 0,
} }
} }
} }
pub struct INI {} pub struct INI {}