labast/src/instructions/native.rs
2024-06-21 01:36:33 +05:00

38 lines
1.1 KiB
Rust

use crate::Stack;
use libloading::{Library, Symbol};
static mut STACKPTR: *mut Stack = 0 as *mut Stack;
#[no_mangle]
unsafe extern fn stack_pop_callback() -> u16 {
(*STACKPTR).memory.pop()
}
#[no_mangle]
unsafe extern fn stack_push_callback(data: u16) {
(*STACKPTR).memory.push(data)
}
#[no_mangle]
unsafe extern fn stack_len_callback() -> usize {
(*STACKPTR).memory.len()
}
pub unsafe fn native(stack: &mut Stack, arg: String) {
STACKPTR = stack as *mut Stack;
#[cfg(not(target_family = "windows"))]
let libsuf: String = ".so".to_owned();
#[cfg(target_family = "windows")]
let libsuf: String = ".dll".to_owned();
let module = Library::new(format!("./{}{}", arg, libsuf)).unwrap();
// C libraries should use
// void (*labashka)(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size);
let func: Symbol<unsafe extern "C" fn(unsafe extern fn() -> u16, unsafe extern fn(u16), unsafe extern fn() -> usize, usize)> = module.get(b"labashka").unwrap();
func(stack_pop_callback,stack_push_callback,stack_len_callback, stack.memory.size());
}