mirror of
https://github.com/icewind1991/ivory.git
synced 2026-06-03 10:44:09 +02:00
Implemented to build empty php extension in Rust
This commit is contained in:
parent
12ca6545e8
commit
b1a4a5fb8d
8 changed files with 204 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
target/
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "php-rs"
|
||||
version = "0.1.0"
|
||||
authors = ["Jin Hu <bixuehujin@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.0"
|
||||
|
||||
[lib]
|
||||
name = "php"
|
||||
|
||||
12
examples/helloworld/Cargo.toml
Normal file
12
examples/helloworld/Cargo.toml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "helloworld"
|
||||
version = "0.1.0"
|
||||
authors = ["Jin Hu <bixuehujin@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.0"
|
||||
php-rs = { path = "../.." }
|
||||
|
||||
[lib]
|
||||
name = "helloworld"
|
||||
crate-type = ["dylib"]
|
||||
53
examples/helloworld/src/lib.rs
Normal file
53
examples/helloworld/src/lib.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#![allow(unused_variables)]
|
||||
#![feature(link_args)]
|
||||
|
||||
extern crate libc;
|
||||
extern crate php;
|
||||
|
||||
use libc::*;
|
||||
use php::*;
|
||||
use php::info::*;
|
||||
|
||||
#[link_args = "-Wl,-undefined,dynamic_lookup"]
|
||||
extern {
|
||||
|
||||
}
|
||||
|
||||
macro_rules! c_str {
|
||||
($s:expr) => { {
|
||||
concat!($s, "\0").as_ptr() as *const c_char
|
||||
} }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn php_module_startup(type_: c_int, module_number: c_int) -> c_int {
|
||||
0
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn php_module_shutdown(type_: c_int, module_number: c_int) -> c_int {
|
||||
0
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn php_module_info() {
|
||||
print_table_start();
|
||||
unsafe {
|
||||
php_info_print_table_row(2, c_str!("A demo PHP extension written in Rust"), c_str!("enabled"));
|
||||
}
|
||||
print_table_end();
|
||||
}
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn get_module() -> *mut zend::Module {
|
||||
|
||||
let mut entry = Box::new(zend::Module::new(
|
||||
c_str!("demo"),
|
||||
c_str!("0.1.0-dev"),
|
||||
));
|
||||
|
||||
entry.set_info_func(php_module_info);
|
||||
|
||||
Box::into_raw(entry)
|
||||
}
|
||||
19
src/info.rs
Normal file
19
src/info.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use libc::*;
|
||||
|
||||
#[link_args = "-Wl,-undefined,dynamic_lookup"]
|
||||
extern {
|
||||
pub fn php_info_print_table_row(num_cols: c_int, ...) -> c_void;
|
||||
pub fn php_info_print_table_row_ex(num_cols: c_int, a: *const c_char, ...);
|
||||
pub fn php_info_print_table_start();
|
||||
pub fn php_info_print_table_end();
|
||||
pub fn php_info_print_hr();
|
||||
}
|
||||
|
||||
pub fn print_table_start() {
|
||||
unsafe { php_info_print_table_start() }
|
||||
}
|
||||
|
||||
pub fn print_table_end() {
|
||||
unsafe { php_info_print_table_end() }
|
||||
}
|
||||
|
||||
9
src/lib.rs
Normal file
9
src/lib.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#![allow(dead_code)]
|
||||
#![allow(unused_variables)]
|
||||
#![feature(link_args)]
|
||||
|
||||
|
||||
extern crate libc;
|
||||
|
||||
pub mod zend;
|
||||
pub mod info;
|
||||
3
src/zend/mod.rs
Normal file
3
src/zend/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub use self::module::*;
|
||||
|
||||
mod module;
|
||||
94
src/zend/module.rs
Normal file
94
src/zend/module.rs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
use std;
|
||||
use std::mem;
|
||||
use libc::*;
|
||||
|
||||
type StartupFunc = extern fn (type_: c_int, module_number: c_int) -> c_int;
|
||||
type ShutdownFunc = extern fn (type_: c_int, module_number: c_int) -> c_int;
|
||||
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;
|
||||
|
||||
|
||||
macro_rules! c_str {
|
||||
($s:expr) => { {
|
||||
concat!($s, "\0").as_ptr() as *const c_char
|
||||
} }
|
||||
}
|
||||
|
||||
pub struct ModuleDep {}
|
||||
pub struct Function {}
|
||||
pub struct INI {}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Module {
|
||||
size: c_ushort,
|
||||
zend_api: c_uint,
|
||||
zend_debug: c_uchar,
|
||||
zts: c_uchar,
|
||||
ini_entry: *const INI,
|
||||
deps: *const ModuleDep,
|
||||
name: *const c_char,
|
||||
functions: *const Function,
|
||||
module_startup_func: Option<StartupFunc>,
|
||||
module_shutdown_func: Option<ShutdownFunc>,
|
||||
request_startup_func: Option<StartupFunc>,
|
||||
request_shutdown_func: Option<ShutdownFunc>,
|
||||
info_func: Option<InfoFunc>,
|
||||
version: *const c_char,
|
||||
globals_size: size_t,
|
||||
globals_ptr: *const c_void,
|
||||
globals_ctor: Option<GlobalsCtorFunc>,
|
||||
globals_dtor: Option<GlobalsDtorFunc>,
|
||||
post_deactivate_func: Option<PostDeactivateFunc>,
|
||||
module_started: c_int,
|
||||
type_: c_uchar,
|
||||
handle: *const c_void,
|
||||
module_number: c_int,
|
||||
build_id: *const c_char,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
pub fn new(name: *const c_char, version: *const c_char) -> Module {
|
||||
Module {
|
||||
size: mem::size_of::<Module>() as u16,
|
||||
zend_api: 20151012,
|
||||
zend_debug: 0,
|
||||
zts: 0,
|
||||
ini_entry: std::ptr::null(),
|
||||
deps: std::ptr::null(),
|
||||
name: name,
|
||||
functions: std::ptr::null(),
|
||||
module_startup_func: None,
|
||||
module_shutdown_func: None,
|
||||
request_startup_func: None,
|
||||
request_shutdown_func: None,
|
||||
info_func: None,
|
||||
version: version,
|
||||
globals_size: 0,
|
||||
globals_ptr: std::ptr::null(),
|
||||
globals_ctor: None,
|
||||
globals_dtor: None,
|
||||
post_deactivate_func: None,
|
||||
module_started: 0,
|
||||
type_: 0,
|
||||
handle: std::ptr::null(),
|
||||
module_number: 0,
|
||||
build_id: c_str!("API20151012,NTS"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_startup_func(&mut self, func: StartupFunc) {
|
||||
self.module_startup_func = Some(func);
|
||||
}
|
||||
|
||||
pub fn set_shutdown_func(&mut self, func: ShutdownFunc) {
|
||||
self.module_shutdown_func = Some(func);
|
||||
}
|
||||
|
||||
pub fn set_info_func(&mut self, func: InfoFunc) {
|
||||
self.info_func = Some(func);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Sync for Module {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue