labast/src/instructions/native.rs

50 lines
1.3 KiB
Rust

use crate::Stack;
use libloading::{Library, Symbol};
static mut STACKPTR: *mut Stack = 0 as *mut Stack;
#[no_mangle]
extern fn stack_pop_callback() -> u16 {
return unsafe {
(*STACKPTR).memory.pop()
};
}
#[no_mangle]
extern fn stack_push_callback(data: u16){
unsafe {
(*STACKPTR).memory.push(data)
};
}
#[no_mangle]
extern fn stack_len_callback() -> usize {
return unsafe{
(*STACKPTR).memory.len()
};
}
pub fn native(stack: &mut Stack, arg: String) {
unsafe {
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 libpref: String = "./".to_owned();
unsafe {
let module = Library::new([libpref, arg, libsuf].join("")).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(extern fn() -> u16, extern fn(u16), extern fn() -> usize, usize)> = module.get(b"labashka").unwrap();
func(stack_pop_callback,stack_push_callback,stack_len_callback, stack.memory.size());
}
}