1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2004-02-16 22:09:52 +03:00
|
|
|
** $Id: ltm.c,v 2.1 2003/12/10 12:13:36 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
|
|
|
|
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lobject.h"
|
1997-11-19 20:29:23 +03:00
|
|
|
#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[] = {
|
2002-08-30 23:09:21 +04:00
|
|
|
"nil", "boolean", "userdata", "number",
|
2004-02-16 22:09:52 +03:00
|
|
|
"string", "table", "function", "userdata", "thread",
|
|
|
|
"proto", "upval"
|
1997-09-16 23:25:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaT_init (lua_State *L) {
|
2001-12-05 23:15:18 +03:00
|
|
|
static const char *const luaT_eventname[] = { /* ORDER TM */
|
2002-06-25 00:18:38 +04:00
|
|
|
"__index", "__newindex",
|
2002-11-14 15:01:35 +03:00
|
|
|
"__gc", "__mode", "__eq",
|
2002-03-18 23:24:14 +03:00
|
|
|
"__add", "__sub", "__mul", "__div",
|
2002-06-12 18:56:22 +04:00
|
|
|
"__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
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 01:10:30 +03:00
|
|
|
/*
|
|
|
|
** function to be used with macro "fasttm": optimized for absence of
|
|
|
|
** tag methods
|
|
|
|
*/
|
2003-12-10 15:13:36 +03:00
|
|
|
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
|
|
|
const TValue *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? */
|
2003-04-03 17:35:34 +04:00
|
|
|
events->flags |= cast(lu_byte, 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-10 15:13:36 +03:00
|
|
|
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
2003-12-01 21:22:56 +03:00
|
|
|
Table *mt;
|
2001-12-05 23:15:18 +03:00
|
|
|
switch (ttype(o)) {
|
2001-01-25 19:45:36 +03:00
|
|
|
case LUA_TTABLE:
|
2003-12-01 21:22:56 +03:00
|
|
|
mt = hvalue(o)->metatable;
|
|
|
|
break;
|
2001-12-05 23:15:18 +03:00
|
|
|
case LUA_TUSERDATA:
|
2003-12-10 15:13:36 +03:00
|
|
|
mt = uvalue(o)->metatable;
|
2003-12-01 21:22:56 +03:00
|
|
|
break;
|
2000-10-05 17:00:17 +04:00
|
|
|
default:
|
2003-12-01 21:22:56 +03:00
|
|
|
mt = NULL;
|
2000-10-05 17:00:17 +04:00
|
|
|
}
|
2003-12-01 21:22:56 +03:00
|
|
|
return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : &luaO_nilobject);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|