Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
ltm.c File Reference
#include "lprefix.h"
#include <string.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lvm.h"

Go to the source code of this file.

Macros

#define ltm_c
 
#define LUA_CORE
 

Functions

void luaT_init (lua_State *L)
 
const TValueluaT_gettm (Table *events, TMS event, TString *ename)
 
const TValueluaT_gettmbyobj (lua_State *L, const TValue *o, TMS event)
 
const char * luaT_objtypename (lua_State *L, const TValue *o)
 
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3)
 
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId res)
 
static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event)
 
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event)
 
void luaT_tryconcatTM (lua_State *L)
 
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, int flip, StkId res, TMS event)
 
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int flip, StkId res, TMS event)
 
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event)
 
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int flip, int isfloat, TMS event)
 
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, const Proto *p)
 
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted)
 

Variables

static const char udatatypename [] = "userdata"
 
LUAI_DDEF const char *const luaT_typenames_ [LUA_TOTALTYPES]
 

Macro Definition Documentation

◆ ltm_c

#define ltm_c

Definition at line 7 of file lua-5.4.3/src/ltm.c.

◆ LUA_CORE

#define LUA_CORE

Definition at line 8 of file lua-5.4.3/src/ltm.c.

Function Documentation

◆ callbinTM()

static int callbinTM ( lua_State * L,
const TValue * p1,
const TValue * p2,
StkId res,
TMS event )
static

Definition at line 137 of file lua-5.4.3/src/ltm.c.

138 {
139 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
140 if (notm(tm))
141 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
142 if (notm(tm)) return 0;
143 luaT_callTMres(L, tm, p1, p2, res);
144 return 1;
145}
void luaT_callTMres(lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId res)
const TValue * luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event)
#define notm(tm)

References luaT_callTMres(), luaT_gettmbyobj(), and notm.

Referenced by luaT_callorderTM(), luaT_trybinTM(), and luaT_tryconcatTM().

◆ luaT_adjustvarargs()

void luaT_adjustvarargs ( lua_State * L,
int nfixparams,
CallInfo * ci,
const Proto * p )

Definition at line 238 of file lua-5.4.3/src/ltm.c.

239 {
240 int i;
241 int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */
242 int nextra = actual - nfixparams; /* number of extra arguments */
243 ci->u.l.nextraargs = nextra;
244 luaD_checkstack(L, p->maxstacksize + 1);
245 /* copy function to the top of the stack */
246 setobjs2s(L, L->top++, ci->func);
247 /* move fixed parameters to the top of the stack */
248 for (i = 1; i <= nfixparams; i++) {
249 setobjs2s(L, L->top++, ci->func + i);
250 setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */
251 }
252 ci->func += actual + 1;
253 ci->top += actual + 1;
254 lua_assert(L->top <= ci->top && ci->top <= L->stack_last);
255}
#define luaD_checkstack(L, n)
#define cast_int(i)
#define lua_assert(c)
#define setobjs2s
#define setnilvalue(obj)
#define s2v(o)
struct CallInfo::@64::@65 l
union CallInfo::@64 u
lu_byte maxstacksize

References cast_int, CallInfo::func, CallInfo::l, lua_assert, luaD_checkstack, Proto::maxstacksize, CallInfo::nextraargs, s2v, setnilvalue, setobjs2s, lua_State::stack_last, CallInfo::top, lua_State::top, and CallInfo::u.

Referenced by luaV_execute().

◆ luaT_callorderiTM()

int luaT_callorderiTM ( lua_State * L,
const TValue * p1,
int v2,
int flip,
int isfloat,
TMS event )

Definition at line 221 of file lua-5.4.3/src/ltm.c.

222 {
223 TValue aux; const TValue *p2;
224 if (isfloat) {
225 setfltvalue(&aux, cast_num(v2));
226 }
227 else
228 setivalue(&aux, v2);
229 if (flip) { /* arguments were exchanged? */
230 p2 = p1; p1 = &aux; /* correct them */
231 }
232 else
233 p2 = &aux;
234 return luaT_callorderTM(L, p1, p2, event);
235}
#define cast_num(i)
#define setfltvalue(obj, x)
#define setivalue(obj, x)
int luaT_callorderTM(lua_State *L, const TValue *p1, const TValue *p2, TMS event)

References cast_num, luaT_callorderTM(), setfltvalue, and setivalue.

◆ luaT_callorderTM()

int luaT_callorderTM ( lua_State * L,
const TValue * p1,
const TValue * p2,
TMS event )

Definition at line 201 of file lua-5.4.3/src/ltm.c.

202 {
203 if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
204 return !l_isfalse(s2v(L->top));
205#if defined(LUA_COMPAT_LT_LE)
206 else if (event == TM_LE) {
207 /* try '!(p2 < p1)' for '(p1 <= p2)' */
208 L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
209 if (callbinTM(L, p2, p1, L->top, TM_LT)) {
210 L->ci->callstatus ^= CIST_LEQ; /* clear mark */
211 return l_isfalse(s2v(L->top));
212 }
213 /* else error will remove this 'ci'; no need to clear mark */
214 }
215#endif
216 luaG_ordererror(L, p1, p2); /* no metamethod found */
217 return 0; /* to avoid warnings */
218}
int luaG_ordererror(lua_State *L, const TValue *p1, const TValue *p2)
#define l_isfalse(o)
@ TM_LE
@ TM_LT
#define CIST_LEQ
static int callbinTM(lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event)

References callbinTM(), CallInfo::callstatus, lua_State::ci, CIST_LEQ, l_isfalse, luaG_ordererror(), s2v, TM_LE, TM_LT, and lua_State::top.

Referenced by luaT_callorderiTM().

◆ luaT_callTM()

void luaT_callTM ( lua_State * L,
const TValue * f,
const TValue * p1,
const TValue * p2,
const TValue * p3 )

Definition at line 103 of file lua-5.4.3/src/ltm.c.

104 {
105 StkId func = L->top;
106 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
107 setobj2s(L, func + 1, p1); /* 1st argument */
108 setobj2s(L, func + 2, p2); /* 2nd argument */
109 setobj2s(L, func + 3, p3); /* 3rd argument */
110 L->top = func + 4;
111 /* metamethod may yield only when called from Lua code */
112 if (isLuacode(L->ci))
113 luaD_call(L, func, 0);
114 else
115 luaD_callnoyield(L, func, 0);
116}
void luaD_call(lua_State *L, StkId func, int nResults)
#define setobj2s
void luaD_callnoyield(lua_State *L, StkId func, int nResults)
#define isLuacode(ci)

References lua_State::ci, isLuacode, luaD_call(), luaD_callnoyield(), setobj2s, and lua_State::top.

◆ luaT_callTMres()

void luaT_callTMres ( lua_State * L,
const TValue * f,
const TValue * p1,
const TValue * p2,
StkId res )

Definition at line 119 of file lua-5.4.3/src/ltm.c.

120 {
121 ptrdiff_t result = savestack(L, res);
122 StkId func = L->top;
123 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
124 setobj2s(L, func + 1, p1); /* 1st argument */
125 setobj2s(L, func + 2, p2); /* 2nd argument */
126 L->top += 3;
127 /* metamethod may yield only when called from Lua code */
128 if (isLuacode(L->ci))
129 luaD_call(L, func, 1);
130 else
131 luaD_callnoyield(L, func, 1);
132 res = restorestack(L, result);
133 setobjs2s(L, res, --L->top); /* move result to its place */
134}
#define savestack(L, p)
#define restorestack(L, n)

References lua_State::ci, isLuacode, luaD_call(), luaD_callnoyield(), restorestack, savestack, setobj2s, setobjs2s, and lua_State::top.

Referenced by callbinTM(), luaV_equalobj(), luaV_finishget(), and luaV_objlen().

◆ luaT_gettm()

const TValue * luaT_gettm ( Table * events,
TMS event,
TString * ename )

Definition at line 60 of file lua-5.4.3/src/ltm.c.

60 {
61 const TValue *tm = luaH_getshortstr(events, ename);
62 lua_assert(event <= TM_EQ);
63 if (notm(tm)) { /* no tag method? */
64 events->flags |= cast_byte(1u<<event); /* cache this fact */
65 return NULL;
66 }
67 else return tm;
68}
#define NULL
Definition gmacros.h:924
#define cast_byte(i)
@ TM_EQ
const TValue * luaH_getshortstr(Table *t, TString *key)

References cast_byte, Table::flags, lua_assert, luaH_getshortstr(), notm, NULL, and TM_EQ.

◆ luaT_gettmbyobj()

const TValue * luaT_gettmbyobj ( lua_State * L,
const TValue * o,
TMS event )

Definition at line 71 of file lua-5.4.3/src/ltm.c.

71 {
72 Table *mt;
73 switch (ttype(o)) {
74 case LUA_TTABLE:
75 mt = hvalue(o)->metatable;
76 break;
77 case LUA_TUSERDATA:
78 mt = uvalue(o)->metatable;
79 break;
80 default:
81 mt = G(L)->mt[ttype(o)];
82 }
83 return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
84}
#define uvalue(o)
#define hvalue(o)
#define ttype(o)
#define G(L)
#define LUA_TTABLE
#define LUA_TUSERDATA
static TString ** tmname
struct Table * metatable

References G, hvalue, LUA_TTABLE, LUA_TUSERDATA, luaH_getshortstr(), Table::metatable, tmname, ttype, and uvalue.

Referenced by callbinTM().

◆ luaT_getvarargs()

void luaT_getvarargs ( lua_State * L,
CallInfo * ci,
StkId where,
int wanted )

Definition at line 258 of file lua-5.4.3/src/ltm.c.

258 {
259 int i;
260 int nextra = ci->u.l.nextraargs;
261 if (wanted < 0) {
262 wanted = nextra; /* get all extra arguments available */
263 checkstackGCp(L, nextra, where); /* ensure stack space */
264 L->top = where + nextra; /* next instruction will need top */
265 }
266 for (i = 0; i < wanted && i < nextra; i++)
267 setobjs2s(L, where + i, ci->func - nextra + i);
268 for (; i < wanted; i++) /* complete required results with nil */
269 setnilvalue(s2v(where + i));
270}
#define checkstackGCp(L, n, p)

References checkstackGCp, CallInfo::func, CallInfo::l, CallInfo::nextraargs, s2v, setnilvalue, setobjs2s, lua_State::top, and CallInfo::u.

Referenced by luaV_execute().

◆ luaT_init()

void luaT_init ( lua_State * L)

Definition at line 38 of file lua-5.4.3/src/ltm.c.

38 {
39 static const char *const luaT_eventname[] = { /* ORDER TM */
40 "__index", "__newindex",
41 "__gc", "__mode", "__len", "__eq",
42 "__add", "__sub", "__mul", "__mod", "__pow",
43 "__div", "__idiv",
44 "__band", "__bor", "__bxor", "__shl", "__shr",
45 "__unm", "__bnot", "__lt", "__le",
46 "__concat", "__call", "__close"
47 };
48 int i;
49 for (i=0; i<TM_N; i++) {
50 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
51 luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
52 }
53}
#define obj2gco(v)
#define luaS_new(L, s)
void luaC_fix(lua_State *L, GCObject *o)

References G, luaC_fix(), luaS_new, obj2gco, TM_N, and tmname.

◆ luaT_objtypename()

const char * luaT_objtypename ( lua_State * L,
const TValue * o )

Definition at line 91 of file lua-5.4.3/src/ltm.c.

91 {
92 Table *mt;
93 if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
94 (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
95 const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
96 if (ttisstring(name)) /* is '__name' a string? */
97 return getstr(tsvalue(name)); /* use it as type name */
98 }
99 return ttypename(ttype(o)); /* else use standard type name */
100}
const char * name
Definition lsqlite3.c:2154
#define tsvalue(o)
#define ttisstring(o)
#define ttistable(o)
#define getstr(ts)
#define ttypename(x)
#define ttisfulluserdata(o)

References getstr, hvalue, luaH_getshortstr(), luaS_new, name, NULL, tsvalue, ttisfulluserdata, ttisstring, ttistable, ttype, ttypename, and uvalue.

◆ luaT_trybinassocTM()

void luaT_trybinassocTM ( lua_State * L,
const TValue * p1,
const TValue * p2,
int flip,
StkId res,
TMS event )

Definition at line 175 of file lua-5.4.3/src/ltm.c.

176 {
177 if (flip)
178 luaT_trybinTM(L, p2, p1, res, event);
179 else
180 luaT_trybinTM(L, p1, p2, res, event);
181}
void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event)

References luaT_trybinTM().

Referenced by luaT_trybiniTM(), and luaV_execute().

◆ luaT_trybiniTM()

void luaT_trybiniTM ( lua_State * L,
const TValue * p1,
lua_Integer i2,
int flip,
StkId res,
TMS event )

Definition at line 184 of file lua-5.4.3/src/ltm.c.

185 {
186 TValue aux;
187 setivalue(&aux, i2);
188 luaT_trybinassocTM(L, p1, &aux, flip, res, event);
189}
void luaT_trybinassocTM(lua_State *L, const TValue *p1, const TValue *p2, int flip, StkId res, TMS event)

References luaT_trybinassocTM(), and setivalue.

Referenced by luaV_execute().

◆ luaT_trybinTM()

void luaT_trybinTM ( lua_State * L,
const TValue * p1,
const TValue * p2,
StkId res,
TMS event )

Definition at line 148 of file lua-5.4.3/src/ltm.c.

149 {
150 if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
151 switch (event) {
152 case TM_BAND: case TM_BOR: case TM_BXOR:
153 case TM_SHL: case TM_SHR: case TM_BNOT: {
154 if (ttisnumber(p1) && ttisnumber(p2))
155 luaG_tointerror(L, p1, p2);
156 else
157 luaG_opinterror(L, p1, p2, "perform bitwise operation on");
158 }
159 /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
160 default:
161 luaG_opinterror(L, p1, p2, "perform arithmetic on");
162 }
163 }
164}
#define ttisnumber(o)
l_noret luaG_opinterror(lua_State *L, const TValue *p1, const TValue *p2, const char *msg)
l_noret luaG_tointerror(lua_State *L, const TValue *p1, const TValue *p2)
@ TM_BAND
@ TM_BNOT
@ TM_BXOR
@ TM_SHR
@ TM_BOR
@ TM_SHL

References callbinTM(), luaG_opinterror(), luaG_tointerror(), TM_BAND, TM_BNOT, TM_BOR, TM_BXOR, TM_SHL, TM_SHR, and ttisnumber.

Referenced by luaT_trybinassocTM().

◆ luaT_tryconcatTM()

void luaT_tryconcatTM ( lua_State * L)

Definition at line 167 of file lua-5.4.3/src/ltm.c.

167 {
168 StkId top = L->top;
169 if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2,
170 TM_CONCAT)))
171 luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
172}
void luaG_concaterror(lua_State *L, StkId p1, StkId p2)
@ TM_CONCAT

References callbinTM(), luaG_concaterror(), s2v, TM_CONCAT, and lua_State::top.

Referenced by luaV_concat().

Variable Documentation

◆ luaT_typenames_

LUAI_DDEF const char* const luaT_typenames_[LUA_TOTALTYPES]
Initial value:
= {
"no value",
"nil", "boolean", udatatypename, "number",
"string", "table", "function", udatatypename, "thread",
"upvalue", "proto"
}
static const char udatatypename[]

Definition at line 30 of file lua-5.4.3/src/ltm.c.

30 {
31 "no value",
32 "nil", "boolean", udatatypename, "number",
33 "string", "table", "function", udatatypename, "thread",
34 "upvalue", "proto" /* these last cases are used for tests only */
35};

◆ udatatypename

const char udatatypename[] = "userdata"
static

Definition at line 28 of file lua-5.4.3/src/ltm.c.