Implement new features #6
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
||||
n3tael
commented
```rust
#[no_mangle]
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();
|
||||
n3tael
commented
тут тоже можно добавить тут тоже можно добавить `unsafe` перед fn, и убрать unsafe-блоки
|
||||
|
||||
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());
|
||||
}
|
||||
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(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());
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user
строки 46-59 это буквально код функции
exec
(за ислюкчением того что вместоarg
,tocall
)так что можно просто вызвать функцию
exec
: