Implement new features #6

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

83
Cargo.lock generated
View File

@ -2,6 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "2.0.1" version = "2.0.1"
@ -13,11 +19,88 @@ name = "labast"
version = "1.0.6" version = "1.0.6"
dependencies = [ dependencies = [
"fastrand", "fastrand",
"libloading",
"pico-args", "pico-args",
] ]
[[package]]
name = "libloading"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c571b676ddfc9a8c12f1f3d3085a7b163966a8fd8098a90640953ce5f6170161"
dependencies = [
"cfg-if",
"windows-sys",
]
[[package]] [[package]]
name = "pico-args" name = "pico-args"
version = "0.5.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View File

@ -18,4 +18,5 @@ panic = "abort"
[dependencies] [dependencies]
fastrand = "2.0.1" fastrand = "2.0.1"
libloading = "0.8.1"
pico-args = "0.5.0" pico-args = "0.5.0"

View File

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

View File

@ -9,3 +9,4 @@ pub mod self_modify;
pub mod special; pub mod special;
pub mod specific; pub mod specific;
pub mod stack_manage; pub mod stack_manage;
pub mod native;

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 {

можно просто написать 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()
};
}
#[no_mangle]
extern fn stack_push_callback(data: u16) -> i32 {
#[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) } ```
unsafe {
(*STACKPTR).memory.push(data)
};
return 0;
}
#[no_mangle]
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() } ```
extern fn stack_len_callback() -> usize {
return unsafe{
(*STACKPTR).memory.len()
};
}
Review

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

тут тоже можно добавить `unsafe` перед fn, и убрать unsafe-блоки
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 {

можно использовать макрос format! чтобы передать название файла:

let module = Library::new(format!("./{}{}", arg, libsuf)).unwrap();

ну и также убрать libpref

можно использовать макрос `format!` чтобы передать название файла: ```rust let module = Library::new(format!("./{}{}", arg, libsuf)).unwrap(); ``` ну и также убрать `libpref`
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 );

из-за того что stack_pop_callback, stack_push_callback и stack_len_callback станут unsafe, нужно изменить тип:

let func: Symbol<extern "C" fn(unsafe extern fn() -> u16, unsafe extern fn(u16), unsafe extern fn() -> usize, usize)> = module.get(b"labashka").unwrap();
из-за того что `stack_pop_callback`, `stack_push_callback` и `stack_len_callback` станут `unsafe`, нужно изменить тип: ```rust let func: Symbol<extern "C" fn(unsafe extern fn() -> u16, unsafe extern fn(u16), unsafe extern fn() -> usize, usize)> = module.get(b"labashka").unwrap(); ```
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());
}
}

4
test/native_test.lb Normal file
View File

@ -0,0 +1,4 @@
@ 0
push -1
#native native_test

13
test/native_test.rs Normal file
View File

@ -0,0 +1,13 @@
// Build with
// rustc --crate-type cdylib native_test.rs -o native_test.so
// or
// rustc --crate-type cdylib native_test.rs -o native_test.dll
#[no_mangle]
unsafe extern "C" fn labashka(pop: extern fn() -> u16, push: extern fn(u16) -> i32, len: extern fn () -> usize, max: usize) -> u32 {
println!("At {}, there's {}.", len(), pop());
push(12);
println!("Maximum size is {}.", max);
return 0;
}