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?
You'd like to compile Roc code within your program?
Long story short, that's not a thing yet, and probably won't ever be a supported feature
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", ...]
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.
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
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
That could be something to look at, but it's unlikely we provide a JS "eval" equivalent for Roc
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?
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.
yeah I'd really like to introduce a libroc
at some point!
Last updated: Jul 05 2025 at 12:14 UTC