fix builds without ffi feature
This commit is contained in:
@@ -4302,62 +4302,50 @@ impl Machine {
|
|||||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||||
}
|
}
|
||||||
&Instruction::CallLoadForeignLib => {
|
&Instruction::CallLoadForeignLib => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.load_foreign_lib());
|
try_or_throw!(self.machine_st, self.load_foreign_lib());
|
||||||
step_or_fail!(self, self.machine_st.p += 1);
|
step_or_fail!(self, self.machine_st.p += 1);
|
||||||
}
|
}
|
||||||
&Instruction::ExecuteLoadForeignLib => {
|
&Instruction::ExecuteLoadForeignLib => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.load_foreign_lib());
|
try_or_throw!(self.machine_st, self.load_foreign_lib());
|
||||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||||
}
|
}
|
||||||
&Instruction::CallForeignCall => {
|
&Instruction::CallForeignCall => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.foreign_call());
|
try_or_throw!(self.machine_st, self.foreign_call());
|
||||||
step_or_fail!(self, self.machine_st.p += 1);
|
step_or_fail!(self, self.machine_st.p += 1);
|
||||||
}
|
}
|
||||||
&Instruction::ExecuteForeignCall => {
|
&Instruction::ExecuteForeignCall => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.foreign_call());
|
try_or_throw!(self.machine_st, self.foreign_call());
|
||||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||||
}
|
}
|
||||||
&Instruction::CallDefineForeignStruct => {
|
&Instruction::CallDefineForeignStruct => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.define_foreign_struct());
|
try_or_throw!(self.machine_st, self.define_foreign_struct());
|
||||||
step_or_fail!(self, self.machine_st.p += 1);
|
step_or_fail!(self, self.machine_st.p += 1);
|
||||||
}
|
}
|
||||||
&Instruction::ExecuteDefineForeignStruct => {
|
&Instruction::ExecuteDefineForeignStruct => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.define_foreign_struct());
|
try_or_throw!(self.machine_st, self.define_foreign_struct());
|
||||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||||
}
|
}
|
||||||
&Instruction::CallFfiAllocate => {
|
&Instruction::CallFfiAllocate => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.ffi_allocate());
|
try_or_throw!(self.machine_st, self.ffi_allocate());
|
||||||
step_or_fail!(self, self.machine_st.p += 1);
|
step_or_fail!(self, self.machine_st.p += 1);
|
||||||
}
|
}
|
||||||
&Instruction::ExecuteFfiAllocate => {
|
&Instruction::ExecuteFfiAllocate => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.ffi_allocate());
|
try_or_throw!(self.machine_st, self.ffi_allocate());
|
||||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||||
}
|
}
|
||||||
&Instruction::CallFfiReadPtr => {
|
&Instruction::CallFfiReadPtr => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.ffi_read_ptr());
|
try_or_throw!(self.machine_st, self.ffi_read_ptr());
|
||||||
step_or_fail!(self, self.machine_st.p += 1);
|
step_or_fail!(self, self.machine_st.p += 1);
|
||||||
}
|
}
|
||||||
&Instruction::ExecuteFfiReadPtr => {
|
&Instruction::ExecuteFfiReadPtr => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.ffi_read_ptr());
|
try_or_throw!(self.machine_st, self.ffi_read_ptr());
|
||||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||||
}
|
}
|
||||||
&Instruction::CallFfiDeallocate => {
|
&Instruction::CallFfiDeallocate => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.ffi_deallocate());
|
try_or_throw!(self.machine_st, self.ffi_deallocate());
|
||||||
step_or_fail!(self, self.machine_st.p += 1);
|
step_or_fail!(self, self.machine_st.p += 1);
|
||||||
}
|
}
|
||||||
&Instruction::ExecuteFfiDeallocate => {
|
&Instruction::ExecuteFfiDeallocate => {
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
try_or_throw!(self.machine_st, self.ffi_deallocate());
|
try_or_throw!(self.machine_st, self.ffi_deallocate());
|
||||||
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -603,6 +603,20 @@ impl MachineState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn missing_feature_error(&self, feature: Atom) -> MachineError {
|
||||||
|
let stub = functor!(
|
||||||
|
atom!("resource_error"),
|
||||||
|
[functor(
|
||||||
|
(functor!(atom!("feature"), [atom_as_cell((feature))]))
|
||||||
|
)]
|
||||||
|
);
|
||||||
|
|
||||||
|
MachineError {
|
||||||
|
stub,
|
||||||
|
location: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn unreachable_error(&self) -> MachineError {
|
pub(super) fn unreachable_error(&self) -> MachineError {
|
||||||
let stub = functor!(atom!("system_error"));
|
let stub = functor!(atom!("system_error"));
|
||||||
|
|
||||||
|
|||||||
@@ -4952,57 +4952,69 @@ impl Machine {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) fn load_foreign_lib(&mut self) -> CallResult {
|
pub(crate) fn load_foreign_lib(&mut self) -> CallResult {
|
||||||
let library_name = self.deref_register(1);
|
fn stub_gen() -> MachineStub {
|
||||||
let args_reg = self.deref_register(2);
|
functor_stub(atom!("$load_foreign_lib"), 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) {
|
#[cfg(feature = "ffi")]
|
||||||
Ok(addrs) => {
|
{
|
||||||
let mut functions = Vec::new();
|
let library_name = self.deref_register(1);
|
||||||
for heap_cell in addrs {
|
let args_reg = self.deref_register(2);
|
||||||
read_heap_cell!(heap_cell,
|
if let Some(library_name) = self.machine_st.value_to_str_like(library_name) {
|
||||||
(HeapCellValueTag::Str, s) => {
|
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
||||||
let name = cell_as_atom_cell!(self.machine_st.heap[s]).get_name();
|
Ok(addrs) => {
|
||||||
let args: Vec<Atom> = match self.machine_st.try_from_list(self.machine_st.heap[s + 1], stub_gen) {
|
let mut functions = Vec::new();
|
||||||
Ok(addrs) => {
|
for heap_cell in addrs {
|
||||||
let mut args = Vec::new();
|
read_heap_cell!(heap_cell,
|
||||||
for heap_cell in addrs {
|
(HeapCellValueTag::Str, s) => {
|
||||||
args.push(cell_as_atom_cell!(heap_cell).get_name());
|
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) {
|
||||||
args
|
Ok(addrs) => {
|
||||||
}
|
let mut args = Vec::new();
|
||||||
Err(e) => return Err(e)
|
for heap_cell in addrs {
|
||||||
};
|
args.push(cell_as_atom_cell!(heap_cell).get_name());
|
||||||
let return_value = cell_as_atom_cell!(self.machine_st.heap[s + 2]);
|
}
|
||||||
functions.push(FunctionDefinition {
|
args
|
||||||
name: name.as_str().to_string(),
|
}
|
||||||
args,
|
Err(e) => return Err(e)
|
||||||
return_value: return_value.get_name(),
|
};
|
||||||
});
|
let return_value = cell_as_atom_cell!(self.machine_st.heap[s + 2]);
|
||||||
}
|
functions.push(FunctionDefinition {
|
||||||
_ => {
|
name: name.as_str().to_string(),
|
||||||
unreachable!()
|
args,
|
||||||
}
|
return_value: return_value.get_name(),
|
||||||
)
|
});
|
||||||
}
|
}
|
||||||
if self
|
_ => {
|
||||||
.foreign_function_table
|
unreachable!()
|
||||||
.load_library(&library_name.as_str(), &functions)
|
}
|
||||||
.is_ok()
|
)
|
||||||
{
|
}
|
||||||
return Ok(());
|
if self
|
||||||
}
|
.foreign_function_table
|
||||||
}
|
.load_library(&library_name.as_str(), &functions)
|
||||||
Err(e) => return Err(e),
|
.is_ok()
|
||||||
};
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
self.machine_st.fail = true;
|
||||||
|
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(feature = "ffi")]
|
||||||
fn map_ffi_arg(
|
fn map_ffi_arg(
|
||||||
&mut self,
|
&mut self,
|
||||||
source: HeapCellValue,
|
source: HeapCellValue,
|
||||||
@@ -5070,46 +5082,55 @@ impl Machine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) fn foreign_call(&mut self) -> CallResult {
|
pub(crate) fn foreign_call(&mut self) -> CallResult {
|
||||||
fn stub_gen() -> Vec<FunctorElement> {
|
fn stub_gen() -> Vec<FunctorElement> {
|
||||||
functor_stub(atom!("foreign_call"), 3)
|
functor_stub(atom!("$foreign_call"), 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
let function_name_arg = self.deref_register(1);
|
#[cfg(feature = "ffi")]
|
||||||
let args_reg = self.deref_register(2);
|
{
|
||||||
let return_value = self.deref_register(3);
|
let function_name_arg = self.deref_register(1);
|
||||||
if let Some(function_name) = self.machine_st.value_to_str_like(function_name_arg) {
|
let args_reg = self.deref_register(2);
|
||||||
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
let return_value = self.deref_register(3);
|
||||||
Ok(args) => {
|
if let Some(function_name) = self.machine_st.value_to_str_like(function_name_arg) {
|
||||||
let args = args
|
match self.machine_st.try_from_list(args_reg, stub_gen) {
|
||||||
.into_iter()
|
Ok(args) => {
|
||||||
.map(|x| self.map_ffi_arg(x, stub_gen))
|
let args = args
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.into_iter()
|
||||||
|
.map(|x| self.map_ffi_arg(x, stub_gen))
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
match self.foreign_function_table.exec(
|
match self.foreign_function_table.exec(
|
||||||
&function_name.as_str(),
|
&function_name.as_str(),
|
||||||
args,
|
args,
|
||||||
&mut self.machine_st.arena,
|
&mut self.machine_st.arena,
|
||||||
) {
|
) {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
return 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, function_name_arg);
|
||||||
return Err(self.machine_st.error_form(err, stub_gen()));
|
return Err(self.machine_st.error_form(err, stub_gen()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
Err(e) => return Err(e),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.machine_st.fail = true;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
self.machine_st.fail = true;
|
#[cfg(not(feature = "ffi"))]
|
||||||
Ok(())
|
{
|
||||||
|
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 {
|
fn unify_ffi_result(&mut self, return_value: HeapCellValue, result: Value) -> CallResult {
|
||||||
match result {
|
match result {
|
||||||
Value::Number(n) => match n {
|
Value::Number(n) => match n {
|
||||||
@@ -5170,133 +5191,173 @@ impl Machine {
|
|||||||
sized_iter_to_heap_list(&mut self.machine_st.heap, cells.len(), cells.into_iter())
|
sized_iter_to_heap_list(&mut self.machine_st.heap, cells.len(), cells.into_iter())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ffi")]
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) fn define_foreign_struct(&mut self) -> CallResult {
|
pub(crate) fn define_foreign_struct(&mut self) -> CallResult {
|
||||||
fn stub_gen() -> MachineStub {
|
fn stub_gen() -> MachineStub {
|
||||||
functor_stub(atom!("$define_foreign_struct"), 2)
|
functor_stub(atom!("$define_foreign_struct"), 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
let struct_name_arg = self.deref_register(1);
|
#[cfg(feature = "ffi")]
|
||||||
let fields_reg = self.deref_register(2);
|
{
|
||||||
if let Some(struct_name) = self.machine_st.value_to_str_like(struct_name_arg) {
|
let struct_name_arg = self.deref_register(1);
|
||||||
let fields: Vec<Atom> = match self.machine_st.try_from_list(fields_reg, stub_gen) {
|
let fields_reg = self.deref_register(2);
|
||||||
Ok(addrs) => {
|
if let Some(struct_name) = self.machine_st.value_to_str_like(struct_name_arg) {
|
||||||
let mut args = Vec::new();
|
let fields: Vec<Atom> = match self.machine_st.try_from_list(fields_reg, stub_gen) {
|
||||||
for heap_cell in addrs {
|
Ok(addrs) => {
|
||||||
let arg_cell = self.machine_st.store(self.machine_st.deref(heap_cell));
|
let mut args = Vec::new();
|
||||||
let Some(arg) = arg_cell.to_atom() else {
|
for heap_cell in addrs {
|
||||||
let err = if arg_cell.is_var() {
|
let arg_cell = self.machine_st.store(self.machine_st.deref(heap_cell));
|
||||||
self.machine_st.instantiation_error()
|
let Some(arg) = arg_cell.to_atom() else {
|
||||||
} else {
|
let err = if arg_cell.is_var() {
|
||||||
self.machine_st.type_error(ValidType::Atom, heap_cell)
|
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()));
|
||||||
};
|
};
|
||||||
|
|
||||||
return Err(self.machine_st.error_form(err, stub_gen()));
|
args.push(arg);
|
||||||
};
|
}
|
||||||
|
args
|
||||||
args.push(arg);
|
|
||||||
}
|
}
|
||||||
args
|
Err(e) => return Err(e),
|
||||||
}
|
};
|
||||||
Err(e) => return Err(e),
|
self.foreign_function_table
|
||||||
};
|
.define_struct(&struct_name.as_str(), fields)
|
||||||
self.foreign_function_table
|
.map_err(|err| {
|
||||||
.define_struct(&struct_name.as_str(), fields)
|
let ffi_error = self.machine_st.ffi_error(err, struct_name_arg);
|
||||||
.map_err(|err| {
|
self.machine_st.error_form(ffi_error, stub_gen())
|
||||||
let ffi_error = self.machine_st.ffi_error(err, struct_name_arg);
|
})?;
|
||||||
self.machine_st.error_form(ffi_error, stub_gen())
|
return Ok(());
|
||||||
})?;
|
}
|
||||||
return Ok(());
|
self.machine_st.fail = true;
|
||||||
|
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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn ffi_allocate(&mut self) -> CallResult {
|
pub(crate) fn ffi_allocate(&mut self) -> CallResult {
|
||||||
let stub_gen = || functor_stub(atom!("$ffi_allocate"), 4);
|
fn stub_gen() -> MachineStub {
|
||||||
|
functor_stub(atom!("$ffi_allocate"), 4)
|
||||||
|
}
|
||||||
|
|
||||||
let allocator = self.deref_register(1);
|
#[cfg(feature = "ffi")]
|
||||||
let ffi_type_arg = self.deref_register(2);
|
{
|
||||||
let ffi_type = ffi_type_arg.to_atom().unwrap();
|
let allocator = self.deref_register(1);
|
||||||
let args = self.deref_register(3);
|
let ffi_type_arg = self.deref_register(2);
|
||||||
let return_value = self.deref_register(4);
|
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 allocator = FfiAllocator::try_from(allocator.to_atom().unwrap()).map_err(|_| {
|
||||||
let machine_error = self
|
let machine_error = self
|
||||||
.machine_st
|
.machine_st
|
||||||
.domain_error(DomainErrorType::Allocator, allocator);
|
.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, ffi_type_arg);
|
|
||||||
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
self.unify_ffi_result(return_value, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn ffi_read_ptr(&mut self) -> CallResult {
|
|
||||||
let stub_gen = || functor_stub(atom!("$ffi_read_ptr"), 3);
|
|
||||||
|
|
||||||
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, ffi_type_arg);
|
|
||||||
self.machine_st.error_form(machine_error, stub_gen())
|
self.machine_st.error_form(machine_error, stub_gen())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
self.unify_ffi_result(return_value, value)
|
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, ffi_type_arg);
|
||||||
|
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, ffi_type_arg);
|
||||||
|
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 {
|
pub(crate) fn ffi_deallocate(&mut self) -> CallResult {
|
||||||
let stub_gen = || functor_stub(atom!("$ffi_deallocate"), 3);
|
fn stub_gen() -> MachineStub {
|
||||||
|
functor_stub(atom!("$ffi_deallocate"), 3)
|
||||||
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, ffi_type_arg);
|
|
||||||
return Err(self.machine_st.error_form(machine_error, stub_gen()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
#[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, ffi_type_arg);
|
||||||
|
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()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
|||||||
Reference in New Issue
Block a user