#![allow(ambiguous_glob_reexports)]
#![allow(unused_variables)]
#![allow(dead_code)]
#![allow(missing_docs)]
use crate::eval::Value;
use crate::diagnostics::Error;
use std::fmt;
pub mod marshal;
pub mod registry;
pub mod examples;
pub mod library;
pub mod c_types;
pub mod callback;
pub mod memory;
pub mod safety;
pub mod arithmetic_functions;
pub mod string_functions;
pub mod list_functions;
pub mod type_checking_functions;
pub mod io_functions;
pub mod builtin_ffi_module;
#[cfg(feature = "ffi")]
pub mod libffi_integration;
pub mod scheme_api;
pub mod profiling;
pub mod ffi_signature;
pub mod registered_function;
pub mod ffi_stats;
pub mod ffi_registry;
pub mod ffi_bridge;
pub use marshal::*;
pub use registry::*;
pub use library::*;
pub use c_types::*;
pub use callback::*;
pub use memory::*;
pub use safety::*;
pub use arithmetic_functions::*;
pub use string_functions::*;
pub use list_functions::*;
pub use type_checking_functions::*;
pub use io_functions::*;
pub use builtin_ffi_module::*;
#[cfg(feature = "ffi")]
pub use libffi_integration::*;
pub use scheme_api::*;
#[allow(ambiguous_glob_reexports)]
pub use profiling::*;
pub use ffi_signature::*;
pub use registered_function::*;
pub use ffi_stats::*;
pub use ffi_registry::*;
pub use ffi_bridge::*;
#[derive(Debug, Clone)]
pub enum FfiError {
FunctionNotFound(String),
ArityMismatch {
function: String,
expected: AritySpec,
actual: usize,
},
TypeMismatch {
function: String,
parameter: usize,
expected: String,
actual: String,
},
RuntimeError {
function: String,
message: String,
},
InvalidSignature(String),
}
impl fmt::Display for FfiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FfiError::FunctionNotFound(name) => {
write!(f, "FFI function not found: {name}")
}
FfiError::ArityMismatch { function, expected, actual } => {
write!(f, "FFI function '{function}' expects {expected} arguments, got {actual}")
}
FfiError::TypeMismatch { function, parameter, expected, actual } => {
write!(f, "FFI function '{function}' parameter {parameter}: expected {expected}, got {actual}")
}
FfiError::RuntimeError { function, message } => {
write!(f, "FFI function '{function}' runtime error: {message}")
}
FfiError::InvalidSignature(msg) => {
write!(f, "Invalid FFI function signature: {msg}")
}
}
}
}
impl std::error::Error for FfiError {}
impl From<FfiError> for Error {
fn from(ffi_error: FfiError) -> Self {
Error::runtime_error(ffi_error.to_string(), None)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum AritySpec {
Exact(usize),
AtLeast(usize),
Range(usize, usize),
}
impl fmt::Display for AritySpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AritySpec::Exact(n) => write!(f, "exactly {n}"),
AritySpec::AtLeast(n) => write!(f, "at least {n}"),
AritySpec::Range(min, max) => write!(f, "between {min} and {max}"),
}
}
}
impl AritySpec {
pub fn check(&self, arg_count: usize) -> bool {
match self {
AritySpec::Exact(n) => arg_count == *n,
AritySpec::AtLeast(n) => arg_count >= *n,
AritySpec::Range(min, max) => arg_count >= *min && arg_count <= *max,
}
}
}
pub trait FfiFunction: Send + Sync {
fn signature(&self) -> &FfiSignature;
fn call(&self, args: &[Value]) -> std::result::Result<Value, FfiError>;
fn validate_args(&self, args: &[Value]) -> std::result::Result<(), FfiError> {
let sig = self.signature();
if !sig.arity.check(args.len()) {
return Err(FfiError::ArityMismatch {
function: sig.name.clone(),
expected: sig.arity.clone(),
actual: args.len(),
});
}
Ok(())
}
}