use libffi::middle instead of libffi::low were possible
This commit is contained in:
committed by
Bennet Bleßmann
parent
6205f2f1f1
commit
3d2439c92b
434
src/ffi.rs
434
src/ffi.rs
@@ -25,17 +25,17 @@ use crate::forms::Number;
|
|||||||
use crate::parser::ast::Fixnum;
|
use crate::parser::ast::Fixnum;
|
||||||
|
|
||||||
use dashu::Integer;
|
use dashu::Integer;
|
||||||
|
use libffi::middle::{Arg, Cif, CodePtr, Type};
|
||||||
|
use libloading::{Library, Symbol};
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use std::alloc::{self, Layout};
|
use std::alloc::{self, Layout};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::ffi::{c_void, CString};
|
use std::ffi::{c_void, CString};
|
||||||
use std::ptr::addr_of_mut;
|
use std::fmt::Debug;
|
||||||
|
use std::marker::PhantomData;
|
||||||
use libffi::low::type_tag::STRUCT;
|
use std::ops::Deref;
|
||||||
use libffi::low::{ffi_abi_FFI_DEFAULT_ABI, ffi_cif, ffi_type, prep_cif, types, CodePtr};
|
|
||||||
use libloading::{Library, Symbol};
|
|
||||||
|
|
||||||
pub struct FunctionDefinition {
|
pub struct FunctionDefinition {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -45,8 +45,8 @@ pub struct FunctionDefinition {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FunctionImpl {
|
pub struct FunctionImpl {
|
||||||
cif: ffi_cif,
|
cif: Cif,
|
||||||
args: Vec<*mut ffi_type>,
|
args: Vec<Type>,
|
||||||
code_ptr: CodePtr,
|
code_ptr: CodePtr,
|
||||||
return_struct_name: Option<String>,
|
return_struct_name: Option<String>,
|
||||||
}
|
}
|
||||||
@@ -57,26 +57,65 @@ pub struct ForeignFunctionTable {
|
|||||||
structs: HashMap<String, StructImpl>,
|
structs: HashMap<String, StructImpl>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
struct StructImpl {
|
struct StructImpl {
|
||||||
ffi_type: ffi_type,
|
ffi_type: Type,
|
||||||
fields: Vec<*mut ffi_type>,
|
fields: Vec<Type>,
|
||||||
atom_fields: Vec<Atom>,
|
atom_fields: Vec<Atom>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for StructImpl {
|
struct PointerArgs<'a, 'val> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
memory: Vec<Arg>,
|
||||||
f.debug_struct("StructImpl")
|
phantom: PhantomData<&'a mut ArgValues<'val>>,
|
||||||
.field("ffi_type", &&"<???>")
|
}
|
||||||
.field("fields", &self.fields)
|
|
||||||
.field("atom_fields", &self.atom_fields)
|
impl Deref for PointerArgs<'_, '_> {
|
||||||
.finish()
|
type Target = [Arg];
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.memory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PointerArgs {
|
enum ArgValues<'a> {
|
||||||
pointers: Vec<*mut c_void>,
|
U8(u8),
|
||||||
_memory: Vec<Box<dyn Any>>,
|
I8(i8),
|
||||||
|
U16(u16),
|
||||||
|
I16(i16),
|
||||||
|
U32(u32),
|
||||||
|
I32(i32),
|
||||||
|
U64(u64),
|
||||||
|
I64(i64),
|
||||||
|
F32(f32),
|
||||||
|
F64(f64),
|
||||||
|
Ptr(*mut c_void, PhantomData<&'a CString>),
|
||||||
|
Struct(Box<dyn Any>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'val> ArgValues<'val> {
|
||||||
|
fn new(
|
||||||
|
val: &'val mut Value,
|
||||||
|
arg_type: &Type,
|
||||||
|
structs_table: &mut HashMap<String, StructImpl>,
|
||||||
|
) -> Result<Self, FFIError> {
|
||||||
|
match (unsafe { *arg_type.as_raw_ptr() }).type_ as u32 {
|
||||||
|
libffi::raw::FFI_TYPE_UINT8 => Ok(Self::U8(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_SINT8 => Ok(Self::I8(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_UINT16 => Ok(Self::U16(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_SINT16 => Ok(Self::I16(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_UINT32 => Ok(Self::U32(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_SINT32 => Ok(Self::I32(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_UINT64 => Ok(Self::U64(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_SINT64 => Ok(Self::I64(val.as_int()?)),
|
||||||
|
libffi::raw::FFI_TYPE_FLOAT => Ok(Self::F32(val.as_float()? as f32)),
|
||||||
|
libffi::raw::FFI_TYPE_DOUBLE => Ok(Self::F64(val.as_float()?)),
|
||||||
|
libffi::raw::FFI_TYPE_POINTER => Ok(Self::Ptr(val.as_ptr()?, PhantomData)),
|
||||||
|
libffi::raw::FFI_TYPE_STRUCT => Ok(Self::Struct(
|
||||||
|
ForeignFunctionTable::build_struct(val, structs_table)?.0,
|
||||||
|
)),
|
||||||
|
_ => Err(FFIError::InvalidFFIType),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ForeignFunctionTable {
|
impl ForeignFunctionTable {
|
||||||
@@ -85,16 +124,25 @@ impl ForeignFunctionTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn define_struct(&mut self, name: &str, atom_fields: Vec<Atom>) -> Result<(), FFIError> {
|
pub fn define_struct(&mut self, name: &str, atom_fields: Vec<Atom>) -> Result<(), FFIError> {
|
||||||
let mut fields: Vec<_> = atom_fields
|
let fields: Vec<_> = atom_fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| self.map_type_ffi(x))
|
.map(|x| self.map_type_ffi(x))
|
||||||
.collect::<Result<_, FFIError>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
fields.push(std::ptr::null_mut::<ffi_type>());
|
let struct_type = libffi::middle::Type::structure(fields.iter().cloned());
|
||||||
let struct_type = ffi_type {
|
|
||||||
type_: STRUCT,
|
unsafe {
|
||||||
elements: fields.as_mut_ptr(),
|
// ensure that size and alignment of struct_type are set properly
|
||||||
..Default::default()
|
use libffi::low::{ffi_abi_FFI_DEFAULT_ABI, prep_cif};
|
||||||
|
prep_cif(
|
||||||
|
&mut Default::default(),
|
||||||
|
ffi_abi_FFI_DEFAULT_ABI,
|
||||||
|
1,
|
||||||
|
struct_type.as_raw_ptr(),
|
||||||
|
[struct_type.as_raw_ptr()].as_mut_ptr(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
self.structs.insert(
|
self.structs.insert(
|
||||||
name.to_string(),
|
name.to_string(),
|
||||||
StructImpl {
|
StructImpl {
|
||||||
@@ -106,24 +154,24 @@ impl ForeignFunctionTable {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_type_ffi(&mut self, source: &Atom) -> Result<*mut ffi_type, FFIError> {
|
fn map_type_ffi(&mut self, source: &Atom) -> Result<libffi::middle::Type, FFIError> {
|
||||||
Ok(match source {
|
Ok(match source {
|
||||||
atom!("sint64") => addr_of_mut!(types::sint64),
|
atom!("sint64") => libffi::middle::Type::i64(),
|
||||||
atom!("sint32") => addr_of_mut!(types::sint32),
|
atom!("sint32") => libffi::middle::Type::i32(),
|
||||||
atom!("sint16") => addr_of_mut!(types::sint16),
|
atom!("sint16") => libffi::middle::Type::i16(),
|
||||||
atom!("sint8") => addr_of_mut!(types::sint8),
|
atom!("sint8") => libffi::middle::Type::i8(),
|
||||||
atom!("uint64") => addr_of_mut!(types::uint64),
|
atom!("uint64") => libffi::middle::Type::u64(),
|
||||||
atom!("uint32") => addr_of_mut!(types::uint32),
|
atom!("uint32") => libffi::middle::Type::u32(),
|
||||||
atom!("uint16") => addr_of_mut!(types::uint16),
|
atom!("uint16") => libffi::middle::Type::u16(),
|
||||||
atom!("uint8") => addr_of_mut!(types::uint8),
|
atom!("uint8") => libffi::middle::Type::u8(),
|
||||||
atom!("bool") => addr_of_mut!(types::sint8),
|
atom!("bool") => libffi::middle::Type::i8(),
|
||||||
atom!("void") => addr_of_mut!(types::void),
|
atom!("void") => libffi::middle::Type::void(),
|
||||||
atom!("cstr") => addr_of_mut!(types::pointer),
|
atom!("cstr") => libffi::middle::Type::pointer(),
|
||||||
atom!("ptr") => addr_of_mut!(types::pointer),
|
atom!("ptr") => libffi::middle::Type::pointer(),
|
||||||
atom!("f32") => addr_of_mut!(types::float),
|
atom!("f32") => libffi::middle::Type::f32(),
|
||||||
atom!("f64") => addr_of_mut!(types::double),
|
atom!("f64") => libffi::middle::Type::f64(),
|
||||||
struct_name => match self.structs.get_mut(&*struct_name.as_str()) {
|
struct_name => match self.structs.get_mut(&*struct_name.as_str()) {
|
||||||
Some(ref mut struct_type) => &mut struct_type.ffi_type,
|
Some(ref mut struct_type) => struct_type.ffi_type.clone(),
|
||||||
None => return Err(FFIError::InvalidFFIType),
|
None => return Err(FFIError::InvalidFFIType),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -141,29 +189,21 @@ impl ForeignFunctionTable {
|
|||||||
let symbol_name: CString = CString::new(function.name.clone())?;
|
let symbol_name: CString = CString::new(function.name.clone())?;
|
||||||
let code_ptr: Symbol<*mut c_void> =
|
let code_ptr: Symbol<*mut c_void> =
|
||||||
library.get(&symbol_name.into_bytes_with_nul())?;
|
library.get(&symbol_name.into_bytes_with_nul())?;
|
||||||
let mut args: Vec<_> = function
|
let args: Vec<_> = function
|
||||||
.args
|
.args
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| self.map_type_ffi(x))
|
.map(|x| self.map_type_ffi(x))
|
||||||
.collect::<Result<_, FFIError>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
let mut cif: ffi_cif = Default::default();
|
let result = self.map_type_ffi(&function.return_value)?;
|
||||||
prep_cif(
|
|
||||||
&mut cif,
|
|
||||||
ffi_abi_FFI_DEFAULT_ABI,
|
|
||||||
args.len(),
|
|
||||||
self.map_type_ffi(&function.return_value)?,
|
|
||||||
args.as_mut_ptr(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let return_struct_name = if (*self.map_type_ffi(&function.return_value)?).type_
|
let cif = libffi::middle::Cif::new(args.clone(), result.clone());
|
||||||
as u32
|
|
||||||
== libffi::raw::FFI_TYPE_STRUCT
|
let return_struct_name =
|
||||||
{
|
if (*result.as_raw_ptr()).type_ as u32 == libffi::raw::FFI_TYPE_STRUCT {
|
||||||
Some(function.return_value.as_str().to_string())
|
Some(function.return_value.as_str().to_string())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
ff_table.table.insert(
|
ff_table.table.insert(
|
||||||
function.name.clone(),
|
function.name.clone(),
|
||||||
@@ -181,61 +221,33 @@ impl ForeignFunctionTable {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_pointer_args(
|
fn build_pointer_args<'args, 'val>(args: &[ArgValues<'val>]) -> PointerArgs<'args, 'val> {
|
||||||
args: &mut [Value],
|
let args = args
|
||||||
type_args: &[*mut ffi_type],
|
.iter()
|
||||||
structs_table: &mut HashMap<String, StructImpl>,
|
.map(|arg| match arg {
|
||||||
) -> Result<PointerArgs, FFIError> {
|
ArgValues::U8(a) => libffi::middle::arg(a),
|
||||||
let mut pointers = Vec::with_capacity(args.len());
|
ArgValues::I8(a) => libffi::middle::arg(a),
|
||||||
let mut _memory = Vec::new();
|
ArgValues::U16(a) => libffi::middle::arg(a),
|
||||||
for i in 0..args.len() {
|
ArgValues::I16(a) => libffi::middle::arg(a),
|
||||||
let field_type = type_args[i];
|
ArgValues::U32(a) => libffi::middle::arg(a),
|
||||||
unsafe {
|
ArgValues::I32(a) => libffi::middle::arg(a),
|
||||||
macro_rules! push_int {
|
ArgValues::U64(a) => libffi::middle::arg(a),
|
||||||
($type:ty) => {{
|
ArgValues::I64(a) => libffi::middle::arg(a),
|
||||||
let n: $type = args[i].as_int()?;
|
ArgValues::F32(a) => libffi::middle::arg(a),
|
||||||
let mut box_value = Box::new(n) as Box<dyn Any>;
|
ArgValues::F64(a) => libffi::middle::arg(a),
|
||||||
pointers.push(&mut *box_value as *mut _ as *mut c_void);
|
ArgValues::Ptr(ptr, _) => unsafe { std::mem::transmute::<*mut c_void, Arg>(*ptr) },
|
||||||
_memory.push(box_value);
|
ArgValues::Struct(s) => unsafe {
|
||||||
}};
|
std::mem::transmute::<*const c_void, Arg>(
|
||||||
}
|
s.as_ref() as *const _ as *const c_void
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
match (*field_type).type_ as u32 {
|
PointerArgs {
|
||||||
libffi::raw::FFI_TYPE_UINT8 => push_int!(u8),
|
memory: args,
|
||||||
libffi::raw::FFI_TYPE_SINT8 => push_int!(i8),
|
phantom: PhantomData,
|
||||||
libffi::raw::FFI_TYPE_UINT16 => push_int!(u16),
|
|
||||||
libffi::raw::FFI_TYPE_SINT16 => push_int!(i16),
|
|
||||||
libffi::raw::FFI_TYPE_UINT32 => push_int!(u32),
|
|
||||||
libffi::raw::FFI_TYPE_SINT32 => push_int!(i32),
|
|
||||||
libffi::raw::FFI_TYPE_UINT64 => push_int!(u64),
|
|
||||||
libffi::raw::FFI_TYPE_SINT64 => push_int!(i64),
|
|
||||||
libffi::raw::FFI_TYPE_FLOAT => {
|
|
||||||
let n: f32 = args[i].as_float()? as f32;
|
|
||||||
let mut box_value = Box::new(n) as Box<dyn Any>;
|
|
||||||
pointers.push(&mut *box_value as *mut _ as *mut c_void);
|
|
||||||
_memory.push(box_value);
|
|
||||||
}
|
|
||||||
libffi::raw::FFI_TYPE_DOUBLE => {
|
|
||||||
let n: f64 = args[i].as_float()?;
|
|
||||||
let mut box_value = Box::new(n) as Box<dyn Any>;
|
|
||||||
pointers.push(&mut *box_value as *mut _ as *mut c_void);
|
|
||||||
_memory.push(box_value);
|
|
||||||
}
|
|
||||||
libffi::raw::FFI_TYPE_POINTER => {
|
|
||||||
let ptr: *mut c_void = args[i].as_ptr()?;
|
|
||||||
pointers.push(ptr);
|
|
||||||
}
|
|
||||||
libffi::raw::FFI_TYPE_STRUCT => {
|
|
||||||
let (mut ptr, _size, _align) =
|
|
||||||
Self::build_struct(&mut args[i], structs_table)?;
|
|
||||||
pointers.push(&mut *ptr as *mut _ as *mut c_void);
|
|
||||||
_memory.push(ptr);
|
|
||||||
}
|
|
||||||
_ => return Err(FFIError::InvalidFFIType),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(PointerArgs { pointers, _memory })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_struct(
|
fn build_struct(
|
||||||
@@ -245,13 +257,11 @@ impl ForeignFunctionTable {
|
|||||||
match arg {
|
match arg {
|
||||||
Value::Struct(ref name, ref mut struct_args) => {
|
Value::Struct(ref name, ref mut struct_args) => {
|
||||||
if let Some(ref mut struct_type) = structs_table.clone().get_mut(name) {
|
if let Some(ref mut struct_type) = structs_table.clone().get_mut(name) {
|
||||||
let layout = Layout::from_size_align(
|
let ffi_type = unsafe { *struct_type.ffi_type.as_raw_ptr() };
|
||||||
struct_type.ffi_type.size,
|
let layout =
|
||||||
struct_type.ffi_type.alignment.into(),
|
Layout::from_size_align(ffi_type.size, ffi_type.alignment.into()).unwrap();
|
||||||
)
|
let align = ffi_type.alignment as usize;
|
||||||
.unwrap();
|
let size = ffi_type.size;
|
||||||
let align = struct_type.ffi_type.alignment as usize;
|
|
||||||
let size = struct_type.ffi_type.size;
|
|
||||||
let ptr = unsafe { alloc::alloc(layout) as *mut c_void };
|
let ptr = unsafe { alloc::alloc(layout) as *mut c_void };
|
||||||
|
|
||||||
if ptr.is_null() {
|
if ptr.is_null() {
|
||||||
@@ -280,9 +290,9 @@ impl ForeignFunctionTable {
|
|||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
let field = struct_type.fields[i];
|
let field = &struct_type.fields[i];
|
||||||
unsafe {
|
unsafe {
|
||||||
match (*field).type_ as u32 {
|
match (*field.as_raw_ptr()).type_ as u32 {
|
||||||
libffi::raw::FFI_TYPE_UINT8 => try_write_int!(u8),
|
libffi::raw::FFI_TYPE_UINT8 => try_write_int!(u8),
|
||||||
libffi::raw::FFI_TYPE_SINT8 => try_write_int!(i8),
|
libffi::raw::FFI_TYPE_SINT8 => try_write_int!(i8),
|
||||||
libffi::raw::FFI_TYPE_UINT16 => try_write_int!(u16),
|
libffi::raw::FFI_TYPE_UINT16 => try_write_int!(u16),
|
||||||
@@ -336,93 +346,96 @@ impl ForeignFunctionTable {
|
|||||||
arena: &mut Arena,
|
arena: &mut Arena,
|
||||||
) -> Result<Value, FFIError> {
|
) -> Result<Value, FFIError> {
|
||||||
let function_impl = self.table.get_mut(name).ok_or(FFIError::FunctionNotFound)?;
|
let function_impl = self.table.get_mut(name).ok_or(FFIError::FunctionNotFound)?;
|
||||||
let mut pointer_args =
|
if function_impl.args.len() != args.len() {
|
||||||
Self::build_pointer_args(&mut args, &function_impl.args, &mut self.structs)?;
|
return Err(FFIError::ArgCountMismatch);
|
||||||
|
}
|
||||||
|
|
||||||
unsafe {
|
let args = args
|
||||||
macro_rules! call_and_return {
|
.iter_mut()
|
||||||
($type:ty) => {{
|
.zip(function_impl.args.iter())
|
||||||
let mut n: $type = 0;
|
.map(|(arg, arg_type)| ArgValues::new(arg, arg_type, &mut self.structs))
|
||||||
libffi::raw::ffi_call(
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
&mut function_impl.cif,
|
|
||||||
Some(*function_impl.code_ptr.as_safe_fun()),
|
let args = Self::build_pointer_args(&args);
|
||||||
&mut n as *mut _ as *mut c_void,
|
|
||||||
pointer_args.pointers.as_mut_ptr() as *mut *mut c_void,
|
macro_rules! call_and_return_int {
|
||||||
);
|
($type:ty) => {{
|
||||||
Ok(Value::Number(fixnum!(Number, n, arena)))
|
let n = function_impl
|
||||||
}};
|
.cif
|
||||||
|
.call::<$type>(function_impl.code_ptr, &args);
|
||||||
|
Ok(Value::Number(fixnum!(Number, n, arena)))
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! call_and_return_float {
|
||||||
|
($type:ty) => {{
|
||||||
|
let n = function_impl
|
||||||
|
.cif
|
||||||
|
.call::<$type>(function_impl.code_ptr, &args);
|
||||||
|
Ok(Value::Number(Number::Float(OrderedFloat(f64::from(n)))))
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
let ffi_rtype = unsafe { *(*function_impl.cif.as_raw_ptr()).rtype };
|
||||||
|
|
||||||
|
match ffi_rtype.type_ as u32 {
|
||||||
|
libffi::raw::FFI_TYPE_VOID => {
|
||||||
|
unsafe {
|
||||||
|
function_impl
|
||||||
|
.cif
|
||||||
|
.call::<c_void>(function_impl.code_ptr, &args)
|
||||||
|
};
|
||||||
|
Ok(Value::Number(Number::Fixnum(Fixnum::build_with(0))))
|
||||||
}
|
}
|
||||||
|
libffi::raw::FFI_TYPE_UINT8 => unsafe { call_and_return_int!(u8) },
|
||||||
|
libffi::raw::FFI_TYPE_SINT8 => unsafe { call_and_return_int!(i8) },
|
||||||
|
libffi::raw::FFI_TYPE_UINT16 => unsafe { call_and_return_int!(u16) },
|
||||||
|
libffi::raw::FFI_TYPE_SINT16 => unsafe { call_and_return_int!(i16) },
|
||||||
|
libffi::raw::FFI_TYPE_UINT32 => unsafe { call_and_return_int!(u32) },
|
||||||
|
libffi::raw::FFI_TYPE_SINT32 => unsafe { call_and_return_int!(i32) },
|
||||||
|
libffi::raw::FFI_TYPE_UINT64 => unsafe { call_and_return_int!(u64) },
|
||||||
|
libffi::raw::FFI_TYPE_SINT64 => unsafe { call_and_return_int!(i64) },
|
||||||
|
libffi::raw::FFI_TYPE_POINTER => {
|
||||||
|
let ptr = unsafe {
|
||||||
|
function_impl
|
||||||
|
.cif
|
||||||
|
.call::<*mut c_void>(function_impl.code_ptr, &args)
|
||||||
|
};
|
||||||
|
Ok(Value::Number(fixnum!(Number, ptr as isize, arena)))
|
||||||
|
}
|
||||||
|
libffi::raw::FFI_TYPE_FLOAT => unsafe { call_and_return_float!(f32) },
|
||||||
|
libffi::raw::FFI_TYPE_DOUBLE => unsafe { call_and_return_float!(f64) },
|
||||||
|
libffi::raw::FFI_TYPE_STRUCT => {
|
||||||
|
let name = &function_impl
|
||||||
|
.return_struct_name
|
||||||
|
.clone()
|
||||||
|
.ok_or(FFIError::StructNotFound)?;
|
||||||
|
let struct_type = self.structs.get(name).ok_or(FFIError::StructNotFound)?;
|
||||||
|
let ffi_type = unsafe { *struct_type.ffi_type.as_raw_ptr() };
|
||||||
|
let layout =
|
||||||
|
Layout::from_size_align(ffi_type.size, ffi_type.alignment.into()).unwrap();
|
||||||
|
let ptr = unsafe { alloc::alloc(layout) };
|
||||||
|
|
||||||
match (*function_impl.cif.rtype).type_ as u32 {
|
if ptr.is_null() {
|
||||||
libffi::raw::FFI_TYPE_VOID => call_and_return!(i32),
|
return Err(FFIError::AllocationFailed);
|
||||||
libffi::raw::FFI_TYPE_UINT8 => call_and_return!(u8),
|
|
||||||
libffi::raw::FFI_TYPE_SINT8 => call_and_return!(i8),
|
|
||||||
libffi::raw::FFI_TYPE_UINT16 => call_and_return!(u16),
|
|
||||||
libffi::raw::FFI_TYPE_SINT16 => call_and_return!(i16),
|
|
||||||
libffi::raw::FFI_TYPE_UINT32 => call_and_return!(u32),
|
|
||||||
libffi::raw::FFI_TYPE_SINT32 => call_and_return!(i32),
|
|
||||||
libffi::raw::FFI_TYPE_UINT64 => call_and_return!(u64),
|
|
||||||
libffi::raw::FFI_TYPE_SINT64 => call_and_return!(i64),
|
|
||||||
libffi::raw::FFI_TYPE_POINTER => {
|
|
||||||
let mut n: *mut c_void = std::ptr::null_mut();
|
|
||||||
libffi::raw::ffi_call(
|
|
||||||
&mut function_impl.cif,
|
|
||||||
Some(*function_impl.code_ptr.as_safe_fun()),
|
|
||||||
&mut n as *mut *mut c_void as *mut c_void,
|
|
||||||
pointer_args.pointers.as_mut_ptr(),
|
|
||||||
);
|
|
||||||
Ok(Value::Number(fixnum!(Number, n as isize, arena)))
|
|
||||||
}
|
}
|
||||||
libffi::raw::FFI_TYPE_FLOAT => {
|
|
||||||
let mut n: f32 = 0.0;
|
let ptr_args: &[Arg] = &args;
|
||||||
|
|
||||||
|
unsafe {
|
||||||
libffi::raw::ffi_call(
|
libffi::raw::ffi_call(
|
||||||
&mut function_impl.cif,
|
function_impl.cif.as_raw_ptr(),
|
||||||
Some(*function_impl.code_ptr.as_safe_fun()),
|
Some(*function_impl.code_ptr.as_safe_fun()),
|
||||||
&mut n as *mut _ as *mut c_void,
|
ptr as *mut c_void,
|
||||||
pointer_args.pointers.as_mut_ptr(),
|
ptr_args.as_ptr() as *mut *mut c_void,
|
||||||
);
|
|
||||||
Ok(Value::Number(Number::Float(OrderedFloat(n.into()))))
|
|
||||||
}
|
|
||||||
libffi::raw::FFI_TYPE_DOUBLE => {
|
|
||||||
let mut n: f64 = 0.0;
|
|
||||||
libffi::raw::ffi_call(
|
|
||||||
&mut function_impl.cif,
|
|
||||||
Some(*function_impl.code_ptr.as_safe_fun()),
|
|
||||||
&mut n as *mut _ as *mut c_void,
|
|
||||||
pointer_args.pointers.as_mut_ptr(),
|
|
||||||
);
|
|
||||||
Ok(Value::Number(Number::Float(OrderedFloat(n))))
|
|
||||||
}
|
|
||||||
libffi::raw::FFI_TYPE_STRUCT => {
|
|
||||||
let name = &function_impl
|
|
||||||
.return_struct_name
|
|
||||||
.clone()
|
|
||||||
.ok_or(FFIError::StructNotFound)?;
|
|
||||||
let struct_type = self.structs.get(name).ok_or(FFIError::StructNotFound)?;
|
|
||||||
let layout = Layout::from_size_align(
|
|
||||||
struct_type.ffi_type.size,
|
|
||||||
struct_type.ffi_type.alignment.into(),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
};
|
||||||
let ptr = alloc::alloc(layout) as *mut c_void;
|
let struct_val = self.read_struct(ptr as *mut c_void, name, struct_type, arena);
|
||||||
|
|
||||||
if ptr.is_null() {
|
unsafe { alloc::dealloc(ptr, layout) };
|
||||||
panic!("allocation failed")
|
struct_val
|
||||||
}
|
|
||||||
|
|
||||||
libffi::raw::ffi_call(
|
|
||||||
&mut function_impl.cif,
|
|
||||||
Some(*function_impl.code_ptr.as_safe_fun()),
|
|
||||||
&mut *ptr as *mut _,
|
|
||||||
pointer_args.pointers.as_mut_ptr(),
|
|
||||||
);
|
|
||||||
let struct_val = self.read_struct(ptr, name, struct_type, arena);
|
|
||||||
#[allow(clippy::from_raw_with_void_ptr)]
|
|
||||||
drop(Box::from_raw(ptr));
|
|
||||||
struct_val
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,9 +450,7 @@ impl ForeignFunctionTable {
|
|||||||
let mut returns = Vec::new();
|
let mut returns = Vec::new();
|
||||||
let mut field_ptr = ptr;
|
let mut field_ptr = ptr;
|
||||||
|
|
||||||
for i in 0..(struct_type.fields.len() - 1) {
|
for (field, type_name) in struct_type.fields.iter().zip(&struct_type.atom_fields) {
|
||||||
let field = struct_type.fields[i];
|
|
||||||
|
|
||||||
macro_rules! read_and_push_int {
|
macro_rules! read_and_push_int {
|
||||||
($type:ty) => {{
|
($type:ty) => {{
|
||||||
field_ptr =
|
field_ptr =
|
||||||
@@ -450,7 +461,7 @@ impl ForeignFunctionTable {
|
|||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
match (*field).type_ as u32 {
|
match (*field.as_raw_ptr()).type_ as u32 {
|
||||||
libffi::raw::FFI_TYPE_UINT8 => read_and_push_int!(u8),
|
libffi::raw::FFI_TYPE_UINT8 => read_and_push_int!(u8),
|
||||||
libffi::raw::FFI_TYPE_SINT8 => read_and_push_int!(i8),
|
libffi::raw::FFI_TYPE_SINT8 => read_and_push_int!(i8),
|
||||||
libffi::raw::FFI_TYPE_UINT16 => read_and_push_int!(u16),
|
libffi::raw::FFI_TYPE_UINT16 => read_and_push_int!(u16),
|
||||||
@@ -475,17 +486,18 @@ impl ForeignFunctionTable {
|
|||||||
field_ptr = field_ptr.add(std::mem::size_of::<f64>());
|
field_ptr = field_ptr.add(std::mem::size_of::<f64>());
|
||||||
}
|
}
|
||||||
libffi::raw::FFI_TYPE_STRUCT => {
|
libffi::raw::FFI_TYPE_STRUCT => {
|
||||||
let substruct = struct_type.atom_fields[i].as_str();
|
let substruct = type_name.as_str();
|
||||||
let struct_type = self
|
let struct_type = self
|
||||||
.structs
|
.structs
|
||||||
.get(&*substruct)
|
.get(&*substruct)
|
||||||
.ok_or(FFIError::StructNotFound)?;
|
.ok_or(FFIError::StructNotFound)?;
|
||||||
field_ptr = field_ptr
|
let ffi_type = *struct_type.ffi_type.as_raw_ptr();
|
||||||
.add(field_ptr.align_offset(struct_type.ffi_type.alignment as usize));
|
field_ptr =
|
||||||
|
field_ptr.add(field_ptr.align_offset(ffi_type.alignment as usize));
|
||||||
let struct_val =
|
let struct_val =
|
||||||
self.read_struct(field_ptr, &substruct, struct_type, arena);
|
self.read_struct(field_ptr, &substruct, struct_type, arena);
|
||||||
returns.push(struct_val?);
|
returns.push(struct_val?);
|
||||||
field_ptr = field_ptr.add(struct_type.ffi_type.size);
|
field_ptr = field_ptr.add(ffi_type.size);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
@@ -549,6 +561,8 @@ pub enum FFIError {
|
|||||||
InvalidStructName,
|
InvalidStructName,
|
||||||
FunctionNotFound,
|
FunctionNotFound,
|
||||||
StructNotFound,
|
StructNotFound,
|
||||||
|
ArgCountMismatch,
|
||||||
|
AllocationFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for FFIError {
|
impl std::fmt::Display for FFIError {
|
||||||
|
|||||||
@@ -599,6 +599,8 @@ impl MachineState {
|
|||||||
FFIError::InvalidStructName => atom!("invalid_struct_name"),
|
FFIError::InvalidStructName => atom!("invalid_struct_name"),
|
||||||
FFIError::FunctionNotFound => atom!("function_not_found"),
|
FFIError::FunctionNotFound => atom!("function_not_found"),
|
||||||
FFIError::StructNotFound => atom!("struct_not_found"),
|
FFIError::StructNotFound => atom!("struct_not_found"),
|
||||||
|
FFIError::ArgCountMismatch => atom!("mismatched_argument_count"),
|
||||||
|
FFIError::AllocationFailed => atom!("allocation_failed"),
|
||||||
};
|
};
|
||||||
let stub = functor!(atom!("ffi_error"), [atom_as_cell(error_atom)]);
|
let stub = functor!(atom!("ffi_error"), [atom_as_cell(error_atom)]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user