binks/import/extern_code/
use_extern_code.rs

1use crate::interpreter::line_handeler::Data;
2use libloading as lib;
3
4/// load and call to a function in a dynamic library file the path to the file need to be without the file extentions (no .dll\.so\...)  
5pub fn call_extern(func: &str, path: &str, args: Vec<Data>) -> anyhow::Result<Data> {
6    let ext = if cfg!(target_os = "windows") {
7        ".dll"
8    } else if cfg!(target_os = "linux") {
9        ".so"
10    } else if cfg!(target_os = "macos") {
11        ".dylib"
12    } else {
13        return Err(anyhow::anyhow!("canot detect OS"));
14    };
15    let mut full_path = path.to_string();
16    full_path.push_str(ext);
17    let lib = lib::Library::new(full_path)?;
18    unsafe {
19        let func: lib::Symbol<unsafe extern "C" fn(Vec<Data>) -> anyhow::Result<Data>> =
20            lib.get(&func.to_string().into_bytes()[..])?;
21        Ok(func(args)?)
22    }
23}