lua/ltm.c

257 lines
7.6 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
** $Id: ltm.c,v 2.56 2017/12/28 15:42:57 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
#define LUA_CORE
2002-12-04 20:38:31 +03:00
#include "lprefix.h"
#include <string.h>
#include "lua.h"
2013-04-29 20:56:50 +04:00
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
1997-09-16 23:25:59 +04:00
#include "lobject.h"
#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"
#include "lvm.h"
1997-09-16 23:25:59 +04: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] = {
"no value",
"nil", "boolean", udatatypename, "number",
"string", "table", "function", udatatypename, "thread",
"upvalue", "proto" /* these last cases are used for tests only */
1997-09-16 23:25:59 +04:00
};
void luaT_init (lua_State *L) {
2001-12-05 23:15:18 +03: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-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]);
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
}
/*
** 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_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
}
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:
mt = uvalue(o)->metatable;
2003-12-01 21:22:56 +03:00
break;
default:
2013-04-12 23:07:09 +04:00
mt = G(L)->mt[ttnov(o)];
}
return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
1997-09-16 23:25:59 +04: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 */
}
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, const TValue *p3) {
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 */
setobj2s(L, func + 3, p3); /* 3rd argument */
L->top = func + 4;
/* metamethod may yield only when called from Lua code */
if (isLuacode(L->ci))
luaD_call(L, func, 0);
else
luaD_callnoyield(L, func, 0);
}
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, StkId res) {
ptrdiff_t result = savestack(L, res);
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;
/* metamethod may yield only when called from Lua code */
if (isLuacode(L->ci))
luaD_call(L, func, 1);
else
luaD_callnoyield(L, func, 1);
res = restorestack(L, result);
setobjs2s(L, res, --L->top); /* move result to its place */
}
static int 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_callTMres(L, tm, p1, p2, res);
return 1;
}
2013-04-29 20:56:50 +04:00
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event) {
if (!callbinTM(L, p1, p2, res, event)) {
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:
case TM_SHL: case TM_SHR: case TM_BNOT: {
if (ttisnumber(p1) && ttisnumber(p2))
luaG_tointerror(L, p1, p2);
2014-11-21 15:15:57 +03:00
else
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
}
2015-03-30 18:42:59 +03:00
/* calls never return, but to avoid warnings: *//* FALLTHROUGH */
default:
luaG_opinterror(L, p1, p2, "perform arithmetic on");
}
2013-04-29 20:56:50 +04:00
}
}
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, int inv, TMS event) {
if (inv)
luaT_trybinTM(L, p2, p1, res, event);
else
luaT_trybinTM(L, p1, p2, res, event);
}
void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
int inv, StkId res, TMS event) {
TValue aux;
setivalue(&aux, i2);
luaT_trybinassocTM(L, p1, &aux, res, inv, event);
}
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
TMS event) {
if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
return !l_isfalse(s2v(L->top));
else if (event == TM_LE) {
/* try '!(p2 < p1)' for '(p1 <= p2)' */
L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
if (callbinTM(L, p2, p1, L->top, TM_LT)) {
L->ci->callstatus ^= CIST_LEQ; /* clear mark */
return l_isfalse(s2v(L->top));
}
/* else error will remove this 'ci'; no need to clear mark */
}
luaG_ordererror(L, p1, p2); /* no metamethod found */
return 0; /* to avoid warnings */
}
2017-11-27 20:44:31 +03:00
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
int inv, TMS event) {
TValue aux; const TValue *p2;
setivalue(&aux, v2);
if (inv) { /* arguments were exchanged? */
p2 = p1; p1 = &aux; /* correct them */
event = (event == TM_LE) ? TM_LT : TM_LE;
}
else
p2 = &aux;
return (luaT_callorderTM(L, p1, p2, event) != inv);
}
void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) {
int i;
Table *vtab;
TValue nname;
int nfixparams = p->numparams; /* number of fixed parameters */
actual -= nfixparams; /* number of extra arguments */
vtab = luaH_new(L); /* create vararg table */
sethvalue2s(L, L->top, vtab); /* anchor it for resizing */
L->top++; /* space ensured by caller */
luaH_resize(L, vtab, actual, 1);
for (i = 0; i < actual; i++) /* put extra arguments into vararg table */
setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1));
setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */
setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */
L->top -= actual; /* remove extra elements from the stack */
sethvalue2s(L, L->top - 1, vtab); /* move table to new top */
luaC_checkGC(L);
}
void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) {
if (!ttistable(t))
luaG_runerror(L, "'vararg' parameter is not a table");
else {
int i;
Table *h = hvalue(t);
if (wanted < 0) { /* get all? */
const TValue *ns = luaH_getstr(h, G(L)->nfield);
2017-11-30 18:37:16 +03:00
int n = (ttisinteger(ns)) ? cast_int(ivalue(ns)) : 0;
wanted = n;
checkstackp(L, n, where);
L->top = where + n;
}
for (i = 0; i < wanted; i++) /* get what is available */
setobj2s(L, where + i, luaH_getint(h, i + 1));
return;
}
}