FFI: Nested structs

This commit is contained in:
Adrián Arroyo Calle
2023-02-26 20:48:00 +01:00
parent 92b262d599
commit f94294dbd9
2 changed files with 184 additions and 139 deletions

View File

@@ -4257,6 +4257,7 @@ impl Machine {
}
}
}
match self.machine_st.try_from_list(args_reg, stub_gen) {
Ok(args) => {
let args: Vec<_> = args.into_iter().map(|x| map_arg(&mut self.machine_st, x)).collect();
@@ -4264,22 +4265,9 @@ impl Machine {
Ok(result) => {
match result {
Value::Int(n) => self.machine_st.unify_fixnum(Fixnum::build_with(n), return_value),
Value::Struct(name, mut args) => {
args.insert(0, Value::CString(CString::new(name).unwrap()));
let struct_list = heap_loc_as_cell!(
iter_to_heap_list(
&mut self.machine_st.heap,
args.into_iter()
.map(|val| {
match val {
Value::Int(n) => fixnum_as_cell!(Fixnum::build_with(n)),
Value::CString(cstr) => atom_as_cell!(self.machine_st.atom_tbl.build_with(&cstr.into_string().unwrap())),
_ => unreachable!()
}
}),
)
);
unify!(self.machine_st, return_value, struct_list);
Value::Struct(name, args) => {
let struct_value = self.build_struct(&name, args);
unify!(self.machine_st, return_value, struct_value);
}
_ => {
unreachable!();
@@ -4302,6 +4290,26 @@ impl Machine {
Ok(())
}
fn build_struct(&mut self, name: &str, mut args: Vec<Value>) -> HeapCellValue {
args.insert(0, Value::CString(CString::new(name).unwrap()));
let cells: Vec<_> = args.into_iter()
.map(|val| {
match val {
Value::Int(n) => fixnum_as_cell!(Fixnum::build_with(n)),
Value::CString(cstr) => atom_as_cell!(self.machine_st.atom_tbl.build_with(&cstr.into_string().unwrap())),
Value::Struct(name, struct_args) => self.build_struct(&name, struct_args),
_ => unreachable!()
}
}).collect();
heap_loc_as_cell!(
iter_to_heap_list(
&mut self.machine_st.heap,
cells.into_iter()
)
)
}
#[inline(always)]
pub(crate) fn define_foreign_struct(&mut self) -> CallResult {
let struct_name = self.deref_register(1);