2016-09-16 10:10:18 +02:00
|
|
|
/*
|
|
|
|
** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
|
|
|
|
** Tag methods
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-08-21 17:39:54 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-16 10:10:18 +02:00
|
|
|
#define ltm_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
|
2021-08-21 17:39:54 +02:00
|
|
|
/* These must be correspond to the LUA_T* defines in lua.h */
|
2016-09-16 10:10:18 +02:00
|
|
|
|
|
|
|
const char *const luaT_typenames[] = {
|
2021-08-21 17:39:54 +02:00
|
|
|
"nil", "boolean", "lightfunction","number", // base type = 0, 1, 2, 3
|
|
|
|
"string", "table", "function", "userdata", "thread", // base type = 4, 5, 6, 7, 8
|
2016-09-16 10:10:18 +02:00
|
|
|
"proto", "upval"
|
|
|
|
};
|
|
|
|
|
|
|
|
void luaT_init (lua_State *L) {
|
|
|
|
static const char *const luaT_eventname[] = { /* ORDER TM */
|
|
|
|
"__index", "__newindex",
|
|
|
|
"__gc", "__mode", "__eq",
|
|
|
|
"__add", "__sub", "__mul", "__div", "__mod",
|
|
|
|
"__pow", "__unm", "__len", "__lt", "__le",
|
2021-08-21 17:39:54 +02:00
|
|
|
"__concat", "__call",
|
|
|
|
"__metatable"
|
2016-09-16 10:10:18 +02:00
|
|
|
};
|
|
|
|
int i;
|
|
|
|
for (i=0; i<TM_N; i++) {
|
|
|
|
G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
|
2019-07-22 11:13:43 +02:00
|
|
|
stringfix(G(L)->tmname[i]); /* never collect these names */
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** function to be used with macro "fasttm": optimized for absence of
|
|
|
|
** tag methods
|
|
|
|
*/
|
|
|
|
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
2021-08-21 17:39:54 +02:00
|
|
|
const TValue *tm = luaH_getstr(events, ename);
|
2016-09-16 10:10:18 +02:00
|
|
|
lua_assert(event <= TM_EQ);
|
2019-07-22 11:13:43 +02:00
|
|
|
if (ttisnil(tm)) { /* no tag method? */
|
2021-08-21 17:39:54 +02:00
|
|
|
if (isrwtable(events)) /* if this a (RW) Table */
|
|
|
|
events->flags |= cast_byte(1u<<event); /* then cache this fact */
|
2019-07-22 11:13:43 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-08-21 17:39:54 +02:00
|
|
|
else return tm;
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
|
|
|
Table *mt;
|
|
|
|
switch (ttype(o)) {
|
|
|
|
case LUA_TTABLE:
|
|
|
|
case LUA_TROTABLE:
|
2021-08-21 17:39:54 +02:00
|
|
|
mt = hvalue(o)->metatable;
|
2016-09-16 10:10:18 +02:00
|
|
|
break;
|
|
|
|
case LUA_TUSERDATA:
|
|
|
|
mt = uvalue(o)->metatable;
|
|
|
|
break;
|
|
|
|
default:
|
2021-08-21 17:39:54 +02:00
|
|
|
mt = G(L)->mt[ttnov(o)];
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|
2021-08-21 17:39:54 +02:00
|
|
|
return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|