4 ms·
I have managed to program a REPL in Lua for game development. I also added a feature that lets you reload any single Lua module at runtime, thanks to late bindi
by nonbirithm 6y ago
I have managed to program a REPL in Lua for game development. I also added a feature that lets you reload any single Lua module at runtime, thanks to late binding through tables. Nobody seems to standardize on a good way of doing it so I had to roll my own but I've saved countless hours of restart time doing so and had fun programming at times.
There really, really should be a standard protocol for communicating with a REPL in a running program like there is for IDEs in the Language Server Protocol. Maybe that way the concept would become more mainstream if you could just take any REPL client and walk up to a program with a REPL server and interactively explore its state. The closest thing to said protocol is nREPL which is what Clojure uses, but there are a lot of nonstandard extensions to the protocol for edge case features only applicable to Clojure, so almost every client assumes you're using Clojure and it greatly reduces the viability of using it for Lua or other languages. It doesn't have the backing of something like Microsoft either, I guess because this REPL thing lives in its own world with the Clojure userbase.
I opened an issue on Clojure's REPL integration with Emacs for supporting more languages[1]. I wanted to take advantage of the effort spent on Clojure's client side tooling for things like the command prompt and state visualiser but apply them to at least Lua and allow others to add their own support for other languages. It would be a really big effort but I've already seen the value of it by integrating a REPL with projects written in Lua. As you said I can't ever imagine using a dynamic language without one again.
[1] https://github.com/clojure-emacs/cider/issues/2848 https://github.com/clojure-emacs/cider/issues/2848
- divs1210 6y agoWill it be possible for you to make the Lua REPL publicly available? Would love to give it a spin with Spring RTS!
- nonbirithm 6y agoIt is, it's just part of a project I'm working on that uses Love2D and integrated with it a bit tightly. But I think it could be useful. Actually when I think about it the thing I made probably isn't a "REPL" in the Clojure sense since all it has is a command line and module hotloading; there is no namespace support and the command line evaluation does not happen in the context of the program, but you can basically call a function that gives you the entire global state of the game to mess with and see changes instantly. Maybe that's a better approach than having global state shared between both as Lua has no namespace support built-in, and I don't want to hack it on to keep things simple. Here is the drawing code for the REPL in-game. https://github.com/Ruin0x11/OpenNefia/blob/master/src/api/gui/menu/ReplLayer.lua https://github.com/Ruin0x11/OpenNefia/blob/master/src/api/gu... Here is the server which listens for commands using a custom protocol. I want to use nREPL at some point but that requires merging some changes into external projects with the client side tooling. https://github.com/Ruin0x11/OpenNefia/blob/master/src/internal/debug_server.lua https://github.com/Ruin0x11/OpenNefia/blob/master/src/intern... Here is the code which handles hotloading. It's complicated but I had to write the system so that it also handles sandboxing external mods and hooks into Lua's module management, running special checks to see if the loaded code is not part of the base game. You can probably get away with the version in lume if you don't need as much. But the point is that this is what ultimately allows changing files and seeing the changes instantly without restarting the program. https://github.com/Ruin0x11/OpenNefia/blob/master/src/internal/env.lua https://github.com/Ruin0x11/OpenNefia/blob/master/src/intern... https://github.com/rxi/lume/blob/98847e7812cf28d3d64b289b03fad71dc704547d/lume.lua#L688 https://github.com/rxi/lume/blob/98847e7812cf28d3d64b289b03f... Here is the integration with Emacs. https://github.com/Ruin0x11/OpenNefia/blob/master/editor/emacs/open-nefia.el https://github.com/Ruin0x11/OpenNefia/blob/master/editor/ema...