lua/ltm.c

145 lines
3.9 KiB
C
Raw Normal View History

1997-09-17 03:25:59 +08:00
/*
2015-03-30 23:42:59 +08:00
** $Id: ltm.c,v 2.33 2014/11/21 12:15:57 roberto Exp roberto $
1997-09-17 03:25:59 +08:00
** Tag methods
** See Copyright Notice in lua.h
*/
2002-12-05 01:38:31 +08:00
#define ltm_c
#define LUA_CORE
2002-12-05 01:38:31 +08:00
#include "lprefix.h"
#include <string.h>
#include "lua.h"
2013-04-30 00:56:50 +08:00
#include "ldebug.h"
#include "ldo.h"
1997-09-17 03:25:59 +08:00
#include "lobject.h"
#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"
#include "lvm.h"
1997-09-17 03:25:59 +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] = {
"no value",
"nil", "boolean", udatatypename, "number",
"string", "table", "function", udatatypename, "thread",
2013-08-28 02:53:35 +08:00
"proto" /* this last case is used for tests only */
1997-09-17 03:25:59 +08:00
};
void luaT_init (lua_State *L) {
2001-12-06 04:15:18 +08:00
static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex",
"__gc", "__mode", "__len", "__eq",
"__add", "__sub", "__mul", "__mod", "__pow",
"__div", "__idiv",
"__band", "__bor", "__bxor", "__shl", "__shr",
"__unm", "__bnot", "__lt", "__le",
"__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]);
luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
2001-01-26 00:45:36 +08:00
}
1997-09-17 03:25:59 +08:00
}
/*
** function to be used with macro "fasttm": optimized for absence of
** tag methods
*/
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
}
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
2003-12-02 02:22:56 +08:00
Table *mt;
2013-04-13 03:07:09 +08:00
switch (ttnov(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:
mt = uvalue(o)->metatable;
2003-12-02 02:22:56 +08:00
break;
default:
2013-04-13 03:07:09 +08:00
mt = G(L)->mt[ttnov(o)];
}
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
}
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, TValue *p3, int hasres) {
ptrdiff_t result = savestack(L, p3);
2014-11-11 01:24:43 +08:00
setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */
setobj2s(L, L->top++, p1); /* 1st argument */
setobj2s(L, L->top++, p2); /* 2nd argument */
if (!hasres) /* no result? 'p3' is third argument */
setobj2s(L, L->top++, p3); /* 3rd argument */
/* metamethod may yield only when called from Lua code */
luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
if (hasres) { /* if has result, move it to its place */
p3 = restorestack(L, result);
setobjs2s(L, p3, --L->top);
}
}
int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event) {
const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
if (ttisnil(tm))
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
if (ttisnil(tm)) return 0;
luaT_callTM(L, tm, p1, p2, res, 1);
return 1;
}
2013-04-30 00:56:50 +08:00
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event) {
if (!luaT_callbinTM(L, p1, p2, res, event)) {
switch (event) {
case TM_CONCAT:
luaG_concaterror(L, p1, p2);
2015-03-30 23:42:59 +08:00
/* call never returns, but to avoid warnings: *//* FALLTHROUGH */
2014-11-21 20:15:57 +08:00
case TM_BAND: case TM_BOR: case TM_BXOR:
case TM_SHL: case TM_SHR: case TM_BNOT: {
lua_Number dummy;
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
luaG_tointerror(L, p1, p2);
2014-11-21 20:15:57 +08:00
else
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
}
2015-03-30 23:42:59 +08:00
/* calls never return, but to avoid warnings: *//* FALLTHROUGH */
default:
luaG_opinterror(L, p1, p2, "perform arithmetic on");
}
2013-04-30 00:56:50 +08:00
}
}
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
TMS event) {
if (!luaT_callbinTM(L, p1, p2, L->top, event))
return -1; /* no metamethod */
else
return !l_isfalse(L->top);
}