/src/testdir/build/lua-master/source/ltm.c
Line | Count | Source |
1 | | /* |
2 | | ** $Id: ltm.c $ |
3 | | ** Tag methods |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define ltm_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <string.h> |
14 | | |
15 | | #include "lua.h" |
16 | | |
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" |
26 | | |
27 | | |
28 | | static const char udatatypename[] = "userdata"; |
29 | | |
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 */ |
35 | | }; |
36 | | |
37 | | |
38 | 58.7k | void luaT_init (lua_State *L) { |
39 | 58.7k | static const char *const luaT_eventname[] = { /* ORDER TM */ |
40 | 58.7k | "__index", "__newindex", |
41 | 58.7k | "__gc", "__mode", "__len", "__eq", |
42 | 58.7k | "__add", "__sub", "__mul", "__mod", "__pow", |
43 | 58.7k | "__div", "__idiv", |
44 | 58.7k | "__band", "__bor", "__bxor", "__shl", "__shr", |
45 | 58.7k | "__unm", "__bnot", "__lt", "__le", |
46 | 58.7k | "__concat", "__call", "__close" |
47 | 58.7k | }; |
48 | 58.7k | int i; |
49 | 1.52M | for (i=0; i<TM_N; i++) { |
50 | 1.46M | G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); |
51 | 1.46M | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ |
52 | 1.46M | } |
53 | 58.7k | } |
54 | | |
55 | | |
56 | | /* |
57 | | ** function to be used with macro "fasttm": optimized for absence of |
58 | | ** tag methods |
59 | | */ |
60 | 3.91M | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { |
61 | 3.91M | const TValue *tm = luaH_Hgetshortstr(events, ename); |
62 | 3.91M | lua_assert(event <= TM_EQ); |
63 | 3.91M | if (notm(tm)) { /* no tag method? */ |
64 | 185k | events->flags |= cast_byte(1u<<event); /* cache this fact */ |
65 | 185k | return NULL; |
66 | 185k | } |
67 | 3.72M | else return tm; |
68 | 3.91M | } |
69 | | |
70 | | |
71 | 146M | const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { |
72 | 146M | Table *mt; |
73 | 146M | switch (ttype(o)) { |
74 | 1.09M | case LUA_TTABLE: |
75 | 1.09M | mt = hvalue(o)->metatable; |
76 | 1.09M | break; |
77 | 3.39M | case LUA_TUSERDATA: |
78 | 3.39M | mt = uvalue(o)->metatable; |
79 | 3.39M | break; |
80 | 142M | default: |
81 | 142M | mt = G(L)->mt[ttype(o)]; |
82 | 146M | } |
83 | 146M | return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); |
84 | 146M | } |
85 | | |
86 | | |
87 | | /* |
88 | | ** Return the name of the type of an object. For tables and userdata |
89 | | ** with metatable, use their '__name' metafield, if present. |
90 | | */ |
91 | 427k | const char *luaT_objtypename (lua_State *L, const TValue *o) { |
92 | 427k | Table *mt; |
93 | 427k | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || |
94 | 427k | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { |
95 | 262 | const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name")); |
96 | 262 | if (ttisstring(name)) /* is '__name' a string? */ |
97 | 440 | return getstr(tsvalue(name)); /* use it as type name */ |
98 | 262 | } |
99 | 427k | return ttypename(ttype(o)); /* else use standard type name */ |
100 | 427k | } |
101 | | |
102 | | |
103 | | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, |
104 | 29.0M | const TValue *p2, const TValue *p3) { |
105 | 29.0M | StkId func = L->top.p; |
106 | 29.0M | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ |
107 | 29.0M | setobj2s(L, func + 1, p1); /* 1st argument */ |
108 | 29.0M | setobj2s(L, func + 2, p2); /* 2nd argument */ |
109 | 29.0M | setobj2s(L, func + 3, p3); /* 3rd argument */ |
110 | 29.0M | L->top.p = func + 4; |
111 | | /* metamethod may yield only when called from Lua code */ |
112 | 29.0M | if (isLuacode(L->ci)) |
113 | 29.0M | luaD_call(L, func, 0); |
114 | 0 | else |
115 | 0 | luaD_callnoyield(L, func, 0); |
116 | 29.0M | } |
117 | | |
118 | | |
119 | | lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, |
120 | 75.5M | const TValue *p2, StkId res) { |
121 | 75.5M | ptrdiff_t result = savestack(L, res); |
122 | 75.5M | StkId func = L->top.p; |
123 | 75.5M | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ |
124 | 75.5M | setobj2s(L, func + 1, p1); /* 1st argument */ |
125 | 75.5M | setobj2s(L, func + 2, p2); /* 2nd argument */ |
126 | 75.5M | L->top.p += 3; |
127 | | /* metamethod may yield only when called from Lua code */ |
128 | 75.5M | if (isLuacode(L->ci)) |
129 | 75.4M | luaD_call(L, func, 1); |
130 | 47.1k | else |
131 | 47.1k | luaD_callnoyield(L, func, 1); |
132 | 75.5M | res = restorestack(L, result); |
133 | 75.5M | setobjs2s(L, res, --L->top.p); /* move result to its place */ |
134 | 75.4M | return ttypetag(s2v(res)); /* return tag of the result */ |
135 | 75.4M | } |
136 | | |
137 | | |
138 | | static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, |
139 | 12.1M | StkId res, TMS event) { |
140 | 12.1M | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ |
141 | 12.1M | if (notm(tm)) |
142 | 3.34M | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ |
143 | 12.1M | if (notm(tm)) |
144 | 121k | return -1; /* tag method not found */ |
145 | 12.0M | else /* call tag method and return the tag of the result */ |
146 | 12.0M | return luaT_callTMres(L, tm, p1, p2, res); |
147 | 12.1M | } |
148 | | |
149 | | |
150 | | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, |
151 | 7.23M | StkId res, TMS event) { |
152 | 7.23M | if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) { |
153 | 98.6k | switch (event) { |
154 | 14.9k | case TM_BAND: case TM_BOR: case TM_BXOR: |
155 | 82.6k | case TM_SHL: case TM_SHR: case TM_BNOT: { |
156 | 82.6k | if (ttisnumber(p1) && ttisnumber(p2)) |
157 | 34.3k | luaG_tointerror(L, p1, p2); |
158 | 48.3k | else |
159 | 48.3k | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); |
160 | 82.6k | } |
161 | | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ |
162 | 15.9k | default: |
163 | 15.9k | luaG_opinterror(L, p1, p2, "perform arithmetic on"); |
164 | 98.6k | } |
165 | 98.6k | } |
166 | 7.23M | } |
167 | | |
168 | | |
169 | | /* |
170 | | ** The use of 'p1' after 'callbinTM' is safe because, when a tag |
171 | | ** method is not found, 'callbinTM' cannot change the stack. |
172 | | */ |
173 | 2.18M | void luaT_tryconcatTM (lua_State *L) { |
174 | 2.18M | StkId p1 = L->top.p - 2; /* first argument */ |
175 | 2.18M | if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0)) |
176 | 2.33k | luaG_concaterror(L, s2v(p1), s2v(p1 + 1)); |
177 | 2.18M | } |
178 | | |
179 | | |
180 | | void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, |
181 | 2.39M | int flip, StkId res, TMS event) { |
182 | 2.39M | if (flip) |
183 | 158k | luaT_trybinTM(L, p2, p1, res, event); |
184 | 2.23M | else |
185 | 2.23M | luaT_trybinTM(L, p1, p2, res, event); |
186 | 2.39M | } |
187 | | |
188 | | |
189 | | void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, |
190 | 1.61M | int flip, StkId res, TMS event) { |
191 | 1.61M | TValue aux; |
192 | 1.61M | setivalue(&aux, i2); |
193 | 1.61M | luaT_trybinassocTM(L, p1, &aux, flip, res, event); |
194 | 1.61M | } |
195 | | |
196 | | |
197 | | /* |
198 | | ** Calls an order tag method. |
199 | | */ |
200 | | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, |
201 | 2.77M | TMS event) { |
202 | 2.77M | int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */ |
203 | 2.77M | if (tag >= 0) /* found tag method? */ |
204 | 2.74M | return !tagisfalse(tag); |
205 | 35.3k | luaG_ordererror(L, p1, p2); /* no metamethod found */ |
206 | 0 | return 0; /* to avoid warnings */ |
207 | 2.77M | } |
208 | | |
209 | | |
210 | | int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, |
211 | 1.52M | int flip, int isfloat, TMS event) { |
212 | 1.52M | TValue aux; const TValue *p2; |
213 | 1.52M | if (isfloat) { |
214 | 2.45k | setfltvalue(&aux, cast_num(v2)); |
215 | 2.45k | } |
216 | 1.52M | else |
217 | 1.52M | setivalue(&aux, v2); |
218 | 1.52M | if (flip) { /* arguments were exchanged? */ |
219 | 1.46M | p2 = p1; p1 = &aux; /* correct them */ |
220 | 1.46M | } |
221 | 58.8k | else |
222 | 58.8k | p2 = &aux; |
223 | 1.52M | return luaT_callorderTM(L, p1, p2, event); |
224 | 1.52M | } |
225 | | |
226 | | |
227 | | /* |
228 | | ** Create a vararg table at the top of the stack, with 'n' elements |
229 | | ** starting at 'f'. |
230 | | */ |
231 | 0 | static void createvarargtab (lua_State *L, StkId f, int n) { |
232 | 0 | int i; |
233 | 0 | TValue key, value; |
234 | 0 | Table *t = luaH_new(L); |
235 | 0 | sethvalue(L, s2v(L->top.p), t); |
236 | 0 | L->top.p++; |
237 | 0 | luaH_resize(L, t, cast_uint(n), 1); |
238 | 0 | setsvalue(L, &key, luaS_new(L, "n")); /* key is "n" */ |
239 | 0 | setivalue(&value, n); /* value is n */ |
240 | | /* No need to anchor the key: Due to the resize, the next operation |
241 | | cannot trigger a garbage collection */ |
242 | 0 | luaH_set(L, t, &key, &value); /* t.n = n */ |
243 | 0 | for (i = 0; i < n; i++) |
244 | 0 | luaH_setint(L, t, i + 1, s2v(f + i)); |
245 | 0 | luaC_checkGC(L); |
246 | 0 | } |
247 | | |
248 | | |
249 | | /* |
250 | | ** initial stack: func arg1 ... argn extra1 ... |
251 | | ** ^ ci->func ^ L->top |
252 | | ** final stack: func nil ... nil extra1 ... func arg1 ... argn |
253 | | ** ^ ci->func |
254 | | */ |
255 | | static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p, |
256 | 10.0M | int totalargs, int nfixparams, int nextra) { |
257 | 10.0M | int i; |
258 | 10.0M | ci->u.l.nextraargs = nextra; |
259 | 10.0M | luaD_checkstack(L, p->maxstacksize + 1); |
260 | | /* copy function to the top of the stack, after extra arguments */ |
261 | 10.0M | setobjs2s(L, L->top.p++, ci->func.p); |
262 | | /* move fixed parameters to after the copied function */ |
263 | 27.2M | for (i = 1; i <= nfixparams; i++) { |
264 | 17.1M | setobjs2s(L, L->top.p++, ci->func.p + i); |
265 | 17.1M | setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ |
266 | 17.1M | } |
267 | 10.0M | ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */ |
268 | 10.0M | ci->top.p += totalargs + 1; |
269 | 10.0M | } |
270 | | |
271 | | |
272 | 10.0M | void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { |
273 | 10.0M | int totalargs = cast_int(L->top.p - ci->func.p) - 1; |
274 | 10.0M | int nfixparams = p->numparams; |
275 | 10.0M | int nextra = totalargs - nfixparams; /* number of extra arguments */ |
276 | 10.0M | if (p->flag & PF_VATAB) { /* does it need a vararg table? */ |
277 | 0 | lua_assert(!(p->flag & PF_VAHID)); |
278 | 0 | createvarargtab(L, ci->func.p + nfixparams + 1, nextra); |
279 | | /* move table to proper place (last parameter) */ |
280 | 0 | setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1); |
281 | 0 | } |
282 | 10.0M | else { /* no table */ |
283 | 10.0M | lua_assert(p->flag & PF_VAHID); |
284 | 10.0M | buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra); |
285 | | /* set vararg parameter to nil */ |
286 | 10.0M | setnilvalue(s2v(ci->func.p + nfixparams + 1)); |
287 | 10.0M | lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); |
288 | 10.0M | } |
289 | 10.0M | } |
290 | | |
291 | | |
292 | 0 | void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) { |
293 | 0 | int nextra = ci->u.l.nextraargs; |
294 | 0 | lua_Integer n; |
295 | 0 | if (tointegerns(rc, &n)) { /* integral value? */ |
296 | 0 | if (l_castS2U(n) - 1 < cast_uint(nextra)) { |
297 | 0 | StkId slot = ci->func.p - nextra + cast_int(n) - 1; |
298 | 0 | setobjs2s(((lua_State*)NULL), ra, slot); |
299 | 0 | return; |
300 | 0 | } |
301 | 0 | } |
302 | 0 | else if (ttisstring(rc)) { /* string value? */ |
303 | 0 | size_t len; |
304 | 0 | const char *s = getlstr(tsvalue(rc), len); |
305 | 0 | if (len == 1 && s[0] == 'n') { /* key is "n"? */ |
306 | 0 | setivalue(s2v(ra), nextra); |
307 | 0 | return; |
308 | 0 | } |
309 | 0 | } |
310 | 0 | setnilvalue(s2v(ra)); /* else produce nil */ |
311 | 0 | } |
312 | | |
313 | | |
314 | | /* |
315 | | ** Get the number of extra arguments in a vararg function. If vararg |
316 | | ** table has been optimized away, that number is in the call info. |
317 | | ** Otherwise, get the field 'n' from the vararg table and check that it |
318 | | ** has a proper value (non-negative integer not larger than the stack |
319 | | ** limit). |
320 | | */ |
321 | 1.92M | static int getnumargs (lua_State *L, CallInfo *ci, Table *h) { |
322 | 1.92M | if (h == NULL) /* no vararg table? */ |
323 | 1.92M | return ci->u.l.nextraargs; |
324 | 0 | else { |
325 | 0 | TValue res; |
326 | 0 | if (luaH_getshortstr(h, luaS_new(L, "n"), &res) != LUA_VNUMINT || |
327 | 0 | l_castS2U(ivalue(&res)) > cast_uint(INT_MAX/2)) |
328 | 0 | luaG_runerror(L, "vararg table has no proper 'n'"); |
329 | 0 | return cast_int(ivalue(&res)); |
330 | 0 | } |
331 | 1.92M | } |
332 | | |
333 | | |
334 | | /* |
335 | | ** Get 'wanted' vararg arguments and put them in 'where'. 'vatab' is |
336 | | ** the register of the vararg table or -1 if there is no vararg table. |
337 | | */ |
338 | | void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted, |
339 | 1.92M | int vatab) { |
340 | 1.92M | Table *h = (vatab < 0) ? NULL : hvalue(s2v(ci->func.p + vatab + 1)); |
341 | 1.92M | int nargs = getnumargs(L, ci, h); /* number of available vararg args. */ |
342 | 1.92M | int i, touse; /* 'touse' is minimum between 'wanted' and 'nargs' */ |
343 | 1.92M | if (wanted < 0) { |
344 | 566k | touse = wanted = nargs; /* get all extra arguments available */ |
345 | 566k | checkstackp(L, nargs, where); /* ensure stack space */ |
346 | 566k | L->top.p = where + nargs; /* next instruction will need top */ |
347 | 566k | } |
348 | 1.35M | else |
349 | 1.35M | touse = (nargs > wanted) ? wanted : nargs; |
350 | 1.92M | if (h == NULL) { /* no vararg table? */ |
351 | 93.4M | for (i = 0; i < touse; i++) /* get vararg values from the stack */ |
352 | 91.5M | setobjs2s(L, where + i, ci->func.p - nargs + i); |
353 | 1.92M | } |
354 | 60 | else { /* get vararg values from vararg table */ |
355 | 60 | for (i = 0; i < touse; i++) { |
356 | 0 | lu_byte tag = luaH_getint(h, i + 1, s2v(where + i)); |
357 | 0 | if (tagisempty(tag)) |
358 | 0 | setnilvalue(s2v(where + i)); |
359 | 0 | } |
360 | 60 | } |
361 | 2.81M | for (; i < wanted; i++) /* complete required results with nil */ |
362 | 1.92M | setnilvalue(s2v(where + i)); |
363 | 1.92M | } |
364 | | |