From f69354ec3161724c4c3661dd34734d0fec309fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Fri, 21 Jun 2024 01:36:33 +0500 Subject: [PATCH] Fix skill issues --- src/execute.rs | 2 +- src/instructions/modules.rs | 18 ++---------------- src/instructions/native.rs | 38 +++++++++++++------------------------ src/parse.rs | 2 +- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/execute.rs b/src/execute.rs index 83c3345..a94355d 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -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 diff --git a/src/instructions/modules.rs b/src/instructions/modules.rs index 3611d4c..26a207b 100644 --- a/src/instructions/modules.rs +++ b/src/instructions/modules.rs @@ -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::(); - - 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); } diff --git a/src/instructions/native.rs b/src/instructions/native.rs index f295a01..fa17612 100644 --- a/src/instructions/native.rs +++ b/src/instructions/native.rs @@ -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 { - (*STACKPTR).memory.pop() - }; +unsafe extern fn stack_pop_callback() -> u16 { + (*STACKPTR).memory.pop() } #[no_mangle] -extern fn stack_push_callback(data: u16){ - unsafe { - (*STACKPTR).memory.push(data) - }; +unsafe extern fn stack_push_callback(data: u16) { + (*STACKPTR).memory.push(data) } #[no_mangle] -extern fn stack_len_callback() -> usize { - return unsafe{ - (*STACKPTR).memory.len() - }; +unsafe extern fn stack_len_callback() -> usize { + (*STACKPTR).memory.len() } -pub fn native(stack: &mut Stack, arg: String) { - unsafe { - STACKPTR = stack as *mut Stack; - } +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(); - // 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 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()); - } + 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 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()); } diff --git a/src/parse.rs b/src/parse.rs index deae48a..27bf731 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -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); }