1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2011-06-03 03:31:40 +08:00
|
|
|
** $Id: ltm.c,v 2.13 2011/02/28 17:32:10 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Tag methods
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2002-12-05 01:38:31 +08:00
|
|
|
#define ltm_c
|
2004-05-01 04:13:38 +08:00
|
|
|
#define LUA_CORE
|
2002-12-05 01:38:31 +08:00
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lobject.h"
|
1997-11-20 01:29:23 +08:00
|
|
|
#include "lstate.h"
|
2001-01-26 00:45:36 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "ltm.h"
|
|
|
|
|
|
|
|
|
2010-01-14 00:18:25 +08:00
|
|
|
static const char udatatypename[] = "userdata";
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2011-03-01 01:32:10 +08:00
|
|
|
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
|
2010-01-14 00:18:25 +08:00
|
|
|
"no value",
|
|
|
|
"nil", "boolean", udatatypename, "number",
|
|
|
|
"string", "table", "function", udatatypename, "thread",
|
2011-03-01 01:32:10 +08:00
|
|
|
"proto", "upval" /* these last two cases are used for tests only */
|
1997-09-17 03:25:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
void luaT_init (lua_State *L) {
|
2001-12-06 04:15:18 +08:00
|
|
|
static const char *const luaT_eventname[] = { /* ORDER TM */
|
2002-06-25 04:18:38 +08:00
|
|
|
"__index", "__newindex",
|
2007-09-11 01:59:32 +08:00
|
|
|
"__gc", "__mode", "__len", "__eq",
|
2005-03-09 02:00:16 +08:00
|
|
|
"__add", "__sub", "__mul", "__div", "__mod",
|
2007-09-11 01:59:32 +08:00
|
|
|
"__pow", "__unm", "__lt", "__le",
|
2002-06-12 22:56:22 +08:00
|
|
|
"__concat", "__call"
|
2001-01-26 00:45:36 +08:00
|
|
|
};
|
|
|
|
int i;
|
2001-12-06 04:15:18 +08:00
|
|
|
for (i=0; i<TM_N; i++) {
|
|
|
|
G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
|
2002-04-30 21:01:48 +08:00
|
|
|
luaS_fix(G(L)->tmname[i]); /* never collect these names */
|
2001-01-26 00:45:36 +08:00
|
|
|
}
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 06:10:30 +08:00
|
|
|
/*
|
|
|
|
** function to be used with macro "fasttm": optimized for absence of
|
|
|
|
** tag methods
|
|
|
|
*/
|
2003-12-10 20:13:36 +08:00
|
|
|
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
|
|
|
const TValue *tm = luaH_getstr(events, ename);
|
2002-08-07 01:06:56 +08:00
|
|
|
lua_assert(event <= TM_EQ);
|
2002-08-05 22:50:39 +08:00
|
|
|
if (ttisnil(tm)) { /* no tag method? */
|
2005-12-23 00:19:56 +08:00
|
|
|
events->flags |= cast_byte(1u<<event); /* cache this fact */
|
2001-12-06 04:15:18 +08:00
|
|
|
return NULL;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
2001-12-06 04:15:18 +08:00
|
|
|
else return tm;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-10 20:13:36 +08:00
|
|
|
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
2003-12-02 02:22:56 +08:00
|
|
|
Table *mt;
|
2011-06-03 03:31:40 +08:00
|
|
|
switch (ttypenv(o)) {
|
2001-01-26 00:45:36 +08:00
|
|
|
case LUA_TTABLE:
|
2003-12-02 02:22:56 +08:00
|
|
|
mt = hvalue(o)->metatable;
|
|
|
|
break;
|
2001-12-06 04:15:18 +08:00
|
|
|
case LUA_TUSERDATA:
|
2003-12-10 20:13:36 +08:00
|
|
|
mt = uvalue(o)->metatable;
|
2003-12-02 02:22:56 +08:00
|
|
|
break;
|
2000-10-05 21:00:17 +08:00
|
|
|
default:
|
2010-04-14 23:13:48 +08:00
|
|
|
mt = G(L)->mt[ttypenv(o)];
|
2000-10-05 21:00:17 +08:00
|
|
|
}
|
2006-01-10 20:50:00 +08:00
|
|
|
return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|