Rhai: An embedded scripting language for Rust

(github.com)

123 points | by airstrike 3 days ago

14 comments

  • chaosprint 9 hours ago
    Here are two real-world examples of using Rhai:

    https://glicol.org/tour#meta1

    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-embed...

    Recent Koto Lang also looks promising:

    https://koto.dev/

  • pseudony 5 hours ago
    Not 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)

    • IshKebab 3 hours ago
      1. This is much easier to embed in Rust projects than Lua.

      2. Doesn't make the mistake of 1-based indexing.

      3. Generally much more modern and nicer language than Lua.

      The reasons I've found not to use it:

      1. Very slow - even slower than Python! Of course often this won't matter but it would be nice if it was fast.

      2. No support for type annotations.

      I've used it before for a config file, and it worked well there. I think if I wanted anything much more complex though I would just embed V8. Then you get a language that everyone already knows and very good type annotations.

      I don't think "ecosystem" really matters much for embeddable languages like this. You app is the ecosystem.

      • Andoryuuta 2 hours ago
        Is there something in specific that makes this easier to use in Rust projects compared to the Lua wrappers/bindings like mlua[0]? Or is it just an overall ergonomics thing?

        Genuine question, as I don't have any prior experience embedding any scripting language into a Rust project.

        [0]: https://github.com/mlua-rs/mlua

        • mkeeter 1 hour ago
          > By default mlua uses pkg-config tool to find lua includes and libraries for the chosen Lua version. In most cases it works as desired, although sometimes could be more preferable to use a custom lua library.

          The fact that Rhai builds with just 'cargo build' shouldn't be underestimated - a Rust project with all pure-Rust dependencies is much easier to maintain / support / distribute across a wide variety of hosts!

          • VWWHFSfQ 1 hour ago
            This is just behind a feature flag like:

                $ cargo add mlua --features lua54,vendored
            
            
            Then mlua will statically build with the Lua sources it bundles itself and no need to link the system Lua or do anything other than "cargo build" like normal.
      • pseudony 3 hours ago
        "suffer" 1-indexing vs no LSP, no books, no test-of-time, no ecosystem, no type annotations, slower.

        I don't mean to be overly harsh, but this is just not a valid/serious answer. The other reasons are fine. This is pure bike-shedding.

        • woodruffw 1 hour ago
          An interesting thing about bikesheds is that sometimes they become bikefortresses: if 99% of the world (including the surrounding userbase, in the case of Rust) expects 0-based indexing, then the choice of something that uses 1-based indexing is harder to justify.

          This doesn't make one better than the other, but precedent/familiarity does matter and represents a valid decision weight.

          • fmbb 1 hour ago
            Sure it is a valid weight. But the weight is a millionth of the weight of the cons that come with using a new unused language for embedding.
            • woodruffw 1 hour ago
              I don't particularly disagree. Although it's worth noting that people don't typically pick embedded languages because they're familiar with the embedded language per se, but instead because they (1) are easy to integrate, and (2) resemble the syntax/semantics of the embedding language. Rhai appears to satisfy both, so I can understand why someone would pick it.

              (I don't have a dog in this fight; I'm not a user of either Lua or Rhai.)

        • mecsred 2 hours ago
          It's absolutely a valid and serious answer? You asked for reasons, omitting two options, and they gave you a list of points for and against not including those two options. That's a good response, you don't need to agree with it, but I don't see how you can call it not serious?
      • VWWHFSfQ 3 hours ago
        > Doesn't make the mistake of 1-based indexing.

        A lot of people would call this a feature, not a mistake. 0-based indexing came from programming languages typically calculating addresses in the memory layout of an array. Whereas in mathematics (the background of the Lua inventor) sequence positions typically start at 1. Also, humans generally start counting from 1. This is intuitive in languages like SQL which also use 1-based indexing.

        It could be argued that 0-based indexing was the mistake since it actually conflates two concepts, the memory layout in the machine, and the actual sequence you want to index.

        • lolinder 3 hours ago
          I agree that it's wrong to label Lua's choice a mistake—when it was created in 1993 there wasn't as overwhelming a consensus in favor of zero-based indexing as there is now. But now the consensus is there, so whether it's a mistake or not it's not worth fighting against. Programmers today learn zero-based indexing and trying to get someone who's used to that to adapt to one-based indexing is not trivial.
    • diggan 4 hours ago
      I haven't tried Rhai and I wouldn't call myself anything more than a casual Lua user, but from a glance, these could be some reasons I see others using Rhai instead of Lua:

      - Closer to Rust syntax/data structures, so easier if you already know Rust but don't know Lua

      - Built-in serde (popular Rust de/serializer) support, if you need that

      - Not sure if existing Rust<>Lua crates have it, but the debugging interface looks like the beginning of something useful (https://rhai.rs/book/engine/debugging/)

      - Made in Rust, so again, if you already use Rust (which you do, if this crate is an option) it'll most likely to be easier to change for your needs, than modifying a Lua runtime

      Personally, I'd probably still go for Lua rather than something like Rhai, if I had to choose something for algol-like scripting support. Otherwise I think Steel (https://github.com/mattwparas/steel) looks like the most useful scripting environment for Rust currently.

      • lionkor 1 hour ago
        mlua has serde support
        • diggan 1 hour ago
          You're right, thanks! I did a quick grep with "serde" on the features exposed by `mlua` before posting that comment, but of course they named the feature "serialize", so didn't show up :/
    • tel 4 hours ago
      I’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.

      • larusso 1 hour ago
        Thanks for these examples. I wrote a POC tool to apply common patches to multiple projects. These patches where supposed to be written as scripts. Idea was to run the same set of action on projects with different layouts based on their names etc. So a normal patch would not work.

        Anyways I used mlua because it was the easiest way to get some scripting to run. But I faced issues writing the scripts. Basic operations had to come from the host like “string endsWith”, “list contains” and some other basic methods (Can’t remember which ones it where in detail). That mixed with the fact that Lua is so different. I knew I could not give this to any other dev in my team without a lot of instructions how lua handles stuff differently. So it’s nice to know I have a potential new goto solution when facing this again. Especially the ability to dumb it down.

      • VWWHFSfQ 4 hours 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 1 hour ago
          I'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)

    • noodletheworld 4 hours ago
      I've worked with moai (1) reasonably extensively, and the lua in it is not easy to use and sucks.

      Specifically, a project that is composed entirely of lua with no other dependencies is indeed very easy to build and maintain. I agree.

      However, my $0.02 would be that if you plan to have a large project with many 3rd party dependencies, then cmake, visual studio, C++, lua and the time spent jumping between them and maintaining those dependencies in a custom build toolchain will cost you more time and effort than the benefits that lua offers (2).

      ...and you do, indeed, need to do that, because c++ lacks a centralized package ecosystem and unified build tooling; and as operating systems change, existing builds stop working.

      So, yes, you may consider my answer to be 'rust'; but the actual answer is 'not C++ and not cmake'.

      That all said, lua is a more mature better system than this is currently, with good resources online and an active community. In cases where a small dependency tree can be maintained, it's still the best choice I'm aware of.

      I'm simply pointing out that there are reasons you would pick it over lua, and I think they're quite compelling for cases where the future dependency graph of your project is unknown / unknown.

      [1] - https://github.com/moai/moai-dev

      [2] - ...and yes, I'm aware that moai is especially egregious in this regard. I get it.

      • larusso 1 hour ago
        Also played around with moai back in the days when Doublefine had their Kickstarter and choose it as the base framework. But I found it hard to make the jump between c++ and lua etc. The docs made the decision when to write what simple. Everything lua until it becomes a bottleneck. But when to start from nothing this decision is still hard. Pull a lib and expose it to Lua or try to write the lib in Lua? Stuff like that. I needed a basic triangulation algorithm for 2d shapes and implemented that myself from a paper about the ear clipping algorithm. Fun times. All hobby stuff just for fun and games.
      • airstrike 1 hour ago
        "Not C++ and not cmake" is right on the money. Also Rhai seems to be in its infancy. Despite its current limitations, there's an argument for supporting it early to see where this road will take us. Hopefully to a relatively fast and more feature rich scripting language and engine that fit snuggly into Rust projects, which would be fantastic.
    • ema 1 hour ago
      Which features would compel you to use a language targeting roughly Lua's niche over Lua?
  • diggan 6 hours ago
    There 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?
  • senkora 3 hours ago
    Since 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

  • fretn 2 hours ago
    I use it in 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

  • satvikpendem 9 hours ago
    When 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?
    • xnacly 8 hours ago
      According 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

      • diggan 6 hours ago
        As 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 5 hours ago
          It 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 5 hours ago
          Millisecond timings are only meaningful on a specific hardware target.
    • MarceColl 9 hours ago
      Not 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.
  • chubs 11 hours ago
    Very impressive. I read the readme and I’m unsure how memory management works, is it GC? And is it OOP or not? Thanks :)
    • kreetx 10 hours ago
      Only guessing, but since the language is for scripting then maybe all garbage could be collected when the script finishes?
      • tommiegannert 10 hours ago
        I 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.
  • Black616Angel 9 hours ago
    What does "Passes Miri." mean? There is no link and online I only find a Malaysian city with that name.
    • Deukhoofd 9 hours ago
      Miri is a Rust tool with a similar function to Valgrind, it checks for undefined behavior.

      https://github.com/rust-lang/miri

      • ChadNauseam 7 hours ago
        For 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 6 hours ago
      Someone 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 :)
  • emigre 7 hours ago
    In an Apollo GraphQL federation, you can use Rhai to customize the Apollo Router's behaviour. The Apollo Router is written in Rust.
  • Epicism 17 hours ago
    That’s very cool! I appreciate how you can simplify interacting with rust applications without rust programming skills.
  • GardenLetter27 7 hours ago
    How does it compare to Rune and the others?
  • oulipo 9 hours ago
    Cool! 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 7 hours ago
      1. 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

  • searealist 9 hours ago
    Needs a comparison to the excellent Rust Starlark Implementation.
    • BiteCode_dev 9 hours ago
      Starlark is for config, not scripting.
      • searealist 8 hours ago
        I'm not sure what you think the difference is.
        • BiteCode_dev 7 hours ago
          You 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

          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

          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 7 hours ago
            IIRC starlark is intentionally NOT turing complete. It doesn't allow recursion, or general loops.
            • BiteCode_dev 7 hours ago
              And rhai is, it allows infinite loops.
  • ilrwbwrkhv 10 hours ago
    This is awesome. Would this be a replacement for Lua use cases in a Rust desktop app?