12 ms·
Rhai: An embedded scripting language for Rust
- Epicism 2y agoThat’s very cool! I appreciate how you can simplify interacting with rust applications without rust programming skills.
- chubs 2y agoVery impressive. I read the readme and I’m unsure how memory management works, is it GC? And is it OOP or not? Thanks :)
- kreetx 2y agoOnly guessing, but since the language is for scripting then maybe all garbage could be collected when the script finishes?
- tommiegannert 2y agoI was also curious. Looking at the code, it seems values are Boxed, but there's a special type called Shared that is an Rc-RefCell (unless Send-enabled.): // Also handle case where target is a `Dynamic` shared value // (returned by a variable resolver, for example) Couldn't find any other information about a GC, so guessing this is pure ref-counting. Speaking of potential memory leaks, there's also string interning happening. I agree this seems to be for short-lived contexts right now.
- phire 2y agoNot even reference counting. There are no references as values are always cloned (or moved).
- ilrwbwrkhv 2y agoThis is awesome. Would this be a replacement for Lua use cases in a Rust desktop app?
- searealist 2y agoNeeds a comparison to the excellent Rust Starlark Implementation.
- BiteCode_dev 2y agoStarlark is for config, not scripting.
- searealist 2y agoI'm not sure what you think the difference is.
- BiteCode_dev 2y agoYou can say that for everything, after all if it's turing complete you can do anything. But practically, the difference is intention, which drives design and ecosystem. E.g: starlark is very oriented toward idempotence and limiting side effects to get reproducible config data. By default they discourage reading files: https://github.com/bazelbuild/bazel/issues/13300 https://github.com/bazelbuild/bazel/issues/13300 But rhai is not particularly oriented toward config, and the doc promotes an extension to read files: https://rhai.rs/book/lib/rhai-fs.html https://rhai.rs/book/lib/rhai-fs.html The tutorials, stdlib, language design anf tooling will all reflect this. You probably don't want to use starlark to automate much action, but it will be well suited to describe states.
- thayne 2y agoIIRC starlark is intentionally NOT turing complete. It doesn't allow recursion, or general loops.
- BiteCode_dev 2y agoAnd rhai is, it allows infinite loops.
- searealist 2y ago
- satvikpendem 2y agoWhen I last read about Rhai it was apparently very slow such that it was simply faster and more ergonomic writing scripts in Rust itself, has that changed?
- MarceColl 2y agoNot sure when you read this, but I can tell you that two years ago it was VERY slow. I used it for a game and I had to redo it in lua some months later because it was a big bottleneck. I don't have more up to date information.
- xnacly 2y agoAccording to the documentation it evaluates by walking the ast, so yes, this is considered very slow. The readme also mentions 1 million loop iterations in 0.14s (140ms). Even my unoptimised naive lips like language [1] (also implemented via a ast walker) does the same in 28.67ms - so yes id consider this pretty slow. [1]: https://github.com/xNaCly/Sophia https://github.com/xNaCly/Sophia
- diggan 2y agoAs someone who is also implementing a naive "walk the AST" evaluator for lisp, what would be considered OK/fast/not-slow in the case for 1 million loop iterations? Would ~30ms be considered fast or "not-slow"?
- xnacly 2y agoIt just mentions a loop, so id say for a loop without any content, it should be less than 50ms, but as the other commentor said, it depends on your hardware and a better measurement is to compare relatively
- wffurr 2y agoMillisecond timings are only meaningful on a specific hardware target.
- ComputerGuru 2y ago
- Black616Angel 2y agoWhat does "Passes Miri." mean? There is no link and online I only find a Malaysian city with that name.
- Deukhoofd 2y agoMiri is a Rust tool with a similar function to Valgrind, it checks for undefined behavior. https://github.com/rust-lang/miri https://github.com/rust-lang/miri
- ChadNauseam 2y agoFor anyone curious for more details, miri works in a different way than valgrind. it is an interpreter for rust that does additional checks at runtime to detect undefined behavior. This allows it to be fully deterministic, simulate other platforms, and do additional checks that I don’t think would be possible for valgrind
- diggan 2y agoSomeone else already answered, just a tip for future searches: If you know somewhat the context (in this case Rust), adding just one keyword to your query (in this case "Miri Rust") will give you the right answer as the first hit :)
- oulipo 2y agoCool! But wouldn't the way to go for scripting in a language these days to just compile to WASM and run in an embedded micro-VM? Why hasn't Rhai made this choice?
- thayne 2y ago1. That require compiling to wasm, and for some use cases, you don't want a compilation step. That might even be a big part of why you are using a scripting language. 2. That requires an entire wasm runtime, which is a pretty heavy dependency
- chaosprint 2y agoHere are two real-world examples of using Rhai: https://glicol.org/tour#meta1 https://glicol.org/tour#meta1 https://glicol.org/tour#meta2 https://glicol.org/tour#meta2 I've embedded Rhai in Glicol to support sample-level audio synthesis, and the performance is [acceptable]. Real-time audio is very computationally intensive, after all. I've tried various embedded scripting languages in Rust, but Rhai probably has the best documentation and playground. For reference, see this survey: https://www.boringcactus.com/2020/09/16/survey-of-rust-embeddable-scripting-languages.html https://www.boringcactus.com/2020/09/16/survey-of-rust-embed... Recent Koto Lang also looks promising: https://koto.dev/ https://koto.dev/
- bhaney 2y agoAnother example using Rhai that was posted here a week or two ago: this (in-browser) 3D modeling software using Fidget. https://www.mattkeeter.com/projects/fidget/demo/ https://www.mattkeeter.com/projects/fidget/demo/
- figomore 2y agoOther embedded scripting language is Steel [1]. It’s being considered to be used in Helix [2]. [1] https://github.com/mattwparas/steel https://github.com/mattwparas/steel [2] https://github.com/helix-editor/helix/pull/8675 https://github.com/helix-editor/helix/pull/8675
- nightpool 2y agoSmall comment, but why does the text editor show an "overwrite" indictor instead of a cursor indicator when I try to change something in the playground? https://i.imgur.com/OAXbXQ2.png https://i.imgur.com/OAXbXQ2.png I tried typing different things, and despite showing an overwrite indicator it seems to be in a normal insert mode. I have no idea which side of the highlight my characters will be inserted on though.
- chaosprint 2y agoThis is totally my personal preference :) I thought it was clearer this way. But there is a new version of the website under construction: https://glicol-x.netlify.app/ https://glicol-x.netlify.app/ It will be changed to the normal look.
- GardenLetter27 2y agoHow does it compare to Rune and the others?
- emigre 2y agoIn an Apollo GraphQL federation, you can use Rhai to customize the Apollo Router's behaviour. The Apollo Router is written in Rust.
- diggan 2y agoThere are a bunch of crates available for scripting in Rust/Bevy (Rhai, Rune, Luau, Teal, etc), anyone who've tried specifically Rhai with specifically Bevy before and could share their experience?
- pseudony 2y agoNot going to dissuade anyone from having fun hobby projects. But legitimate question: why would I choose this over Lua, which is probably faster, super easy to embed and has a larger ecosystem. (Please, saying "Rust" or memory-safety can be assumed already to be understood, but not considered compelling arguments)
- thayne 2y ago> memory-safety can be assumed already to be understood, but not considered compelling arguments Depending on your application, memory safety could be a very compelling argument.
- tel 2y agoI’m already writing a Rust system. I’ve tried integrating with mlua. It works, but Rhai is simpler to embed. Simpler to build. (I also tried Piccolo which is very cool but also not simple.) Rhai also doesn’t include a lot of complexity that Lua does. It encourages you to write extension types in Rust, which is what I want. Rhai doesn’t have GC, just refcounts. Rhai also can disable a lot of features, say if you just want an expression language. I use it to write trading logic. I like that it’s stripped down and simple.
- VWWHFSfQ 2y ago> I’ve tried integrating with mlua. It works, but Rhai is simpler to embed. Simpler to build. I'm curious what challenges you faced when embedding Lua via mlua? I've done it many times in projects and I've always found it to be trivial. $ cargo add mlua --features lua54,vendored And then bringing it into Rust is simple like: let lua = mlua::Lua(); Are your requirements more complicated than this that makes it not as easy? I've never had to mess around with linking the system Lua, or anything else. mlua just brings its own Lua distribution with the `vendored` feature.
- azdle 2y agoI've used rlua (mlua is a fork of it) and mlua extensively. The part you've shown is absolutely the easy part. Doing anything interesting with the runtime after that is much less obvious. Even something as simple as 'load a lua file that returns a table of functions and use those functions from rust' is surprisingly hard to figure out. (I know, I just did that a few weeks ago.) I haven't used Rhai, but Lua has a lot of impedance mismatch with Rust that could be avoided with a fresh language. (Or maybe even just a fresh implementation of Lua, like piccolo is trying: https://github.com/kyren/piccolo https://github.com/kyren/piccolo)
- senkora 2y agoSince a quick ctrl-f didn’t find any mention here or on the README: I assume the name is a reference to ChaiScript, which is a similar embedded scripting language for C++. https://github.com/ChaiScript/ChaiScript https://github.com/ChaiScript/ChaiScript
- ForLoveOfCats 2y agoMy understanding is that the creator of Rhai, Sophia Turner, is one of the original creators of ChaiScript, along with her cousin Jason Turner
- fretn 2y agoI use it in rsmodules https://github.com/fretn/rsmodules https://github.com/fretn/rsmodules and I’m very happy with my choice. Was easy to implement and the earliest versions already offered what I needed. If the rhai author is reading this: thank you for this nice piece of software