Merge pull request #3063 from Skgland/ffi++
ffi API extension and fixes
This commit is contained in:
@@ -4302,35 +4302,53 @@ impl Machine {
|
||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||
}
|
||||
&Instruction::CallLoadForeignLib => {
|
||||
#[cfg(feature = "ffi")]
|
||||
try_or_throw!(self.machine_st, self.load_foreign_lib());
|
||||
step_or_fail!(self, self.machine_st.p += 1);
|
||||
}
|
||||
&Instruction::ExecuteLoadForeignLib => {
|
||||
#[cfg(feature = "ffi")]
|
||||
try_or_throw!(self.machine_st, self.load_foreign_lib());
|
||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||
}
|
||||
&Instruction::CallForeignCall => {
|
||||
#[cfg(feature = "ffi")]
|
||||
try_or_throw!(self.machine_st, self.foreign_call());
|
||||
step_or_fail!(self, self.machine_st.p += 1);
|
||||
}
|
||||
&Instruction::ExecuteForeignCall => {
|
||||
#[cfg(feature = "ffi")]
|
||||
try_or_throw!(self.machine_st, self.foreign_call());
|
||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||
}
|
||||
&Instruction::CallDefineForeignStruct => {
|
||||
#[cfg(feature = "ffi")]
|
||||
try_or_throw!(self.machine_st, self.define_foreign_struct());
|
||||
step_or_fail!(self, self.machine_st.p += 1);
|
||||
}
|
||||
&Instruction::ExecuteDefineForeignStruct => {
|
||||
#[cfg(feature = "ffi")]
|
||||
try_or_throw!(self.machine_st, self.define_foreign_struct());
|
||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||
}
|
||||
&Instruction::CallFfiAllocate => {
|
||||
try_or_throw!(self.machine_st, self.ffi_allocate());
|
||||
step_or_fail!(self, self.machine_st.p += 1);
|
||||
}
|
||||
&Instruction::ExecuteFfiAllocate => {
|
||||
try_or_throw!(self.machine_st, self.ffi_allocate());
|
||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||
}
|
||||
&Instruction::CallFfiReadPtr => {
|
||||
try_or_throw!(self.machine_st, self.ffi_read_ptr());
|
||||
step_or_fail!(self, self.machine_st.p += 1);
|
||||
}
|
||||
&Instruction::ExecuteFfiReadPtr => {
|
||||
try_or_throw!(self.machine_st, self.ffi_read_ptr());
|
||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||
}
|
||||
&Instruction::CallFfiDeallocate => {
|
||||
try_or_throw!(self.machine_st, self.ffi_deallocate());
|
||||
step_or_fail!(self, self.machine_st.p += 1);
|
||||
}
|
||||
&Instruction::ExecuteFfiDeallocate => {
|
||||
try_or_throw!(self.machine_st, self.ffi_deallocate());
|
||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||
}
|
||||
&Instruction::CallJsEval => {
|
||||
try_or_throw!(self.machine_st, self.js_eval());
|
||||
step_or_fail!(self, self.machine_st.p += 1);
|
||||
|
||||
@@ -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,
|
||||
@@ -603,6 +649,21 @@ impl MachineState {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // not used when all features are enabled
|
||||
pub(super) fn missing_feature_error(&self, feature: Atom) -> MachineError {
|
||||
let stub = functor!(
|
||||
atom!("representation_error"),
|
||||
[functor(
|
||||
(functor!(atom!("feature"), [atom_as_cell((feature))]))
|
||||
)]
|
||||
);
|
||||
|
||||
MachineError {
|
||||
stub,
|
||||
location: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn unreachable_error(&self) -> MachineError {
|
||||
let stub = functor!(atom!("system_error"));
|
||||
|
||||
@@ -613,26 +674,46 @@ impl MachineState {
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
pub(super) fn ffi_error(&self, err: FfiError) -> 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 => atom!("struct_not_found"),
|
||||
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)]);
|
||||
pub(super) fn ffi_error(&mut self, err: FfiError) -> MachineError {
|
||||
match err {
|
||||
FfiError::ValueCast(expected, actual) => {
|
||||
let stub = functor!(
|
||||
atom!("domain_error"),
|
||||
[atom_as_cell(expected), atom_as_cell(actual)]
|
||||
);
|
||||
|
||||
MachineError {
|
||||
stub,
|
||||
location: None,
|
||||
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 => self.domain_error(
|
||||
DomainErrorType::NonCStrFfiArgumentType,
|
||||
atom_as_cell!(atom!("cstr")),
|
||||
),
|
||||
FfiError::NullPtr => self.domain_error(
|
||||
DomainErrorType::NonNullPtr,
|
||||
fixnum_as_cell!(Fixnum::build_with(0)),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -813,6 +894,16 @@ pub(crate) enum DomainErrorType {
|
||||
OperatorSpecifier,
|
||||
OperatorPriority,
|
||||
Directive,
|
||||
Allocator,
|
||||
FfiStruct,
|
||||
ZeroOrOne,
|
||||
NonNullPtr,
|
||||
PtrLike,
|
||||
F64,
|
||||
FfiArgument,
|
||||
FfiArgumentType,
|
||||
FixedSizedInt,
|
||||
NonCStrFfiArgumentType,
|
||||
}
|
||||
|
||||
impl DomainErrorType {
|
||||
@@ -827,6 +918,16 @@ impl DomainErrorType {
|
||||
DomainErrorType::OperatorSpecifier => atom!("operator_specifier"),
|
||||
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"),
|
||||
DomainErrorType::NonCStrFfiArgumentType => atom!("non_cstr_ffi_argument_type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -841,6 +942,8 @@ pub(crate) enum RepFlag {
|
||||
// MaxInteger,
|
||||
// MinInteger,
|
||||
Term,
|
||||
FfiLayout,
|
||||
FfiAbi,
|
||||
}
|
||||
|
||||
impl RepFlag {
|
||||
@@ -852,7 +955,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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1032,6 +1137,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 = Result<(), Vec<FunctorElement>>;
|
||||
pub type CallResult<Ok = ()> = Result<Ok, MachineStub>;
|
||||
|
||||
// size may be an upper bound.
|
||||
// true_size is calculated to compute the exact offset.
|
||||
|
||||
@@ -4959,170 +4959,226 @@ impl Machine {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
#[inline(always)]
|
||||
pub(crate) fn load_foreign_lib(&mut self) -> CallResult {
|
||||
let library_name = self.deref_register(1);
|
||||
let args_reg = self.deref_register(2);
|
||||
if let Some(library_name) = self.machine_st.value_to_str_like(library_name) {
|
||||
let stub_gen = || functor_stub(atom!("use_foreign_module"), 2);
|
||||
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
||||
Ok(addrs) => {
|
||||
let mut functions = Vec::new();
|
||||
for heap_cell in addrs {
|
||||
read_heap_cell!(heap_cell,
|
||||
(HeapCellValueTag::Str, s) => {
|
||||
let name = cell_as_atom_cell!(self.machine_st.heap[s]).get_name();
|
||||
let args: Vec<Atom> = match self.machine_st.try_from_list(self.machine_st.heap[s + 1], stub_gen) {
|
||||
Ok(addrs) => {
|
||||
let mut args = Vec::new();
|
||||
for heap_cell in addrs {
|
||||
args.push(cell_as_atom_cell!(heap_cell).get_name());
|
||||
}
|
||||
args
|
||||
}
|
||||
Err(e) => return Err(e)
|
||||
};
|
||||
let return_value = cell_as_atom_cell!(self.machine_st.heap[s + 2]);
|
||||
functions.push(FunctionDefinition {
|
||||
name: name.as_str().to_string(),
|
||||
args,
|
||||
return_value: return_value.get_name(),
|
||||
});
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
)
|
||||
}
|
||||
if self
|
||||
.foreign_function_table
|
||||
.load_library(&library_name.as_str(), &functions)
|
||||
.is_ok()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
fn stub_gen() -> MachineStub {
|
||||
functor_stub(atom!("$load_foreign_lib"), 2)
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
{
|
||||
let library_name = self.deref_register(1);
|
||||
let args_reg = self.deref_register(2);
|
||||
if let Some(library_name) = self.machine_st.value_to_str_like(library_name) {
|
||||
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
||||
Ok(addrs) => {
|
||||
let mut functions = Vec::new();
|
||||
for heap_cell in addrs {
|
||||
read_heap_cell!(heap_cell,
|
||||
(HeapCellValueTag::Str, s) => {
|
||||
let name = cell_as_atom_cell!(self.machine_st.heap[s]).get_name();
|
||||
let args: Vec<Atom> = match self.machine_st.try_from_list(self.machine_st.heap[s + 1], stub_gen) {
|
||||
Ok(addrs) => {
|
||||
let mut args = Vec::new();
|
||||
for heap_cell in addrs {
|
||||
args.push(cell_as_atom_cell!(heap_cell).get_name());
|
||||
}
|
||||
args
|
||||
}
|
||||
Err(e) => return Err(e)
|
||||
};
|
||||
let return_value = cell_as_atom_cell!(self.machine_st.heap[s + 2]);
|
||||
functions.push(FunctionDefinition {
|
||||
name,
|
||||
args,
|
||||
return_value: return_value.get_name(),
|
||||
});
|
||||
}
|
||||
_ => {
|
||||
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_err()
|
||||
{
|
||||
self.machine_st.fail = true;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
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()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ffi"))]
|
||||
{
|
||||
let err = self.machine_st.missing_feature_error(atom!("ffi"));
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
self.machine_st.fail = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
fn map_ffi_arg(
|
||||
&mut self,
|
||||
source: HeapCellValue,
|
||||
stub_gen: impl Copy + Fn() -> MachineStub,
|
||||
) -> CallResult<Value> {
|
||||
let source = self.machine_st.store(self.machine_st.deref(source));
|
||||
if let Ok(number) = Number::try_from((source, &self.machine_st.arena.f64_tbl)) {
|
||||
Ok(Value::Number(number))
|
||||
} else if let Some(string) = self.machine_st.value_to_str_like(source) {
|
||||
Ok(Value::CString(CString::new(&*string.as_str()).unwrap()))
|
||||
} else if let Ok(args) = self.machine_st.try_from_list(source, stub_gen) {
|
||||
// structs are lists represented as lists
|
||||
// the head is a string with the struct type name
|
||||
// the tail are the struct field values
|
||||
|
||||
let mut iter = args.into_iter();
|
||||
|
||||
if let Some(head) = iter.next() {
|
||||
let head = self.machine_st.store(self.machine_st.deref(head));
|
||||
if let Some(struct_name) = head.to_atom() {
|
||||
Ok(Value::Struct(
|
||||
struct_name,
|
||||
iter.map(|x| self.map_ffi_arg(x, stub_gen))
|
||||
.collect::<Result<_, _>>()?,
|
||||
))
|
||||
} else if head.is_var() {
|
||||
let err = self.machine_st.instantiation_error();
|
||||
|
||||
let src = stub_gen();
|
||||
|
||||
let culprit = functor!(atom!("-"), [atom_as_cell((atom!("var"))), cell(head)]);
|
||||
|
||||
let src = functor!(atom!("."), [functor(culprit), list([functor(src)])]);
|
||||
|
||||
Err(self.machine_st.error_form(err, src))
|
||||
} else {
|
||||
// first element of a struct needs to be the type
|
||||
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
|
||||
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();
|
||||
|
||||
let src = stub_gen();
|
||||
|
||||
let culprit = functor!(atom!("-"), [atom_as_cell((atom!("var"))), cell(source)]);
|
||||
|
||||
let src = functor!(atom!("."), [functor(culprit), list([functor(src)])]);
|
||||
|
||||
Err(self.machine_st.error_form(err, src))
|
||||
} else {
|
||||
let err = self
|
||||
.machine_st
|
||||
.domain_error(DomainErrorType::FfiArgument, source);
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn foreign_call(&mut self) -> CallResult {
|
||||
fn stub_gen() -> Vec<FunctorElement> {
|
||||
functor_stub(atom!("foreign_call"), 3)
|
||||
functor_stub(atom!("$foreign_call"), 3)
|
||||
}
|
||||
|
||||
fn map_arg(
|
||||
machine_st: &mut MachineState,
|
||||
source: HeapCellValue,
|
||||
) -> Result<crate::ffi::Value, FfiError> {
|
||||
if let Ok(number) = Number::try_from((source, &machine_st.arena.f64_tbl)) {
|
||||
Ok(Value::Number(number))
|
||||
} else if let Some(string) = machine_st.value_to_str_like(source) {
|
||||
Ok(Value::CString(CString::new(&*string.as_str()).unwrap()))
|
||||
} else if let Ok(args) = machine_st.try_from_list(source, stub_gen) {
|
||||
// structs are lists represented as lists
|
||||
// the head is a string with the struct type name
|
||||
// the tail are the struct field values
|
||||
#[cfg(feature = "ffi")]
|
||||
{
|
||||
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) = function_name_arg.to_atom() {
|
||||
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
||||
Ok(args) => {
|
||||
let args = args
|
||||
.into_iter()
|
||||
.map(|x| self.map_ffi_arg(x, stub_gen))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
let mut iter = args.into_iter();
|
||||
if let Some(struct_name) = machine_st.value_to_str_like(iter.next().unwrap()) {
|
||||
Ok(Value::Struct(
|
||||
struct_name.as_str().to_string(),
|
||||
iter.map(|x| map_arg(machine_st, x))
|
||||
.collect::<Result<_, _>>()?,
|
||||
))
|
||||
} else {
|
||||
// empty list is an invalid struct repr
|
||||
Err(FfiError::InvalidStruct)
|
||||
}
|
||||
} else {
|
||||
Err(FfiError::InvalidArgument)
|
||||
}
|
||||
}
|
||||
|
||||
let function_name = 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) {
|
||||
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
||||
Ok(args) => {
|
||||
let args = match args
|
||||
.into_iter()
|
||||
.map(|x| map_arg(&mut self.machine_st, x))
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
{
|
||||
Ok(args) => args,
|
||||
Err(err) => {
|
||||
let err = self.machine_st.ffi_error(err);
|
||||
return Err(self.machine_st.error_form(err, stub_gen()));
|
||||
}
|
||||
};
|
||||
|
||||
match self.foreign_function_table.exec(
|
||||
&function_name.as_str(),
|
||||
args,
|
||||
&mut self.machine_st.arena,
|
||||
) {
|
||||
Ok(result) => {
|
||||
match result {
|
||||
Value::Number(n) => match n {
|
||||
Number::Float(OrderedFloat(n)) => {
|
||||
let n = float_alloc!(n, self.machine_st.arena);
|
||||
self.machine_st.unify_f64(n, return_value)
|
||||
}
|
||||
Number::Integer(typed_arena_ptr) => {
|
||||
self.machine_st.unify_big_int(typed_arena_ptr, return_value)
|
||||
}
|
||||
Number::Rational(typed_arena_ptr) => {
|
||||
self.machine_st
|
||||
.unify_rational(typed_arena_ptr, return_value);
|
||||
}
|
||||
Number::Fixnum(fixnum) => {
|
||||
self.machine_st.unify_fixnum(fixnum, return_value)
|
||||
}
|
||||
},
|
||||
Value::Struct(name, args) => {
|
||||
let struct_value = resource_error_call_result!(
|
||||
self.machine_st,
|
||||
self.build_struct(&name, args)
|
||||
);
|
||||
|
||||
unify!(self.machine_st, return_value, struct_value);
|
||||
}
|
||||
Value::CString(cstr) => {
|
||||
let str_cell = resource_error_call_result!(
|
||||
self.machine_st,
|
||||
self.machine_st.heap.allocate_cstr(cstr.to_str().unwrap())
|
||||
);
|
||||
|
||||
unify!(self.machine_st, str_cell, return_value);
|
||||
}
|
||||
match self.foreign_function_table.exec(
|
||||
function_name,
|
||||
args,
|
||||
&mut self.machine_st.arena,
|
||||
) {
|
||||
Ok(result) => self.unify_ffi_result(return_value, result),
|
||||
Err(e) => {
|
||||
let err = self.machine_st.ffi_error(e);
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
let err = self.machine_st.ffi_error(e);
|
||||
return Err(self.machine_st.error_form(err, stub_gen()));
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
Err(e) => return 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;
|
||||
#[cfg(not(feature = "ffi"))]
|
||||
{
|
||||
let err = self.machine_st.missing_feature_error(atom!("ffi"));
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
fn unify_ffi_result(&mut self, return_value: HeapCellValue, result: Value) -> CallResult {
|
||||
match result {
|
||||
Value::Number(n) => match n {
|
||||
Number::Float(OrderedFloat(n)) => {
|
||||
let n = float_alloc!(n, self.machine_st.arena);
|
||||
self.machine_st.unify_f64(n, return_value)
|
||||
}
|
||||
Number::Integer(typed_arena_ptr) => {
|
||||
self.machine_st.unify_big_int(typed_arena_ptr, return_value)
|
||||
}
|
||||
Number::Rational(typed_arena_ptr) => {
|
||||
self.machine_st
|
||||
.unify_rational(typed_arena_ptr, return_value);
|
||||
}
|
||||
Number::Fixnum(fixnum) => self.machine_st.unify_fixnum(fixnum, return_value),
|
||||
},
|
||||
Value::Struct(name, args) => {
|
||||
let struct_value =
|
||||
resource_error_call_result!(self.machine_st, self.build_struct(name, args));
|
||||
|
||||
unify!(self.machine_st, return_value, struct_value);
|
||||
}
|
||||
Value::CString(cstr) => {
|
||||
let str_cell = resource_error_call_result!(
|
||||
self.machine_st,
|
||||
self.machine_st.heap.allocate_cstr(cstr.to_str().unwrap())
|
||||
);
|
||||
|
||||
unify!(self.machine_st, str_cell, return_value);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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()
|
||||
@@ -5138,7 +5194,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>>()?;
|
||||
@@ -5146,33 +5202,174 @@ impl Machine {
|
||||
sized_iter_to_heap_list(&mut self.machine_st.heap, cells.len(), cells.into_iter())
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
#[inline(always)]
|
||||
pub(crate) fn define_foreign_struct(&mut self) -> CallResult {
|
||||
let struct_name = 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) {
|
||||
let stub_gen = || functor_stub(atom!("define_foreign_struct"), 2);
|
||||
let fields: Vec<Atom> = match self.machine_st.try_from_list(fields_reg, stub_gen) {
|
||||
Ok(addrs) => {
|
||||
let mut args = Vec::new();
|
||||
for heap_cell in addrs {
|
||||
args.push(cell_as_atom_cell!(heap_cell).get_name());
|
||||
}
|
||||
args
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
self.foreign_function_table
|
||||
.define_struct(&struct_name.as_str(), fields)
|
||||
.map_err(|err| {
|
||||
let ffi_error = self.machine_st.ffi_error(err);
|
||||
self.machine_st.error_form(ffi_error, stub_gen())
|
||||
})?;
|
||||
return Ok(());
|
||||
fn stub_gen() -> MachineStub {
|
||||
functor_stub(atom!("$define_foreign_struct"), 2)
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
{
|
||||
let struct_name_arg = self.machine_st.store(self.deref_register(1));
|
||||
let fields_reg = self.deref_register(2);
|
||||
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();
|
||||
for heap_cell in addrs {
|
||||
let arg_cell = self.machine_st.store(self.machine_st.deref(heap_cell));
|
||||
let Some(arg) = arg_cell.to_atom() else {
|
||||
let err = if arg_cell.is_var() {
|
||||
self.machine_st.instantiation_error()
|
||||
} else {
|
||||
self.machine_st.type_error(ValidType::Atom, heap_cell)
|
||||
};
|
||||
|
||||
return Err(self.machine_st.error_form(err, stub_gen()));
|
||||
};
|
||||
|
||||
args.push(arg);
|
||||
}
|
||||
args
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
self.foreign_function_table
|
||||
.define_struct(struct_name, fields)
|
||||
.map_err(|err| {
|
||||
let ffi_error = self.machine_st.ffi_error(err);
|
||||
self.machine_st.error_form(ffi_error, stub_gen())
|
||||
})?;
|
||||
Ok(())
|
||||
} else {
|
||||
let err = self.machine_st.type_error(ValidType::Atom, struct_name_arg);
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ffi"))]
|
||||
{
|
||||
let err = self.machine_st.missing_feature_error(atom!("ffi"));
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn ffi_allocate(&mut self) -> CallResult {
|
||||
fn stub_gen() -> MachineStub {
|
||||
functor_stub(atom!("$ffi_allocate"), 4)
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
{
|
||||
let allocator = self.deref_register(1);
|
||||
let ffi_type_arg = self.deref_register(2);
|
||||
let ffi_type = ffi_type_arg.to_atom().unwrap();
|
||||
let args = self.deref_register(3);
|
||||
let return_value = self.deref_register(4);
|
||||
|
||||
let allocator = FfiAllocator::try_from(allocator.to_atom().unwrap()).map_err(|_| {
|
||||
let machine_error = self
|
||||
.machine_st
|
||||
.domain_error(DomainErrorType::Allocator, allocator);
|
||||
self.machine_st.error_form(machine_error, stub_gen())
|
||||
})?;
|
||||
|
||||
let args = self.map_ffi_arg(args, stub_gen)?;
|
||||
|
||||
let value = match self.foreign_function_table.allocate(
|
||||
allocator,
|
||||
ffi_type,
|
||||
args,
|
||||
&mut self.machine_st.arena,
|
||||
) {
|
||||
Ok(value) => value,
|
||||
Err(ffi_error) => {
|
||||
let machine_error = self.machine_st.ffi_error(ffi_error);
|
||||
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
||||
}
|
||||
};
|
||||
|
||||
self.unify_ffi_result(return_value, value)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ffi"))]
|
||||
{
|
||||
let err = self.machine_st.missing_feature_error(atom!("ffi"));
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn ffi_read_ptr(&mut self) -> CallResult {
|
||||
fn stub_gen() -> MachineStub {
|
||||
functor_stub(atom!("$ffi_read_ptr"), 3)
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
{
|
||||
let ffi_type_arg = self.deref_register(1);
|
||||
let ffi_type = ffi_type_arg.to_atom().unwrap();
|
||||
let ptr = self.deref_register(2);
|
||||
let return_value = self.deref_register(3);
|
||||
|
||||
let ptr = self.map_ffi_arg(ptr, stub_gen)?;
|
||||
|
||||
let value = self
|
||||
.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);
|
||||
self.machine_st.error_form(machine_error, stub_gen())
|
||||
})?;
|
||||
|
||||
self.unify_ffi_result(return_value, value)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ffi"))]
|
||||
{
|
||||
let err = self.machine_st.missing_feature_error(atom!("ffi"));
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn ffi_deallocate(&mut self) -> CallResult {
|
||||
fn stub_gen() -> MachineStub {
|
||||
functor_stub(atom!("$ffi_deallocate"), 3)
|
||||
}
|
||||
|
||||
#[cfg(feature = "ffi")]
|
||||
{
|
||||
let allocator = self.deref_register(1);
|
||||
let ffi_type_arg = self.deref_register(2);
|
||||
let ffi_type = ffi_type_arg.to_atom().unwrap();
|
||||
let ptr = self.deref_register(3);
|
||||
|
||||
let allocator = FfiAllocator::try_from(allocator.to_atom().unwrap()).map_err(|_| {
|
||||
let machine_error = self
|
||||
.machine_st
|
||||
.domain_error(DomainErrorType::Allocator, allocator);
|
||||
self.machine_st.error_form(machine_error, stub_gen())
|
||||
})?;
|
||||
|
||||
let ptr = self.map_ffi_arg(ptr, stub_gen)?;
|
||||
|
||||
match self
|
||||
.foreign_function_table
|
||||
.deallocate(allocator, ffi_type, ptr)
|
||||
{
|
||||
Ok(value) => value,
|
||||
Err(ffi_error) => {
|
||||
let machine_error = self.machine_st.ffi_error(ffi_error);
|
||||
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ffi"))]
|
||||
{
|
||||
let err = self.machine_st.missing_feature_error(atom!("ffi"));
|
||||
Err(self.machine_st.error_form(err, stub_gen()))
|
||||
}
|
||||
self.machine_st.fail = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
Reference in New Issue
Block a user