Stream: beginners

Topic: Calling compiler from rust


view this post on Zulip Max (Aug 30 2024 at 18:54):

Hi, I would like to explore the idea of compiling (possibly untrusted) code from within my program. Are there already bindings, in rust say, to call the compiler so one does not need to do shell command stuff?

view this post on Zulip Sam Mohr (Aug 30 2024 at 18:56):

You'd like to compile Roc code within your program?

view this post on Zulip Sam Mohr (Aug 30 2024 at 18:56):

Long story short, that's not a thing yet, and probably won't ever be a supported feature

view this post on Zulip Sam Mohr (Aug 30 2024 at 18:59):

However, you could fork an existing platform like basic-cli and add a function call to do it on the Rust side. However, that would basically come out to a heavier version of Cmd.exec "roc" ["build", ...]

view this post on Zulip Max (Aug 30 2024 at 20:17):

Yeah, maybe Cmd.exec wouldn't even hurt so much. Do you happen to know how it is done on the website? I haven't looked it up, but afaik the repl available there is the compiler being called in wasm.

view this post on Zulip Sam Mohr (Aug 30 2024 at 20:19):

Oh, that's a bit more complicated. The source for that starts here: https://github.com/roc-lang/roc/blob/f6c969af293270feb90dfdc8fcc3ffbe18f9b268/crates/repl_wasm/src/repl.rs#L182

view this post on Zulip Sam Mohr (Aug 30 2024 at 20:20):

We compile a WASM version of the REPL, send your code to it, compile and link it, stringify the results, and then show it in JS

view this post on Zulip Sam Mohr (Aug 30 2024 at 20:21):

That could be something to look at, but it's unlikely we provide a JS "eval" equivalent for Roc

view this post on Zulip Sam Mohr (Aug 30 2024 at 20:21):

However, as time has gone on, the compiler has become more and more modular. Maybe if you think it's really valuable, we could discuss exposing a more general Rust interface for the compiler?

view this post on Zulip Luke Boswell (Aug 30 2024 at 20:40):

I think libroc is a good idea.

I've used the compiler in this way just pulling in the rust crates and it works well, though it's definitely not a normal approach. I wouldn't recommend it though rn as things are still changing and evolving.

view this post on Zulip Richard Feldman (Aug 30 2024 at 21:25):

yeah I'd really like to introduce a libroc at some point!


Last updated: Jul 05 2025 at 12:14 UTC