lua-users home
lua-l archive

lua hacking wondering

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I am trying to install an '__existingindex' metatable method. But not quite working. The code I am trying to do: (excuse the lack of a patch, but I am using a custom directory arrangement for my lua framework (osx))
ltm.c @ 28
void luaT_init (lua_State *L) {
 static const char *const luaT_eventname[] = { /* ORDER TM */
 "__index", "__existingindex", "__newindex", "__usedindex",
ltm.h @ 18
typedef enum {
 TM_INDEX,
 TM_EXISTINGINDEX,
 TM_NEWINDEX,
lvm.c @ 147
/*
** Function to index a table.
** Receives the table at `t' and the key at `key'.
** leaves the result at `res'.
*/
const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key,
 int loop) {
 if (loop > MAXTAGLOOP)
 luaG_runerror(L, "loop in gettable");
 if (ttistable(t)) { /* `t' is a table? */
 Table *h = hvalue(t);
 /* start EXISTINGINDEX HACK */
 const TObject *tm = fasttm(L, h->metatable, TM_EXISTINGINDEX);
 if (tm != NULL)
 {
 if (ttisfunction(tm))
 {
 callTMres(L, tm, t, key);
 return L->top;
 }
 }
 /* end HACK */
 const TObject *v = luaH_get(h, key); /* do a primitive get */
 if (!ttisnil(v)) return v;
 else return luaV_index(L, t, key, loop+1);
 }
 else return luaV_getnotable(L, t, key, loop+1);
}
I do not claim to understand lua internals very well, but I basically could figure out all of it but how the loops were working.
This is how it works:
Anubis:~/.tmp/lua-5.0.2/src/lua jdrake$ rlwrap lua
Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> blah = {}
> blah.x = 5
> print (blah.x)
5
> ximp = {}
> function ximp.__existingindex(t,k) print("existing index: ", k); return k end
> setmetatable(blah, ximp)
> print (blah.x)
5
>
From what I can tell the function definitely isn't being called.
An example of __index being called:
> function ximp.__index(t,k) print("index: ", k); return 0 end
> print (blah.y)
index: y
0
Any ideas here?
"Reportedly, he remarked to one of his Christian missionary friends: 'What you teach us to do is admirable, but what you teach us to believe is foolish'."
- - Referring to King Mongkut of Siam
http://en.wikipedia.org/wiki/Mongkut
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)
iD8DBQFA0Ryo0noMGorKkKgRAszoAJ4qmpLslNKOG4A4UXmNwpdBMeI7vACfafPQ
ZdP6RG3hGY/dHJTrINYp+Z8=
=IKYA
-----END PGP SIGNATURE-----

AltStyle によって変換されたページ (->オリジナル) /