by anilakar · 8y ago · 0 comments
Having written a ton of networking code in Lua I can tell it has a lot of design defects that most other programming languages have avoided. Especially the deeply embedded one-based indexing and lack of a separate integer type (chars are strings, ints are floats) make it wearing to type any code that has to parse any binary data. Even bit manipulation requires going through the FFI. Considering that Lua is often touted as the optimal lightweight solution in small embedded systems, these features are must-haves.
The builtin string pattern matching library attempts to mimic regular expressions, but bites the user in the leg every time they attempt to use it for anything remotely regex-y. Unfortunately most usual string manipulation seems to revolve around them and the string.gsub function.
The inconvenient string library is also largely irrelevant. The developers call their language eight-bit transparent which in reality means that the programmer needs a separate Unicode library, rendering the whole standard string library mostly useless. One cannot even format UTF-8 text, because the string library doesn't differentiate between characters and bytes.
The official documentation mostly revolves around C bindings. The actual Lua language documentation is provided by another web site that looks like it has been written by an attention-deficit child. Instead of getting to the point, the articles often first provide a list of ways on how not to implement things, or they are actually benchmarks of fifteen different ways to call the standard library to get the same effect. Sometimes, the provided code does not even work – for example, the given XML parser code crashes when it hits one of the error paths.
The small runtime and fast VM do not really matter when the language itself is lacking and requires a metric ton of external libraries to achieve even the simplest things that are possible in standard C and Rust, which provide the same level of performance and better type safety.
One based indexing is just a design choice, while it complicates interoperation, I find it justified as Lua tables are actually hash tables, so it would be weird to model it upon memory offsets. It reminds me that I am only dealing with a hashmap with integers as keys.
Bitwise and UTF-8 string operations are lacking out of the box, but these are arguably too specialized to be expected in a general scripting language. The patterns are a little underpowered compared to other languages, but it is still quite useful. I suggest looking into LPEG for more complicated operations. Lua 5.3 do have some functions to deal with UTF-8 codepoints.
Also, regarding Unicode, I'd rather the PL provide only operations on bytes, and let me decide what "characters" are to be represented. Nearly all PL providing some Unicode facilities fail in different ways, just let me deal with ICU myself if I really need Unicode. Also it is a royal pain in the butt to deal with different encodings, with a language having special "characters" and "strings" constructs.
I don't understand the complaint on documentation. It is one of the most practical and clear technical writing that I have seen, if a little terse. Perhaps you mistook the wiki as the main document? (The Lua language manual is an one-page HTML page.)
One based indexing isn't just about memory offsets or getting used to it, it makes lots of things a nightmare. Like index-math that involves the modulo operator, which will result in zero values, which are invalid for 1-based indexing. Or turning a 2D or 3D index into a 1D index, which now requires completely unnecessary additions or subtractions of 1s. And other index magic that I frequently need for computer graphics and number crunching.
If this is true, and I actually agree with you, then where is Lua actually better than an alternative?
And, while I don't share the specific points of the parent poster, I experienced similar issues with Lua. I found that while the Lua core language itself is small, I had to compile in so much extra stuff to make it useful that it wasn't actually small anymore.
And, once you get large enough that you start demanding big ARM Cortex M4 microcontrollers, Python is now in your scope.
There are entire games built around modding via lua such as gary's mod which similar to a web browser has to download random code from a server. Then there are mods like computercraft/opencomputers for minecraft in which direct file access to the host doesn't make any sense at all because every computer has it's own filesystem.
I just looked up Python's sandboxing capabilities and I can't find anything. All I see is answers like [0] that suggest that it's not trivial.
What about other implementations like pypy that claim to have a sandbox? [1] I can see lots of warnings like "This is not actively maintained."
The lack of mature sandboxing for python makes it impossible to use for the usecases I've described above.
[0] https://stackoverflow.com/questions/3068139/how-can-i-sandbo... [1] http://doc.pypy.org/en/latest/sandbox.html
I think the idea is that most functionality is meant to be provided by the host program. If you want a standalone scripting language, then Lua probably isn't for you.
Why is Lua an oft-recommended programming language for embedded development if one is not allowed to form opinions on the language?
>so it would be weird to model it upon memory offsets
Why does the language provide table constructors {1, 2, 3} that do one-based indexing then? Why is there a table length operator (#) that works on one-based indexes? Why does ipairs work with one-based indexes? A sequential data type (array, list, for some quirky reason named "tables" in lua) is one of the most basic data structures of a programming language. If Lua is supposed to be a scripting language, why should a programmer care about the underlying implementation?
>Bitwise and UTF-8 string operations are lacking out of the box, but these are arguably too specialized to be expected in a general scripting language.
Is Lua a toy or a tool?
>Lua 5.3 do have some functions to deal with UTF-8 codepoints
I welcome you to the real world, where 5.1 is still the latest available release available on too many platforms.
>Perhaps you mistook the wiki as the main document?
Lua-users.org turns up as the first result when you google for documentation.
I have never heard anyone recommend Lua for embedded development. It's an embeddable language in the sense that you can easily embed the entire interpreter in a larger application and extend it. That it happens to be relatively small and easy on memory is a bonus, not a reason to believe that it's a great systems programming language.
I'm not at all surprised that it disappointed you as a language for low level networking code. Most Lua success stories are about its use as a high level glue/scripting/configuration language, letting the application handle the low level code via an API.
The runtime is small and, all things considered, it's very fast. While I never got to a place where I was writing low-level networking code with it, when I was still in embedded I did use it for a number of tasks that it was perhaps not quite perfectly suited for because of it's characteristics. I'm no longer in embedded, but I can easily imagine having pushed that idea further with projects like LuaJIT showing up in more recent years.
Also the documentation provided by lua.org is quite accessible: https://www.lua.org/manual/5.3
What I'd really like is stdlib to provide a Buffer type, as opposed to having to use a table of integers which then gets pushed into a string with chunked calls to string.char(table.unpack(buf, n, n+4096))
Example of what I'm complaining about: https://github.com/serprex/luwa/blob/master/rt/make.lua#L758
So all said I don't disagree with you: Lua can fall short, it doesn't have the community other languages have built up, & the community it has is rather fractured. Like LuaJIT making 5.2 what most call Lua, but also that being an extension language it's hard to upgrade without losing compatibility, so projects like Redis can't upgrade without deprecating 5.2 for a 5.3 version & making some method to signal 5.3 & then eventually removing 5.2 while packaging both in the interim
The "actual Lua language documentation" you refer to probably is the wiki? I agree it is crap.
By all means, stay away from Lua and use Python and Node for new projects; we like that competitive advantage.
I pretty much agree that Lua can survive such throughput, but I question whether Lua can survive the code complexity. I have written about Lua previously on [1] with a vocal conclsion that Lua cannot really handle a large code base (in the order of 10^5 lines), so I like to hear otherwise (hint: I hadn't). That's why...
> By all means, stay away from Lua and use Python and Node for new projects; [...]
...we have ultimately dropped (a heavy duty use of) Lua from all new projects. C# seems to suit our needs (or any other statically & strongly typed languages---obviously excluding C/C++---would probably do).
We have shipped and maintain a series of games built on progressive iterations of the same codebase; we have on the order of 200 KLOCs of Lua in the common "engine" code and tooling, and 200-400 KLOCs additional game-specific code per game. We tend to hit our milestones and performance targets on six platforms, with programmer quality of life way above the game industry "standards" (i.e. we do normal working hours).
Can things be better? Of course, they always can. But om our experience, large-scale use of Lua is not the disaster it usually is assumed/predicted to be.
For the context, we had about 100 and 200 KLOCs of Lua code for the server and client respectively. We had to use Lua 5.1 because there were not much tooling nor library around 5.2 or later at that time (pain point #1). We of course needed 64-bit integers and sticked to special string representations with some metatable hack, which itself was quite usable. There were several, often critical, bugs that were found to be fixed in 5.2+ but left out in 5.1 even though everyone knows that Lua 5.1 won't die out any sooner (pain point #2): the most critical one for us was the use of thread-unsafe `localtime` which made my jaw dropping. Also there was no clear migration path from 5.1 (AFAIK this holds for 5.2 as well) to later versions (pain point #3); we had to build many system around Lua's dynamism to deal with the complexity and, as you can guess, every minor release of Lua breaks enough dynamism to prevent easy migration.
Besides from this, every single library we have tried to use had a critical issue: luasocket plus luasec had spectacularily failed at correctly handling transport timeouts (pain point #4; we did use ASIO as a backend, we only needed them for some external services), luasocket alone had a very annoying and easy-to-mess API for every but simplest HTTP request (pain point #5; eventually we had to shell out to curl which was much, much better than any other Lua-based solution), dkjson had no control over empty arrays vs. empty objects which caused some interop problem; and so on and so on (I can't remember all of them). Ah, of course there are also shitty wikis but I dare not to describe them. We eventually had learned to avoid any external library or even snippet. I feel like that Lua actively encourages DIY mindset which is absurd for modern large-scale programming.
Have I mentioned that we had used Lua's dynamism to deal with the complexity? Well, we could only try. We had built our own version of unit testing system because, well, obviously Lua is built for embedding and standalone unit testing does not make sense when you have a custom environment, right? This one is acceptable, but the lack of coverage calculation is not (pain point #6). I had built a custom one, but that was waaaay slow that we couldn't use all the time (and I'm pretty sure that a right debugging API will immersely change the situation, but there is still none as of 5.3). Debugging was also abysmal (pain point #7), again Lua is built for embedding and standalone debugging experience does not make sense, right? We already knew and used MobDebug [1] which kinda worked but was brittle. We had eventually built a serious debugging tool (open-sourced!) that integrates to Visual Studio Code via Language Server Protocol. The remaining story goes much like this: there are no or only partial tooling around embedded context (supposedly the biggest use case of Lua), and we had wasted too much time to build ours including a type checker (!!!!) [2].
I do agree that Lua has some upsides as well. In fact, Lua greatly helped us to quickly build a functioning product from the scratch, and the ability to safely discard the whole state massively simplified the error recovery process (alluding Erlang's approach). I really, really want to like Lua because it seems the only one viable programming language with those charactistics. But my own experience says otherwise.
This is such an unfair point. Lua has a single manual that covers the language, the standard library, and yes, it also covers C FFI (unsurprisingly for an extensibility / scripting language), but FFI actually comes after the language in the docs. And the manual is both very readable and very thorough where it talks about the language - it's quite easy to learn the whole thing just from it alone.
> lack of a separate integer type (chars are strings, ints are floats)
Lua 5.3 introduced integer types. [1]
> Even bit manipulation requires going through the FFI.
LuaJIT includes a bit manipulation library, but its FFI acceleration is optional. Also, Lua 5.3 introduced bitwise operators. [1]
> One cannot even format UTF-8 text
Lua 5.3 introduced "basic utf-8 support" [1] although I am not sure to what extent.
> The official documentation mostly revolves around C bindings. The actual Lua language documentation is provided by another web site that looks like it has been written by an attention-deficit child
Again, I suspect you are criticizing LuaJIT. The actual Lua documentation [2] has never let me down.
5.3 where all this are solved is the most recent stable version -- not what most people have used for the entire history of Lua (and not default in all distros).
Nope, plain old Lua 5.1 which is still the newest one can get on many platforms. You're stuck with what you have, not what was released yesterday.
>The actual Lua documentation has never let me down.
I read the document you linked and found the documentation lacking on anything but basic syntax and C bindings (the chapter on C API comes before the Lua standard library, btw). The standard library function documentation underspecifies parameter types and both return value types and numbers.
I find the Lua Short Reference [1] much more accessible than the official documentation. It has the very same information in a much more readable format.
>Lua 5.3 introduced "basic utf-8 support"
Looking at the documentation you linked, the Unicode support does not integrate with the string library. Walking a UTF-8 string manually character-by-character has never been difficult and Lua 5.3's support doesn't seem to address any other use cases.
[1] http://lua-users.org/files/wiki_insecure/users/thomasl/luare...
Most devs aren't interested in the additional burden of maintaining and crawling through the code when an edge case pops up.
Other than that, as others pointed out, most of your complaints are addressed in 5.3.
That's already reason enough for me not to use it. One based indexing caused me so many headaches in the past in Mathematica...
As a matter of fact, I had the exact same argument in a Julia thread (modulos don't work, polynomial accesses don't work).
It honked off a whole bunch of people who've never had to do any of these things for a living and therefore considered them edge cases.
Additionally, for LUA, being one-based while trying to claim it's the scripting language for C inter-operability is quite preposterous.
Same goes for indexing, you could write some functions like idx2(x, y, width, height), idx3(x, y, z, width, height, depth) that would compute the 1d index for you instead of using y*width+x (or the equivalent form adjusted for 1-based indexing) like you normally would.
> Even bit manipulation requires going through the FFI
This, and some of the other things, have not been true for years.
make generic local
Provided that you have a GNU compiler toolchain for the target you should have no problem doing this. If you don't I'm sure there's more effort involved, but ultimately Lua can build with any C99 compliant compiler and most C89 compilers (with the caveat that it can't guarantee that there is a 64-bit integer type in C89).How can you say that? The Lua documentation is on par with Kernighan and Ritchie.
I've switched to using Wren for my new projets. Despite not being as mature as lua (you may have to add some functionalities yourself):
* the syntax is much nicer.
* it's simpler/cleaner to embed.
* it's (allegedly) faster if you are running on a platform where it's forbidden to JIT (e.g. iOS).
* Classes in Lua are a hack, and consequently the syntax for them is terrible.
* Using "end" instead of "}". That decreases readability.
* no "+=" or "++" operators.
Edit: If you have a device with enough computing power to run a scripting language interpreter, you can use Python in most cases.
Lua is very dynamic, easy to code for (syntactical quirks aside, I'm talking from a "cognitive load" standpoint) while C and especially Rust are a lot more static, require more careful coding but can result in faster and, at least in the case of Rust, more robust code.
Lua is the type of language you embed in your C app to make it configurable.
That's not a fair comparison. A large chunk of the cognitive load comes from the awkward syntax and other design decisions that stem from it.
>Lua is the type of language you embed in your C app to make it configurable.
Situations where configuration files aren't sophisticated enough but a Python interpreter is too heavy are few and far between.
I strongly disagree. The unfortunate design decision of folding arrays into hashtables forces you to be very careful when you conceptually need a sequential list, because if you store a null value in it, you cut your list in half. Ooops.
An array is the most basic, simplest, most useful non-scalar datatype there is, and Lua bungles it.