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