lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerome Vuarand wrote:
[...]
> Lua doesn't use reference counting, but a garbage collector mechanism.
> However the effect is the same. The problem when you write a = b, is
> that the object referenced by 'a' (if there is one) is not modified at
> all, and has no knowledge of the variables referencing it, so it doesn't
> make sense call a metamethod on it when 'a' is modified.

One way of thinking about this is by considering *all* variables in Lua to be
pointers to structures in memory (with the 'structure' that represents a
number being encoded in the pointer representation being a mere optimisation).

Variable assignment simply updates a pointer to point at something else.

In your example, you had two userdatas that wrap 0x4000001 and 0x4000002. If a
and b point to these userdatas, and you do a=b, then the userdatas remain
unchanged; but both a and b now point to the userdata that wraps 0x4000002.
(The garbage collector may then collect the first userdata, which is now
unused, of course.) This is not the same as modifying the two userdatas so
that they both wrap the same value.

(In fact, Lua only has one form of structured data --- the table --- and there
is no way of embedding one table inside another table. When you have a
construct like {{1}}, what you actually get is this:

one = 1
t1 = {one}
t2 = {t1}

That is, three separate objects referring to each other by pointers.)

- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGu6iOf9E0noFvlzgRAgTuAKCjv4HByaEfmOuEaaB9eIeKiyWp0gCgipGc
meEB+AaYOQUt9M1gMxs40wY=
=kHV6
-----END PGP SIGNATURE-----