Closed segeljakt closed 7 months ago
Oops, I think I found a workaround. It is possible to achieve what I need using:
#[test]
fn test_string_concat() {
let lib = load();
let fun = fun(&lib, "concat");
unsafe {
let layout = Layout::new::<StabbyString>();
let ptr = alloc(layout);
// Create string type
let string_ty = &mut libffi::low::ffi_type {
size: layout.size(),
alignment: layout.align() as u16,
type_: libffi::raw::FFI_TYPE_STRUCT as u16,
elements: [Type::structure([]).as_raw_ptr()].as_mut_ptr(),
} as *mut ffi_type;
let mut arg_types = [string_ty, string_ty];
let nargs = arg_types.len() as u32;
let mut cif = ffi_cif::default();
ffi_prep_cif(
&mut cif,
ffi_abi_FFI_DEFAULT_ABI,
nargs,
string_ty,
arg_types.as_mut_ptr(),
);
let a0 = StabbyString::from("hello");
let a1 = StabbyString::from("world");
ffi_call(
&mut cif,
Some(*CodePtr(*fun).as_safe_fun()),
ptr as *mut c_void,
[Arg::new(&a0), Arg::new(&a1)].as_ptr() as *mut *mut c_void,
);
std::mem::forget(a0);
std::mem::forget(a1);
let s = ptr.cast::<StabbyString>().read();
assert_eq!(s, "helloworld");
};
}
Hi there,
I'm currently on holiday with little computer access, so I can't make very detailed answers, but I'm sure integration would be possible (although it'd require a bit of effort).
Very happy that you found a workaround that suits you :)
Thanks, no problem. Hope you have a great holiday :)
I'm trying to use
stabby
in an interpreter for a programming language to dynamically load libraries whose functions can have complex signatures that are not known until interpretation-time. To load the libraries, I am usinglibloading
andlibffi
:For example, if I have this library:
I can load it dynamically and run it with:
I would like to do the same thing as above, but for more complex types like
String
,Vec<T>
,Rc<T>
, which is what brought me to stabby. I think I can already use stabby for this purpose, but a challenge is that I would need to constructType
s that correctly represent the layout of each stabby type. I think this is an ok solution, but it might get difficult if the ABI changes. If it aligns with stabby's goals, would it be possible to extend stabby with a feature that provideslibffi
Type information for each stabby type?For example, given a library:
This could be an imaginary API:
Thanks for the great crate.