lua/ltm.c

277 lines
8.3 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
** $Id: ltm.c $
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
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
"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", "__close"
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) {
2023-05-16 22:53:29 +03:00
const TValue *tm = luaH_Hgetshortstr(events, ename);
2002-08-06 21:06:56 +04:00
lua_assert(event <= TM_EQ);
if (notm(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;
switch (ttype(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:
mt = G(L)->mt[ttype(o)];
}
2023-05-16 22:53:29 +03:00
return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
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)) {
2024-01-29 20:29:24 +03:00
const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name"));
if (ttisstring(name)) /* is '__name' a string? */
return getstr(tsvalue(name)); /* use it as type name */
}
return ttypename(ttype(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.p;
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.p = 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);
}
lu_byte 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.p;
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.p += 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.p); /* move result to its place */
return ttypetag(s2v(res)); /* return tag of the result */
}
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 (notm(tm))
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
if (notm(tm))
return -1; /* tag method not found */
else /* call tag method and return the tag of the result */
return luaT_callTMres(L, tm, p1, p2, res);
}
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 (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) {
switch (event) {
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
}
}
/*
** The use of 'p1' after 'callbinTM' is safe because, when a tag
** method is not found, 'callbinTM' cannot change the stack.
*/
void luaT_tryconcatTM (lua_State *L) {
StkId p1 = L->top.p - 2; /* first argument */
if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0))
luaG_concaterror(L, s2v(p1), s2v(p1 + 1));
}
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
int flip, StkId res, TMS event) {
2019-07-05 21:03:15 +03:00
if (flip)
luaT_trybinTM(L, p2, p1, res, event);
else
luaT_trybinTM(L, p1, p2, res, event);
}
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
2019-07-05 21:03:15 +03:00
int flip, StkId res, TMS event) {
TValue aux;
setivalue(&aux, i2);
luaT_trybinassocTM(L, p1, &aux, flip, res, event);
}
/*
** Calls an order tag method.
** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
** behavior: if there is no '__le', try '__lt', based on l <= r iff
** !(r < l) (assuming a total order). If the metamethod yields during
** this substitution, the continuation has to know about it (to negate
** the result of r<l); bit CIST_LEQ in the call status keeps that
** information.
*/
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
TMS event) {
int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */
if (tag >= 0) /* found tag method? */
return !tagisfalse(tag);
#if defined(LUA_COMPAT_LT_LE)
else if (event == TM_LE) {
/* try '!(p2 < p1)' for '(p1 <= p2)' */
L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
tag = callbinTM(L, p2, p1, L->top.p, TM_LT);
L->ci->callstatus ^= CIST_LEQ; /* clear mark */
if (tag >= 0) /* found tag method? */
return tagisfalse(tag);
}
#endif
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,
2019-07-05 21:03:15 +03:00
int flip, int isfloat, TMS event) {
2017-11-27 20:44:31 +03:00
TValue aux; const TValue *p2;
if (isfloat) {
setfltvalue(&aux, cast_num(v2));
}
else
setivalue(&aux, v2);
2019-07-05 21:03:15 +03:00
if (flip) { /* arguments were exchanged? */
2017-11-27 20:44:31 +03:00
p2 = p1; p1 = &aux; /* correct them */
}
else
p2 = &aux;
2018-02-21 18:49:32 +03:00
return luaT_callorderTM(L, p1, p2, event);
2017-11-27 20:44:31 +03:00
}
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
const Proto *p) {
int i;
int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
int nextra = actual - nfixparams; /* number of extra arguments */
ci->u.l.nextraargs = nextra;
luaD_checkstack(L, p->maxstacksize + 1);
/* copy function to the top of the stack */
setobjs2s(L, L->top.p++, ci->func.p);
/* move fixed parameters to the top of the stack */
for (i = 1; i <= nfixparams; i++) {
setobjs2s(L, L->top.p++, ci->func.p + i);
setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
}
ci->func.p += actual + 1;
ci->top.p += actual + 1;
lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
}
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
int i;
int nextra = ci->u.l.nextraargs;
if (wanted < 0) {
wanted = nextra; /* get all extra arguments available */
checkstackp(L, nextra, where); /* ensure stack space */
L->top.p = where + nextra; /* next instruction will need top */
}
for (i = 0; i < wanted && i < nextra; i++)
setobjs2s(L, where + i, ci->func.p - nextra + i);
for (; i < wanted; i++) /* complete required results with nil */
setnilvalue(s2v(where + i));
}
2018-02-27 20:48:28 +03:00