News for hackers. Stuff that matters.
Re:Golua – A Lua 5.3 engine implemented in Go by drej on Friday November 16, 2018 @11:41AM (#18469817)
In case you are wondering why this exists, there is a discussion here: https://github.com/Azure/golua/issues/36
Re:Golua – A Lua 5.3 engine implemented in Go by wahern on Friday November 16, 2018 @11:54AM (#18469919)
> The VM is designed differently (I'm encouraging Brian to write a blog post about this), but in a nutshell it is designed for better error handling and for adding a debugger

Looking at the VM, they implement function calls by recursively re-entering the VM in Go. The VM implements neither tail calls nor coroutines, and I don't see how they could implement the latter without some serious refactoring.

Perhaps by adding a debugger they mean they implemented the VM such that they could repurpose existing Go debuggers, which is why there's a 1:1 mapping between the Lua call chain and the Go runtime call chain.

Re:Golua – A Lua 5.3 engine implemented in Go by mfer on Friday November 16, 2018 @12:08PM (#18470010)
This was open sourced very early. It is being built with the intent of supporting Helm v3 and by open sourcing it early it enables other Helm maintainers to get involved and to import it into Helm v3 early.

I expect more docs, details, and improvements will be coming

Re:Golua – A Lua 5.3 engine implemented in Go by wereHamster on Saturday November 17, 2018 @05:27AM (#18474885)
You mean this helm: https://helm.sh?
Re:Golua – A Lua 5.3 engine implemented in Go by stcredzero on Friday November 16, 2018 @02:48PM (#18471449)
in a nutshell it is designed for better error handling and for adding a debugger

Better debugging is one of the big things holding Lua back, IMHO.

Re:Golua – A Lua 5.3 engine implemented in Go by nikki93 on Friday November 16, 2018 @05:54PM (#18472616)
I feel like the main thing to work on here is the frontend, given the "debug." library built into Lua(JIT). ZeroBrane has a frontend, for example.
Re:Golua – A Lua 5.3 engine implemented in Go by mwkaufma on Friday November 16, 2018 @01:30PM (#18470774)
Ah, for reliability and debugging. I guess that's why they're not too perf-concerned about dispatching opcodes through function pointers: https://github.com/Azure/golua/blob/master/lua/exec.go#L92
Re:Golua – A Lua 5.3 engine implemented in Go by nwmcsween on Friday November 16, 2018 @05:41PM (#18472541)
This is going to be horribly slow compared to say computed goto
Re:Golua – A Lua 5.3 engine implemented in Go by mwkaufma on Friday November 16, 2018 @10:57PM (#18473844)
Are computed gotos even available in golang? Even in C that's a compiler extension.
Re:Golua – A Lua 5.3 engine implemented in Go by ioddly on Friday November 16, 2018 @12:06PM (#18469994)
I'd think one of the big advantages of rewriting Lua in a GC'd language would be that you could ditch the stack API, which is necessary to keep track of pointers but not so intuitive to use. Seems like they didn't take that route though.
Re:Golua – A Lua 5.3 engine implemented in Go by wahern on Friday November 16, 2018 @12:31PM (#18470207)
What's the alternative to a stack API? If you look at Python and Perl they use a combination of (1) an explicit stack similar to Lua, (2) instantiation of heavy-weight list or array objects for passing arguments, (3) explicit reference counting, and (4) code generation.

Lua's stack protocol can sometimes be verbose, but I find it infinitely more simple and elegant than the existing alternatives.

I think you tend to see long, unwieldy code blocks that invoke Lua functions multiple times because you can, despite how tricky it can be to track the top of your stack. You rarely see this in Perl or Python because it becomes impossible to manage long before that point. So the solution for Lua is the same as for other interfaces--use shorter, simpler functions and compose them. This is usually easier and more performant to do in Lua, anyhow, because there's less fixed boiler plate and indirection necessary; not to mention Lua gives you many more facilities for stashing and managing your C state across calls (like closure upvalues, userdata values, continuation context cookies, etc) without resorting to global variables either inside or outside the VM.

Re:Golua – A Lua 5.3 engine implemented in Go by ioddly on Friday November 16, 2018 @01:03PM (#18470534)
Yeah, implementing a precise GC is a pain, but in Go the hard work of that is already done for you. If I were to write my own Lua-in-Go, my inclination would be to try to make an API something like this

    table := lua.MakeTable()
    table.Set(lua.MakeString("five"), lua.MakeNumber(5))
Rather than manipulating values on the stack. But it's possible there are reasons for not doing that that I'm unaware of. Was just curious.
Re:Golua – A Lua 5.3 engine implemented in Go by wahern on Friday November 16, 2018 @01:44PM (#18470899)
Why do you prefer that over

  lua_pushstring(L, "five");
  lua_pushnumber(L, 5);
The above is shorter and also significantly more efficient because you're not creating the intermediate table object. Even in Go, given Lua's coroutine and tailcall semantics (assuming they were implemented) the Go compiler would likely be forced to always heap allocate the argument list table. There's a reason PUC Lua is blazingly fast, and its partly the same reason why LuaJIT is so fast--the Lua stack protocol is a thin abstraction that easily admits a simple, efficient implementation yet still managing to provide an effective interface boundary that doesn't leak (beyond the fact of the protocol itself).

I don't think there's any good middle ground here. The best options are (1) Lua's explicit stack protocol or (2) some sort of type inference that permitted a direct call, like lua_tcall(L, "five", 5), into the VM. You can actually implement the latter in C somewhat easily (at least if you stick to simple data types) by using _Generic and __VA_ARG__ macros to generate the stack pushing code.

But I rarely see this done because it's a lot of magic for little gain. And where I have seen it done it's always been annoying because it's too leaky--invariably you have to fallback to the stack pushing code because there's a limit to the types of automagic type coercions you can accomplish between two drastically different languages. So you have to learn their magical wrapper routines in addition to the regular Lua API.

Many years ago I abused __VA_ARG__, __builtin_types_compatible_p, and libffi so I could invoke ad hoc functions as libevent callbacks without having to proxy the call through a function that unwrapped a pointer-to-void cookie. See http://25thandclement.com/~william/projects/delegate.c.html I still think it's kinda clever but I can't remember the last time I actually used it. Even in code bases already using delegate.c I ended up returning to the slightly more repetitive but transparent and idiomatic patterns for installing libevent events.

Re:Golua – A Lua 5.3 engine implemented in Go by omaranto on Friday November 16, 2018 @04:37PM (#18472172)
I'm guessing the code was intended to be equivalent to:

    lua_newtable(L);
    lua_pushstring(L, "five");
    lua_pushnumber(L, 5);
    lua_settable(L, -3);
Re:Golua – A Lua 5.3 engine implemented in Go by wahern on Friday November 16, 2018 @05:44PM (#18472547)
Ah, I see. So it's more a preference for a richer standard library, like,

  lua_newtable(L);
  luaL_setfieldi(L, -1, "five", 5);
rather than having to roll your own. Part of the function of the stack protocol is to minimize the number of unique functions needed to load and store values across language boundaries. So there's a single [low-level] function for communicating a number value in a type-safe manner, rather than a multitude of functions--one routine for setting a number value for a table string key, another for a number value for a table number key, another for a string value for a table number key, etc. The permutations explode, and unless the host language has some sort of generics capability then it adds significant complexity both to the interface and to implementation maintenance. (See Perl XS.) Alternatively you can reduce the number of permuted interfaces by using printf-style formatting strings and va_lists, but that's not type safe. (See Py_BuildValue.)

Notably, Lua 5.3 removed lua_pushunsigned, lua_tounsigned, luaL_checkint, luaL_checklong, and many similar routines as there was never an end to the number of precomposed type coercing, constraint checking auxiliary routines people demanded. They decided that the only way to win that game was to not play at all. But with C11 _Generic and a dash of compiler extension magic you can actually implement a richer set of type agnostic (but type safe) numeric load/store interfaces in just a handful of auxiliary routines. For better or worse, though, Lua doesn't target C11 nor wish to depend on compiler extensions. "Batteries not included" is a whole 'nother debate in the Lua universe.

Re:Golua – A Lua 5.3 engine implemented in Go by as-j on Friday November 16, 2018 @11:08PM (#18473890)
> Lua's stack protocol can sometimes be verbose, but I find it infinitely more simple and elegant than the existing alternatives.

This. TL;DNR, used a C/Lua binding to explain the Lua stack to a fellow engineer. Took a few post-it notes to keep track of 4 levels of stack, 1 pcall back to Lua to sort an array, and forward the results on. It just worked, was elegant, and blew our minds.

Today on Friday at 5pm I was talking about a ‘problem’ we had with a Lua daemons compared to their C brothers. Due to the use of tables, each time you queried them, the output order was always random. Computers don’t care, but a big user is also engineers and finding fields wandering all over the place is a pita. So what if we ordered the keys alphabetically instead? The code is Lua -> C -> IPC -> Output. The order is set in the Lua->C binding, so what if we sorted it in the binding.

But the tables are arbitrary size and you’d have to allocate memory, put them in a structure of some kind, etc, etc. Then we realized we could just put all the keys in a Lua array, and sort them with table.sort(). We could then use the array to get key/values in order, all from within the C binding.

Being late Friday, and having talked it through let’s just do it. Plus it was a handy teaching moment to explain to a fellow engineer how Lua binding worked, including a call back into Lua. By 6pm we had the binding recompiled, tested and it’s just worked.

Lessons learned:

1. The stack can seem confusing, but once you learn how to work with it, it’s pretty amazing.

2. The Lua reference docs are amazing. They clearly list what they pop and what they push onto the stack.

3. Calling back into Lua is.

4. It looks like write only code. Reading the code needs a post it note to keep track of the stack, along with the Lua reference manual. -- comments...

Re:Golua – A Lua 5.3 engine implemented in Go by ZeikJT on Wednesday November 28, 2018 @09:36PM (#18557602)
"3. Calling back into Lua is."

Dear God, the suspense is killing me!

Re:Golua – A Lua 5.3 engine implemented in Go by snarfy on Friday November 16, 2018 @04:07PM (#18471968)
I'm curious if it uses the Go GC for handling Lua objects, or if it has a Go implementation of the Lua GC.
Re:Golua – A Lua 5.3 engine implemented in Go by Scarbutt on Friday November 16, 2018 @01:29PM (#18470764)
It doesn't say why they need this for azure.
Re:Golua – A Lua 5.3 engine implemented in Go by gabrtv on Friday November 16, 2018 @02:28PM (#18471301)
Re:Golua – A Lua 5.3 engine implemented in Go by robertsd247 on Friday November 16, 2018 @04:56PM (#18472292)
...so Microsoft's own version of something? Got it.