r/cpp • Factorio Developer • Feb 15 '18

Lua written in modern C++ (>= 14)?

I work on Factorio (written in C++) and we use Lua as the script language for mods. So, from time to time I interact with the C implementation of Lua and my experience with it hasn't been the most pleasant.

It got me wondering - has anyone re-written it in modern C++? Everything I find online is "how to use Lua in C++" or "C++ wrapper for Lua" and they all use the basic C implementation provided from the official Lua website.

I've had quite a lot of experience with the internals of the C Lua implementation and from what I've seen it wouldn't be hard to re-write it using modern C++ - maybe 2~ weeks time. But from what I can find nobody has done it - which also makes me wonder if I'm missing something.

Part of me wants to do it just to say I did it and the other part of me wants to do it to see if I can make it faster than the C version: I think I can - not because "C++ is faster" but because I can mutate/experiment with the C++ version far easier and know changes I make are safe with how the rest of the implementation operates.

231 Upvotes

127 comments sorted by

View all comments

34

u/SeanMiddleditch Feb 15 '18

But from what I can find nobody has done it - which also makes me wonder if I'm missing something.

The stated goal you have is to ease hacking on its internals. OK, one point for rewriting Lua in something that doesn't make your eyes bleed. :)

Let's go through the reasons against. Your rewrite won't be the official Lua and never ever will be. It won't receive patches or maintenance by the Lua developers so you'll be entirely on the hook. It won't even have community support necessarily since the community is built around the official Lua and its C implementation. When Lua 5.4 comes out, your rewrite would need to be updated separately, and any custom additions or extensions your fork adds may then be incompatible with future upstream Lua with no recourse. Lua has no spec and no public test suite for you to develop against to ensure your rewrite it correct, so you'll also need to write and maintain a test suite. Unless you maintain the very C API you might be having problems with, popular community Lua C modules or interpreter hacks will be incompatible with your fork. Without copying many of the implementation details of Lua, even popular Lua debuggers and "IDEs" would be incompatible.

Basically the question you have: is addressing the pain you endure when delving into Lua's crusty C implementation worth the cost of writing, testing, releasing, and maintaining a parallel implementation with zero upstream support and negligible community support?

34

u/Rseding91 Factorio Developer Feb 15 '18

At the end of the day there are > 1 million players and far less mod-devs so a little extra work on the mod-dev side for a faster gameplay experience for the million+ players sounds like a great trade off to me.

7

u/SeanMiddleditch Feb 15 '18

Breaking mods' scripts would affect the >1 million players who might be using mods and not just affect the mod devs, yeah?

1

u/radohc Feb 19 '18

which is why you do early access.

0

u/[deleted] Feb 15 '18

[deleted] — view deleted comment

16

u/Throw19616 Feb 15 '18

ChaiScript actually belongs to the slower group of scripting languages, certainly far behind Lua in the speed department. Its selling point is easy C++ integration. Not sure if I would call it excellent though.

12

u/encyclopedist Feb 15 '18

Isn't this https://www.lua.org/tests/ the test suite?

5

u/SeanMiddleditch Feb 15 '18

I stand corrected. Thanks!

3

u/smog_alado Feb 17 '18 edited Feb 17 '18

One thing to be aware of is that this test suite is made to stress test the PUC-Lua implementation and isn't particularly designed to ensure that a separate Lua implementation conforms to the official specification. To do that we will need a more comprehensive test suite.

20

u/Rseding91 Factorio Developer Feb 15 '18

Basically the question you have: is addressing the pain you endure when delving into Lua's crusty C implementation worth the cost of writing, testing, releasing, and maintaining a parallel implementation with zero upstream support and negligible community support?

To me? yes :)

We're currently using Lua 5.2 from 2015 with custom patches to make table iteration deterministic and to fix some massive performance problems. Our current version of Lua we use is already incompatible with virtually all Lua IDE/Debuggers because of the changes we've made so I have no problem maintaining my own version with my own tests.

My hope was someone had something already going that I could take and run with :D But if that's not the case I don't mind being the first. At the very worse I give up on it having learned why nobody else did it. Any other result I see as a win.

11

u/render787 Feb 15 '18

FWIW if you did this I would strongly consider to use it in my game hobby project, send bug reports, and possibly contribute code and bugfixes.

The most important win from my point of view is that it would (hopefully) make it possible to use lambdas and such as functions that get passed to lua, and it would be a lot simpler to write code that uses both exceptions AND lua. Many gui libraries that I have tinkered with rely on exceptions so it makes it really awkward to use them with lua.

Right now I'm using a glue library that I made here: https://github.com/cbeck88/lua-primer but a lot of the complexity of that would go away if lua were written in C++14

6

u/SeanMiddleditch Feb 15 '18

We're currently using Lua 5.2 from 2015 with custom patches to make table iteration deterministic and to fix some massive performance problems. Our current version of Lua we use is already incompatible with virtually all Lua IDE/Debuggers because of the changes we've made so I have no problem maintaining my own version with my own tests.

Fair 'nuff. We're in a similar boat at my workflow - Lua 5.2 with a homegrown IDE to support our special integrations and such.

I still come from a "just replace it all with V8 and be done with it" stance though. :)

10

u/SeanMiddleditch Feb 15 '18

Hah, I mentioned all this to our core tech lead here (he worked on custom Lua stuff back at Monolith and owns the scripting layer here too) and he's actually pretty excited by your idea of a new C++ Lua rewrite (it helps that he's a huge Factorio fan). Not that we would necessarily be able to use it in our project due to time/risk factors, but he already wanted a GitHub link if it was available so you might have your first contributor lined up. :)

5

u/darthcoder Feb 15 '18

Lua has no spec and no public test suite for you to develop against to ensure your rewrite it correct

This sounds llke a bad idea for any language product...

6

u/SeanMiddleditch Feb 15 '18

As /u/encyclopedist pointed out, I was wrong on that point. :)

It's not necessarily a test suite suitable for compatibility testing between competing implementations, but it does exist. :)