News for hackers. Stuff that matters.
Re:Learn Lua in 15 Minutes (2013) by VWWHFSfQ on Tuesday June 30, 2020 @06:25PM (#23695477)
Lua is a funky little language and runtime but it is absolutely perfect for scripting in the domain that it was designed for: embedded.

I used to do a lot of game dev and it was incredible for testing different interactions/movements etc at runtime just to get all the inputs right and then implement in the game code. It has saved me probably 1,000s of man-hours in my career. Nowadays I use it in NGINX for high-velocity requests that are just backed by Redis. Lua, LuaJIT, and Redis have made me a lot of money in my career. I'm forever indebted to their incredible creators and community.

Re:Learn Lua in 15 Minutes (2013) by AceJohnny2 on Tuesday June 30, 2020 @06:45PM (#23695678)
How does it compare to TCL?

I understand that the fact of the matter is that Lua dominates, but I'm curious if there are technical reasons or "right thing/right time" kind of luck, after TCL's star dimmed.

Re:Learn Lua in 15 Minutes (2013) by ufo on Tuesday June 30, 2020 @10:46PM (#23697213)
I'm not sure there is an easy answer for the question of why one language became more popular than the other but there some very interesting parallels that can be drawn between Lua and TCL. One thing that both have in common is that both languages were purposefully designed to be able to be embedded with other languages, such as C. But they do this in very different ways.

The problem of embedding one language inside another is how do you make code written in one language call functions and objects defined in the other language? For example, when an object is transferred from the scripting language to the host language, how does the garbage collector keep track of it? This isn't easy to do and in some languages it is quite cumbersome. For instance, in Python the C programmer has to manually increment and decrement the reference counts for the python objects that they interact with.

TCL solves this problem by making everything a string, taking the "everything is a string" mantra farther than any other programming language. Strings are easy to pass from one language to the other.

Lua went the opposite direction. Lua exposes a rich API that lets other languages interact with Lua. The API lets other languages create Lua objects and call Lua functions, as well as send C objects and functions to Lua. Lua is entirely designed around this API and even the core standard library is built using it, ensuring that every Lua feature is accessible to C code as well. One interesting example of this is exception handling. In most languages this is done using try-catch blocks. But that feature isn't easy to use when embedding one language inside another. How would you write try-catch blocks in C? Because of this, Lua has a different way to do exception handling. Instead of try-catch blocks, there is a "pcall" function that invokes a function and returns whether there was an exception or not. Function calls and if-then-else statements are things that are also available to other languages via the Lua API...

    local ok, err = pcall(function()
        something_that_may_throw_an_exception()
    end)
    if not ok then
        -- handle exception
    end
Re:Learn Lua in 15 Minutes (2013) by brians on Tuesday June 30, 2020 @08:34PM (#23696439)
That’s a really good comparison!

Lua has real types in a dynamic language; in Tcl everything is a string.

They can both pass functions around, but Tcl’s work by “eval” all the time—so security is quite tricky. Lua has real higher order functions.

Lua has lexical scope. In fact, by semantics it’s almost a Scheme dialect (say, R4RS) with extraordinary syntax.

Re:Learn Lua in 15 Minutes (2013) by Symmetry on Wednesday July 01, 2020 @09:32AM (#23701101)
Scheme in ALGOL's clothing, with the lists swapped out for hash tables is how I sometimes describe it.
Re:Learn Lua in 15 Minutes (2013) by corysama on Tuesday June 30, 2020 @07:33PM (#23696054)
Don't know much about TCL. But, if you are familiar with JavaScript, Lua ended up being semantically a much cleaner, simpler JS without all the WOT?? and committees to please.

It is small, fast, simple, powerful and easy to embed.

It's also very easy to contain in that the standard library is small and most of that can be removed easily. Isolating Lua's allocation arena and controlling Lua's GC is pretty easy.

Re:Learn Lua in 15 Minutes (2013) by hajile on Tuesday June 30, 2020 @10:18PM (#23697022)
All the little operators that make things nice like += are missing. Their object implementation is meta-tables, so every single codebase does stuff in fundamentally different paradigms. Operator overloading in such a dynamic language makes things extremely hard to understand. While `"0" == 0` is false, the language will still sloppily convert between floats and ints and will also convert strings to numbers in other places. Worst of all, array indexes are base 1.
Re:Learn Lua in 15 Minutes (2013) by hombre_fatal on Wednesday July 01, 2020 @03:36AM (#23698853)
Lua manages to surprise even Javascript developers with how bad its standard library is.
Re:Learn Lua in 15 Minutes (2013) by catalogia on Tuesday June 30, 2020 @08:16PM (#23696349)
mpv supports both Lua and Javascript scripting, with more or less the same API for both. The wiki lists 10 scripts from the community written in Javascript vs a few hundred written in Lua. There are probably network effects in play; people choose Lua because that allows them to mine snippets of code from a larger collection of userscripts. Even so though, I think it's a testament to Lua.

https://github.com/mpv-player/mpv/wiki/User-Scripts

Re:Learn Lua in 15 Minutes (2013) by panzagl on Tuesday June 30, 2020 @08:56PM (#23696598)
JavaScript moves so fast- it's gone from WAT to WOT
Re:Learn Lua in 15 Minutes (2013) by tyingq on Wednesday July 01, 2020 @07:54AM (#23700427)
Squirrel-lang is worth a look as well. http://www.squirrel-lang.org
Re:Learn Lua in 15 Minutes (2013) by CyberDildonics on Wednesday July 01, 2020 @09:19AM (#23700981)
There is barely a comparison to be made. Lua is faster, LuaJIT is much faster, lua is much easier to embed and lua is much more elegant and way less awkward to program in. TCL ends up being funky and lua ends up working.

When there was no competition in the space of already made scripting languages that were easy to embed (except for possibly perl) TCL had a place, but it is a relic of the past now and for good reason.

Re:Learn Lua in 15 Minutes (2013) by arthurcolle on Tuesday June 30, 2020 @06:38PM (#23695617)
> Nowadays I use it in NGINX for high-velocity requests that are just backed by Redis.

Can you talk more about this? Sounds like some dark magic

Re:Learn Lua in 15 Minutes (2013) by VWWHFSfQ on Tuesday June 30, 2020 @06:49PM (#23695715)
We use NGINX + Lua [1] to collect real-time diagnostic and performance data from physical machines while they operate. We collect about 150,000 data points (HTTP requests) per second with 2 NGINX servers and 2 Redis servers. We've benchmarked this particular server group up to almost a million req/s before it started to break down (median resp time > 100ms).

[1] https://github.com/openresty/lua-nginx-module

Re:Learn Lua in 15 Minutes (2013) by bepvte on Wednesday July 01, 2020 @12:22AM (#23697711)
Do you rely on redis persistence at all?
Re:Learn Lua in 15 Minutes (2013) by ende42 on Wednesday July 01, 2020 @02:17AM (#23698325)
We recently implemented a simple streaming server based on Openresty and Redis. Redis is used as buffer. We load tested it up to 2000 mountpoints (128kbit mp3 streams) with 15.000 clients.

The architecture has some nice properties. For example the buffer being not in-process means (besides an obvious performance hit) that the streaming server can be seamlessly redeployed without loosing the buffer.

The Openresty Lua implementation is amazing.

Re:Learn Lua in 15 Minutes (2013) by mpalfrey on Wednesday July 01, 2020 @03:41AM (#23698887)
+1 for Openresty Lua being great. It's saved my ass a few times.
Re:Learn Lua in 15 Minutes (2013) by leetrout on Tuesday June 30, 2020 @06:44PM (#23695671)
Not the OP but I used openresty (fork of nginx) to cache POST requests to Redis.

It’s lightning fast.

16,000+ requests per second served from the cache.

Re:Learn Lua in 15 Minutes (2013) by ethanpil on Wednesday July 01, 2020 @12:22AM (#23697706)
Can you point me in the right direction for this type of implementation?
Re:Learn Lua in 15 Minutes (2013) by anonymoushn on Thursday July 02, 2020 @11:06PM (#23720681)
At one company that served a lot of requests from an Openresty cache, we used nginx's cache for caching full HTTP responses and lua-resty-mlcache for caching other things. lua-resty-mlcache requires one to provide a callback to get the data as a string, then a way to deserialize the string, because the l2 cache in nginx's shared memory zone will store the item as a string. We used msgpack for this part.
Re:Learn Lua in 15 Minutes (2013) by leetrout on Thursday July 02, 2020 @02:54PM (#23716669)
Re:Learn Lua in 15 Minutes (2013) by huseyinkeles on Wednesday July 01, 2020 @05:21AM (#23699537)
> I'm forever indebted to their incredible creators and community.

Consider donating some money to them if you aren't already :)

Re:Learn Lua in 15 Minutes (2013) by VadimPR on Wednesday July 01, 2020 @01:44AM (#23698148)
Agreed. We've embedded it inside Mudlet (https://github.com/Mudlet/Mudlet) for a decade and people have started their careers in programming thanks to wanting to code for their favourite text game.
Re:Learn Lua in 15 Minutes (2013) by stevekemp on Wednesday July 01, 2020 @01:26AM (#23698026)
Agreed entirely, but of course it can be embedded in more things than people might expect. For example redis, HAProxy, nginx, etc.

I wrote a console-based mail-client which was configured and scripted entirely within lua. That was my solution to the weird not-quite-real configuration options available to mutt. I don't use it any more, and it never became terribly popular, but it was a wonderful fit.

Re:Learn Lua in 15 Minutes (2013) by rsecora on Tuesday June 30, 2020 @07:11PM (#23695885)
I can't agree more.

Do you use any library for the binding? I use sol2.

Re:Learn Lua in 15 Minutes (2013) by spacechild1 on Tuesday June 30, 2020 @09:04PM (#23696648)
I can also recommend sol2 / sol3. It makes interoperability between lua and c++ almost seemless.