/src/testdir/build/lua-master/source/ltm.c
Line | Count | Source (jump to first uncovered line) |
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 | 15.8k | void luaT_init (lua_State *L) { |
39 | 15.8k | static const char *const luaT_eventname[] = { /* ORDER TM */ |
40 | 15.8k | "__index", "__newindex", |
41 | 15.8k | "__gc", "__mode", "__len", "__eq", |
42 | 15.8k | "__add", "__sub", "__mul", "__mod", "__pow", |
43 | 15.8k | "__div", "__idiv", |
44 | 15.8k | "__band", "__bor", "__bxor", "__shl", "__shr", |
45 | 15.8k | "__unm", "__bnot", "__lt", "__le", |
46 | 15.8k | "__concat", "__call", "__close" |
47 | 15.8k | }; |
48 | 15.8k | int i; |
49 | 411k | for (i=0; i<TM_N; i++) { |
50 | 395k | G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); |
51 | 395k | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ |
52 | 395k | } |
53 | 15.8k | } |
54 | | |
55 | | |
56 | | /* |
57 | | ** function to be used with macro "fasttm": optimized for absence of |
58 | | ** tag methods |
59 | | */ |
60 | 69.7k | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { |
61 | 69.7k | const TValue *tm = luaH_getshortstr(events, ename); |
62 | 69.7k | lua_assert(event <= TM_EQ); |
63 | 69.7k | if (notm(tm)) { /* no tag method? */ |
64 | 6.44k | events->flags |= cast_byte(1u<<event); /* cache this fact */ |
65 | 6.44k | return NULL; |
66 | 6.44k | } |
67 | 63.3k | else return tm; |
68 | 69.7k | } |
69 | | |
70 | | |
71 | 1.29M | const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { |
72 | 1.29M | Table *mt; |
73 | 1.29M | switch (ttype(o)) { |
74 | 3.22k | case LUA_TTABLE: |
75 | 3.22k | mt = hvalue(o)->metatable; |
76 | 3.22k | break; |
77 | 161k | case LUA_TUSERDATA: |
78 | 161k | mt = uvalue(o)->metatable; |
79 | 161k | break; |
80 | 1.13M | default: |
81 | 1.13M | mt = G(L)->mt[ttype(o)]; |
82 | 1.29M | } |
83 | 1.29M | return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); |
84 | 1.29M | } |
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 | 6.64k | const char *luaT_objtypename (lua_State *L, const TValue *o) { |
92 | 6.64k | Table *mt; |
93 | 6.64k | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || |
94 | 6.64k | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { |
95 | 0 | const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); |
96 | 0 | if (ttisstring(name)) /* is '__name' a string? */ |
97 | 0 | return getstr(tsvalue(name)); /* use it as type name */ |
98 | 0 | } |
99 | 6.64k | return ttypename(ttype(o)); /* else use standard type name */ |
100 | 6.64k | } |
101 | | |
102 | | |
103 | | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, |
104 | 0 | const TValue *p2, const TValue *p3) { |
105 | 0 | StkId func = L->top.p; |
106 | 0 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ |
107 | 0 | setobj2s(L, func + 1, p1); /* 1st argument */ |
108 | 0 | setobj2s(L, func + 2, p2); /* 2nd argument */ |
109 | 0 | setobj2s(L, func + 3, p3); /* 3rd argument */ |
110 | 0 | L->top.p = func + 4; |
111 | | /* metamethod may yield only when called from Lua code */ |
112 | 0 | if (isLuacode(L->ci)) |
113 | 0 | luaD_call(L, func, 0); |
114 | 0 | else |
115 | 0 | luaD_callnoyield(L, func, 0); |
116 | 0 | } |
117 | | |
118 | | |
119 | | void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, |
120 | 1.02M | const TValue *p2, StkId res) { |
121 | 1.02M | ptrdiff_t result = savestack(L, res); |
122 | 1.02M | StkId func = L->top.p; |
123 | 1.02M | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ |
124 | 1.02M | setobj2s(L, func + 1, p1); /* 1st argument */ |
125 | 1.02M | setobj2s(L, func + 2, p2); /* 2nd argument */ |
126 | 1.02M | L->top.p += 3; |
127 | | /* metamethod may yield only when called from Lua code */ |
128 | 1.02M | if (isLuacode(L->ci)) |
129 | 1.02M | luaD_call(L, func, 1); |
130 | 0 | else |
131 | 0 | luaD_callnoyield(L, func, 1); |
132 | 1.02M | res = restorestack(L, result); |
133 | 1.02M | setobjs2s(L, res, --L->top.p); /* move result to its place */ |
134 | 1.02M | } |
135 | | |
136 | | |
137 | | static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, |
138 | 1.02M | StkId res, TMS event) { |
139 | 1.02M | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ |
140 | 1.02M | if (notm(tm)) |
141 | 15.8k | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ |
142 | 1.02M | if (notm(tm)) return 0; |
143 | 1.02M | luaT_callTMres(L, tm, p1, p2, res); |
144 | 1.02M | return 1; |
145 | 1.02M | } |
146 | | |
147 | | |
148 | | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, |
149 | 1.02M | StkId res, TMS event) { |
150 | 1.02M | if (l_unlikely(!callbinTM(L, p1, p2, res, event))) { |
151 | 225 | switch (event) { |
152 | 38 | case TM_BAND: case TM_BOR: case TM_BXOR: |
153 | 77 | case TM_SHL: case TM_SHR: case TM_BNOT: { |
154 | 77 | if (ttisnumber(p1) && ttisnumber(p2)) |
155 | 19 | luaG_tointerror(L, p1, p2); |
156 | 58 | else |
157 | 58 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); |
158 | 77 | } |
159 | | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ |
160 | 148 | default: |
161 | 148 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); |
162 | 225 | } |
163 | 225 | } |
164 | 1.02M | } |
165 | | |
166 | | |
167 | 10 | void luaT_tryconcatTM (lua_State *L) { |
168 | 10 | StkId top = L->top.p; |
169 | 10 | if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, |
170 | 10 | TM_CONCAT))) |
171 | 10 | luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); |
172 | 10 | } |
173 | | |
174 | | |
175 | | void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, |
176 | 786k | int flip, StkId res, TMS event) { |
177 | 786k | if (flip) |
178 | 1.78k | luaT_trybinTM(L, p2, p1, res, event); |
179 | 785k | else |
180 | 785k | luaT_trybinTM(L, p1, p2, res, event); |
181 | 786k | } |
182 | | |
183 | | |
184 | | void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, |
185 | 604 | int flip, StkId res, TMS event) { |
186 | 604 | TValue aux; |
187 | 604 | setivalue(&aux, i2); |
188 | 604 | luaT_trybinassocTM(L, p1, &aux, flip, res, event); |
189 | 604 | } |
190 | | |
191 | | |
192 | | /* |
193 | | ** Calls an order tag method. |
194 | | ** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old |
195 | | ** behavior: if there is no '__le', try '__lt', based on l <= r iff |
196 | | ** !(r < l) (assuming a total order). If the metamethod yields during |
197 | | ** this substitution, the continuation has to know about it (to negate |
198 | | ** the result of r<l); bit CIST_LEQ in the call status keeps that |
199 | | ** information. |
200 | | */ |
201 | | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, |
202 | 56 | TMS event) { |
203 | 56 | if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */ |
204 | 0 | return !l_isfalse(s2v(L->top.p)); |
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.p, TM_LT)) { |
210 | | L->ci->callstatus ^= CIST_LEQ; /* clear mark */ |
211 | | return l_isfalse(s2v(L->top.p)); |
212 | | } |
213 | | /* else error will remove this 'ci'; no need to clear mark */ |
214 | | } |
215 | | #endif |
216 | 56 | luaG_ordererror(L, p1, p2); /* no metamethod found */ |
217 | 0 | return 0; /* to avoid warnings */ |
218 | 56 | } |
219 | | |
220 | | |
221 | | int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, |
222 | 15 | int flip, int isfloat, TMS event) { |
223 | 15 | TValue aux; const TValue *p2; |
224 | 15 | if (isfloat) { |
225 | 6 | setfltvalue(&aux, cast_num(v2)); |
226 | 6 | } |
227 | 9 | else |
228 | 9 | setivalue(&aux, v2); |
229 | 15 | if (flip) { /* arguments were exchanged? */ |
230 | 5 | p2 = p1; p1 = &aux; /* correct them */ |
231 | 5 | } |
232 | 10 | else |
233 | 10 | p2 = &aux; |
234 | 15 | return luaT_callorderTM(L, p1, p2, event); |
235 | 15 | } |
236 | | |
237 | | |
238 | | void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, |
239 | 17.6k | const Proto *p) { |
240 | 17.6k | int i; |
241 | 17.6k | int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */ |
242 | 17.6k | int nextra = actual - nfixparams; /* number of extra arguments */ |
243 | 17.6k | ci->u.l.nextraargs = nextra; |
244 | 17.6k | luaD_checkstack(L, p->maxstacksize + 1); |
245 | | /* copy function to the top of the stack */ |
246 | 17.6k | setobjs2s(L, L->top.p++, ci->func.p); |
247 | | /* move fixed parameters to the top of the stack */ |
248 | 17.7k | for (i = 1; i <= nfixparams; i++) { |
249 | 69 | setobjs2s(L, L->top.p++, ci->func.p + i); |
250 | 69 | setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ |
251 | 69 | } |
252 | 17.6k | ci->func.p += actual + 1; |
253 | 17.6k | ci->top.p += actual + 1; |
254 | 17.6k | lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); |
255 | 17.6k | } |
256 | | |
257 | | |
258 | 21.4k | void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { |
259 | 21.4k | int i; |
260 | 21.4k | int nextra = ci->u.l.nextraargs; |
261 | 21.4k | if (wanted < 0) { |
262 | 9.02k | wanted = nextra; /* get all extra arguments available */ |
263 | 9.02k | checkstackGCp(L, nextra, where); /* ensure stack space */ |
264 | 9.02k | L->top.p = where + nextra; /* next instruction will need top */ |
265 | 9.02k | } |
266 | 2.85M | for (i = 0; i < wanted && i < nextra; i++) |
267 | 2.83M | setobjs2s(L, where + i, ci->func.p - nextra + i); |
268 | 36.6k | for (; i < wanted; i++) /* complete required results with nil */ |
269 | 21.4k | setnilvalue(s2v(where + i)); |
270 | 21.4k | } |
271 | | |