1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2017-05-08 18:57:23 +03:00
|
|
|
** $Id: ltm.c,v 2.39 2017/04/11 18:41:09 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Tag methods
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define ltm_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_CORE
|
2002-12-04 20:38:31 +03:00
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
2013-04-29 20:56:50 +04:00
|
|
|
#include "ldebug.h"
|
2016-12-22 16:08:50 +03:00
|
|
|
#include "ldo.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"
|
2013-04-25 20:07:52 +04:00
|
|
|
#include "lvm.h"
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
2010-01-13 19:18:25 +03:00
|
|
|
static const char udatatypename[] = "userdata";
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2011-02-28 20:32:10 +03:00
|
|
|
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
|
2010-01-13 19:18:25 +03:00
|
|
|
"no value",
|
|
|
|
"nil", "boolean", udatatypename, "number",
|
|
|
|
"string", "table", "function", udatatypename, "thread",
|
2017-04-11 21:41:09 +03:00
|
|
|
"upvalue", "proto" /* these last cases are used for tests only */
|
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",
|
2007-09-10 21:59:32 +04:00
|
|
|
"__gc", "__mode", "__len", "__eq",
|
2013-12-16 23:06:52 +04:00
|
|
|
"__add", "__sub", "__mul", "__mod", "__pow",
|
|
|
|
"__div", "__idiv",
|
2013-12-31 00:47:58 +04:00
|
|
|
"__band", "__bor", "__bxor", "__shl", "__shr",
|
|
|
|
"__unm", "__bnot", "__lt", "__le",
|
2002-06-12 18:56:22 +04:00
|
|
|
"__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]);
|
2014-07-18 17:36:14 +04:00
|
|
|
luaC_fix(L, obj2gco(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) {
|
2015-11-03 18:47:30 +03:00
|
|
|
const TValue *tm = luaH_getshortstr(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? */
|
2005-12-22 19:19:56 +03:00
|
|
|
events->flags |= cast_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;
|
2013-04-12 23:07:09 +04:00
|
|
|
switch (ttnov(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:
|
2013-04-12 23:07:09 +04:00
|
|
|
mt = G(L)->mt[ttnov(o)];
|
2000-10-05 17:00:17 +04:00
|
|
|
}
|
2015-11-03 18:47:30 +03:00
|
|
|
return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-25 19:59:42 +04:00
|
|
|
|
2016-02-26 22:20:15 +03:00
|
|
|
/*
|
|
|
|
** Return the name of the type of an object. For tables and userdata
|
|
|
|
** with metatable, use their '__name' metafield, if present.
|
|
|
|
*/
|
|
|
|
const char *luaT_objtypename (lua_State *L, const TValue *o) {
|
|
|
|
Table *mt;
|
|
|
|
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
|
|
|
|
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
|
|
|
|
const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
|
|
|
|
if (ttisstring(name)) /* is '__name' a string? */
|
|
|
|
return getstr(tsvalue(name)); /* use it as type name */
|
|
|
|
}
|
|
|
|
return ttypename(ttnov(o)); /* else use standard type name */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 19:59:42 +04: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);
|
2015-11-02 21:48:07 +03:00
|
|
|
StkId func = L->top;
|
|
|
|
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
|
|
|
setobj2s(L, func + 1, p1); /* 1st argument */
|
|
|
|
setobj2s(L, func + 2, p2); /* 2nd argument */
|
|
|
|
L->top += 3;
|
2013-04-25 19:59:42 +04:00
|
|
|
if (!hasres) /* no result? 'p3' is third argument */
|
|
|
|
setobj2s(L, L->top++, p3); /* 3rd argument */
|
|
|
|
/* metamethod may yield only when called from Lua code */
|
2015-11-02 21:48:07 +03:00
|
|
|
if (isLua(L->ci))
|
|
|
|
luaD_call(L, func, hasres);
|
|
|
|
else
|
|
|
|
luaD_callnoyield(L, func, hasres);
|
2013-04-25 19:59:42 +04:00
|
|
|
if (hasres) { /* if has result, move it to its place */
|
|
|
|
p3 = restorestack(L, result);
|
|
|
|
setobjs2s(L, p3, --L->top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-08 18:57:23 +03:00
|
|
|
static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
|
|
StkId res, TMS event) {
|
2013-04-25 19:59:42 +04:00
|
|
|
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-25 20:07:52 +04:00
|
|
|
|
2013-04-29 20:56:50 +04:00
|
|
|
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
|
|
StkId res, TMS event) {
|
2017-05-08 18:57:23 +03:00
|
|
|
if (!callbinTM(L, p1, p2, res, event)) {
|
2013-12-31 00:47:58 +04:00
|
|
|
switch (event) {
|
|
|
|
case TM_CONCAT:
|
|
|
|
luaG_concaterror(L, p1, p2);
|
2015-03-30 18:42:59 +03:00
|
|
|
/* call never returns, but to avoid warnings: *//* FALLTHROUGH */
|
2014-11-21 15:15:57 +03:00
|
|
|
case TM_BAND: case TM_BOR: case TM_BXOR:
|
2014-04-12 00:17:39 +04:00
|
|
|
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
|
|
|
lua_Number dummy;
|
|
|
|
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
|
2013-12-31 00:47:58 +04:00
|
|
|
luaG_tointerror(L, p1, p2);
|
2014-11-21 15:15:57 +03:00
|
|
|
else
|
2014-11-10 17:46:05 +03:00
|
|
|
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
|
2014-04-12 00:17:39 +04:00
|
|
|
}
|
2015-03-30 18:42:59 +03:00
|
|
|
/* calls never return, but to avoid warnings: *//* FALLTHROUGH */
|
2013-12-31 00:47:58 +04:00
|
|
|
default:
|
2014-11-10 17:46:05 +03:00
|
|
|
luaG_opinterror(L, p1, p2, "perform arithmetic on");
|
2013-12-31 00:47:58 +04:00
|
|
|
}
|
2013-04-29 20:56:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 20:07:52 +04:00
|
|
|
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
|
|
TMS event) {
|
2017-05-08 18:57:23 +03:00
|
|
|
if (!callbinTM(L, p1, p2, L->top, event))
|
2013-04-25 20:07:52 +04:00
|
|
|
return -1; /* no metamethod */
|
|
|
|
else
|
|
|
|
return !l_isfalse(L->top);
|
|
|
|
}
|
|
|
|
|