cleanup ffi error handling and use Atom instead of &str in appropriate places
This commit is contained in:
@@ -3,7 +3,7 @@ use crate::atom_table::*;
|
||||
use crate::parser::ast::*;
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
use crate::ffi::FfiError;
|
||||
use crate::ffi::{self, FfiError};
|
||||
use crate::forms::*;
|
||||
use crate::functor_macro::*;
|
||||
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)]
|
||||
pub(super) fn functor_stub(name: Atom, arity: usize) -> MachineStub {
|
||||
functor!(atom!("/"), [atom_as_cell(name), fixnum(arity)])
|
||||
@@ -418,6 +442,28 @@ impl MachineState {
|
||||
[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 {
|
||||
stub,
|
||||
location: None,
|
||||
@@ -628,42 +674,43 @@ impl MachineState {
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
pub(super) fn ffi_error(&self, err: FfiError, culprit: HeapCellValue) -> MachineError {
|
||||
let error_atom = match err {
|
||||
FfiError::ValueCast => atom!("value_cast"),
|
||||
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) => {
|
||||
pub(super) fn ffi_error(&mut self, err: FfiError) -> MachineError {
|
||||
match err {
|
||||
FfiError::ValueCast(expected, actual) => {
|
||||
let stub = functor!(
|
||||
atom!("ffi_error"),
|
||||
[
|
||||
atom_as_cell((atom!("struct_not_found"))),
|
||||
atom_as_cell(culprit)
|
||||
]
|
||||
atom!("domain_error"),
|
||||
[atom_as_cell(expected), atom_as_cell(actual)]
|
||||
);
|
||||
|
||||
return MachineError {
|
||||
MachineError {
|
||||
stub,
|
||||
location: None,
|
||||
};
|
||||
}
|
||||
}
|
||||
FfiError::ArgCountMismatch => atom!("mismatched_argument_count"),
|
||||
FfiError::AllocationFailed => atom!("allocation_failed"),
|
||||
FfiError::LayoutError => atom!("layout_error"),
|
||||
FfiError::UnsupportedAbi => atom!("unsupported_abi"),
|
||||
};
|
||||
let stub = functor!(
|
||||
atom!("ffi_error"),
|
||||
[atom_as_cell(error_atom), cell(culprit)]
|
||||
);
|
||||
|
||||
MachineError {
|
||||
stub,
|
||||
location: None,
|
||||
FfiError::ValueOutOfRange(domain, culprit) => self.domain_error(domain, culprit),
|
||||
FfiError::FunctionNotFound(name) => {
|
||||
self.existence_error(ExistenceError::FfiFunction(name))
|
||||
}
|
||||
FfiError::StructNotFound(name) => {
|
||||
self.existence_error(ExistenceError::FfiStructType(name))
|
||||
}
|
||||
FfiError::ArgCountMismatch => self.unreachable_error(),
|
||||
FfiError::AllocationFailed => MachineError {
|
||||
stub: functor!(atom!("resource_error"), [atom_as_cell((atom!("heap")))]),
|
||||
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,
|
||||
Directive,
|
||||
Allocator,
|
||||
FfiStruct,
|
||||
ZeroOrOne,
|
||||
NonNullPtr,
|
||||
PtrLike,
|
||||
F64,
|
||||
FfiArgument,
|
||||
FfiArgumentType,
|
||||
FixedSizedInt,
|
||||
}
|
||||
|
||||
impl DomainErrorType {
|
||||
@@ -860,6 +915,14 @@ impl DomainErrorType {
|
||||
DomainErrorType::OperatorPriority => atom!("operator_priority"),
|
||||
DomainErrorType::Directive => atom!("directive"),
|
||||
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,
|
||||
// MinInteger,
|
||||
Term,
|
||||
FfiLayout,
|
||||
FfiAbi,
|
||||
}
|
||||
|
||||
impl RepFlag {
|
||||
@@ -885,7 +950,9 @@ impl RepFlag {
|
||||
RepFlag::MaxArity => atom!("max_arity"),
|
||||
RepFlag::Term => atom!("term"),
|
||||
// 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),
|
||||
Stream(HeapCellValue),
|
||||
Process(HeapCellValue),
|
||||
FfiFunction(Atom),
|
||||
FfiStructType(Atom),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::arena::*;
|
||||
use crate::atom_table::*;
|
||||
use crate::forms::*;
|
||||
use crate::functor_macro::*;
|
||||
use crate::heap_iter::*;
|
||||
use crate::heap_print::*;
|
||||
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.
|
||||
// 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]);
|
||||
functions.push(FunctionDefinition {
|
||||
name: name.as_str().to_string(),
|
||||
name,
|
||||
args,
|
||||
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
|
||||
.foreign_function_table
|
||||
.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"))]
|
||||
@@ -5034,9 +5040,9 @@ impl Machine {
|
||||
|
||||
if let Some(head) = iter.next() {
|
||||
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(
|
||||
struct_name.as_str().to_string(),
|
||||
struct_name,
|
||||
iter.map(|x| self.map_ffi_arg(x, stub_gen))
|
||||
.collect::<Result<_, _>>()?,
|
||||
))
|
||||
@@ -5052,17 +5058,15 @@ impl Machine {
|
||||
Err(self.machine_st.error_form(err, src))
|
||||
} else {
|
||||
// first element of a struct needs to be the type
|
||||
Err(self.machine_st.error_form(
|
||||
self.machine_st.ffi_error(FfiError::ValueOutOfRange, head),
|
||||
stub_gen(),
|
||||
))
|
||||
let err = self.machine_st.type_error(ValidType::Atom, head);
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
} else {
|
||||
// empty list is an invalid struct repr
|
||||
Err(self.machine_st.error_form(
|
||||
self.machine_st.ffi_error(FfiError::ValueOutOfRange, source),
|
||||
stub_gen(),
|
||||
))
|
||||
let err = self
|
||||
.machine_st
|
||||
.domain_error(DomainErrorType::FfiStruct, source);
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
} else if self.machine_st.deref(source).is_var() {
|
||||
let err = self.machine_st.instantiation_error();
|
||||
@@ -5075,10 +5079,10 @@ impl Machine {
|
||||
|
||||
Err(self.machine_st.error_form(err, src))
|
||||
} else {
|
||||
Err(self.machine_st.error_form(
|
||||
self.machine_st.ffi_error(FfiError::InvalidArgument, source),
|
||||
stub_gen(),
|
||||
))
|
||||
let err = self
|
||||
.machine_st
|
||||
.domain_error(DomainErrorType::FfiArgument, source);
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5090,10 +5094,10 @@ impl Machine {
|
||||
|
||||
#[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 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) {
|
||||
Ok(args) => {
|
||||
let args = args
|
||||
@@ -5102,25 +5106,25 @@ impl Machine {
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
match self.foreign_function_table.exec(
|
||||
&function_name.as_str(),
|
||||
function_name,
|
||||
args,
|
||||
&mut self.machine_st.arena,
|
||||
) {
|
||||
Ok(result) => {
|
||||
return self.unify_ffi_result(return_value, result);
|
||||
}
|
||||
Ok(result) => self.unify_ffi_result(return_value, result),
|
||||
Err(e) => {
|
||||
let err = self.machine_st.ffi_error(e, function_name_arg);
|
||||
return Err(self.machine_st.error_form(err, stub_gen()));
|
||||
let err = self.machine_st.ffi_error(e);
|
||||
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"))]
|
||||
@@ -5149,7 +5153,7 @@ impl Machine {
|
||||
},
|
||||
Value::Struct(name, args) => {
|
||||
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);
|
||||
}
|
||||
@@ -5166,8 +5170,8 @@ impl Machine {
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
fn build_struct(&mut self, name: &str, mut args: Vec<Value>) -> Result<HeapCellValue, usize> {
|
||||
args.insert(0, Value::CString(CString::new(name).unwrap()));
|
||||
fn build_struct(&mut self, name: Atom, mut args: Vec<Value>) -> Result<HeapCellValue, usize> {
|
||||
args.insert(0, Value::CString(CString::new(&*name.as_str()).unwrap()));
|
||||
|
||||
let cells: Vec<_> = args
|
||||
.into_iter()
|
||||
@@ -5183,7 +5187,7 @@ impl Machine {
|
||||
&self.machine_st.atom_tbl,
|
||||
&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>>()?;
|
||||
@@ -5199,9 +5203,9 @@ impl Machine {
|
||||
|
||||
#[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);
|
||||
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) {
|
||||
Ok(addrs) => {
|
||||
let mut args = Vec::new();
|
||||
@@ -5224,15 +5228,16 @@ impl Machine {
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
self.foreign_function_table
|
||||
.define_struct(&struct_name.as_str(), fields)
|
||||
.define_struct(struct_name, fields)
|
||||
.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())
|
||||
})?;
|
||||
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"))]
|
||||
@@ -5272,7 +5277,7 @@ impl Machine {
|
||||
) {
|
||||
Ok(value) => value,
|
||||
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()));
|
||||
}
|
||||
};
|
||||
@@ -5305,7 +5310,7 @@ impl Machine {
|
||||
.foreign_function_table
|
||||
.read_ptr(ffi_type, ptr, &mut self.machine_st.arena)
|
||||
.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())
|
||||
})?;
|
||||
|
||||
@@ -5346,7 +5351,7 @@ impl Machine {
|
||||
{
|
||||
Ok(value) => value,
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user