lua/ltm.c

71 lines
1.5 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
2002-12-04 20:38:31 +03:00
** $Id: ltm.c,v 1.104 2002/11/14 11:51:50 roberto Exp roberto $
1997-09-16 23:25:59 +04:00
** Tag methods
** See Copyright Notice in lua.h
*/
#include <string.h>
2002-12-04 20:38:31 +03:00
#define ltm_c
#include "lua.h"
1997-09-16 23:25:59 +04:00
#include "lobject.h"
#include "lstate.h"
2001-01-25 19:45:36 +03:00
#include "lstring.h"
#include "ltable.h"
1997-09-16 23:25:59 +04:00
#include "ltm.h"
2001-12-05 23:15:18 +03:00
const char *const luaT_typenames[] = {
"nil", "boolean", "userdata", "number",
"string", "table", "function", "userdata", "thread"
1997-09-16 23:25:59 +04:00
};
void luaT_init (lua_State *L) {
2001-12-05 23:15:18 +03:00
static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex",
"__gc", "__mode", "__eq",
2002-03-18 23:24:14 +03:00
"__add", "__sub", "__mul", "__div",
"__pow", "__unm", "__lt", "__le",
"__concat", "__call"
2001-01-25 19:45:36 +03:00
};
int i;
2001-12-05 23:15:18 +03:00
for (i=0; i<TM_N; i++) {
G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
2002-04-30 17:01:48 +04:00
luaS_fix(G(L)->tmname[i]); /* never collect these names */
2001-01-25 19:45:36 +03:00
}
1997-09-16 23:25:59 +04:00
}
/*
** function to be used with macro "fasttm": optimized for absence of
** tag methods
*/
2001-12-05 23:15:18 +03:00
const TObject *luaT_gettm (Table *events, TMS event, TString *ename) {
const TObject *tm = luaH_getstr(events, ename);
2002-08-06 21:06:56 +04:00
lua_assert(event <= TM_EQ);
2002-08-05 18:50:39 +04:00
if (ttisnil(tm)) { /* no tag method? */
2002-05-20 23:51:06 +04:00
events->flags |= (1u<<event); /* cache this fact */
2001-12-05 23:15:18 +03:00
return NULL;
1997-09-16 23:25:59 +04:00
}
2001-12-05 23:15:18 +03:00
else return tm;
1997-09-16 23:25:59 +04:00
}
2001-12-05 23:15:18 +03:00
const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) {
TString *ename = G(L)->tmname[event];
2001-12-05 23:15:18 +03:00
switch (ttype(o)) {
2001-01-25 19:45:36 +03:00
case LUA_TTABLE:
2002-01-30 20:26:44 +03:00
return luaH_getstr(hvalue(o)->metatable, ename);
2001-12-05 23:15:18 +03:00
case LUA_TUSERDATA:
2002-01-30 20:26:44 +03:00
return luaH_getstr(uvalue(o)->uv.metatable, ename);
default:
return &luaO_nilobject;
}
1997-09-16 23:25:59 +04:00
}