Add "#native" instruction.

This commit is contained in:
Bedohswe 2024-02-09 17:39:47 +05:00
parent 75c5dce36e
commit 75c8c7a885
7 changed files with 154 additions and 1 deletions

83
Cargo.lock generated
View File

@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "fastrand"
version = "2.0.1"
@ -13,11 +19,88 @@ name = "labast"
version = "1.0.5"
dependencies = [
"fastrand",
"libloading",
"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]]
name = "pico-args"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]
fastrand = "2.0.1"
libloading = "0.8.1"
pico-args = "0.5.0"

3
scripts/native_test.lb Normal file
View File

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

12
scripts/native_test.rs Normal file
View File

@ -0,0 +1,12 @@
// 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;
}

View File

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

View File

@ -39,4 +39,5 @@ pub mod print;
pub mod println;
pub mod insert;
pub mod random;
pub mod rjmp;
pub mod rjmp;
pub mod native;

View File

@ -0,0 +1,49 @@
use crate::Stack;
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()
};
}
#[no_mangle]
extern fn stack_push_callback(data: u16) -> i32 {
unsafe {
(*STACKPTR).memory.push(data)
};
return 0;
}
#[no_mangle]
extern fn stack_len_callback() -> usize {
return unsafe{
(*STACKPTR).memory.len()
};
}
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 {
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 );
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());
}
}