Implement new features #6

Merged
n3tael merged 9 commits from bedohswe/labast:master into master 2024-06-20 20:39:40 +00:00
4 changed files with 17 additions and 43 deletions
Showing only changes of commit f69354ec31 - Show all commits

View File

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

а тут обернуть вызов функции в unsafe-блок

а тут обернуть вызов функции в unsafe-блок
}, },
// Self-modifying // 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 len: u16 = stack.memory.pop();
let mut rev: String = String::new(); let mut rev: String = String::new();
for _i in 0..len { for _i in 0..len {
rev.push(stack.memory.pop() as u8 as char); rev.push(stack.memory.pop() as u8 as char);
} }
let tocall: String = rev.chars().rev().collect::<String>(); let tocall: String = rev.chars().rev().collect::<String>();
exec(stack, tocall, mod_name);
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);
}
} }
Review

строки 46-59 это буквально код функции exec (за ислюкчением того что вместо arg, tocall)

так что можно просто вызвать функцию exec:

exec(&mut stack, tocall, mod_name);
строки 46-59 это буквально код функции `exec` (за ислюкчением того что вместо `arg`, `tocall`) так что можно просто вызвать функцию `exec`: ```rust exec(&mut stack, tocall, mod_name); ```

View File

@ -4,46 +4,34 @@ use libloading::{Library, Symbol};
static mut STACKPTR: *mut Stack = 0 as *mut Stack; static mut STACKPTR: *mut Stack = 0 as *mut Stack;
#[no_mangle] #[no_mangle]
extern fn stack_pop_callback() -> u16 { unsafe extern fn stack_pop_callback() -> u16 {

можно просто написать unsafe перед extern, и убрать return

#[no_mangle]
unsafe extern fn stack_pop_callback() -> u16 {
    (*STACKPTR).memory.pop()
}
можно просто написать `unsafe` перед `extern`, и убрать `return` ```rust #[no_mangle] unsafe extern fn stack_pop_callback() -> u16 { (*STACKPTR).memory.pop() } ```
return unsafe { (*STACKPTR).memory.pop()
(*STACKPTR).memory.pop()
};
} }
#[no_mangle] #[no_mangle]
extern fn stack_push_callback(data: u16){ unsafe extern fn stack_push_callback(data: u16) {
unsafe { (*STACKPTR).memory.push(data)
(*STACKPTR).memory.push(data)
};
} }
#[no_mangle]
unsafe extern fn stack_push_callback(data: u16){
    (*STACKPTR).memory.push(data)
}
```rust #[no_mangle] unsafe extern fn stack_push_callback(data: u16){ (*STACKPTR).memory.push(data) } ```
#[no_mangle] #[no_mangle]
extern fn stack_len_callback() -> usize { unsafe extern fn stack_len_callback() -> usize {
return unsafe{ (*STACKPTR).memory.len()
(*STACKPTR).memory.len()
};
} }
Review
#[no_mangle]
unsafe extern fn stack_len_callback() -> usize {
    (*STACKPTR).memory.len()
}
```rust #[no_mangle] unsafe extern fn stack_len_callback() -> usize { (*STACKPTR).memory.len() } ```
pub fn native(stack: &mut Stack, arg: String) { pub unsafe fn native(stack: &mut Stack, arg: String) {
unsafe { STACKPTR = stack as *mut Stack;
STACKPTR = stack as *mut Stack;
}
#[cfg(not(target_family = "windows"))] #[cfg(not(target_family = "windows"))]
let libsuf: String = ".so".to_owned(); let libsuf: String = ".so".to_owned();
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
let libsuf: String = ".dll".to_owned(); let libsuf: String = ".dll".to_owned();
Review

тут тоже можно добавить unsafe перед fn, и убрать unsafe-блоки

тут тоже можно добавить `unsafe` перед fn, и убрать unsafe-блоки
let libpref: String = "./".to_owned(); let module = Library::new(format!("./{}{}", arg, libsuf)).unwrap();
// C libraries should use
unsafe { // void (*labashka)(unsigned short (*pop)(void), void (*push)(unsigned short), size_t (*len)(void), size_t max_size);
let module = Library::new([libpref, arg, libsuf].join("")).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();
// C libraries should use func(stack_pop_callback,stack_push_callback,stack_len_callback, stack.memory.size());
// 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());
}
} }

View File

@ -47,7 +47,7 @@ pub fn parse(stack: &mut Stack, file_content: &str) {
} }
} }
if name != "" { if name.is_empty() {

для чего это?

p.s.: можно использовать name.is_empty()

для чего это? p.s.: можно использовать `name.is_empty()`
let inst = Instruction { name, arg, data, ispiped: false, isdrained: false }; let inst = Instruction { name, arg, data, ispiped: false, isdrained: false };
stack.program.push(inst); stack.program.push(inst);
} }