Adventures in Lua Embedding
In a personal project which I’ve been working on and off on for the last three or so years (without really getting much done to be totally honest), I’ve decided to use Lua for the configuration file. The project itself is in C++, so this lead me to use the Lua C API to embed it in the project. Prior to deciding on Lua, I was using a manually parsed key/value format. The only thing to really change between the new format an the old is an ‘=’ between the key & value (since it’s just a Lua file after all.) The advantage is that I don’t have to worry about trying to manually parse it, because I wasn’t really checking for errors in the file and assuming everything was fine while reading the input (mainly because I was too lazy to actually implement error checking.) That alone (and having an extensible format) is reason enough to use it.
Now, things weren’t all quick and easy. I learned a pretty hard lesson along the way. Anyone can tell you that it’s good practice to keep the stack balanced. In my case it was totally required. At some point I either overlooked or got careless to the fact that lua_to* does not remove the element off the stack. So as I was trying to push and pull variables from Lua I was encountering crashes. I’m ashamed to say it took me more hours than I’d like to say before I thought to check my logic to see if the stack was getting out of balance. It seems like one of those, “I don’t ever want to have to spend that long tracking down something so silly again,” mistakes. I think I will always make sure the stack is well balanced when working with Lua from now on (unless there’s a good reason to leave things there.) So I suppose it was worth it to learn to be more careful in the future.