Merge pull request #2672 from kitsunehunter/laundry-keys
[RRG-proxmark3.git] / client / deps / liblua / ltm.c
blobf459add61b8b42a4ba3646a294e6d9f53cd67484
1 /*
2 ** $Id: ltm.c $
3 ** Tag methods
4 ** See Copyright Notice in lua.h
5 */
7 #define ltm_c
8 #define LUA_CORE
10 #include "lprefix.h"
13 #include <string.h>
15 #include "lua.h"
17 #include "ldebug.h"
18 #include "ldo.h"
19 #include "lgc.h"
20 #include "lobject.h"
21 #include "lstate.h"
22 #include "lstring.h"
23 #include "ltable.h"
24 #include "ltm.h"
25 #include "lvm.h"
28 static const char udatatypename[] = "userdata";
30 LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
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 */
38 void luaT_init(lua_State *L) {
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"
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 */
57 ** function to be used with macro "fasttm": optimized for absence of
58 ** tag methods
60 const TValue *luaT_gettm(Table *events, TMS event, TString *ename) {
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 } else return tm;
70 const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event) {
71 Table *mt;
72 switch (ttype(o)) {
73 case LUA_TTABLE:
74 mt = hvalue(o)->metatable;
75 break;
76 case LUA_TUSERDATA:
77 mt = uvalue(o)->metatable;
78 break;
79 default:
80 mt = G(L)->mt[ttype(o)];
82 return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
87 ** Return the name of the type of an object. For tables and userdata
88 ** with metatable, use their '__name' metafield, if present.
90 const char *luaT_objtypename(lua_State *L, const TValue *o) {
91 Table *mt;
92 if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
93 (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
94 const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
95 if (ttisstring(name)) /* is '__name' a string? */
96 return getstr(tsvalue(name)); /* use it as type name */
98 return ttypename(ttype(o)); /* else use standard type name */
102 void luaT_callTM(lua_State *L, const TValue *f, const TValue *p1,
103 const TValue *p2, const TValue *p3) {
104 StkId func = L->top.p;
105 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
106 setobj2s(L, func + 1, p1); /* 1st argument */
107 setobj2s(L, func + 2, p2); /* 2nd argument */
108 setobj2s(L, func + 3, p3); /* 3rd argument */
109 L->top.p = func + 4;
110 /* metamethod may yield only when called from Lua code */
111 if (isLuacode(L->ci))
112 luaD_call(L, func, 0);
113 else
114 luaD_callnoyield(L, func, 0);
118 void luaT_callTMres(lua_State *L, const TValue *f, const TValue *p1,
119 const TValue *p2, StkId res) {
120 ptrdiff_t result = savestack(L, res);
121 StkId func = L->top.p;
122 setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
123 setobj2s(L, func + 1, p1); /* 1st argument */
124 setobj2s(L, func + 2, p2); /* 2nd argument */
125 L->top.p += 3;
126 /* metamethod may yield only when called from Lua code */
127 if (isLuacode(L->ci))
128 luaD_call(L, func, 1);
129 else
130 luaD_callnoyield(L, func, 1);
131 res = restorestack(L, result);
132 setobjs2s(L, res, --L->top.p); /* move result to its place */
136 static int callbinTM(lua_State *L, const TValue *p1, const TValue *p2,
137 StkId res, TMS event) {
138 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
139 if (notm(tm))
140 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
141 if (notm(tm)) return 0;
142 luaT_callTMres(L, tm, p1, p2, res);
143 return 1;
147 void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2,
148 StkId res, TMS event) {
149 if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
150 switch (event) {
151 case TM_BAND:
152 case TM_BOR:
153 case TM_BXOR:
154 case TM_SHL:
155 case TM_SHR:
156 case TM_BNOT: {
157 if (ttisnumber(p1) && ttisnumber(p2))
158 luaG_tointerror(L, p1, p2);
159 else
160 luaG_opinterror(L, p1, p2, "perform bitwise operation on");
162 /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
163 default:
164 luaG_opinterror(L, p1, p2, "perform arithmetic on");
170 void luaT_tryconcatTM(lua_State *L) {
171 StkId top = L->top.p;
172 if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2,
173 TM_CONCAT)))
174 luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
178 void luaT_trybinassocTM(lua_State *L, const TValue *p1, const TValue *p2,
179 int flip, StkId res, TMS event) {
180 if (flip)
181 luaT_trybinTM(L, p2, p1, res, event);
182 else
183 luaT_trybinTM(L, p1, p2, res, event);
187 void luaT_trybiniTM(lua_State *L, const TValue *p1, lua_Integer i2,
188 int flip, StkId res, TMS event) {
189 TValue aux;
190 setivalue(&aux, i2);
191 luaT_trybinassocTM(L, p1, &aux, flip, res, event);
196 ** Calls an order tag method.
197 ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
198 ** behavior: if there is no '__le', try '__lt', based on l <= r iff
199 ** !(r < l) (assuming a total order). If the metamethod yields during
200 ** this substitution, the continuation has to know about it (to negate
201 ** the result of r<l); bit CIST_LEQ in the call status keeps that
202 ** information.
204 int luaT_callorderTM(lua_State *L, const TValue *p1, const TValue *p2,
205 TMS event) {
206 if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */
207 return !l_isfalse(s2v(L->top.p));
208 #if defined(LUA_COMPAT_LT_LE)
209 else if (event == TM_LE) {
210 /* try '!(p2 < p1)' for '(p1 <= p2)' */
211 L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
212 if (callbinTM(L, p2, p1, L->top.p, TM_LT)) {
213 L->ci->callstatus ^= CIST_LEQ; /* clear mark */
214 return l_isfalse(s2v(L->top.p));
216 /* else error will remove this 'ci'; no need to clear mark */
218 #endif
219 luaG_ordererror(L, p1, p2); /* no metamethod found */
220 return 0; /* to avoid warnings */
224 int luaT_callorderiTM(lua_State *L, const TValue *p1, int v2,
225 int flip, int isfloat, TMS event) {
226 TValue aux;
227 const TValue *p2;
228 if (isfloat) {
229 setfltvalue(&aux, cast_num(v2));
230 } else
231 setivalue(&aux, v2);
232 if (flip) { /* arguments were exchanged? */
233 p2 = p1;
234 p1 = &aux; /* correct them */
235 } else
236 p2 = &aux;
237 return luaT_callorderTM(L, p1, p2, event);
241 void luaT_adjustvarargs(lua_State *L, int nfixparams, CallInfo *ci,
242 const Proto *p) {
243 int i;
244 int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
245 int nextra = actual - nfixparams; /* number of extra arguments */
246 ci->u.l.nextraargs = nextra;
247 luaD_checkstack(L, p->maxstacksize + 1);
248 /* copy function to the top of the stack */
249 setobjs2s(L, L->top.p++, ci->func.p);
250 /* move fixed parameters to the top of the stack */
251 for (i = 1; i <= nfixparams; i++) {
252 setobjs2s(L, L->top.p++, ci->func.p + i);
253 setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
255 ci->func.p += actual + 1;
256 ci->top.p += actual + 1;
257 lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
261 void luaT_getvarargs(lua_State *L, CallInfo *ci, StkId where, int wanted) {
262 int i;
263 int nextra = ci->u.l.nextraargs;
264 if (wanted < 0) {
265 wanted = nextra; /* get all extra arguments available */
266 checkstackGCp(L, nextra, where); /* ensure stack space */
267 L->top.p = where + nextra; /* next instruction will need top */
269 for (i = 0; i < wanted && i < nextra; i++)
270 setobjs2s(L, where + i, ci->func.p - nextra + i);
271 for (; i < wanted; i++) /* complete required results with nil */
272 setnilvalue(s2v(where + i));