Add #native

This commit is contained in:
2024-06-17 04:14:13 +05:00
parent c7708db06e
commit fddedb7828
7 changed files with 157 additions and 1 deletions
+5 -1
View File
@@ -143,7 +143,11 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
"exec" => {
let arg = instruction.arg.clone();
instructions::modules::exec(stack, arg, mod_name.clone())
}
},
"native" => {
let arg = instruction.arg.clone();
instructions::native::native(stack, arg)
},
// Self-modifying
"insert" => {
+1
View File
@@ -9,3 +9,4 @@ pub mod self_modify;
pub mod special;
pub mod specific;
pub mod stack_manage;
pub mod native;
+50
View File
@@ -0,0 +1,50 @@
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) -> i32 {
unsafe {
(*STACKPTR).memory.push(data)
};
return 0;
}
#[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
// int labashka(unsinged short (*pop)(), void (*push)(unsinged short), size_t (*len)(), size_t max_size );
let func: Symbol<unsafe extern "C" fn(extern fn() -> u16, extern fn(u16) -> i32, extern fn () -> usize, usize) -> u32> = module.get(b"labashka").unwrap();
func(stack_pop_callback,stack_push_callback,stack_len_callback, stack.memory.size());
}
}