Lua, a Taste of Genius
I’m going off my normal web development track here to just mention something which happens very very rarely. Let me start by making an outrageous claim: Lua is a work of a genius. If that interested you at all or if you just want to see how I support this claim, then please continue reading. Otherwise… read anyways?
A project (which has to do with the web, really!) which I’m currently working on personally has some backend written in C which is pluginnable using Lua. Originally this was in Ruby with… plugins being in Ruby, but Ruby is… like… impossible to sandbox. All Ruby has is crappy $SAFE levels. So I made the decision, a little late (after the tool was written), to rewrite it using old fashioned C and Lua, since Lua was built for these things.
Let me throw up a quick disclaimer and assure everyone that I’ve never used Lua extensively. But I do play World of Warcraft quite a bit (shame on me!) and I know that their addons are written in Lua, and through a few google searches I’ve come to find that a lot of big name companies use it for plugins. It is what Lua was made for afterall.
First of all… Lua the language itself is pretty cool. Its functional, it can be object oriented, it supports these wacky but powerful things called coroutines, and its fast (for a scripted language). It has a few things that take a while to get used to (metatables et al), but I picked it up in about 2 hours.
Next, Lua was built in mind with being a super light-weight, extensible language built for plug-in type jobs. So it comes with basically no standard libraries straight out of the box, you’re expected to add them in via the C API. Doing this is super simple…
OKAY BLAH! ON TO WHY ITS SO GENIUS…
It uses this really ingenious idea of a stack to store everything. Want to call a function? Push the function name onto the Lua stack, followed by the arguments, call the function. So they’re not genius for coming up with this idea, since this is all that computers do under the covers with every application, but they’re genius for choosing to use it to talk with C. Why? Because what I would have done is come up with a ton of nice little functions like “lua_call_func(name, …)” where … are the args to cover up all the dirty internals. But then… how would you know the types of the variables given? How would Lua know whether to garbage collect them? (It is a garbage collected language) Plus, Lua has some abstract types like “tables” which can get even more abstract with metatables! Ahhh!
Using a stack keeps it simple stupid! There are only a few types you can push onto a stack (integer, string, nil, c function, basically). Then using various tricks of the stack, complicated structures can be created such as entire objects with inheritance, private variables, and more, using… 3 data types? AWESOME.
And I never have to think whether I own the memory or Lua. I know that whatever I push onto a stack, Lua owns. I may have to pop it myself but I can free my HEAP-allocated memory after pushing it onto the stack. I never have to question anything.
Anyways, that was my quick 5-minute unedited heart-pouring to Lua. ![]()
