1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2018-02-09 23:16:06 +08:00
|
|
|
** $Id: ltm.c,v 2.59 2018/02/07 15:18:04 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
|
2004-05-01 04:13:38 +08:00
|
|
|
#define LUA_CORE
|
2002-12-05 01:38:31 +08:00
|
|
|
|
2014-11-03 03:19:04 +08:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
2013-04-30 00:56:50 +08:00
|
|
|
#include "ldebug.h"
|
2016-12-22 21:08:50 +08:00
|
|
|
#include "ldo.h"
|
2018-01-28 20:08:04 +08:00
|
|
|
#include "lgc.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lobject.h"
|
1997-11-20 01:29:23 +08:00
|
|
|
#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"
|
2013-04-26 00:07:52 +08:00
|
|
|
#include "lvm.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2010-01-14 00:18:25 +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] = {
|
2010-01-14 00:18:25 +08:00
|
|
|
"no value",
|
|
|
|
"nil", "boolean", udatatypename, "number",
|
|
|
|
"string", "table", "function", udatatypename, "thread",
|
2017-04-12 02:41:09 +08:00
|
|
|
"upvalue", "proto" /* these last cases are used for tests only */
|
1997-09-17 03:25:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
void luaT_init (lua_State *L) {
|
2001-12-06 04:15:18 +08:00
|
|
|
static const char *const luaT_eventname[] = { /* ORDER TM */
|
2002-06-25 04:18:38 +08:00
|
|
|
"__index", "__newindex",
|
2007-09-11 01:59:32 +08:00
|
|
|
"__gc", "__mode", "__len", "__eq",
|
2013-12-17 03:06:52 +08:00
|
|
|
"__add", "__sub", "__mul", "__mod", "__pow",
|
|
|
|
"__div", "__idiv",
|
2013-12-31 04:47:58 +08:00
|
|
|
"__band", "__bor", "__bxor", "__shl", "__shr",
|
|
|
|
"__unm", "__bnot", "__lt", "__le",
|
2002-06-12 22:56:22 +08:00
|
|
|
"__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]);
|
2014-07-18 21:36:14 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 06:10:30 +08:00
|
|
|
/*
|
|
|
|
** function to be used with macro "fasttm": optimized for absence of
|
|
|
|
** tag methods
|
|
|
|
*/
|
2003-12-10 20:13:36 +08:00
|
|
|
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
2015-11-03 23:47:30 +08:00
|
|
|
const TValue *tm = luaH_getshortstr(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-10 20:13:36 +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:
|
2003-12-10 20:13:36 +08:00
|
|
|
mt = uvalue(o)->metatable;
|
2003-12-02 02:22:56 +08:00
|
|
|
break;
|
2000-10-05 21:00:17 +08:00
|
|
|
default:
|
2013-04-13 03:07:09 +08:00
|
|
|
mt = G(L)->mt[ttnov(o)];
|
2000-10-05 21:00:17 +08:00
|
|
|
}
|
2015-11-03 23:47:30 +08:00
|
|
|
return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2013-04-25 23:59:42 +08:00
|
|
|
|
2016-02-27 03:20:15 +08: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 23:59:42 +08:00
|
|
|
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *p2, const TValue *p3) {
|
2017-12-28 23:42:57 +08:00
|
|
|
StkId func = L->top;
|
2017-06-29 23:06:44 +08: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 */
|
2017-12-20 22:58:05 +08:00
|
|
|
L->top = func + 4;
|
2017-06-29 23:06:44 +08:00
|
|
|
/* metamethod may yield only when called from Lua code */
|
2017-12-20 00:40:17 +08:00
|
|
|
if (isLuacode(L->ci))
|
2017-06-29 23:06:44 +08:00
|
|
|
luaD_call(L, func, 0);
|
|
|
|
else
|
|
|
|
luaD_callnoyield(L, func, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-28 23:42:57 +08:00
|
|
|
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
|
|
|
|
const TValue *p2, StkId res) {
|
2017-06-29 23:06:44 +08:00
|
|
|
ptrdiff_t result = savestack(L, res);
|
2015-11-03 02:48:07 +08: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 23:59:42 +08:00
|
|
|
/* metamethod may yield only when called from Lua code */
|
2017-12-20 00:40:17 +08:00
|
|
|
if (isLuacode(L->ci))
|
2017-06-29 23:06:44 +08:00
|
|
|
luaD_call(L, func, 1);
|
2015-11-03 02:48:07 +08:00
|
|
|
else
|
2017-06-29 23:06:44 +08:00
|
|
|
luaD_callnoyield(L, func, 1);
|
|
|
|
res = restorestack(L, result);
|
2017-12-20 22:58:05 +08:00
|
|
|
setobjs2s(L, res, --L->top); /* move result to its place */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-08 23:57:23 +08:00
|
|
|
static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
|
|
StkId res, TMS event) {
|
2013-04-25 23:59:42 +08: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;
|
2017-12-28 23:42:57 +08:00
|
|
|
luaT_callTMres(L, tm, p1, p2, res);
|
2013-04-25 23:59:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-04-26 00:07:52 +08:00
|
|
|
|
2013-04-30 00:56:50 +08:00
|
|
|
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
|
|
StkId res, TMS event) {
|
2017-05-08 23:57:23 +08:00
|
|
|
if (!callbinTM(L, p1, p2, res, event)) {
|
2013-12-31 04:47:58 +08:00
|
|
|
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:
|
2014-04-12 04:17:39 +08:00
|
|
|
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
2017-11-08 22:50:23 +08:00
|
|
|
if (ttisnumber(p1) && ttisnumber(p2))
|
2013-12-31 04:47:58 +08:00
|
|
|
luaG_tointerror(L, p1, p2);
|
2014-11-21 20:15:57 +08:00
|
|
|
else
|
2014-11-10 22:46:05 +08:00
|
|
|
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
|
2014-04-12 04:17:39 +08:00
|
|
|
}
|
2015-03-30 23:42:59 +08:00
|
|
|
/* calls never return, but to avoid warnings: *//* FALLTHROUGH */
|
2013-12-31 04:47:58 +08:00
|
|
|
default:
|
2014-11-10 22:46:05 +08:00
|
|
|
luaG_opinterror(L, p1, p2, "perform arithmetic on");
|
2013-12-31 04:47:58 +08:00
|
|
|
}
|
2013-04-30 00:56:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-14 02:32:09 +08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-28 02:59:08 +08:00
|
|
|
void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
|
|
|
|
int inv, StkId res, TMS event) {
|
2017-12-14 02:32:09 +08:00
|
|
|
TValue aux;
|
2017-09-28 02:59:08 +08:00
|
|
|
setivalue(&aux, i2);
|
2017-12-14 02:32:09 +08:00
|
|
|
luaT_trybinassocTM(L, p1, &aux, res, inv, event);
|
2017-09-28 02:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-26 00:07:52 +08:00
|
|
|
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
|
|
TMS event) {
|
2017-11-24 03:18:10 +08:00
|
|
|
if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
|
2017-06-29 23:06:44 +08:00
|
|
|
return !l_isfalse(s2v(L->top));
|
2017-11-24 03:18:10 +08:00
|
|
|
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 */
|
2013-04-26 00:07:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-13 21:04:33 +08:00
|
|
|
|
2017-11-28 01:44:31 +08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-09 23:16:06 +08:00
|
|
|
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci) {
|
2017-05-13 21:04:33 +08:00
|
|
|
int i;
|
2018-02-09 23:16:06 +08:00
|
|
|
int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */
|
2018-01-28 21:39:52 +08:00
|
|
|
int nextra = actual - nfixparams; /* number of extra arguments */
|
2018-02-09 23:16:06 +08:00
|
|
|
ci->u.l.nextraargs = nextra;
|
|
|
|
checkstackGC(L, nfixparams + 1);
|
|
|
|
/* copy function and fixed parameters to the top of the stack */
|
|
|
|
for (i = 0; i <= nfixparams; i++) {
|
|
|
|
setobjs2s(L, L->top++, ci->func + i);
|
|
|
|
setnilvalue(s2v(ci->func + i)); /* erase original copy (for GC) */
|
|
|
|
}
|
|
|
|
ci->func += actual + 1;
|
|
|
|
ci->top += actual + 1;
|
2017-05-13 21:04:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-09 23:16:06 +08: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 */
|
|
|
|
checkstackp(L, nextra, where); /* ensure stack space */
|
|
|
|
L->top = where + nextra; /* next instruction will need top */
|
2017-05-13 21:04:33 +08:00
|
|
|
}
|
2018-02-09 23:16:06 +08:00
|
|
|
for (i = 0; i < wanted && i < nextra; i++)
|
|
|
|
setobjs2s(L, where + i, ci->func - nextra + i);
|
|
|
|
for (; i < wanted; i++) /* complete required results with nil */
|
|
|
|
setnilvalue(s2v(where + i));
|
2017-05-13 21:04:33 +08:00
|
|
|
}
|