Embedding rust in my app as a user-facing language

I'm interested in embedding Rust (or more precisely, rustc) in my app much like one might embed Lua (which I have done). That is, users of the app would write rust code in the app. To avoid crashes, would have to disable unsafe. I can't invoke rustc and dynamically load the result because my app runs on iOS, where I can't launch processes.

Can this be done without tremendous effort?

I found this very old article: Embedding and customizing the Rust compiler
also found a repl project (which seems to use some unfortunate methods, but not sure): GitHub - evcxr/evcxr

thanks!

1 Like

You can do this, but it will take a fair bit of effort. Rustc exposes an interface through rustc_driver: rustc_driver and rustc_interface - Rust Compiler Development Guide
Rustc is more of a framework than a library; I'm not sure how easy it will be to embed it in another application. Note also that this will tie you to a specific version of the toolchain; if you want to update to a newer toolchain you'll have to also update your code to deal with breaking changes in the compiler interface.

2 Likes

Note also that if you can't launch processes you probably won't be able to use the LLVM backend, which uses the system linker. I think Cranelift has a JIT mode but I think that may also be banned by Apple's policies?

Yeah, JIT is banned too. Was thinking on iOS I could use a WASM interpreter? Forgot to mention that part.

(for Lua, I use LuaJIT with the JIT only turned on for macOS)

That seems possible, yes, Cranelift has a wasm backend. I haven't tried to use it but I know Fastly at least uses it in production and sponsors the work.

You might also be interested in https://rhai.rs/, which is a much smaller rust-like language designed for exactly this use case.

That looks quite nice (and perhaps I should have used that instead of Lua), but this is for writing DSP kernels so it should be statically typed and fast at least on some platforms where I can JIT.

1 Like

I also recently looked into scripting support, but unlike you I'm not targeting a platform that doesn't allow me to spawn new processes. I stumbled across Extism in a Hacker News post, which allows you to embed WASM into your application. Looks fairly easy to embed a (simple) WASM file and execute a function from it. This section makes me believe that Extism can even run WebAssembly that uses WASI. I don't know how you'd get the Rust code compiled to WASM on iOS though...

1 Like

As an aside, you're going to want to sandbox it anyway. There's more types of crashes than UB, and even if you don't care about that, rustc/std are not soundness bug free.

3 Likes

Can you elaborate a bit? I can't sandbox anything on iOS since I can't use a separate process. That rustc could crash my app due to a bug is ok. I bet rustc is more stable than some of the 3rd party libraries I use, and my own code as well.

Also, another problem which I forgot to mention earlier is that I have to ensure the code doesn't run too long and lock up the audio thread. So I would have to get rustc to instrument the code with the occasional timer check. Currently I do that with Lua using a "debug hook."

It sounded like you wanted to modify the compiler to restrict what your users could do. If you don't care about crashes (e.g. users can only hurt themselves by writing bad code), it may not matter (but then why modify the compiler?).

I do need to ensure that users can't crash or hang the app, which is what I've done with Lua.

What are the other crashes you're referring to other than UB? I thought all of those would be panics that I could handle.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.