use std::ffi::c_char;
use std::path::Path;
use anyhow::Result;
use crate::DynLib;
pub struct VTablePlugin<T: Copy> {
_lib: DynLib,
vtable: T,
}
impl<T: Copy> VTablePlugin<T> {
pub unsafe fn load(path: &Path, symbol: &[u8]) -> Result<Self> {
let lib = unsafe { DynLib::load(path) }?;
unsafe { Self::from_lib(lib, symbol) }
}
pub unsafe fn from_lib(lib: DynLib, symbol: &[u8]) -> Result<Self> {
let getter: unsafe extern "C" fn() -> *const T = unsafe { lib.symbol(symbol) }?;
let vtable = unsafe { getter() };
let vtable = unsafe { vtable.as_ref() }
.copied()
.ok_or_else(|| anyhow::anyhow!("vtable getter returned null"))?;
Ok(Self { _lib: lib, vtable })
}
pub fn vtable(&self) -> &T {
&self.vtable
}
}
pub type MathSession = *mut std::ffi::c_void;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct GeneratedFunction {
pub name: *const c_char,
pub signature: *const c_char,
pub arg_count: u32,
pub id: u32,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MathModuleVtable {
pub create_session: unsafe extern "C" fn() -> MathSession,
pub destroy_session: unsafe extern "C" fn(MathSession),
pub add_i32: unsafe extern "C" fn(MathSession, i32, i32) -> i32,
pub add_i64: unsafe extern "C" fn(MathSession, i64, i64) -> i64,
pub add_f64: unsafe extern "C" fn(MathSession, f64, f64) -> f64,
pub mul_i32: unsafe extern "C" fn(MathSession, i32, i32) -> i32,
pub mul_f64: unsafe extern "C" fn(MathSession, f64, f64) -> f64,
pub pi: unsafe extern "C" fn(MathSession) -> f64,
pub tau: unsafe extern "C" fn(MathSession) -> f64,
pub generated_count: unsafe extern "C" fn(MathSession) -> u32,
pub generated_at: unsafe extern "C" fn(MathSession, u32) -> GeneratedFunction,
pub module_name: unsafe extern "C" fn() -> *const c_char,
pub module_version: unsafe extern "C" fn() -> u32,
}
unsafe impl Send for MathModuleVtable {}
unsafe impl Sync for MathModuleVtable {}
#[cfg(test)]
mod cpp_math_tests {
use super::*;
use std::ffi::CStr;
use std::path::PathBuf;
fn math_module_path() -> PathBuf {
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.push("../../cpp/build/math_module");
p.push("libmath_module.so");
p
}
#[test]
fn load_cpp_math_module_via_vtable() {
let path = math_module_path();
if !path.exists() {
eprintln!("SKIP: {} not found — build cpp/ first", path.display());
return;
}
let plugin =
unsafe { VTablePlugin::<MathModuleVtable>::load(&path, b"math_module_get_vtable\0") }
.expect("failed to load math_module");
let vt = plugin.vtable();
unsafe {
let name = CStr::from_ptr((vt.module_name)());
assert_eq!(name.to_str().unwrap(), "core-ast-math");
assert_eq!((vt.module_version)(), 1);
}
let session = unsafe { (vt.create_session)() };
assert!(!session.is_null());
assert_eq!(unsafe { (vt.generated_count)(session) }, 0);
let result = unsafe { (vt.add_i32)(session, 10, 20) };
assert_eq!(result, 30);
assert_eq!(unsafe { (vt.generated_count)(session) }, 1);
let result = unsafe { (vt.mul_f64)(session, 3.0, 7.0) };
assert!((result - 21.0).abs() < 1e-10);
assert_eq!(unsafe { (vt.generated_count)(session) }, 2);
let pi_val = unsafe { (vt.pi)(session) };
assert!((pi_val - std::f64::consts::PI).abs() < 1e-10);
assert_eq!(unsafe { (vt.generated_count)(session) }, 3);
let func0 = unsafe { (vt.generated_at)(session, 0) };
let func0_name = unsafe { CStr::from_ptr(func0.name) }.to_str().unwrap();
assert_eq!(func0_name, "add_i32");
let func1 = unsafe { (vt.generated_at)(session, 1) };
let func1_name = unsafe { CStr::from_ptr(func1.name) }.to_str().unwrap();
assert_eq!(func1_name, "mul_f64");
let func2 = unsafe { (vt.generated_at)(session, 2) };
let func2_name = unsafe { CStr::from_ptr(func2.name) }.to_str().unwrap();
assert_eq!(func2_name, "pi");
let _ = unsafe { (vt.add_i32)(session, 1, 2) };
assert_eq!(unsafe { (vt.generated_count)(session) }, 3);
unsafe { (vt.destroy_session)(session) };
}
}