Cranelift - interacting with Rust data types

I have been experimenting with Cranelift, and was wondering what limits there are on interacting with Rust data. Just as an example, supposing I wanted to have a toy language, where the datatype is String rather than integer, and these are to be Rust Strings, could that work?

Some additional questions:

(a) Can I call Rust functions to perform some operations? For example, creating a new String. Do I need to create wrapper functions with a known calling convention?

(b) Can cranelift code access the String directly? I guess when compiling with Cranelift, the code would need to find out the details of how the String data structure is laid out. How would that work?

(c) Any relevant code examples ?

Cranelift speaks only primitive types. If you want to interact with Rust types directly, you'd need to match ABI.

Matching ABI in general is very hard, so for practicality you'd probably want to only ever deal in thin pointers to sized things (so &String or &&str) and call known functions dealing in that.

If you want specifics, you'll probably have better luck at https://bytecodealliance.zulipchat.com/ thank here.

Thanks. After I posted I wondered about using String::into_raw_parts although it is only in nightly.

I guess using raw pointers makes sense, as it avoids any ABI problems?

Interop with cranelift has essentially all the same problems as interop with C over extern "C" -- just a bit harder since you're doing the ABI yourself. So the same kinds of approaches that work for FFI will work for what's basically manual-FFI in cranelift.

1 Like

Though much easier than in the x86-32 days :yum:

1 Like