diff --git a/Cargo.lock b/Cargo.lock index 991b277..1484a35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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.6" 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" diff --git a/Cargo.toml b/Cargo.toml index dce0edb..2eeb200 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ panic = "abort" [dependencies] fastrand = "2.0.1" +libloading = "0.8.1" pico-args = "0.5.0" diff --git a/src/execute.rs b/src/execute.rs index 8305b10..707f390 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -143,7 +143,11 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name "exec" => { let arg = instruction.arg.clone(); instructions::modules::exec(stack, arg, mod_name.clone()) - } + }, + "native" => { + let arg = instruction.arg.clone(); + instructions::native::native(stack, arg) + }, // Self-modifying "insert" => { diff --git a/src/instructions/mod.rs b/src/instructions/mod.rs index a08c315..586aee4 100644 --- a/src/instructions/mod.rs +++ b/src/instructions/mod.rs @@ -9,3 +9,4 @@ pub mod self_modify; pub mod special; pub mod specific; pub mod stack_manage; +pub mod native; diff --git a/src/instructions/native.rs b/src/instructions/native.rs new file mode 100644 index 0000000..4bd4bbd --- /dev/null +++ b/src/instructions/native.rs @@ -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 { + 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 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()); + } +} + + diff --git a/test/native_test.lb b/test/native_test.lb new file mode 100644 index 0000000..1ec891a --- /dev/null +++ b/test/native_test.lb @@ -0,0 +1,4 @@ +@ 0 + push -1 + #native native_test + diff --git a/test/native_test.rs b/test/native_test.rs new file mode 100644 index 0000000..b6f7fc5 --- /dev/null +++ b/test/native_test.rs @@ -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; +} +