use mlua::prelude::*; use std::{env, path::PathBuf}; pub fn make() -> LuaResult { let (dylib_path, dylib_ext, separator); if cfg!(target_os = "macos") { dylib_path = env::var("DYLD_FALLBACK_LIBRARY_PATH").unwrap(); dylib_ext = "dylib"; separator = ":"; } else if cfg!(target_os = "linux") { dylib_path = env::var("LD_LIBRARY_PATH").unwrap(); dylib_ext = "so"; separator = ":"; } else if cfg!(target_os = "windows") { dylib_path = env::var("PATH").unwrap(); dylib_ext = "dll"; separator = ";"; } else { panic!("unknown target os"); }; let mut cpath = dylib_path .split(separator) .take(3) .map(|p| { let mut path = PathBuf::from(p); path.push(format!("lib?.{}", dylib_ext)); path.to_str().unwrap().to_owned() }) .collect::>() .join(";"); if cfg!(target_os = "windows") { cpath = cpath.replace("\\", "\\\\"); cpath = cpath.replace("lib?.", "?."); } let lua = unsafe { Lua::unsafe_new() }; // To be able to load C modules lua.load(&format!("package.cpath = \"{}\"", cpath)).exec()?; Ok(lua) }