tags:

views:

126

answers:

5

I've skimmed Programing in Lua, I've looked at the Lua Reference.

However, they both tells me this function does this, but not how.

When reading SICP, I got this feeling of: "ah, here's the computational model underlying scheme"; I'm trying to get the same sense concerning lua -- i.e. a concise description of it's vm, a "how" rather than a "what".

Does anyone know of a good document (besides the C source) describing this?

Thanks!

+6  A: 

You might want to read the No-Frills Intro to Lua 5(.1) VM Instructions (pick a link, click on the Docs tab, choose English -> Go).

I don't remember exactly where I've seen it, but I remember reading that Lua's authors specifically discourage end-users from getting into too much detail on the VM; I think they want it to be as much of an implementation detail as possible.

Mark Rushakoff
+2  A: 

The computational model underlying Lua is pretty much the same as the computational model underlying Scheme, except that the central data structure is not the cons cell; it's the mutable hash table. (At least until you get into metaprogramming with metatables.) Otherwise all the familiar stuff is there: nested first-class functions with mutable local variables (let-bound variables in Scheme), and so on.

It's not clear to me that you'd get much from a study of the VM. I did some hacking on the VM a while back and it's a lot like any other register-oriented VM, although maybe a bit cleaner. Only a handful of instructions are Lua-specific.

If you're curious about the metatables, the semantics is described clearly, if somewhat verbosely, in Section 2.8 of the reference manual for Lua 5.1. If you look at the VM code in src/lvm.c you'll see almost exactly that logic implemented in C (e.g., the internal Arith function). The VM instructions are specialized for the common cases, but it's all terribly straightforward; nothing clever is involved.

For years I've been wanting a more formal specification of Lua's computational model, but my tastes run more toward formal semantics...

Norman Ramsey
I'm really curious about everything involving metatables. If they're not documented in the VM, where are they documented?
anon
I've added some notes about metatables, but there's really not much to say.
Norman Ramsey
+3  A: 

See http://www.lua.org/source/5.1/lopcodes.h.html . The list starts at OP_MOVE.

lhf
+2  A: 

Besides already mentioned A No-Frills Introduction to Lua 5.1 VM Instructions, you may be interested in this excellent post by Mike Pall on how to read Lua source.

Also see related Lua-Users Wiki page.

Alexander Gladysh
A: 

I've found The Implementation of Lua 5.1 very useful for understanding what Lua is actually doing.

It explains the hashing techniques, garbage collection and some other bits and pieces.

Matthew Monaghan