Why not Lua
julien.danjou.info
julien.danjou.info
"Being small is not an excuse
One common argument to choose Lua is that it has a small footprint. Yeah, that's true, but that's useless. Bummer! When I program, I don't have any resource usage pressure. People who have such pressure are either paranoids or playing in the world of embedded computers. This is also a no more existing conception since quad core processors equiped phones are coming into the market. I'm rather confident that what we used to call embedded devices are just dead and are now plain computers"
How I would savor watching him sit in one of our engineering meetings where we discuss byte and cycle budgets available to subroutines on our network routing layer. And, I'd really like to see what he thinks about our 1 MByte firmware limits, 4 MByte flash footprints....
Ironically, We've only recent had the memory budget to consider LUA.
We heard this argument a decade ago with CPU. "The hardware will get faster. Moore's Law, lol." Oops?
> I recently worked on a project where the microcontroller had 768 bytes of RAM and around 32k flash.
Luxury! I'm in an entertainment industry startup. Our main product uses an AVR board with 512 bytes of RAM and 8k of flash. Program size optimization for 8k is a significant challenge for us.
We also use an embedded networking device with 8M of flash, which is about the sweet spot for Lua...Perl, Python, and Ruby are all too bloated for this context.
To the author, embedded apparently means "phones." With the rise of Arduino and hardware hacking in general, I think there are more and more opportunities involving development under extreme constraints, not fewer.
That sounds plausible. We had a 512k dsPIC (I think that's as big as they come) and considered Lua on it, but it wasn't quite practical.
While I might agree with Julien that Lua has a few quirks that can reduce your productivity, I have never had any problem finding helpful resources to help solve my specific problems.
I think that avoiding Lua because of these limitations is quitting too soon, or throwing the baby out with the bathwater. I think Lua is a very useful tool with wide applicability.
The IRC channel #lua on Freenode is a friendly and welcoming place.
The official Lua mailing list is also a great place to explore the particulars of a problem and receive the benefit of many expert eyes on your code. [0]
There is example code in various places on the Internet showing good ways to do careful stack management with C/Lua. I found these to be the most helpful. [1]
If you are used to stack-based programming in languages like Forth or PostScript, you will probably feel at home.
If you are not, you will have a learning curve before you start to feel productive.
0: http://vlists.pepperfish.net/cgi-bin/mailman/listinfo/lua-l-...
1: http://lua-users.org/wiki/SampleCode (see "Lua C API samples")
1: http://cc.byexamples.com/2008/11/19/lua-stack-dump-for-c/
1: http://docclox.blogspot.com/2011/01/about-lua-stack.html
Edit: clarity, tone, links, more links, typo
- reference-counting simplifies some stuff, but introduce other bug classes (e.g. circular references). Moreover, ref-counting GCs are generally outperformed by mark & sweep GCs (because most objects have a very short lifespan with dynamic languages).
- to create mutual references between userdata, you have to use the stack. It's cumbersome but it ensures that the GC state is always consistent. The choice is between cumbersome, buggy user code, and buggy GC code.
- lack of paradigms (or "officially preferred way to do things", as magistrally done by the Python community), is indeed annoying. It would be a fatal flaw in a general purpose language. In a language intended to be embedded, it is merely a debatable choice, although one I don't support. The language's purpose is limited to provide basic building blocks, APIs and paradigms are to be provided by the embedding application. I wished preferred paradigms were strongly suggested, too.
It would be possible to define a very elegant and productive programming ecosystem around Lua; one which would probably be marginally better than Python or Ruby at many tasks. But I'm not convinced that the improvements would be dramatic enough to let that Lua++ carve its own niche between the established generalist, stand-alone languages. Better stay the best language to embed with C IMO.
I've done huge projects using Lua as an embedded language and I really enjoyed the experience. I found the stack API easy to grasp, and the performance was outstanding. SWIG (swig.org) made wrapper generation a snap, and having Lua objects with access to the C++ data model allowed us to write test code that used introspection and reflection very naturally.
This article strikes me as a case of "This language isn't my favorite, so I don't think anyone should use it."
This is an example where a valid personal opinion ("I don't like stack-based apis") crosses the line into something else ("Lua is defective").
And realistically...how often is it going to come up that you need the embedding to pass around more than a few numbers and strings?
In his own words: "I liked Python object model and wanted to have it in Lua, and spending time rewriting Python is just not worth it. I probably should have chose Python, not Lua. YMMV."
I guess he needs to learn more about compilers/interpreters implementation to understand that the reasons he dislikes Lua are the same reasons that make it fast and small and embeddable.
lua_pushnumbe(L, mycomputingfunction(L, ...));
are just calling for trouble.
In my mind Lua and C code remain in two different spaces. C handles low-level stuff and returns a status, simple objects or just references. Lua is there to manipulate handles on C-level objects. Code separated this way has never given me any trouble for debugging.
The argument about blaming Lua for using little resources and being uselessly fast is so preposterous I would rather consider it either a bad joke or sheer inexperience.
There are very few libraries for Lua and that's its main weakness. However, because the C interface is so awesome, that never really hurt in practice—at least for our application. For something like a web app it would be completely untenable.
As for the language itself, I love Lua. The syntax is easy to learn and the concepts aren't too difficult. Plus, the (relatively) easy binding can lead to a lot of interesting opportunities, like scripting NES games with FCEUX. I actually starting working on a genetic algorithm in FCEUX a while ago and I had a lot of fun doing it.
Look at awesomewm (dude wrote it)
And connecting "C" and "LuaJIT" through FFI - that's quite easy. I've started a little project with such bindings http://github.com/malkia/ufo - OpenGL, OpenCL, AntTweakBar (OpenGL "property" dialog), ZeroMQ, GLFW (GLUT like library that can be used without callbacks), and few others.
ffi.cdef[[ void glutDisplayFunc(void (func)(void)); ]]
glut = ffi.load( "glut.dll/so/dylib/etc" ) glut.glutDisplayFunc( you_cant_put_lua_callback_function_here )
There are probably some ways using coroutines (as Mike explained), or later if luajit allows libffi to build the trampoline (but Mike said that right now luajit -> C -> luajit would not work for the same luajit context). But maybe a different context would work, and luajit can create different luajit context using itself through the FFI.
All this as altnernative to the natural lua "C" binding way described in the article.
Right now everything I'm doing is without any such binding. It's purely experimental right now.
I've also did some small port of the zile editor (lua version) to directly use curses (curses.dll) instead of bindings
github.com/malkia/luajit-zile
the curses.dll is not there yet.
The way I do it is, there's a C function that takes a Lua function as an argument (through the stack) and sticks it in the Lua registry, then stores the (int) index that it's stored at. Later on I can pull the function back out into the Lua stack and pcall it.
https://github.com/randrews/ballmaze/blob/master/Ballmaze/Lu...
from doing bad things to python i know that it's very frustrating when the language suddenly becomes "less dynamic than expected" because something is hardcoded (usually for sensible performance-related reasons - but that doesn't stop it hurting). how serious is this issue with lua (eg the inability to redefine # as described in the article).
It's just a small change in lvm.c (http://www.lua.org/source/5.1/lvm.c.html) - search for OP_LEN. Yeah, really - that's it.
The Lua codebase is pretty good reading, by the way.
It comes standard as part of LuaJIT, which is likely what should be used for anything like a wireshark plugin (I don't know what wireshark actually uses, but LuaJIT is a drop-in replacement for the most part).
Bitop (or something similar) is included in Lua 5.2 as well.
http://lua-users.org/lists/lua-l/2002-09/msg00134.html
If you can load a C extension module then there's:
Yes Lua is that small.
Lua itself is nothing like Forth - it's more like a cleaned-up hybrid of Python and Javascript.