Fix skill issues

This commit is contained in:
bʰedoh₂ swé 2024-06-21 01:36:33 +05:00
parent fb1207bae5
commit f69354ec31
4 changed files with 17 additions and 43 deletions

View File

@ -159,7 +159,7 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
},
"native" => {
let arg = instruction.arg.clone();
instructions::native::native(stack, arg)
unsafe {instructions::native::native(stack, arg)}
},
// Self-modifying

View File

@ -35,26 +35,12 @@ pub fn exec(mut stack: &mut Stack, arg: String, mod_name: String) {
}
}
pub fn call(mut stack: &mut Stack, mod_name: String) {
pub fn call(stack: &mut Stack, mod_name: String) {
let len: u16 = stack.memory.pop();
let mut rev: String = String::new();
for _i in 0..len {
rev.push(stack.memory.pop() as u8 as char);
}
let tocall: String = rev.chars().rev().collect::<String>();
if mod_name == tocall {
eprintln!("{}", RunError::ExecuteItself(tocall.to_string()));
std::process::exit(2);
}
let lines = fs::read_to_string(&tocall).expect(&format!("{}", RunError::FailToReadFile));
let mut temp_stack = Stack::new(stack.memory.size());
parse(&mut temp_stack, &lines);
execute(&mut temp_stack, Some(&mut stack), tocall.clone());
for item in temp_stack.memory.iter() {
stack.memory.push(*item);
}
exec(stack, tocall, mod_name);
}

View File

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

View File

@ -47,7 +47,7 @@ pub fn parse(stack: &mut Stack, file_content: &str) {
}
}
if name != "" {
if name.is_empty() {
let inst = Instruction { name, arg, data, ispiped: false, isdrained: false };
stack.program.push(inst);
}