1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2018-08-23 20:26:12 +03: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
|
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"
|
2018-01-28 15:08:04 +03:00
|
|
|
#include "lgc.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
|
|
|
|
2020-01-31 17:09:53 +03:00
|
|
|
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
|
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",
|
2018-10-17 16:44:42 +03:00
|
|
|
"__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]);
|
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) {
|
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);
|
2018-02-23 16:16:18 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2018-02-26 17:16:05 +03:00
|
|
|
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:
|
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:
|
2018-02-26 17:16:05 +03:00
|
|
|
mt = G(L)->mt[ttype(o)];
|
2000-10-05 17:00:17 +04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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)) {
|
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 */
|
2016-02-26 22:20:15 +03:00
|
|
|
}
|
2018-02-26 17:16:05 +03:00
|
|
|
return ttypename(ttype(o)); /* else use standard type name */
|
2016-02-26 22:20:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 19:59:42 +04:00
|
|
|
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
2017-06-29 18:06:44 +03:00
|
|
|
const TValue *p2, const TValue *p3) {
|
2022-10-29 18:06:37 +03:00
|
|
|
StkId func = L->top.p;
|
2017-06-29 18:06:44 +03:00
|
|
|
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 */
|
2022-10-29 18:06:37 +03:00
|
|
|
L->top.p = func + 4;
|
2017-06-29 18:06:44 +03:00
|
|
|
/* metamethod may yield only when called from Lua code */
|
2017-12-19 19:40:17 +03:00
|
|
|
if (isLuacode(L->ci))
|
2017-06-29 18:06:44 +03:00
|
|
|
luaD_call(L, func, 0);
|
|
|
|
else
|
|
|
|
luaD_callnoyield(L, func, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-29 21:10:50 +03:00
|
|
|
int luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
|
|
|
|
const TValue *p2, StkId res) {
|
2017-06-29 18:06:44 +03:00
|
|
|
ptrdiff_t result = savestack(L, res);
|
2022-10-29 18:06:37 +03:00
|
|
|
StkId func = L->top.p;
|
2015-11-02 21:48:07 +03:00
|
|
|
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
|
|
|
setobj2s(L, func + 1, p1); /* 1st argument */
|
|
|
|
setobj2s(L, func + 2, p2); /* 2nd argument */
|
2022-10-29 18:06:37 +03:00
|
|
|
L->top.p += 3;
|
2013-04-25 19:59:42 +04:00
|
|
|
/* metamethod may yield only when called from Lua code */
|
2017-12-19 19:40:17 +03:00
|
|
|
if (isLuacode(L->ci))
|
2017-06-29 18:06:44 +03:00
|
|
|
luaD_call(L, func, 1);
|
2015-11-02 21:48:07 +03:00
|
|
|
else
|
2017-06-29 18:06:44 +03:00
|
|
|
luaD_callnoyield(L, func, 1);
|
|
|
|
res = restorestack(L, result);
|
2022-10-29 18:06:37 +03:00
|
|
|
setobjs2s(L, res, --L->top.p); /* move result to its place */
|
2024-03-29 21:10:50 +03:00
|
|
|
return ttypetag(s2v(res)); /* return tag of the result */
|
2017-12-20 17:58:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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 */
|
2018-02-23 16:16:18 +03:00
|
|
|
if (notm(tm))
|
2013-04-25 19:59:42 +04:00
|
|
|
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
|
2024-03-29 21:10:50 +03:00
|
|
|
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-25 19:59:42 +04:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-03-29 21:10:50 +03:00
|
|
|
if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) {
|
2013-12-31 00:47:58 +04:00
|
|
|
switch (event) {
|
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: {
|
2017-11-08 17:50:23 +03:00
|
|
|
if (ttisnumber(p1) && ttisnumber(p2))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-29 21:10:50 +03:00
|
|
|
/*
|
|
|
|
** The use of 'p1' after 'callbinTM' is safe because, when a tag
|
|
|
|
** method is not found, 'callbinTM' cannot change the stack.
|
|
|
|
*/
|
2019-07-26 20:59:39 +03:00
|
|
|
void luaT_tryconcatTM (lua_State *L) {
|
2024-03-29 21:10:50 +03:00
|
|
|
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));
|
2019-07-26 20:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 21:32:09 +03:00
|
|
|
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
|
2019-08-27 19:59:39 +03:00
|
|
|
int flip, StkId res, TMS event) {
|
2019-07-05 21:03:15 +03:00
|
|
|
if (flip)
|
2017-12-13 21:32:09 +03:00
|
|
|
luaT_trybinTM(L, p2, p1, res, event);
|
|
|
|
else
|
|
|
|
luaT_trybinTM(L, p1, p2, res, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 21:10:42 +03:00
|
|
|
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) {
|
2017-12-13 21:32:09 +03:00
|
|
|
TValue aux;
|
2017-09-27 21:59:08 +03:00
|
|
|
setivalue(&aux, i2);
|
2019-08-27 19:59:39 +03:00
|
|
|
luaT_trybinassocTM(L, p1, &aux, flip, res, event);
|
2017-09-27 21:59:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-10 19:50:20 +03:00
|
|
|
/*
|
|
|
|
** 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.
|
|
|
|
*/
|
2013-04-25 20:07:52 +04:00
|
|
|
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
|
|
TMS event) {
|
2024-03-29 21:10:50 +03:00
|
|
|
int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */
|
|
|
|
if (tag >= 0) /* found tag method? */
|
|
|
|
return !tagisfalse(tag);
|
2018-08-24 16:17:54 +03:00
|
|
|
#if defined(LUA_COMPAT_LT_LE)
|
2017-11-23 22:18:10 +03:00
|
|
|
else if (event == TM_LE) {
|
2024-03-29 21:10:50 +03:00
|
|
|
/* 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);
|
2017-11-23 22:18:10 +03:00
|
|
|
}
|
2018-08-24 16:17:54 +03:00
|
|
|
#endif
|
2017-11-23 22:18:10 +03:00
|
|
|
luaG_ordererror(L, p1, p2); /* no metamethod found */
|
|
|
|
return 0; /* to avoid warnings */
|
2013-04-25 20:07:52 +04:00
|
|
|
}
|
|
|
|
|
2017-05-13 16:04:33 +03:00
|
|
|
|
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;
|
2019-03-22 19:37:17 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-17 22:20:00 +03:00
|
|
|
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
|
2018-06-08 22:06:59 +03:00
|
|
|
const Proto *p) {
|
2017-05-13 16:04:33 +03:00
|
|
|
int i;
|
2022-10-29 18:06:37 +03:00
|
|
|
int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
|
2018-01-28 16:39:52 +03:00
|
|
|
int nextra = actual - nfixparams; /* number of extra arguments */
|
2018-02-09 18:16:06 +03:00
|
|
|
ci->u.l.nextraargs = nextra;
|
2020-07-08 00:03:48 +03:00
|
|
|
luaD_checkstack(L, p->maxstacksize + 1);
|
2018-02-15 18:34:29 +03:00
|
|
|
/* copy function to the top of the stack */
|
2022-10-29 18:06:37 +03:00
|
|
|
setobjs2s(L, L->top.p++, ci->func.p);
|
2018-02-15 18:34:29 +03:00
|
|
|
/* move fixed parameters to the top of the stack */
|
|
|
|
for (i = 1; i <= nfixparams; i++) {
|
2022-10-29 18:06:37 +03:00
|
|
|
setobjs2s(L, L->top.p++, ci->func.p + i);
|
|
|
|
setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
|
2018-02-09 18:16:06 +03:00
|
|
|
}
|
2022-10-29 18:06:37 +03:00
|
|
|
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);
|
2017-05-13 16:04:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-09 18:16:06 +03:00
|
|
|
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 */
|
2022-11-24 16:20:15 +03:00
|
|
|
checkstackp(L, nextra, where); /* ensure stack space */
|
2022-10-29 18:06:37 +03:00
|
|
|
L->top.p = where + nextra; /* next instruction will need top */
|
2017-05-13 16:04:33 +03:00
|
|
|
}
|
2018-02-09 18:16:06 +03:00
|
|
|
for (i = 0; i < wanted && i < nextra; i++)
|
2022-10-29 18:06:37 +03:00
|
|
|
setobjs2s(L, where + i, ci->func.p - nextra + i);
|
2018-02-09 18:16:06 +03:00
|
|
|
for (; i < wanted; i++) /* complete required results with nil */
|
|
|
|
setnilvalue(s2v(where + i));
|
2017-05-13 16:04:33 +03:00
|
|
|
}
|
2018-02-27 20:48:28 +03:00
|
|
|
|