lua-users home
lua-l archive

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

yep, I understand that's what Lua currently does.

the reason I'm proposing the __assign metamethod is so that the host can override that behaviour if it wishes to so that a more natural lua syntax can be used to copy (and otherwise manipulate) C++ objects.

it also covers the case where you have a table or userdata on LHS and want to assign a string value to it. Lua would most likely change the type of the lhs. If the objects are in a table it's less of an issue since __newindex covers it.

in fact since __newindex is effectively the same as my proposed __assign but restricted to indexes in a table, I don't see the problem in generalising the concept to allow it to apply to any type, instead of just table indexes. A precedent has been set :)

Any comments on __cast proposal? I foresee fewer issues with this one, since the base lib already introduces a __tostring metamethod, and a __cast is simply a generalization of that to include passing up a type specifier as well, plus not requiring an explicit lua call to tostring(var) to do the conversion, but rather conversion happens when the type is checked or converted as it is used.

I've modified luaV_tostring and luaV_tonumber to make the metamethod call and it seems to be working well. Had to modify the prototype for luaV_tonumber to include a lua_State* as well though. Only about 8 instances of calls to that though and the #define tonumber(o,n)


David Given wrote:
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.)

- --
┌── ─── ───────────────────
│ "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
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla -


Adrien de Croy - WinGate Proxy Server -