mirror of https://gitlab.com/slondr/rust-guile.git
wrote a macro to automate function registration for no-param functions
This commit is contained in:
parent
dc8aae5a5e
commit
b4d0980441
31
src/main.rs
31
src/main.rs
|
@ -16,36 +16,51 @@ extern "C" fn register_functions(_data: *mut c_void) -> *mut c_void {
|
|||
ret
|
||||
}
|
||||
|
||||
unsafe extern "C" fn init_scm() {
|
||||
unsafe fn init_scm() {
|
||||
// this function launches the scheme process and starts bootstrapping the runtime
|
||||
// any code executed after this function but before `scm_shell()' can talk to the scheme runtime
|
||||
scm_with_guile(Some(register_functions), std::ptr::null_mut());
|
||||
}
|
||||
|
||||
unsafe extern "C" fn run_scm(argc: c_int, argv: *mut *mut c_char) {
|
||||
unsafe fn run_scm(argc: c_int, argv: *mut *mut c_char) {
|
||||
// spawn the scheme shell, shifting execution from Rust mode to Guile mode
|
||||
scm_shell(argc, argv);
|
||||
}
|
||||
|
||||
unsafe extern "C" fn hello_from_rust() -> SCM {
|
||||
extern "C" fn hello_from_rust() -> SCM {
|
||||
// an example function demonstrating that Rust functions work in Scheme
|
||||
println!("Hello from Rust!");
|
||||
scm_from_int8(0)
|
||||
unsafe {
|
||||
scm_from_int8(0)
|
||||
}
|
||||
}
|
||||
|
||||
//fn register_void_function(fn_name: &[u8], f: fn() -> SCM) {
|
||||
macro_rules! register_void_function {
|
||||
($x:expr, $y:expr) => {
|
||||
// first convert the function name to a (const char *) so Guile can read it
|
||||
let fn_name: *const c_char = CStr::from_bytes_with_nul($x).unwrap().as_ptr();
|
||||
// convert the function into a (void *) to match with Guile's execution expectations
|
||||
let function: *mut c_void = $y as *mut c_void;
|
||||
// register the function as a subroutine in Guile
|
||||
scm_c_define_gsubr(fn_name, 0, 0, 0, function);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut ___args = std::env::args().map(|mut arg| arg.as_mut_ptr() as *mut c_char).collect::<Vec<*mut c_char>>();
|
||||
let argc = ___args.len() as c_int;
|
||||
let argv: *mut *mut c_char = ___args.as_mut_ptr();
|
||||
|
||||
unsafe {
|
||||
scm_gc_disable();
|
||||
let hello: *mut c_void = hello_from_rust as *mut c_void;
|
||||
// let hello: *mut c_void = hello_from_rust as *mut c_void;
|
||||
|
||||
let fn_name: *const c_char = CStr::from_bytes_with_nul(b"hello-from-rust\0").unwrap().as_ptr();
|
||||
// let fn_name: *const c_char = CStr::from_bytes_with_nul(b"hello-from-rust\0").unwrap().as_ptr();
|
||||
|
||||
init_scm();
|
||||
scm_c_define_gsubr(fn_name, 0 as c_int, 0 as c_int, 0 as c_int, hello);
|
||||
// scm_c_define_gsubr(fn_name, 0 as c_int, 0 as c_int, 0 as c_int, hello);
|
||||
register_void_function!(b"hello-from-rust\0", hello_from_rust);
|
||||
run_scm(argc, argv)
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue