#![deny(unsafe_code)]
#[derive(Clone, Copy)]
pub(crate) struct Entry(seam::Raw);
pub(crate) fn open(path: &std::path::Path) -> Option<Entry> {
use std::os::unix::ffi::OsStrExt;
let path = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?;
seam::open(&path).map(Entry)
}
pub(crate) fn call(entry: Entry, args: &[i64]) -> Option<i64> {
let mut out = 0i64;
(seam::call(entry.0, args, &mut out) == 0).then_some(out)
}
#[allow(unsafe_code)]
mod seam {
use std::ffi::{c_char, c_int, c_void, CStr};
pub(super) type Raw = extern "C" fn(*const i64, usize, *mut i64) -> i32;
const RTLD_NOW: c_int = 2;
#[cfg(target_os = "macos")]
const RTLD_LOCAL: c_int = 4;
#[cfg(not(target_os = "macos"))]
const RTLD_LOCAL: c_int = 0;
extern "C" {
fn dlopen(path: *const c_char, mode: c_int) -> *mut c_void;
fn dlsym(handle: *mut c_void, name: *const c_char) -> *mut c_void;
}
pub(super) fn open(path: &CStr) -> Option<Raw> {
let handle = unsafe { dlopen(path.as_ptr(), RTLD_NOW | RTLD_LOCAL) };
if handle.is_null() {
return None;
}
let symbol = unsafe { dlsym(handle, c"blue_def_entry".as_ptr()) };
if symbol.is_null() {
return None;
}
Some(unsafe { std::mem::transmute::<*mut c_void, Raw>(symbol) })
}
pub(super) fn call(entry: Raw, args: &[i64], out: &mut i64) -> i32 {
entry(args.as_ptr(), args.len(), out)
}
}