cleanup ffi error handling and use Atom instead of &str in appropriate places
This commit is contained in:
163
src/ffi.rs
163
src/ffi.rs
@@ -22,6 +22,7 @@ and finally we add the pointer the size of what we've written.
|
|||||||
use crate::arena::Arena;
|
use crate::arena::Arena;
|
||||||
use crate::atom_table::Atom;
|
use crate::atom_table::Atom;
|
||||||
use crate::forms::Number;
|
use crate::forms::Number;
|
||||||
|
use crate::machine::machine_errors::DomainErrorType;
|
||||||
use crate::parser::ast::{Fixnum, MightNotFitInFixnum};
|
use crate::parser::ast::{Fixnum, MightNotFitInFixnum};
|
||||||
|
|
||||||
use dashu::Integer;
|
use dashu::Integer;
|
||||||
@@ -39,7 +40,7 @@ use std::ops::Deref;
|
|||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
|
|
||||||
pub struct FunctionDefinition {
|
pub struct FunctionDefinition {
|
||||||
pub name: String,
|
pub name: Atom,
|
||||||
pub return_value: Atom,
|
pub return_value: Atom,
|
||||||
pub args: Vec<Atom>,
|
pub args: Vec<Atom>,
|
||||||
}
|
}
|
||||||
@@ -100,10 +101,10 @@ impl FunctionImpl {
|
|||||||
return_type_name: Atom,
|
return_type_name: Atom,
|
||||||
args: &[Arg],
|
args: &[Arg],
|
||||||
arena: &mut Arena,
|
arena: &mut Arena,
|
||||||
structs_table: &HashMap<String, StructImpl>,
|
structs_table: &HashMap<Atom, StructImpl>,
|
||||||
) -> Result<Value, FfiError> {
|
) -> Result<Value, FfiError> {
|
||||||
let struct_type = structs_table
|
let struct_type = structs_table
|
||||||
.get(&*return_type_name.as_str())
|
.get(&return_type_name)
|
||||||
.ok_or(FfiError::StructNotFound(return_type_name))?;
|
.ok_or(FfiError::StructNotFound(return_type_name))?;
|
||||||
let ffi_type = unsafe { *struct_type.ffi_type.as_raw_ptr() };
|
let ffi_type = unsafe { *struct_type.ffi_type.as_raw_ptr() };
|
||||||
|
|
||||||
@@ -121,12 +122,8 @@ impl FunctionImpl {
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let struct_val = struct_type.read(
|
let struct_val =
|
||||||
alloc.ptr.as_ptr(),
|
struct_type.read(alloc.ptr.as_ptr(), return_type_name, structs_table, arena);
|
||||||
&return_type_name.as_str(),
|
|
||||||
structs_table,
|
|
||||||
arena,
|
|
||||||
);
|
|
||||||
|
|
||||||
drop(alloc);
|
drop(alloc);
|
||||||
|
|
||||||
@@ -137,7 +134,7 @@ impl FunctionImpl {
|
|||||||
&self,
|
&self,
|
||||||
args: &[Arg],
|
args: &[Arg],
|
||||||
arena: &mut Arena,
|
arena: &mut Arena,
|
||||||
structs_table: &HashMap<String, StructImpl>,
|
structs_table: &HashMap<Atom, StructImpl>,
|
||||||
) -> Result<Value, FfiError> {
|
) -> Result<Value, FfiError> {
|
||||||
let call_fn: unsafe fn(&Self, &[Arg], &mut Arena) -> Result<Value, FfiError> =
|
let call_fn: unsafe fn(&Self, &[Arg], &mut Arena) -> Result<Value, FfiError> =
|
||||||
match self.return_type {
|
match self.return_type {
|
||||||
@@ -164,8 +161,8 @@ impl FunctionImpl {
|
|||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct ForeignFunctionTable {
|
pub struct ForeignFunctionTable {
|
||||||
table: HashMap<String, FunctionImpl>,
|
table: HashMap<Atom, FunctionImpl>,
|
||||||
structs: HashMap<String, StructImpl>,
|
structs: HashMap<Atom, StructImpl>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@@ -183,7 +180,7 @@ impl StructImpl {
|
|||||||
|
|
||||||
fn build(
|
fn build(
|
||||||
&self,
|
&self,
|
||||||
structs_table: &HashMap<String, StructImpl>,
|
structs_table: &HashMap<Atom, StructImpl>,
|
||||||
struct_args: &mut [Value],
|
struct_args: &mut [Value],
|
||||||
) -> Result<FfiStruct, FfiError> {
|
) -> Result<FfiStruct, FfiError> {
|
||||||
let args = ArgValue::build_args(struct_args, &self.fields, structs_table)?;
|
let args = ArgValue::build_args(struct_args, &self.fields, structs_table)?;
|
||||||
@@ -249,8 +246,8 @@ impl StructImpl {
|
|||||||
fn read(
|
fn read(
|
||||||
&self,
|
&self,
|
||||||
ptr: *mut c_void,
|
ptr: *mut c_void,
|
||||||
struct_name: &str,
|
struct_name: Atom,
|
||||||
struct_table: &HashMap<String, StructImpl>,
|
struct_table: &HashMap<Atom, StructImpl>,
|
||||||
arena: &mut Arena,
|
arena: &mut Arena,
|
||||||
) -> Result<Value, FfiError> {
|
) -> Result<Value, FfiError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -315,7 +312,7 @@ impl StructImpl {
|
|||||||
FfiType::F32 => read_float::<f32>(ptr, &mut layout),
|
FfiType::F32 => read_float::<f32>(ptr, &mut layout),
|
||||||
FfiType::F64 => read_float::<f64>(ptr, &mut layout),
|
FfiType::F64 => read_float::<f64>(ptr, &mut layout),
|
||||||
FfiType::Struct(substruct) => {
|
FfiType::Struct(substruct) => {
|
||||||
let Some(substruct_type) = struct_table.get(&*substruct.as_str()) else {
|
let Some(substruct_type) = struct_table.get(substruct) else {
|
||||||
return Err(FfiError::StructNotFound(*substruct));
|
return Err(FfiError::StructNotFound(*substruct));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -328,19 +325,15 @@ impl StructImpl {
|
|||||||
.map_err(|_| FfiError::LayoutError)?;
|
.map_err(|_| FfiError::LayoutError)?;
|
||||||
layout = new_layout;
|
layout = new_layout;
|
||||||
let field_ptr = ptr.byte_offset(offset as isize);
|
let field_ptr = ptr.byte_offset(offset as isize);
|
||||||
let struct_val = substruct_type.read(
|
let struct_val =
|
||||||
field_ptr,
|
substruct_type.read(field_ptr, *substruct, struct_table, arena)?;
|
||||||
&substruct.as_str(),
|
|
||||||
struct_table,
|
|
||||||
arena,
|
|
||||||
)?;
|
|
||||||
Ok(struct_val)
|
Ok(struct_val)
|
||||||
}
|
}
|
||||||
FfiType::Void => unreachable!("void is not a valid field type"),
|
FfiType::Void => return Err(FfiError::VoidArgumentType),
|
||||||
};
|
};
|
||||||
returns.push(val?);
|
returns.push(val?);
|
||||||
}
|
}
|
||||||
Ok(Value::Struct(struct_name.to_string(), returns))
|
Ok(Value::Struct(struct_name, returns))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -427,7 +420,7 @@ impl FfiType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_type(self, structs_table: &HashMap<String, StructImpl>) -> Result<Type, FfiError> {
|
fn to_type(self, structs_table: &HashMap<Atom, StructImpl>) -> Result<Type, FfiError> {
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
Self::I64 => libffi::middle::Type::i64(),
|
Self::I64 => libffi::middle::Type::i64(),
|
||||||
Self::I32 => libffi::middle::Type::i32(),
|
Self::I32 => libffi::middle::Type::i32(),
|
||||||
@@ -444,7 +437,7 @@ impl FfiType {
|
|||||||
Self::F32 => libffi::middle::Type::f32(),
|
Self::F32 => libffi::middle::Type::f32(),
|
||||||
Self::F64 => libffi::middle::Type::f64(),
|
Self::F64 => libffi::middle::Type::f64(),
|
||||||
Self::Struct(struct_name) => structs_table
|
Self::Struct(struct_name) => structs_table
|
||||||
.get(&*struct_name.as_str())
|
.get(&struct_name)
|
||||||
.ok_or(FfiError::StructNotFound(struct_name))?
|
.ok_or(FfiError::StructNotFound(struct_name))?
|
||||||
.ffi_type
|
.ffi_type
|
||||||
.clone(),
|
.clone(),
|
||||||
@@ -471,7 +464,7 @@ impl<'val> ArgValue<'val> {
|
|||||||
fn new(
|
fn new(
|
||||||
val: &'val mut Value,
|
val: &'val mut Value,
|
||||||
arg_type: &FfiType,
|
arg_type: &FfiType,
|
||||||
structs_table: &HashMap<String, StructImpl>,
|
structs_table: &HashMap<Atom, StructImpl>,
|
||||||
) -> Result<Self, FfiError> {
|
) -> Result<Self, FfiError> {
|
||||||
match arg_type {
|
match arg_type {
|
||||||
FfiType::U8 => Ok(Self::U8(val.as_int()?)),
|
FfiType::U8 => Ok(Self::U8(val.as_int()?)),
|
||||||
@@ -486,27 +479,27 @@ impl<'val> ArgValue<'val> {
|
|||||||
FfiType::F64 => Ok(Self::F64(val.as_float()?)),
|
FfiType::F64 => Ok(Self::F64(val.as_float()?)),
|
||||||
FfiType::Ptr => Ok(Self::Ptr(val.as_ptr()?, PhantomData)),
|
FfiType::Ptr => Ok(Self::Ptr(val.as_ptr()?, PhantomData)),
|
||||||
FfiType::CStr => Ok(Self::Ptr(val.as_ptr()?, PhantomData)),
|
FfiType::CStr => Ok(Self::Ptr(val.as_ptr()?, PhantomData)),
|
||||||
FfiType::Struct(atom) => {
|
FfiType::Struct(arg_type_name) => {
|
||||||
let (name, args) = val.as_struct()?;
|
let (val_type_name, args) = val.as_struct()?;
|
||||||
|
|
||||||
if &*atom.as_str() != name {
|
if *arg_type_name != val_type_name {
|
||||||
return Err(FfiError::ValueCast);
|
return Err(FfiError::ValueCast(*arg_type_name, val_type_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some(struct_type) = structs_table.get(name) else {
|
let Some(struct_type) = structs_table.get(&val_type_name) else {
|
||||||
return Err(FfiError::StructNotFound(*atom));
|
return Err(FfiError::StructNotFound(*arg_type_name));
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self::Struct(struct_type.build(structs_table, args)?))
|
Ok(Self::Struct(struct_type.build(structs_table, args)?))
|
||||||
}
|
}
|
||||||
FfiType::Void => Err(FfiError::InvalidArgumentType),
|
FfiType::Void => Err(FfiError::VoidArgumentType),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_args(
|
fn build_args(
|
||||||
args: &'val mut [Value],
|
args: &'val mut [Value],
|
||||||
types: &[FfiType],
|
types: &[FfiType],
|
||||||
structs_table: &HashMap<String, StructImpl>,
|
structs_table: &HashMap<Atom, StructImpl>,
|
||||||
) -> Result<Vec<Self>, FfiError> {
|
) -> Result<Vec<Self>, FfiError> {
|
||||||
if types.len() != args.len() {
|
if types.len() != args.len() {
|
||||||
return Err(FfiError::ArgCountMismatch);
|
return Err(FfiError::ArgCountMismatch);
|
||||||
@@ -590,7 +583,7 @@ impl ForeignFunctionTable {
|
|||||||
self.table.extend(other.table);
|
self.table.extend(other.table);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn define_struct(&mut self, name: &str, atom_fields: Vec<Atom>) -> Result<(), FfiError> {
|
pub fn define_struct(&mut self, name: Atom, atom_fields: Vec<Atom>) -> Result<(), FfiError> {
|
||||||
let fields: Vec<_> = atom_fields.iter().map(FfiType::from_atom).collect();
|
let fields: Vec<_> = atom_fields.iter().map(FfiType::from_atom).collect();
|
||||||
let struct_type = libffi::middle::Type::structure(
|
let struct_type = libffi::middle::Type::structure(
|
||||||
fields
|
fields
|
||||||
@@ -612,7 +605,7 @@ impl ForeignFunctionTable {
|
|||||||
};
|
};
|
||||||
|
|
||||||
self.structs.insert(
|
self.structs.insert(
|
||||||
name.to_string(),
|
name,
|
||||||
StructImpl {
|
StructImpl {
|
||||||
ffi_type: struct_type,
|
ffi_type: struct_type,
|
||||||
fields,
|
fields,
|
||||||
@@ -629,7 +622,7 @@ impl ForeignFunctionTable {
|
|||||||
let mut ff_table: ForeignFunctionTable = Default::default();
|
let mut ff_table: ForeignFunctionTable = Default::default();
|
||||||
let library = unsafe { Library::new(library_name) }?;
|
let library = unsafe { Library::new(library_name) }?;
|
||||||
for function in functions {
|
for function in functions {
|
||||||
let symbol_name: CString = CString::new(function.name.clone())?;
|
let symbol_name: CString = CString::new(&*function.name.as_str())?;
|
||||||
let code_ptr: Symbol<*mut c_void> =
|
let code_ptr: Symbol<*mut c_void> =
|
||||||
unsafe { library.get(symbol_name.as_bytes_with_nul()) }?;
|
unsafe { library.get(symbol_name.as_bytes_with_nul()) }?;
|
||||||
let args: Vec<_> = function.args.iter().map(FfiType::from_atom).collect();
|
let args: Vec<_> = function.args.iter().map(FfiType::from_atom).collect();
|
||||||
@@ -643,7 +636,7 @@ impl ForeignFunctionTable {
|
|||||||
);
|
);
|
||||||
|
|
||||||
ff_table.table.insert(
|
ff_table.table.insert(
|
||||||
function.name.clone(),
|
function.name,
|
||||||
FunctionImpl {
|
FunctionImpl {
|
||||||
cif,
|
cif,
|
||||||
args,
|
args,
|
||||||
@@ -659,11 +652,14 @@ impl ForeignFunctionTable {
|
|||||||
|
|
||||||
pub fn exec(
|
pub fn exec(
|
||||||
&mut self,
|
&mut self,
|
||||||
fn_name: &str,
|
fn_name: Atom,
|
||||||
mut args: Vec<Value>,
|
mut args: Vec<Value>,
|
||||||
arena: &mut Arena,
|
arena: &mut Arena,
|
||||||
) -> Result<Value, FfiError> {
|
) -> Result<Value, FfiError> {
|
||||||
let fn_impl = self.table.get(fn_name).ok_or(FfiError::FunctionNotFound)?;
|
let fn_impl = self
|
||||||
|
.table
|
||||||
|
.get(&fn_name)
|
||||||
|
.ok_or(FfiError::FunctionNotFound(fn_name))?;
|
||||||
|
|
||||||
let args = ArgValue::build_args(&mut args, &fn_impl.args, &self.structs)?;
|
let args = ArgValue::build_args(&mut args, &fn_impl.args, &self.structs)?;
|
||||||
|
|
||||||
@@ -695,13 +691,13 @@ impl ForeignFunctionTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
match FfiType::from_atom(&kind) {
|
match FfiType::from_atom(&kind) {
|
||||||
FfiType::Void => Err(FfiError::InvalidFfiType),
|
FfiType::Void => Err(FfiError::VoidArgumentType),
|
||||||
FfiType::Bool => {
|
FfiType::Bool => {
|
||||||
let val = args.as_int::<u8>()?;
|
let val = args.as_int::<u8>()?;
|
||||||
let init = match val {
|
let init = match val {
|
||||||
0 => false,
|
0 => false,
|
||||||
1 => true,
|
1 => true,
|
||||||
_ => return Err(FfiError::ValueOutOfRange),
|
_ => return Err(FfiError::ValueOutOfRange(DomainErrorType::ZeroOrOne, args)),
|
||||||
};
|
};
|
||||||
allocate_primitive::<bool>(allocator, init, arena)
|
allocate_primitive::<bool>(allocator, init, arena)
|
||||||
}
|
}
|
||||||
@@ -716,10 +712,10 @@ impl ForeignFunctionTable {
|
|||||||
FfiType::F32 => allocate_primitive::<f32>(allocator, args.as_float()? as f32, arena),
|
FfiType::F32 => allocate_primitive::<f32>(allocator, args.as_float()? as f32, arena),
|
||||||
FfiType::F64 => allocate_primitive::<f64>(allocator, args.as_float()?, arena),
|
FfiType::F64 => allocate_primitive::<f64>(allocator, args.as_float()?, arena),
|
||||||
FfiType::Ptr => allocate_primitive::<*mut c_void>(allocator, args.as_ptr()?, arena),
|
FfiType::Ptr => allocate_primitive::<*mut c_void>(allocator, args.as_ptr()?, arena),
|
||||||
FfiType::CStr => Err(FfiError::InvalidFfiType),
|
FfiType::CStr => Err(FfiError::CStrFieldType),
|
||||||
FfiType::Struct(_) => {
|
FfiType::Struct(_) => {
|
||||||
let Some(struct_impl) = self.structs.get(&*kind.as_str()) else {
|
let Some(struct_impl) = self.structs.get(&kind) else {
|
||||||
return Err(FfiError::InvalidStruct);
|
return Err(FfiError::StructNotFound(kind));
|
||||||
};
|
};
|
||||||
|
|
||||||
let (_, args) = args.as_struct()?;
|
let (_, args) = args.as_struct()?;
|
||||||
@@ -755,11 +751,11 @@ impl ForeignFunctionTable {
|
|||||||
let ptr = ptr.as_ptr()?;
|
let ptr = ptr.as_ptr()?;
|
||||||
|
|
||||||
let Some(ptr) = NonNull::new(ptr) else {
|
let Some(ptr) = NonNull::new(ptr) else {
|
||||||
return Err(FfiError::ValueOutOfRange);
|
return Err(FfiError::NullPtr);
|
||||||
};
|
};
|
||||||
|
|
||||||
match FfiType::from_atom(&kind) {
|
match FfiType::from_atom(&kind) {
|
||||||
FfiType::Void => Err(FfiError::InvalidFfiType),
|
FfiType::Void => Err(FfiError::VoidArgumentType),
|
||||||
FfiType::Bool | FfiType::U8 => Ok(unsafe { read_int::<u8>(ptr, arena) }),
|
FfiType::Bool | FfiType::U8 => Ok(unsafe { read_int::<u8>(ptr, arena) }),
|
||||||
FfiType::I8 => Ok(unsafe { read_int::<i8>(ptr, arena) }),
|
FfiType::I8 => Ok(unsafe { read_int::<i8>(ptr, arena) }),
|
||||||
FfiType::U16 => Ok(unsafe { read_int::<u16>(ptr, arena) }),
|
FfiType::U16 => Ok(unsafe { read_int::<u16>(ptr, arena) }),
|
||||||
@@ -782,11 +778,11 @@ impl ForeignFunctionTable {
|
|||||||
unsafe { CStr::from_ptr(ptr.as_ptr().cast()) }.to_owned(),
|
unsafe { CStr::from_ptr(ptr.as_ptr().cast()) }.to_owned(),
|
||||||
)),
|
)),
|
||||||
FfiType::Struct(_) => {
|
FfiType::Struct(_) => {
|
||||||
let Some(struct_impl) = self.structs.get(&*kind.as_str()) else {
|
let Some(struct_impl) = self.structs.get(&kind) else {
|
||||||
return Err(FfiError::InvalidStruct);
|
return Err(FfiError::StructNotFound(kind));
|
||||||
};
|
};
|
||||||
|
|
||||||
struct_impl.read(ptr.as_ptr(), &kind.as_str(), &self.structs, arena)
|
struct_impl.read(ptr.as_ptr(), kind, &self.structs, arena)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -805,11 +801,11 @@ impl ForeignFunctionTable {
|
|||||||
let ptr = ptr.as_ptr()?;
|
let ptr = ptr.as_ptr()?;
|
||||||
|
|
||||||
let Some(ptr) = NonNull::new(ptr) else {
|
let Some(ptr) = NonNull::new(ptr) else {
|
||||||
return Err(FfiError::ValueOutOfRange);
|
return Err(FfiError::NullPtr);
|
||||||
};
|
};
|
||||||
|
|
||||||
match FfiType::from_atom(&kind) {
|
match FfiType::from_atom(&kind) {
|
||||||
FfiType::Void => return Err(FfiError::InvalidFfiType),
|
FfiType::Void => return Err(FfiError::VoidArgumentType),
|
||||||
FfiType::Bool => deallocate_primitive::<bool>(allocator, ptr),
|
FfiType::Bool => deallocate_primitive::<bool>(allocator, ptr),
|
||||||
FfiType::U8 => deallocate_primitive::<u8>(allocator, ptr),
|
FfiType::U8 => deallocate_primitive::<u8>(allocator, ptr),
|
||||||
FfiType::I8 => deallocate_primitive::<i8>(allocator, ptr),
|
FfiType::I8 => deallocate_primitive::<i8>(allocator, ptr),
|
||||||
@@ -822,10 +818,10 @@ impl ForeignFunctionTable {
|
|||||||
FfiType::F32 => deallocate_primitive::<f32>(allocator, ptr),
|
FfiType::F32 => deallocate_primitive::<f32>(allocator, ptr),
|
||||||
FfiType::F64 => deallocate_primitive::<f64>(allocator, ptr),
|
FfiType::F64 => deallocate_primitive::<f64>(allocator, ptr),
|
||||||
FfiType::Ptr => deallocate_primitive::<*mut c_void>(allocator, ptr),
|
FfiType::Ptr => deallocate_primitive::<*mut c_void>(allocator, ptr),
|
||||||
FfiType::CStr => return Err(FfiError::InvalidFfiType),
|
FfiType::CStr => return Err(FfiError::CStrFieldType),
|
||||||
FfiType::Struct(_) => {
|
FfiType::Struct(_) => {
|
||||||
let Some(struct_impl) = self.structs.get(&*kind.as_str()) else {
|
let Some(struct_impl) = self.structs.get(&kind) else {
|
||||||
return Err(FfiError::InvalidStruct);
|
return Err(FfiError::StructNotFound(kind));
|
||||||
};
|
};
|
||||||
|
|
||||||
let layout = struct_impl.layout()?;
|
let layout = struct_impl.layout()?;
|
||||||
@@ -845,7 +841,7 @@ impl ForeignFunctionTable {
|
|||||||
pub enum Value {
|
pub enum Value {
|
||||||
Number(Number),
|
Number(Number),
|
||||||
CString(CString),
|
CString(CString),
|
||||||
Struct(String, Vec<Value>),
|
Struct(Atom, Vec<Value>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
@@ -857,22 +853,27 @@ impl Value {
|
|||||||
match self {
|
match self {
|
||||||
Value::Number(Number::Integer(ibig_ptr)) => {
|
Value::Number(Number::Integer(ibig_ptr)) => {
|
||||||
let ibig: &Integer = ibig_ptr;
|
let ibig: &Integer = ibig_ptr;
|
||||||
ibig.clone()
|
ibig.clone().try_into().map_err(|_| {
|
||||||
.try_into()
|
FfiError::ValueOutOfRange(DomainErrorType::FixedSizedInt, self.clone())
|
||||||
.map_err(|_| FfiError::ValueOutOfRange)
|
})
|
||||||
}
|
}
|
||||||
Value::Number(Number::Fixnum(fixnum)) => fixnum
|
Value::Number(Number::Fixnum(fixnum)) => fixnum.get_num().try_into().map_err(|_| {
|
||||||
.get_num()
|
FfiError::ValueOutOfRange(DomainErrorType::FixedSizedInt, self.clone())
|
||||||
.try_into()
|
}),
|
||||||
.map_err(|_| FfiError::ValueOutOfRange),
|
_ => Err(FfiError::ValueOutOfRange(
|
||||||
_ => Err(FfiError::ValueCast),
|
DomainErrorType::FixedSizedInt,
|
||||||
|
self.clone(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_float(&self) -> Result<f64, FfiError> {
|
fn as_float(&self) -> Result<f64, FfiError> {
|
||||||
match self {
|
match self {
|
||||||
&Value::Number(Number::Float(OrderedFloat(f))) => Ok(f),
|
&Value::Number(Number::Float(OrderedFloat(f))) => Ok(f),
|
||||||
_ => Err(FfiError::ValueCast),
|
_ => Err(FfiError::ValueOutOfRange(
|
||||||
|
DomainErrorType::F64,
|
||||||
|
self.clone(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -882,33 +883,39 @@ impl Value {
|
|||||||
Value::Number(Number::Fixnum(fixnum)) => Ok(std::ptr::with_exposed_provenance_mut(
|
Value::Number(Number::Fixnum(fixnum)) => Ok(std::ptr::with_exposed_provenance_mut(
|
||||||
fixnum.get_num() as usize,
|
fixnum.get_num() as usize,
|
||||||
)),
|
)),
|
||||||
_ => Err(FfiError::ValueCast),
|
_ => Err(FfiError::ValueOutOfRange(
|
||||||
|
DomainErrorType::PtrLike,
|
||||||
|
self.clone(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_struct(&mut self) -> Result<(&str, &mut [Self]), FfiError> {
|
fn as_struct(&mut self) -> Result<(Atom, &mut [Self]), FfiError> {
|
||||||
match self {
|
match self {
|
||||||
Value::Struct(name, values) => Ok((name, values)),
|
Value::Struct(name, values) => Ok((*name, values)),
|
||||||
_ => Err(FfiError::ValueCast),
|
_ => Err(FfiError::ValueOutOfRange(
|
||||||
|
DomainErrorType::FfiStruct,
|
||||||
|
self.clone(),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum FfiError {
|
pub enum FfiError {
|
||||||
ValueCast,
|
ValueCast(Atom, Atom),
|
||||||
ValueOutOfRange,
|
ValueOutOfRange(DomainErrorType, Value),
|
||||||
InvalidArgumentType,
|
VoidArgumentType,
|
||||||
InvalidArgument,
|
FunctionNotFound(Atom),
|
||||||
InvalidFfiType,
|
|
||||||
InvalidStruct,
|
|
||||||
FunctionNotFound,
|
|
||||||
StructNotFound(Atom),
|
StructNotFound(Atom),
|
||||||
ArgCountMismatch,
|
ArgCountMismatch,
|
||||||
AllocationFailed,
|
AllocationFailed,
|
||||||
// LayoutError should never occour
|
// LayoutError should never occour
|
||||||
LayoutError,
|
LayoutError,
|
||||||
|
UnsupportedTypedef,
|
||||||
UnsupportedAbi,
|
UnsupportedAbi,
|
||||||
|
CStrFieldType,
|
||||||
|
NullPtr,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for FfiError {
|
impl std::fmt::Display for FfiError {
|
||||||
@@ -922,7 +929,7 @@ impl Error for FfiError {}
|
|||||||
impl From<libffi::low::Error> for FfiError {
|
impl From<libffi::low::Error> for FfiError {
|
||||||
fn from(value: libffi::low::Error) -> Self {
|
fn from(value: libffi::low::Error) -> Self {
|
||||||
match value {
|
match value {
|
||||||
libffi::low::Error::Typedef => FfiError::InvalidFfiType,
|
libffi::low::Error::Typedef => FfiError::UnsupportedTypedef,
|
||||||
libffi::low::Error::Abi => FfiError::UnsupportedAbi,
|
libffi::low::Error::Abi => FfiError::UnsupportedAbi,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::atom_table::*;
|
|||||||
use crate::parser::ast::*;
|
use crate::parser::ast::*;
|
||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
#[cfg(feature = "ffi")]
|
||||||
use crate::ffi::FfiError;
|
use crate::ffi::{self, FfiError};
|
||||||
use crate::forms::*;
|
use crate::forms::*;
|
||||||
use crate::functor_macro::*;
|
use crate::functor_macro::*;
|
||||||
use crate::machine::heap::*;
|
use crate::machine::heap::*;
|
||||||
@@ -275,6 +275,30 @@ impl DomainError for MachineStub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ffi")]
|
||||||
|
impl DomainError for ffi::Value {
|
||||||
|
fn domain_error(self, machine_st: &mut MachineState, error: DomainErrorType) -> MachineError {
|
||||||
|
use ffi::Value;
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Value::Number(number) => number.domain_error(machine_st, error),
|
||||||
|
Value::CString(cstring) => {
|
||||||
|
let str = cstring.to_string_lossy().into_owned();
|
||||||
|
let stub = functor!(
|
||||||
|
atom!("domain_error"),
|
||||||
|
[atom_as_cell((error.as_atom())), string(str)]
|
||||||
|
);
|
||||||
|
|
||||||
|
MachineError {
|
||||||
|
stub,
|
||||||
|
location: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Struct(atom, _values) => atom_as_cell!(atom).domain_error(machine_st, error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(super) fn functor_stub(name: Atom, arity: usize) -> MachineStub {
|
pub(super) fn functor_stub(name: Atom, arity: usize) -> MachineStub {
|
||||||
functor!(atom!("/"), [atom_as_cell(name), fixnum(arity)])
|
functor!(atom!("/"), [atom_as_cell(name), fixnum(arity)])
|
||||||
@@ -418,6 +442,28 @@ impl MachineState {
|
|||||||
[atom_as_cell((atom!("process"))), cell(culprit)]
|
[atom_as_cell((atom!("process"))), cell(culprit)]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
MachineError {
|
||||||
|
stub,
|
||||||
|
location: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExistenceError::FfiFunction(atom) => {
|
||||||
|
let stub = functor!(
|
||||||
|
atom!("existence_error"),
|
||||||
|
[atom_as_cell((atom!("ffi_function"))), atom_as_cell(atom)]
|
||||||
|
);
|
||||||
|
|
||||||
|
MachineError {
|
||||||
|
stub,
|
||||||
|
location: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExistenceError::FfiStructType(atom) => {
|
||||||
|
let stub = functor!(
|
||||||
|
atom!("existence_error"),
|
||||||
|
[atom_as_cell((atom!("ffi_struct_type"))), atom_as_cell(atom)]
|
||||||
|
);
|
||||||
|
|
||||||
MachineError {
|
MachineError {
|
||||||
stub,
|
stub,
|
||||||
location: None,
|
location: None,
|
||||||
@@ -628,42 +674,43 @@ impl MachineState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
#[cfg(feature = "ffi")]
|
||||||
pub(super) fn ffi_error(&self, err: FfiError, culprit: HeapCellValue) -> MachineError {
|
pub(super) fn ffi_error(&mut self, err: FfiError) -> MachineError {
|
||||||
let error_atom = match err {
|
match err {
|
||||||
FfiError::ValueCast => atom!("value_cast"),
|
FfiError::ValueCast(expected, actual) => {
|
||||||
FfiError::ValueOutOfRange => atom!("value_out_of_range"),
|
|
||||||
FfiError::InvalidFfiType => atom!("invalid_ffi_type"),
|
|
||||||
FfiError::InvalidArgumentType => atom!("invalid_argument_type"),
|
|
||||||
FfiError::InvalidArgument => atom!("invalid_argument"),
|
|
||||||
FfiError::InvalidStruct => atom!("invalid_struct"),
|
|
||||||
FfiError::FunctionNotFound => atom!("function_not_found"),
|
|
||||||
FfiError::StructNotFound(culprit) => {
|
|
||||||
let stub = functor!(
|
let stub = functor!(
|
||||||
atom!("ffi_error"),
|
atom!("domain_error"),
|
||||||
[
|
[atom_as_cell(expected), atom_as_cell(actual)]
|
||||||
atom_as_cell((atom!("struct_not_found"))),
|
|
||||||
atom_as_cell(culprit)
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return MachineError {
|
MachineError {
|
||||||
stub,
|
stub,
|
||||||
location: None,
|
location: None,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
FfiError::ArgCountMismatch => atom!("mismatched_argument_count"),
|
FfiError::ValueOutOfRange(domain, culprit) => self.domain_error(domain, culprit),
|
||||||
FfiError::AllocationFailed => atom!("allocation_failed"),
|
FfiError::FunctionNotFound(name) => {
|
||||||
FfiError::LayoutError => atom!("layout_error"),
|
self.existence_error(ExistenceError::FfiFunction(name))
|
||||||
FfiError::UnsupportedAbi => atom!("unsupported_abi"),
|
}
|
||||||
};
|
FfiError::StructNotFound(name) => {
|
||||||
let stub = functor!(
|
self.existence_error(ExistenceError::FfiStructType(name))
|
||||||
atom!("ffi_error"),
|
}
|
||||||
[atom_as_cell(error_atom), cell(culprit)]
|
FfiError::ArgCountMismatch => self.unreachable_error(),
|
||||||
);
|
FfiError::AllocationFailed => MachineError {
|
||||||
|
stub: functor!(atom!("resource_error"), [atom_as_cell((atom!("heap")))]),
|
||||||
MachineError {
|
location: None,
|
||||||
stub,
|
},
|
||||||
location: None,
|
FfiError::LayoutError => self.representation_error(RepFlag::FfiLayout),
|
||||||
|
FfiError::UnsupportedTypedef => self.representation_error(RepFlag::FfiLayout),
|
||||||
|
FfiError::UnsupportedAbi => self.representation_error(RepFlag::FfiAbi),
|
||||||
|
FfiError::VoidArgumentType => self.domain_error(
|
||||||
|
DomainErrorType::FfiArgumentType,
|
||||||
|
atom_as_cell!(atom!("void")),
|
||||||
|
),
|
||||||
|
FfiError::CStrFieldType => todo!(),
|
||||||
|
FfiError::NullPtr => self.domain_error(
|
||||||
|
DomainErrorType::NonNullPtr,
|
||||||
|
fixnum_as_cell!(Fixnum::build_with(0)),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -845,6 +892,14 @@ pub(crate) enum DomainErrorType {
|
|||||||
OperatorPriority,
|
OperatorPriority,
|
||||||
Directive,
|
Directive,
|
||||||
Allocator,
|
Allocator,
|
||||||
|
FfiStruct,
|
||||||
|
ZeroOrOne,
|
||||||
|
NonNullPtr,
|
||||||
|
PtrLike,
|
||||||
|
F64,
|
||||||
|
FfiArgument,
|
||||||
|
FfiArgumentType,
|
||||||
|
FixedSizedInt,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DomainErrorType {
|
impl DomainErrorType {
|
||||||
@@ -860,6 +915,14 @@ impl DomainErrorType {
|
|||||||
DomainErrorType::OperatorPriority => atom!("operator_priority"),
|
DomainErrorType::OperatorPriority => atom!("operator_priority"),
|
||||||
DomainErrorType::Directive => atom!("directive"),
|
DomainErrorType::Directive => atom!("directive"),
|
||||||
DomainErrorType::Allocator => atom!("allocator"),
|
DomainErrorType::Allocator => atom!("allocator"),
|
||||||
|
DomainErrorType::ZeroOrOne => atom!("zero_or_one"),
|
||||||
|
DomainErrorType::FfiStruct => atom!("ffi_struct"),
|
||||||
|
DomainErrorType::NonNullPtr => atom!("non_null_pointer"),
|
||||||
|
DomainErrorType::PtrLike => atom!("pointer_like"),
|
||||||
|
DomainErrorType::F64 => atom!("f64"),
|
||||||
|
DomainErrorType::FfiArgument => atom!("ffi_argument"),
|
||||||
|
DomainErrorType::FfiArgumentType => atom!("ffi_argument_type"),
|
||||||
|
DomainErrorType::FixedSizedInt => atom!("fixed_sized_int"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -874,6 +937,8 @@ pub(crate) enum RepFlag {
|
|||||||
// MaxInteger,
|
// MaxInteger,
|
||||||
// MinInteger,
|
// MinInteger,
|
||||||
Term,
|
Term,
|
||||||
|
FfiLayout,
|
||||||
|
FfiAbi,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RepFlag {
|
impl RepFlag {
|
||||||
@@ -885,7 +950,9 @@ impl RepFlag {
|
|||||||
RepFlag::MaxArity => atom!("max_arity"),
|
RepFlag::MaxArity => atom!("max_arity"),
|
||||||
RepFlag::Term => atom!("term"),
|
RepFlag::Term => atom!("term"),
|
||||||
// RepFlag::MaxInteger => atom!("max_integer"),
|
// RepFlag::MaxInteger => atom!("max_integer"),
|
||||||
// RepFlag::MinInteger => atom!("min_integer")
|
// RepFlag::MinInteger => atom!("min_integer"),
|
||||||
|
RepFlag::FfiLayout => atom!("ffi_layout"),
|
||||||
|
RepFlag::FfiAbi => atom!("ffi_abi"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1065,6 +1132,8 @@ pub enum ExistenceError {
|
|||||||
SourceSink(HeapCellValue),
|
SourceSink(HeapCellValue),
|
||||||
Stream(HeapCellValue),
|
Stream(HeapCellValue),
|
||||||
Process(HeapCellValue),
|
Process(HeapCellValue),
|
||||||
|
FfiFunction(Atom),
|
||||||
|
FfiStructType(Atom),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::arena::*;
|
use crate::arena::*;
|
||||||
use crate::atom_table::*;
|
use crate::atom_table::*;
|
||||||
use crate::forms::*;
|
use crate::forms::*;
|
||||||
use crate::functor_macro::*;
|
|
||||||
use crate::heap_iter::*;
|
use crate::heap_iter::*;
|
||||||
use crate::heap_print::*;
|
use crate::heap_print::*;
|
||||||
use crate::machine::attributed_variables::*;
|
use crate::machine::attributed_variables::*;
|
||||||
@@ -184,7 +183,7 @@ impl IndexMut<RegType> for MachineState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type CallResult<Ok = ()> = Result<Ok, Vec<FunctorElement>>;
|
pub type CallResult<Ok = ()> = Result<Ok, MachineStub>;
|
||||||
|
|
||||||
// size may be an upper bound.
|
// size may be an upper bound.
|
||||||
// true_size is calculated to compute the exact offset.
|
// true_size is calculated to compute the exact offset.
|
||||||
|
|||||||
@@ -4982,29 +4982,35 @@ impl Machine {
|
|||||||
};
|
};
|
||||||
let return_value = cell_as_atom_cell!(self.machine_st.heap[s + 2]);
|
let return_value = cell_as_atom_cell!(self.machine_st.heap[s + 2]);
|
||||||
functions.push(FunctionDefinition {
|
functions.push(FunctionDefinition {
|
||||||
name: name.as_str().to_string(),
|
name,
|
||||||
args,
|
args,
|
||||||
return_value: return_value.get_name(),
|
return_value: return_value.get_name(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
unreachable!()
|
let err = self.machine_st.unreachable_error();
|
||||||
}
|
return Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if self
|
if self
|
||||||
.foreign_function_table
|
.foreign_function_table
|
||||||
.load_library(&library_name.as_str(), &functions)
|
.load_library(&library_name.as_str(), &functions)
|
||||||
.is_ok()
|
.is_err()
|
||||||
{
|
{
|
||||||
return Ok(());
|
self.machine_st.fail = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
Err(e) => return Err(e),
|
Err(e) => Err(e),
|
||||||
};
|
}
|
||||||
|
} else {
|
||||||
|
let err = self
|
||||||
|
.machine_st
|
||||||
|
.type_error(ValidType::InCharacter, library_name);
|
||||||
|
Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
}
|
}
|
||||||
self.machine_st.fail = true;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "ffi"))]
|
#[cfg(not(feature = "ffi"))]
|
||||||
@@ -5034,9 +5040,9 @@ impl Machine {
|
|||||||
|
|
||||||
if let Some(head) = iter.next() {
|
if let Some(head) = iter.next() {
|
||||||
let head = self.machine_st.store(self.machine_st.deref(head));
|
let head = self.machine_st.store(self.machine_st.deref(head));
|
||||||
if let Some(struct_name) = self.machine_st.value_to_str_like(head) {
|
if let Some(struct_name) = head.to_atom() {
|
||||||
Ok(Value::Struct(
|
Ok(Value::Struct(
|
||||||
struct_name.as_str().to_string(),
|
struct_name,
|
||||||
iter.map(|x| self.map_ffi_arg(x, stub_gen))
|
iter.map(|x| self.map_ffi_arg(x, stub_gen))
|
||||||
.collect::<Result<_, _>>()?,
|
.collect::<Result<_, _>>()?,
|
||||||
))
|
))
|
||||||
@@ -5052,17 +5058,15 @@ impl Machine {
|
|||||||
Err(self.machine_st.error_form(err, src))
|
Err(self.machine_st.error_form(err, src))
|
||||||
} else {
|
} else {
|
||||||
// first element of a struct needs to be the type
|
// first element of a struct needs to be the type
|
||||||
Err(self.machine_st.error_form(
|
let err = self.machine_st.type_error(ValidType::Atom, head);
|
||||||
self.machine_st.ffi_error(FfiError::ValueOutOfRange, head),
|
Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
stub_gen(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// empty list is an invalid struct repr
|
// empty list is an invalid struct repr
|
||||||
Err(self.machine_st.error_form(
|
let err = self
|
||||||
self.machine_st.ffi_error(FfiError::ValueOutOfRange, source),
|
.machine_st
|
||||||
stub_gen(),
|
.domain_error(DomainErrorType::FfiStruct, source);
|
||||||
))
|
Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
}
|
}
|
||||||
} else if self.machine_st.deref(source).is_var() {
|
} else if self.machine_st.deref(source).is_var() {
|
||||||
let err = self.machine_st.instantiation_error();
|
let err = self.machine_st.instantiation_error();
|
||||||
@@ -5075,10 +5079,10 @@ impl Machine {
|
|||||||
|
|
||||||
Err(self.machine_st.error_form(err, src))
|
Err(self.machine_st.error_form(err, src))
|
||||||
} else {
|
} else {
|
||||||
Err(self.machine_st.error_form(
|
let err = self
|
||||||
self.machine_st.ffi_error(FfiError::InvalidArgument, source),
|
.machine_st
|
||||||
stub_gen(),
|
.domain_error(DomainErrorType::FfiArgument, source);
|
||||||
))
|
Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5090,10 +5094,10 @@ impl Machine {
|
|||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
#[cfg(feature = "ffi")]
|
||||||
{
|
{
|
||||||
let function_name_arg = self.deref_register(1);
|
let function_name_arg = self.machine_st.store(self.deref_register(1));
|
||||||
let args_reg = self.deref_register(2);
|
let args_reg = self.deref_register(2);
|
||||||
let return_value = self.deref_register(3);
|
let return_value = self.deref_register(3);
|
||||||
if let Some(function_name) = self.machine_st.value_to_str_like(function_name_arg) {
|
if let Some(function_name) = function_name_arg.to_atom() {
|
||||||
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
||||||
Ok(args) => {
|
Ok(args) => {
|
||||||
let args = args
|
let args = args
|
||||||
@@ -5102,25 +5106,25 @@ impl Machine {
|
|||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
match self.foreign_function_table.exec(
|
match self.foreign_function_table.exec(
|
||||||
&function_name.as_str(),
|
function_name,
|
||||||
args,
|
args,
|
||||||
&mut self.machine_st.arena,
|
&mut self.machine_st.arena,
|
||||||
) {
|
) {
|
||||||
Ok(result) => {
|
Ok(result) => self.unify_ffi_result(return_value, result),
|
||||||
return self.unify_ffi_result(return_value, result);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let err = self.machine_st.ffi_error(e, function_name_arg);
|
let err = self.machine_st.ffi_error(e);
|
||||||
return Err(self.machine_st.error_form(err, stub_gen()));
|
Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => return Err(e),
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
let err = self
|
||||||
|
.machine_st
|
||||||
|
.type_error(ValidType::Atom, function_name_arg);
|
||||||
|
Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
}
|
}
|
||||||
|
|
||||||
self.machine_st.fail = true;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "ffi"))]
|
#[cfg(not(feature = "ffi"))]
|
||||||
@@ -5149,7 +5153,7 @@ impl Machine {
|
|||||||
},
|
},
|
||||||
Value::Struct(name, args) => {
|
Value::Struct(name, args) => {
|
||||||
let struct_value =
|
let struct_value =
|
||||||
resource_error_call_result!(self.machine_st, self.build_struct(&name, args));
|
resource_error_call_result!(self.machine_st, self.build_struct(name, args));
|
||||||
|
|
||||||
unify!(self.machine_st, return_value, struct_value);
|
unify!(self.machine_st, return_value, struct_value);
|
||||||
}
|
}
|
||||||
@@ -5166,8 +5170,8 @@ impl Machine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
#[cfg(feature = "ffi")]
|
||||||
fn build_struct(&mut self, name: &str, mut args: Vec<Value>) -> Result<HeapCellValue, usize> {
|
fn build_struct(&mut self, name: Atom, mut args: Vec<Value>) -> Result<HeapCellValue, usize> {
|
||||||
args.insert(0, Value::CString(CString::new(name).unwrap()));
|
args.insert(0, Value::CString(CString::new(&*name.as_str()).unwrap()));
|
||||||
|
|
||||||
let cells: Vec<_> = args
|
let cells: Vec<_> = args
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -5183,7 +5187,7 @@ impl Machine {
|
|||||||
&self.machine_st.atom_tbl,
|
&self.machine_st.atom_tbl,
|
||||||
&cstr.into_string().unwrap()
|
&cstr.into_string().unwrap()
|
||||||
)),
|
)),
|
||||||
Value::Struct(name, struct_args) => self.build_struct(&name, struct_args)?,
|
Value::Struct(name, struct_args) => self.build_struct(name, struct_args)?,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect::<Result<_, usize>>()?;
|
.collect::<Result<_, usize>>()?;
|
||||||
@@ -5199,9 +5203,9 @@ impl Machine {
|
|||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
#[cfg(feature = "ffi")]
|
||||||
{
|
{
|
||||||
let struct_name_arg = self.deref_register(1);
|
let struct_name_arg = self.machine_st.store(self.deref_register(1));
|
||||||
let fields_reg = self.deref_register(2);
|
let fields_reg = self.deref_register(2);
|
||||||
if let Some(struct_name) = self.machine_st.value_to_str_like(struct_name_arg) {
|
if let Some(struct_name) = struct_name_arg.to_atom() {
|
||||||
let fields: Vec<Atom> = match self.machine_st.try_from_list(fields_reg, stub_gen) {
|
let fields: Vec<Atom> = match self.machine_st.try_from_list(fields_reg, stub_gen) {
|
||||||
Ok(addrs) => {
|
Ok(addrs) => {
|
||||||
let mut args = Vec::new();
|
let mut args = Vec::new();
|
||||||
@@ -5224,15 +5228,16 @@ impl Machine {
|
|||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
};
|
};
|
||||||
self.foreign_function_table
|
self.foreign_function_table
|
||||||
.define_struct(&struct_name.as_str(), fields)
|
.define_struct(struct_name, fields)
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
let ffi_error = self.machine_st.ffi_error(err, struct_name_arg);
|
let ffi_error = self.machine_st.ffi_error(err);
|
||||||
self.machine_st.error_form(ffi_error, stub_gen())
|
self.machine_st.error_form(ffi_error, stub_gen())
|
||||||
})?;
|
})?;
|
||||||
return Ok(());
|
Ok(())
|
||||||
|
} else {
|
||||||
|
let err = self.machine_st.type_error(ValidType::Atom, struct_name_arg);
|
||||||
|
Err(self.machine_st.error_form(err, stub_gen()))
|
||||||
}
|
}
|
||||||
self.machine_st.fail = true;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "ffi"))]
|
#[cfg(not(feature = "ffi"))]
|
||||||
@@ -5272,7 +5277,7 @@ impl Machine {
|
|||||||
) {
|
) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(ffi_error) => {
|
Err(ffi_error) => {
|
||||||
let machine_error = self.machine_st.ffi_error(ffi_error, ffi_type_arg);
|
let machine_error = self.machine_st.ffi_error(ffi_error);
|
||||||
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -5305,7 +5310,7 @@ impl Machine {
|
|||||||
.foreign_function_table
|
.foreign_function_table
|
||||||
.read_ptr(ffi_type, ptr, &mut self.machine_st.arena)
|
.read_ptr(ffi_type, ptr, &mut self.machine_st.arena)
|
||||||
.map_err(|ffi_error| {
|
.map_err(|ffi_error| {
|
||||||
let machine_error = self.machine_st.ffi_error(ffi_error, ffi_type_arg);
|
let machine_error = self.machine_st.ffi_error(ffi_error);
|
||||||
self.machine_st.error_form(machine_error, stub_gen())
|
self.machine_st.error_form(machine_error, stub_gen())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@@ -5346,7 +5351,7 @@ impl Machine {
|
|||||||
{
|
{
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(ffi_error) => {
|
Err(ffi_error) => {
|
||||||
let machine_error = self.machine_st.ffi_error(ffi_error, ffi_type_arg);
|
let machine_error = self.machine_st.ffi_error(ffi_error);
|
||||||
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user