Live data from Hacker News

Using Lua with C++

edw.is

21–30 of 43 posts

Re: Using Lua with C++

#21
For those interested in using Lua or a similar syntax I curated a list of typed Lua-related projects:

- https://luau-lang.org (Lua derivative, interpreted / sandboxed, from Roblox)

- https://terralang.org (Lua JIT compiler)

- https://nelua.io (Lua-syntax -> C compiler (WASM too w/ Emscripten)

- https://github.com/pallene-lang/pallene (Lua AOT compiler)

- https://github.com/teal-language/tl (Teal -> Lua compiler)

- https://typescripttolua.github.io/ (TS -> Lua compiler)

- https://github.com/sumneko/lua-language-server (IDE only typing)

With minimum effort you can get a lot of benefit from using Sumneko's Lua language server in VSCode for near many Lua versions (LuaJIT, PUC-Rio Lua, etc.)

All of the projects serve different purposes, whether you need/love Lua for speed (JIT, for games), low memory usage (AOT, for embedded), or simplicity (all of them).

Re: Using Lua with C++

#22
Great to see sol2 mentioned. We use it in Codea[1] to bind to our game/render engine. The article is right about the lack of safety and inscrutable messages, though. Recently we bound almost the entire iOS Objective-C APIs into Lua, though not using sol2, this used the Obj-C runtime headers and a lot of Lua C-hackery.

[1] https://apps.apple.com/us/app/codea/id439571171

Re: Using Lua with C++

#24
post #23

Use SWIG to integrate C++ into Lua: https://www.swig.org/

I don't think that this approach is that good. First of all, you need to write a custom "interface" file. Then, you need to integrate running of swig into your build process, which makes the build complicated.

With sol, it's much easier - you just register function/classes in your C++ code - it's much easier to do than with Lua C API and doesn't require running any "post-processing" steps at all.

Re: Using Lua with C++

#25
post #10

Lua seems to be widespread in games, but in fact is a very nice tool to make day-to-day apps user-extendible (with neovim and mpv being a couple of examples).

I have built professional, scientifically sensitive, important apps with Lua. A recent project involved doing fluidic particulate quantification in an embedded, field portable instrument, which was delivered on time and under-budget - and represented a major revenue source for the customer (40 million dollars worth of orders) when it was shipped. The client was told it "could not be done" with the hardware as specifi…

Out of interest, is there any public information about your project?

I am currently prototyping a Lua environment for instrument control in a regulated environment. I've added several customisations and safety mitigations to make it suitable for use, and it works beautifully. One thing I would like is to be able to showcase examples of use in other products.

If you, or anyone else, have any examples I could point people to, to demonstrate existing real-world uses of Lua, that would be really appreciated.

Re: Using Lua with C++

#26
post #23

Use SWIG to integrate C++ into Lua: https://www.swig.org/

Have you tried sol2? It's vastly more capable and transparent. It's also 100% native C++ code, so is just part of your application, with no extra tooling required.

Re: Using Lua with C++

#27

> Start with writing new code in Lua (faster to prototype) and then see if you need REALLY to move this code (or parts of it) to C++. This is the only part of the article I disagree with. I've found that the biggest problem with using Lua in a C++ game is writing code in the "wrong" language, and the mess that can cause. The boundary between Lua and C++ needs solid validation to keep bugs out, and this makes it neith…

I think I should clarify this in the article further. What I meant to say is: "after you've implemented the basics, e.g. input handling, basic renderer, file I/O etc. and you've got a basic game running, then you can start with prototyping code in Lua".

I feel like I've fallen into a trap of writing the code in C++ first ("because it's fast!") and then porting it to Lua when I realized that I need to write too much "binding" code.

For example, if you're writing a very simple AI, you can write a FSM/Behaviour Tree implementation entirely in Lua and then write AI logic directly in Lua. This way, you won't need to write binding code between Lua/C++ for these FSM/BT implementations.

And then, after you've written some code with these structures, you can see if you need to port it to C++ for performance or not. And if you do, it will be easier to design API since you already know which parts you need to expose.

One thing that makes prototyping in Lua fast is that the code, which looks simple in Lua, can easily become a pretty difficult template meta-programming code in C++.

Re: Using Lua with C++

#28
post #3

Lua is a joy to work with, one of my favorite (scripting) languages. Having tables as the primary underlying data structure gives it a very LISP-y feel despite having an ALGOL syntax, providing a nice middle ground between the two styles.

Lua really is a joy, and after 40 years of software development is still one of the top ways, in my personal list, to get serious software built. I've done quite a few projects where I built a library of C modules to do the heavy lifting for some particular purpose (data in/out mostly), glommed these functions into the LuaVM, then built the application logic, very rapidly and smoothly, in Lua - outcompeting others wo…

"I think its really important for any developer, whether they are encased in the glories of the browser or otherwise, to understand how to add the Lua VM - to anything - and then use that as a means of productive application development."

I completely agree! I feel that I should add thoughts about this into the article.

A lot of people (justifiably) complain that C and C++ are not memory-safe and that it's hard to write readable and safe code in it. Lua allows you to hide the scary parts of C and C++ and write simpler and safer code by default.

Re: Using Lua with C++

#29
post #5

Earlier quoted context omitted.

I discovered this technique only a few months ago, and couldn't agree more. I also cannot believe I've missed out on it for so long, having been a software engineer professionally for 13 years now, with 10 years of computer science specific education before that. How is this not taught in every computer science curriculum?

I think it once was taught, but was considered passé in various places during the "Java" revolution, and in general I think there is a degradation of capabilities in most comp-sci programs, a consequence of interference by market players who invest in the schools in order to keep their technologies relevant. If anyone reading this wants to see what the fuss is, you can get started very easily with antirez' (Redis fam…

It's a shame that Java (and JavaScript) replaced the "Lua with C/C++/whatever" approach. JavaScript is incredibly hard to write in a "correct" way. It has way too many quirks and an incredibly complex tooling system (npm, etc.).

Lua is pretty much a "minimal" language which gets things done. It's not perfect, but it certainly doesn't suffer from the bloat which many other languages have.

Re: Using Lua with C++

#30
post #5

Earlier quoted context omitted.

I think it once was taught, but was considered passé in various places during the "Java" revolution, and in general I think there is a degradation of capabilities in most comp-sci programs, a consequence of interference by market players who invest in the schools in order to keep their technologies relevant. If anyone reading this wants to see what the fuss is, you can get started very easily with antirez' (Redis fam…

There's also the book "Creating Solid APIs with Lua" by Tyler Neylon [1,2] which seems to be quite specifically addressing this idea of embedding lua in something so as to implement highly-movable application logic in lua rather than the something you started out with. [1] https://www.oreilly.com/library/view/creating-solid-apis/978... [2] https://github.com/tylerneylon/APIsWithLua

Thanks a lot! Never heard of this book. Will read it and mention it in the article if I enjoy it. :)
Post reply on HN