fix builds without ffi feature

This commit is contained in:
Skgland
2025-08-24 21:08:52 +02:00
committed by Bennet Bleßmann
parent 1f7a60dec9
commit 6f3f66c407
3 changed files with 251 additions and 188 deletions

View File

@@ -4302,62 +4302,50 @@ 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 => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.ffi_allocate());
step_or_fail!(self, self.machine_st.p += 1);
}
&Instruction::ExecuteFfiAllocate => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.ffi_allocate());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
}
&Instruction::CallFfiReadPtr => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.ffi_read_ptr());
step_or_fail!(self, self.machine_st.p += 1);
}
&Instruction::ExecuteFfiReadPtr => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.ffi_read_ptr());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
}
&Instruction::CallFfiDeallocate => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.ffi_deallocate());
step_or_fail!(self, self.machine_st.p += 1);
}
&Instruction::ExecuteFfiDeallocate => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.ffi_deallocate());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
}

View File

@@ -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 {
let stub = functor!(atom!("system_error"));

View File

@@ -4952,13 +4952,17 @@ impl Machine {
Ok(())
}
#[cfg(feature = "ffi")]
#[inline(always)]
pub(crate) fn load_foreign_lib(&mut self) -> CallResult {
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) {
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();
@@ -5003,6 +5007,14 @@ impl Machine {
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(feature = "ffi")]
fn map_ffi_arg(
&mut self,
source: HeapCellValue,
@@ -5070,13 +5082,14 @@ impl Machine {
}
}
#[cfg(feature = "ffi")]
#[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)
}
#[cfg(feature = "ffi")]
{
let function_name_arg = self.deref_register(1);
let args_reg = self.deref_register(2);
let return_value = self.deref_register(3);
@@ -5110,6 +5123,14 @@ impl Machine {
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(feature = "ffi")]
fn unify_ffi_result(&mut self, return_value: HeapCellValue, result: Value) -> CallResult {
match result {
Value::Number(n) => match n {
@@ -5170,13 +5191,14 @@ 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 {
fn stub_gen() -> MachineStub {
functor_stub(atom!("$define_foreign_struct"), 2)
}
#[cfg(feature = "ffi")]
{
let struct_name_arg = 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) {
@@ -5213,9 +5235,20 @@ impl Machine {
Ok(())
}
pub(crate) fn ffi_allocate(&mut self) -> CallResult {
let stub_gen = || functor_stub(atom!("$ffi_allocate"), 4);
#[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();
@@ -5247,9 +5280,20 @@ impl Machine {
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);
#[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);
@@ -5268,9 +5312,20 @@ impl Machine {
self.unify_ffi_result(return_value, value)
}
pub(crate) fn ffi_deallocate(&mut self) -> CallResult {
let stub_gen = || functor_stub(atom!("$ffi_deallocate"), 3);
#[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();
@@ -5295,10 +5350,16 @@ impl Machine {
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"))]
#[inline(always)]
pub(crate) fn js_eval(&mut self) -> CallResult {