General purpose scripting language with no non-Rust dependencies
from amanda@aggregatet.org to rust@programming.dev on 23 Jun 2024 06:52
https://aggregatet.org/post/114171

Is there a good general-ish purpose scripting language (something like Lua on the smaller end or Python on the bigger) that’s implemented in only Rust, ideally with a relatively low number of dependencies?

Have you used it yourself, if so for what and what was your experience?

Bonus points if it’s reasonably fast (ideally JITed, though I’m not sure if that’s been done at all in Rust).

#rust

threaded - newest

onlinepersona@programming.dev on 23 Jun 2024 07:46 next collapse

github.com/alilleybrinker/langs-in-rust ?

KillTheMule@programming.dev on 23 Jun 2024 07:48 next collapse

Seems to be missing mlua at least: github.com/mlua-rs/mlua

amanda@aggregatet.org on 23 Jun 2024 20:36 collapse

According to the readme, that’s Lua bindings and not the language itself, that’s probably why it’s not on the list since it wasn’t written in Rust.

KillTheMule@programming.dev on 23 Jun 2024 21:10 collapse

That might indeed be the case, I did not look very closely, but mlua has gotten quite some coverage (and I do like lua quite a bit), so I wanted it mentioned :)

BB_C@programming.dev on 23 Jun 2024 14:27 collapse

I don’t know how I didn’t register the existence of roc.
I’m reading through the tutorial, and it looks very interesting.

arendjr@programming.dev on 23 Jun 2024 08:17 next collapse

It’s limited to JS runtimes, but this discussion might be of use: github.com/biomejs/biome/discussions/2467 I think you may find Boa fits your criteria, except for the JIT part.

Kualk@lemm.ee on 23 Jun 2024 10:03 next collapse

With requirements like yours, just use RUST itself.

GO language can be used as scripting language on Linux.

I imagine the same approach can be used with RUST.

5C5C5C@programming.dev on 23 Jun 2024 10:32 collapse

Disclaimer that I have no experience with writing compilers myself, but conceptually I don’t see any obvious reason that someone couldn’t create a JIT compiler for Rust so that it can be treated like a scripting language and do rapid iteration…

amanda@aggregatet.org on 23 Jun 2024 20:26 collapse

Sure you could JIT Rust, the question is if you can write a JIT compiler in rust since it needs to do some quite scary stuff to swap in compiled routines when evaluating code. I’m not even sure if unsafe is enough for that, you may need goto or arbitrary function pointers (which is kind of the same thing)

5C5C5C@programming.dev on 24 Jun 2024 01:55 next collapse

Considering most JIT compilers for JavaScript are written in C++, I can’t conceive of a reason you couldn’t implement one in Rust.

Is part of your requirement that unsafe doesn’t get used anywhere in the dependency tree? If so you’d have to take away most of the Rust std library since many implementations in there have small strategic uses of unsafe under the hood.

In my entire software engineering career, which spans embedded systems to CAD applications, I’ve never encountered a case where GOTO is actually needed (but maybe some places where it can be used as a dirty shortcut to save you some lines of code).

As for arbitrary function pointers, if those function pointers are written in Rust then they’ll come with all the safety assurances afforded to Rust code. I suppose if you’re worried about the danger of running ussr-code with unsafe in it, you could probably have your JIT refuse to compile the unsafe keyword specifically.

amanda@aggregatet.org on 24 Jun 2024 05:15 collapse

The only part of a JIT compiler I don’t understand how it works is the part that swaps in compiled routines during interpretation. That’s the point I’m unsure of how to write in Rust because it seems like it would require very custom control flow. It might be that you can handle this by storing your compiled instructions somewhere using the C calling convention and then having Rust call your compiled function like a C function.

In essence, what you have is a Rust program that has to produce machine code (easy!), store it somewhere in RAM (also easy, I think), and then somehow call it (how???). The final part seems like the difficult one since passing execution into arbitrary memory they just wrote is just the sort of thing programs aren’t normally supposed to do.

Kualk@lemm.ee on 26 Jun 2024 01:12 collapse

I originally meant solution described here:

blog.cloudflare.com/using-go-as-a-scripting-langu…

FizzyOrange@programming.dev on 23 Jun 2024 12:38 next collapse

I assume you’re talking about embeddable languages. I’ve used Rhai and it’s quite nice but I wish it had type annotations.

Gluon is another option but IMO they’ve gone way too far into crazy and unergonomic ML-style syntax. Which is weird considering it’s implemented in Rust which has much nicer syntax for the same things which they could have copied.

amanda@aggregatet.org on 23 Jun 2024 20:27 collapse

Thanks, I think Rhai is what I’d try at this point. Pretty much ticks all my boxes!

erlend_sh@lemmy.world on 23 Jun 2024 22:39 collapse

Lua in pure Rust: github.com/kyren/piccolo

amanda@aggregatet.org on 24 Jun 2024 05:09 collapse

Cool! Thanks!