Live data from Hacker News

Interesting things about the Lua interpreter (2020)

thesephist.com

1–10 of 44 posts

Re: Interesting things about the Lua interpreter (2020)

#4
> The 5.0 VM is a register machine, which operates on a set of virtual registers that can store and act on the local variables of a function, in addition to the traditional runtime stack.

This is a common source of confusion, because the name "register machine" makes people think about CPU registers. However, the registers in a register VM are merely slots in the traditional runtime stack. The difference between a stack and register machine has to do with how the bytecode instructions are encoded. In a stack machine, most instructions implicitly pop arguments from and push their results to the top of the stack. The instructions are byte-sized, encoding just the operation name. For example, to add 10+10

    LOADK 10
    DUP
    ADD
Meanwhile, in a register machine the instructions can read and write to any slot in the stack. Instructions are larger, because in addition to the operation name they also encode the indexes of the input slots and the index of the output slot. But it's worth it because you need less instructions in total and do less stack shuffling.

    LOADK 1 10
    ADD 1 1 2

Re: Interesting things about the Lua interpreter (2020)

#5

I love that they went from a single-pass interpreter to a byte-code virtual machine, and now like PHP, also has direct access to C & system libraries! Lua has come a long way since it's advent.

The paper and article are about lua 5.0 which came out in 2003. I don't believe it had any prominence at all before that so this stuff has "always" been there in lua unless you were in the CS dept at puc-rio in the 90s.

Re: Interesting things about the Lua interpreter (2020)

#6
post #3

The Lua interpreters, "upvalues" sound suspiciously like the results of Tcl's, "upvar", can anybody comment on how similar they actually are?

I don't think there's any deep connection. "upvar" in Tcl is a language feature. Upvalues in Lua are purely an implementation detail of the interpreter.

If you want all the gritty details, a chapter of Crafting Interpreters walks through a complete implementation of Lua's approach to closures:

http://craftinginterpreters.com/closures.html

Re: Interesting things about the Lua interpreter (2020)

#7
Timing on this is confusing. Article was written in 2020 about a paper published in 2003 or so about the design of lua 5.0.

It's still a really insightful and approachable paper that's worth reading, and it does still help understand the constraints and approach of lua. But the current "old" version of the language is 5.2 released in 2011, and there have been a couple major versions after that as well.

So some of it may not actually be applicable to using lua now, depending on what your "now" looks like.

Re: Interesting things about the Lua interpreter (2020)

#8
post #3

The Lua interpreters, "upvalues" sound suspiciously like the results of Tcl's, "upvar", can anybody comment on how similar they actually are?

I don't think there's any deep connection. "upvar" in Tcl is a language feature. Upvalues in Lua are purely an implementation detail of the interpreter. If you want all the gritty details, a chapter of Crafting Interpreters walks through a complete implementation of Lua's approach to closures: http://craftinginterpreters.com/closures.html

I'm not sure about that though I haven't used tcl in a while and was never an expert. I think tcl upvar allows you to explicitly do what lua's closure implementation automatically does.

They aren't the same thing but they are closely related concepts and similar solutions to the same potential problem.

Re: Interesting things about the Lua interpreter (2020)

#9
post #3

The Lua interpreters, "upvalues" sound suspiciously like the results of Tcl's, "upvar", can anybody comment on how similar they actually are?

Nowadays the upvalues in Lua are bona-fide lexical scoping a-la Scheme or Javascript. However, the name comes from Lua 4.0 where the upvalues worked in a more unusual way. Not in the same way as Tcl though, but just as surprising for the uninitiated.

https://www.lua.org/manual/4.0/manual.html#4.6

Re: Interesting things about the Lua interpreter (2020)

#10

Timing on this is confusing. Article was written in 2020 about a paper published in 2003 or so about the design of lua 5.0. It's still a really insightful and approachable paper that's worth reading, and it does still help understand the constraints and approach of lua. But the current "old" version of the language is 5.2 released in 2011, and there have been a couple major versions after that as well. So some of it…

For the most part, the info is still valid today. The main thing that changed since then is that Lua 5.3 introduced integers and several new bytecode instructions for the common case where one of the operands is a small integer constant.
Post reply on HN