Embedded and sandboxed scripting languages in Rust
from AsudoxDev@programming.dev to rust@programming.dev on 12 Nov 2024 08:21
https://programming.dev/post/21622044
from AsudoxDev@programming.dev to rust@programming.dev on 12 Nov 2024 08:21
https://programming.dev/post/21622044
I want to allow the users of my project be able to write the driving logic, while I provide the tools. What are some embedded scripting languages in Rust that can be sandboxed and are easy for absolute beginners?
edit: Thanks for all of your answers, I decided to go with lua using mlua
threaded - newest
You can embed Lua pretty easily using a few different libraries. One I liked in particular was
mlua
. Lua is tiny, usually sufficiently fast, and easy to learn.Otherwise, there are some others like Rhai, but there may be fewer resources available for users if you go with a less popular language.
Thanks for this. Seems like I’ll go with Lua. I actually did try Rhai before posting this and it did work pretty easily and had a very nice and convenient plugin system. But it had no LSP and on top of that, it wasn’t as easy as Lua.
The most battletested way is via bindings to other languages. Ex. mlua with luau sandboxing Or Javascript via v8 or deno_core
There are also a few languages implemented in rust like rhai
There is also the option of compiling the user code to wasm and using wasmtime to run it
Seems like Luau will do. Thanks
Many languages can be compiled to wasm, and it’s quite easy to load and run wasm from rust
That way you could give the user multiple options, they could write it in rust, c, c++, javascript, etc
WASM is a bit overkill and is also complicated. I need a simple scripting language that won’t stress the users out. As mentioned in my post, I expect them to be absolute beginners.
I didn’t mean that the users would write wasm, just that they could write it in some language, then your program would compile it to wasm and run it
Gives some flexibility as to the choice of language and it runs sandboxed
yeah no no, who writes wasm by hand?
it’s just that this interoperability with all languages is a bit overkill. I also don’t know how I would expose a rust struct that has methods in wasm
Although the OP has already decided to go with Lua, if anyone else looking at this thread is considering their options for making a plugin system, WASM/WASI is an excellent choice - mostly because it allows users to use Python (a favorite among programming newbies) along with any other language that compiles to WebAssembly (which will eventually be all languages).
You can even use something like Extism which is purpose-built as a drop-in library for creating a plugin system with WASM plugins, or you can use other runtime options like Wasmtime (built in Rust, from the ByteCode Alliance - where the WASI spec is being developed) or the WasmEdge runtime (built in C++, which is a CNCF project and is already being used in areas like eBPF to help users build Linux Kernel extensions).
In short, if you’re building a project that has to be maintained long-term and you need a plugin system, then choose WebAssembly - you can’t go wrong.
Cheers!
Boa is a remarkably well-developed JS runtime written in Rust. Nice API, and well sandboxed. We’re considering adopting it for Biome as well.
Thanks for the suggestion. Though JS itself is a pretty weird language and has some weird behaviours, I’d rather not use it. It’s also a bit too advanced for my use case. (somewhat simple control flow, maybe some math, etc.)
One more vote for using WASM. Using WASM has the benefit of allowing the user to use almost whatever language they want, as long as it can compile to WASM. So the user doesn’t have to learn some bespoke scripting-specific language.
There's a list of good options on Are We Game Yet, hope one of these crates is useful.
https://arewegameyet.rs/ecosystem/scripting/
Crates.io has tags like scripting. It suggests languages like rhai, dyon, or rune.
Would something like Steel work?
The Helix editor is working on adding it as a plugin scripting language. You could take a look at the in-progress fork to see how they’re doing it.
PreFix notation for mathematical operations. Hehe.
Interesting for me, but I’d rather not give this to someone who us more interested in making a plugin and calling it a day.
There is a Python interpreter written in Rust. It’s apparently intended to (besides being fast an all that) make Rust scriptable.
I’m aware that pyo3 is a thing, but last I checked that was just Rust bindings to CPython. Is this not that?
github.com/RustPython/RustPython
what the fuck
Another meme answer:
nu
.I never actually used
nu
for anything. But I’ve been thinking (unironically) thatnu
with its built-infrom_json
andto_json
can be interesting.The use-case I had in mind is not games or anything like that, but some system or dev tools that traditionally utilized shell scripts, but are moving towards better languages like python. So I thought a single binary that embeds
nu
, but also has a lot of sub-commands that implement a lot of sub-tasks in Rust directly, and with JSON used as an exchange format, the combination can be interesting.Now that I think about it more, this can work in both directions, with main execution being in nu (what I had in mind), or in Rust.
nu
even has an lsp server, so the development experience should theoretically be good.As a sysadmin Python is very far from a better language than Shell, it is much too fragile over time for that. You can’t even rely on a Python script running unmodified on the oldest currently supported OS versions and the latest ones.