GCC Code Coverage Report


Directory: ./
File: firmware/ext/lua/lvm.c
Date: 2025-12-07 16:27:22
Coverage Exec Excl Total
Lines: 52.6% 481 0 915
Functions: 56.2% 18 0 32
Branches: 32.3% 331 0 1025
Decisions: 42.6% 20 - 47

Line Branch Decision Exec Source
1 /*
2 ** $Id: lvm.c $
3 ** Lua virtual machine
4 ** See Copyright Notice in lua.h
5 */
6
7 #define lvm_c
8 #define LUA_CORE
9
10 #include "lprefix.h"
11
12 #include <float.h>
13 #include <limits.h>
14 #include <math.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "lua.h"
20
21 #include "ldebug.h"
22 #include "ldo.h"
23 #include "lfunc.h"
24 #include "lgc.h"
25 #include "lobject.h"
26 #include "lopcodes.h"
27 #include "lstate.h"
28 #include "lstring.h"
29 #include "ltable.h"
30 #include "ltm.h"
31 #include "lvm.h"
32
33
34 /*
35 ** By default, use jump tables in the main interpreter loop on gcc
36 ** and compatible compilers.
37 */
38 #if !defined(LUA_USE_JUMPTABLE)
39 #if defined(__GNUC__)
40 #define LUA_USE_JUMPTABLE 1
41 #else
42 #define LUA_USE_JUMPTABLE 0
43 #endif
44 #endif
45
46
47
48 /* limit for table tag-method chains (to avoid infinite loops) */
49 #define MAXTAGLOOP 2000
50
51
52 /*
53 ** 'l_intfitsf' checks whether a given integer is in the range that
54 ** can be converted to a float without rounding. Used in comparisons.
55 */
56
57 /* number of bits in the mantissa of a float */
58 #define NBM (l_floatatt(MANT_DIG))
59
60 /*
61 ** Check whether some integers may not fit in a float, testing whether
62 ** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.)
63 ** (The shifts are done in parts, to avoid shifting by more than the size
64 ** of an integer. In a worst case, NBM == 113 for long double and
65 ** sizeof(long) == 32.)
66 */
67 #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
68 >> (NBM - (3 * (NBM / 4)))) > 0
69
70 /* limit for integers that fit in a float */
71 #define MAXINTFITSF ((lua_Unsigned)1 << NBM)
72
73 /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */
74 #define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF))
75
76 #else /* all integers fit in a float precisely */
77
78 #define l_intfitsf(i) 1
79
80 #endif
81
82
83 /*
84 ** Try to convert a value from string to a number value.
85 ** If the value is not a string or is a string not representing
86 ** a valid numeral (or if coercions from strings to numbers
87 ** are disabled via macro 'cvt2num'), do not modify 'result'
88 ** and return 0.
89 */
90 18 static int l_strton (const TValue *obj, TValue *result) {
91 lua_assert(obj != result);
92
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 time.
2/2
✓ Decision 'true' taken 17 times.
✓ Decision 'false' taken 1 time.
18 if (!cvt2num(obj)) /* is object not a string? */
93 17 return 0;
94 else {
95 1 TString *st = tsvalue(obj);
96
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 return (luaO_str2num(getstr(st), result) == tsslen(st) + 1);
97 }
98 }
99
100
101 /*
102 ** Try to convert a value to a float. The float case is already handled
103 ** by the macro 'tonumber'.
104 */
105
1/1
✓ Decision 'true' taken 211 times.
211 int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
106 TValue v;
107
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 10 times.
2/2
✓ Decision 'true' taken 201 times.
✓ Decision 'false' taken 10 times.
211 if (ttisinteger(obj)) {
108 201 *n = cast_num(ivalue(obj));
109 201 return 1;
110 }
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 10 times.
10 else if (l_strton(obj, &v)) { /* string coercible to number? */
112 *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */
113 return 1;
114 }
115 else
116 10 return 0; /* conversion failed */
117 }
118
119
120 /*
121 ** try to convert a float to an integer, rounding according to 'mode'.
122 */
123 281 int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
124 281 lua_Number f = l_floor(n);
125
2/2
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 56 times.
2/2
✓ Decision 'true' taken 225 times.
✓ Decision 'false' taken 56 times.
281 if (n != f) { /* not an integral value? */
126
1/2
✓ Branch 0 taken 225 times.
✗ Branch 1 not taken.
1/2
✓ Decision 'true' taken 225 times.
✗ Decision 'false' not taken.
225 if (mode == F2Ieq) return 0; /* fails if mode demands integral value */
127 else if (mode == F2Iceil) /* needs ceil? */
128 f += 1; /* convert floor to ceil (remember: n != f) */
129 }
130
2/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
56 return lua_numbertointeger(f, p);
131 }
132
133
134 /*
135 ** try to convert a value to an integer, rounding according to 'mode',
136 ** without string coercion.
137 ** ("Fast track" handled by macro 'tointegerns'.)
138 */
139 8 int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
140
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 7 times.
2/2
✓ Decision 'true' taken 1 time.
✓ Decision 'false' taken 7 times.
8 if (ttisfloat(obj))
141 1 return luaV_flttointeger(fltvalue(obj), p, mode);
142
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
1/2
✓ Decision 'true' taken 7 times.
✗ Decision 'false' not taken.
7 else if (ttisinteger(obj)) {
143 7 *p = ivalue(obj);
144 7 return 1;
145 }
146 else
147 return 0;
148 }
149
150
151 /*
152 ** try to convert a value to an integer.
153 */
154 8 int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
155 TValue v;
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 8 times.
8 if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
157 obj = &v; /* change it to point to its corresponding number */
158 8 return luaV_tointegerns(obj, p, mode);
159 }
160
161
162 /*
163 ** Try to convert a 'for' limit to an integer, preserving the semantics
164 ** of the loop. Return true if the loop must not run; otherwise, '*p'
165 ** gets the integer limit.
166 ** (The following explanation assumes a positive step; it is valid for
167 ** negative steps mutatis mutandis.)
168 ** If the limit is an integer or can be converted to an integer,
169 ** rounding down, that is the limit.
170 ** Otherwise, check whether the limit can be converted to a float. If
171 ** the float is too large, clip it to LUA_MAXINTEGER. If the float
172 ** is too negative, the loop should not run, because any initial
173 ** integer value is greater than such limit; so, the function returns
174 ** true to signal that. (For this latter case, no integer limit would be
175 ** correct; even a limit of LUA_MININTEGER would run the loop once for
176 ** an initial value equal to LUA_MININTEGER.)
177 */
178 7 static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
179 lua_Integer *p, lua_Integer step) {
180
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 7 times.
7 if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
181 /* not coercible to in integer */
182 lua_Number flim; /* try to convert to float */
183 if (!tonumber(lim, &flim)) /* cannot convert to float? */
184 luaG_forerror(L, lim, "limit");
185 /* else 'flim' is a float out of integer bounds */
186 if (luai_numlt(0, flim)) { /* if it is positive, it is too large */
187 if (step < 0) return 1; /* initial value must be less than it */
188 *p = LUA_MAXINTEGER; /* truncate */
189 }
190 else { /* it is less than min integer */
191 if (step > 0) return 1; /* initial value must be greater than it */
192 *p = LUA_MININTEGER; /* truncate */
193 }
194 }
195
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return (step > 0 ? init > *p : init < *p); /* not to run? */
196 }
197
198
199 /*
200 ** Prepare a numerical for loop (opcode OP_FORPREP).
201 ** Return true to skip the loop. Otherwise,
202 ** after preparation, stack will be as follows:
203 ** ra : internal index (safe copy of the control variable)
204 ** ra + 1 : loop counter (integer loops) or limit (float loops)
205 ** ra + 2 : step
206 ** ra + 3 : control variable
207 */
208 7 static int forprep (lua_State *L, StkId ra) {
209 7 TValue *pinit = s2v(ra);
210 7 TValue *plimit = s2v(ra + 1);
211 7 TValue *pstep = s2v(ra + 2);
212
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
2/2
✓ Decision 'true' taken 7 times.
✓ Decision 'false' taken 7 times.
14 if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
213 7 lua_Integer init = ivalue(pinit);
214 7 lua_Integer step = ivalue(pstep);
215 lua_Integer limit;
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 7 times.
7 if (step == 0)
217 luaG_runerror(L, "'for' step is zero");
218 7 setivalue(s2v(ra + 3), init); /* control variable */
219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 7 times.
7 if (forlimit(L, init, plimit, &limit, step))
220 return 1; /* skip the loop */
221 else { /* prepare loop counter */
222 lua_Unsigned count;
223
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
1/2
✓ Decision 'true' taken 7 times.
✗ Decision 'false' not taken.
7 if (step > 0) { /* ascending loop? */
224 7 count = l_castS2U(limit) - l_castS2U(init);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 7 times.
7 if (step != 1) /* avoid division in the too common case */
226 count /= l_castS2U(step);
227 }
228 else { /* step < 0; descending loop */
229 count = l_castS2U(init) - l_castS2U(limit);
230 /* 'step+1' avoids negating 'mininteger' */
231 count /= l_castS2U(-(step + 1)) + 1u;
232 }
233 /* store the counter in place of the limit (which won't be
234 needed anymore) */
235 7 setivalue(plimit, l_castU2S(count));
236 }
237 }
238 else { /* try making all values floats */
239 lua_Number init; lua_Number limit; lua_Number step;
240 if (l_unlikely(!tonumber(plimit, &limit)))
241 luaG_forerror(L, plimit, "limit");
242 if (l_unlikely(!tonumber(pstep, &step)))
243 luaG_forerror(L, pstep, "step");
244 if (l_unlikely(!tonumber(pinit, &init)))
245 luaG_forerror(L, pinit, "initial value");
246 if (step == 0)
247 luaG_runerror(L, "'for' step is zero");
248 if (luai_numlt(0, step) ? luai_numlt(limit, init)
249 : luai_numlt(init, limit))
250 return 1; /* skip the loop */
251 else {
252 /* make sure internal values are all floats */
253 setfltvalue(plimit, limit);
254 setfltvalue(pstep, step);
255 setfltvalue(s2v(ra), init); /* internal index */
256 setfltvalue(s2v(ra + 3), init); /* control variable */
257 }
258 }
259 7 return 0;
260 }
261
262
263 /*
264 ** Execute a step of a float numerical for loop, returning
265 ** true iff the loop must continue. (The integer case is
266 ** written online with opcode OP_FORLOOP, for performance.)
267 */
268 static int floatforloop (StkId ra) {
269 lua_Number step = fltvalue(s2v(ra + 2));
270 lua_Number limit = fltvalue(s2v(ra + 1));
271 lua_Number idx = fltvalue(s2v(ra)); /* internal index */
272 idx = luai_numadd(L, idx, step); /* increment index */
273 if (luai_numlt(0, step) ? luai_numle(idx, limit)
274 : luai_numle(limit, idx)) {
275 chgfltvalue(s2v(ra), idx); /* update internal index */
276 setfltvalue(s2v(ra + 3), idx); /* and control variable */
277 return 1; /* jump back */
278 }
279 else
280 return 0; /* finish the loop */
281 }
282
283
284 /*
285 ** Finish the table access 'val = t[key]'.
286 ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to
287 ** t[k] entry (which must be empty).
288 */
289 6035 void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
290 const TValue *slot) {
291 int loop; /* counter to avoid infinite loops */
292 const TValue *tm; /* metamethod */
293
1/2
✓ Branch 0 taken 6035 times.
✗ Branch 1 not taken.
6035 for (loop = 0; loop < MAXTAGLOOP; loop++) {
294
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5995 times.
6035 if (slot == NULL) { /* 't' is not a table? */
295 lua_assert(!ttistable(t));
296 40 tm = luaT_gettmbyobj(L, t, TM_INDEX);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (l_unlikely(notm(tm)))
298 luaG_typeerror(L, t, "index"); /* no metamethod */
299 /* else will try the metamethod */
300 }
301 else { /* 't' is a table */
302 lua_assert(isempty(slot));
303
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5995 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5995 tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */
304
1/2
✓ Branch 0 taken 5995 times.
✗ Branch 1 not taken.
5995 if (tm == NULL) { /* no metamethod? */
305 5995 setnilvalue(s2v(val)); /* result is nil */
306 5995 return;
307 }
308 /* else will try the metamethod */
309 }
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (ttisfunction(tm)) { /* is metamethod a function? */
311 luaT_callTMres(L, tm, t, key, val); /* call it */
312 return;
313 }
314 40 t = tm; /* else try to access 'tm[key]' */
315
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
40 if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */
316 40 setobj2s(L, val, slot); /* done */
317 40 return;
318 }
319 /* else repeat (tail call 'luaV_finishget') */
320 }
321 luaG_runerror(L, "'__index' chain too long; possible loop");
322 }
323
324
325 /*
326 ** Finish a table assignment 't[key] = val'.
327 ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points
328 ** to the entry 't[key]', or to a value with an absent key if there
329 ** is no such entry. (The value at 'slot' must be empty, otherwise
330 ** 'luaV_fastget' would have done the job.)
331 */
332 47320 void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
333 TValue *val, const TValue *slot) {
334 int loop; /* counter to avoid infinite loops */
335
1/2
✓ Branch 0 taken 47320 times.
✗ Branch 1 not taken.
47320 for (loop = 0; loop < MAXTAGLOOP; loop++) {
336 const TValue *tm; /* '__newindex' metamethod */
337
1/2
✓ Branch 0 taken 47320 times.
✗ Branch 1 not taken.
47320 if (slot != NULL) { /* is 't' a table? */
338 47320 Table *h = hvalue(t); /* save 't' table */
339 lua_assert(isempty(slot)); /* slot must be empty */
340
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 47320 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
47320 tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */
341
1/2
✓ Branch 0 taken 47320 times.
✗ Branch 1 not taken.
47320 if (tm == NULL) { /* no metamethod? */
342 47320 sethvalue2s(L, L->top.p, h); /* anchor 't' */
343 47320 L->top.p++; /* assume EXTRA_STACK */
344 47320 luaH_finishset(L, h, key, slot, val); /* set new value */
345 47320 L->top.p--;
346 47320 invalidateTMcache(h);
347
3/6
✓ Branch 0 taken 19021 times.
✓ Branch 1 taken 28299 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19021 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
47320 luaC_barrierback(L, obj2gco(h), val);
348 47320 return;
349 }
350 /* else will try the metamethod */
351 }
352 else { /* not a table; check metamethod */
353 tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
354 if (l_unlikely(notm(tm)))
355 luaG_typeerror(L, t, "index");
356 }
357 /* try the metamethod */
358 if (ttisfunction(tm)) {
359 luaT_callTM(L, tm, t, key, val);
360 return;
361 }
362 t = tm; /* else repeat assignment over 'tm' */
363 if (luaV_fastget(L, t, key, slot, luaH_get)) {
364 luaV_finishfastset(L, t, slot, val);
365 return; /* done */
366 }
367 /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
368 }
369 luaG_runerror(L, "'__newindex' chain too long; possible loop");
370 }
371
372
373 /*
374 ** Compare two strings 'ts1' x 'ts2', returning an integer less-equal-
375 ** -greater than zero if 'ts1' is less-equal-greater than 'ts2'.
376 ** The code is a little tricky because it allows '\0' in the strings
377 ** and it uses 'strcoll' (to respect locales) for each segment
378 ** of the strings. Note that segments can compare equal but still
379 ** have different lengths.
380 */
381 static int l_strcmp (const TString *ts1, const TString *ts2) {
382 const char *s1 = getstr(ts1);
383 size_t rl1 = tsslen(ts1); /* real length */
384 const char *s2 = getstr(ts2);
385 size_t rl2 = tsslen(ts2);
386 for (;;) { /* for each segment */
387 int temp = strcoll(s1, s2);
388 if (temp != 0) /* not equal? */
389 return temp; /* done */
390 else { /* strings are equal up to a '\0' */
391 size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */
392 size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */
393 if (zl2 == rl2) /* 's2' is finished? */
394 return (zl1 == rl1) ? 0 : 1; /* check 's1' */
395 else if (zl1 == rl1) /* 's1' is finished? */
396 return -1; /* 's1' is less than 's2' ('s2' is not finished) */
397 /* both strings longer than 'zl'; go on comparing after the '\0' */
398 zl1++; zl2++;
399 s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2;
400 }
401 }
402 }
403
404
405 /*
406 ** Check whether integer 'i' is less than float 'f'. If 'i' has an
407 ** exact representation as a float ('l_intfitsf'), compare numbers as
408 ** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'.
409 ** If 'ceil(f)' is out of integer range, either 'f' is greater than
410 ** all integers or less than all integers.
411 ** (The test with 'l_intfitsf' is only for performance; the else
412 ** case is correct for all values, but it is slow due to the conversion
413 ** from float to int.)
414 ** When 'f' is NaN, comparisons must result in false.
415 */
416 l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
417 if (l_intfitsf(i))
418 return luai_numlt(cast_num(i), f); /* compare them as floats */
419 else { /* i < f <=> i < ceil(f) */
420 lua_Integer fi;
421 if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
422 return i < fi; /* compare them as integers */
423 else /* 'f' is either greater or less than all integers */
424 return f > 0; /* greater? */
425 }
426 }
427
428
429 /*
430 ** Check whether integer 'i' is less than or equal to float 'f'.
431 ** See comments on previous function.
432 */
433 l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
434 if (l_intfitsf(i))
435 return luai_numle(cast_num(i), f); /* compare them as floats */
436 else { /* i <= f <=> i <= floor(f) */
437 lua_Integer fi;
438 if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
439 return i <= fi; /* compare them as integers */
440 else /* 'f' is either greater or less than all integers */
441 return f > 0; /* greater? */
442 }
443 }
444
445
446 /*
447 ** Check whether float 'f' is less than integer 'i'.
448 ** See comments on previous function.
449 */
450 l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
451 if (l_intfitsf(i))
452 return luai_numlt(f, cast_num(i)); /* compare them as floats */
453 else { /* f < i <=> floor(f) < i */
454 lua_Integer fi;
455 if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
456 return fi < i; /* compare them as integers */
457 else /* 'f' is either greater or less than all integers */
458 return f < 0; /* less? */
459 }
460 }
461
462
463 /*
464 ** Check whether float 'f' is less than or equal to integer 'i'.
465 ** See comments on previous function.
466 */
467 l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
468 if (l_intfitsf(i))
469 return luai_numle(f, cast_num(i)); /* compare them as floats */
470 else { /* f <= i <=> ceil(f) <= i */
471 lua_Integer fi;
472 if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
473 return fi <= i; /* compare them as integers */
474 else /* 'f' is either greater or less than all integers */
475 return f < 0; /* less? */
476 }
477 }
478
479
480 /*
481 ** Return 'l < r', for numbers.
482 */
483 2 l_sinline int LTnum (const TValue *l, const TValue *r) {
484 lua_assert(ttisnumber(l) && ttisnumber(r));
485
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ttisinteger(l)) {
486 2 lua_Integer li = ivalue(l);
487
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ttisinteger(r))
488 2 return li < ivalue(r); /* both are integers */
489 else /* 'l' is int and 'r' is float */
490 return LTintfloat(li, fltvalue(r)); /* l < r ? */
491 }
492 else {
493 lua_Number lf = fltvalue(l); /* 'l' must be float */
494 if (ttisfloat(r))
495 return luai_numlt(lf, fltvalue(r)); /* both are float */
496 else /* 'l' is float and 'r' is int */
497 return LTfloatint(lf, ivalue(r));
498 }
499 }
500
501
502 /*
503 ** Return 'l <= r', for numbers.
504 */
505 l_sinline int LEnum (const TValue *l, const TValue *r) {
506 lua_assert(ttisnumber(l) && ttisnumber(r));
507 if (ttisinteger(l)) {
508 lua_Integer li = ivalue(l);
509 if (ttisinteger(r))
510 return li <= ivalue(r); /* both are integers */
511 else /* 'l' is int and 'r' is float */
512 return LEintfloat(li, fltvalue(r)); /* l <= r ? */
513 }
514 else {
515 lua_Number lf = fltvalue(l); /* 'l' must be float */
516 if (ttisfloat(r))
517 return luai_numle(lf, fltvalue(r)); /* both are float */
518 else /* 'l' is float and 'r' is int */
519 return LEfloatint(lf, ivalue(r));
520 }
521 }
522
523
524 /*
525 ** return 'l < r' for non-numbers.
526 */
527 static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
528 lua_assert(!ttisnumber(l) || !ttisnumber(r));
529 if (ttisstring(l) && ttisstring(r)) /* both are strings? */
530 return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
531 else
532 return luaT_callorderTM(L, l, r, TM_LT);
533 }
534
535
536 /*
537 ** Main operation less than; return 'l < r'.
538 */
539 2 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
540
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
541 2 return LTnum(l, r);
542 else return lessthanothers(L, l, r);
543 }
544
545
546 /*
547 ** return 'l <= r' for non-numbers.
548 */
549 static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
550 lua_assert(!ttisnumber(l) || !ttisnumber(r));
551 if (ttisstring(l) && ttisstring(r)) /* both are strings? */
552 return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
553 else
554 return luaT_callorderTM(L, l, r, TM_LE);
555 }
556
557
558 /*
559 ** Main operation less than or equal to; return 'l <= r'.
560 */
561 int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
562 if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
563 return LEnum(l, r);
564 else return lessequalothers(L, l, r);
565 }
566
567
568 /*
569 ** Main operation for equality of Lua values; return 't1 == t2'.
570 ** L == NULL means raw equality (no metamethods)
571 */
572 881 int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
573 const TValue *tm;
574
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 683 times.
881 if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */
575
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 198 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
198 if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER)
576 198 return 0; /* only numbers can be equal with different variants */
577 else { /* two numbers with different variants */
578 /* One of them is an integer. If the other does not have an
579 integer value, they cannot be equal; otherwise, compare their
580 integer values. */
581 lua_Integer i1, i2;
582 return (luaV_tointegerns(t1, &i1, F2Ieq) &&
583 luaV_tointegerns(t2, &i2, F2Ieq) &&
584 i1 == i2);
585 }
586 }
587 /* values have same type and same variant */
588
5/10
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 401 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 46 times.
✗ Branch 9 not taken.
683 switch (ttypetag(t1)) {
589 82 case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1;
590 147 case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2));
591 7 case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
592 case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
593 case LUA_VLCF: return fvalue(t1) == fvalue(t2);
594 401 case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
595 case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
596 case LUA_VUSERDATA: {
597 if (uvalue(t1) == uvalue(t2)) return 1;
598 else if (L == NULL) return 0;
599 tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
600 if (tm == NULL)
601 tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
602 break; /* will try TM */
603 }
604 46 case LUA_VTABLE: {
605
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (hvalue(t1) == hvalue(t2)) return 1;
606 else if (L == NULL) return 0;
607 tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
608 if (tm == NULL)
609 tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
610 break; /* will try TM */
611 }
612 default:
613 return gcvalue(t1) == gcvalue(t2);
614 }
615 if (tm == NULL) /* no TM? */
616 return 0; /* objects are different */
617 else {
618 luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */
619 return !l_isfalse(s2v(L->top.p));
620 }
621 }
622
623
624 /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
625 #define tostring(L,o) \
626 (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
627
628 #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
629
630 /* copy strings in stack from top - n up to top - 1 to buffer */
631 118 static void copy2buff (StkId top, int n, char *buff) {
632 118 size_t tl = 0; /* size already copied */
633 do {
634 309 TString *st = tsvalue(s2v(top - n));
635
2/2
✓ Branch 0 taken 301 times.
✓ Branch 1 taken 8 times.
309 size_t l = tsslen(st); /* length of string being copied */
636 309 memcpy(buff + tl, getstr(st), l * sizeof(char));
637 309 tl += l;
638
2/2
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 118 times.
309 } while (--n > 0);
639 118 }
640
641
642 /*
643 ** Main operation for concatenation: concat 'total' values in the stack,
644 ** from 'L->top.p - total' up to 'L->top.p - 1'.
645 */
646 152 void luaV_concat (lua_State *L, int total) {
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
152 if (total == 1)
648 return; /* "all" values already concatenated */
649 do {
650 152 StkId top = L->top.p;
651 152 int n = 2; /* number of elements handled in this pass (at least 2) */
652
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 110 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
152 if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
653
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 133 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
152 !tostring(L, s2v(top - 1)))
654 luaT_tryconcatTM(L); /* may invalidate 'top' */
655
4/4
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 113 times.
152 else if (isemptystr(s2v(top - 1))) /* second operand is empty? */
656
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
34 cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
657
3/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
118 else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
658 setobjs2s(L, top - 2, top - 1); /* result is second op. */
659 }
660 else {
661 /* at least two non-empty string values; get as many as possible */
662
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 5 times.
118 size_t tl = tsslen(tsvalue(s2v(top - 1)));
663 TString *ts;
664 /* collect total length and number of strings */
665
5/6
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 23 times.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
309 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
666
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 3 times.
191 size_t l = tsslen(tsvalue(s2v(top - n - 1)));
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191 times.
191 if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) {
668 L->top.p = top - total; /* pop strings to avoid wasting stack */
669 luaG_runerror(L, "string length overflow");
670 }
671 191 tl += l;
672 }
673
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 8 times.
118 if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
674 char buff[LUAI_MAXSHORTLEN];
675 110 copy2buff(top, n, buff); /* copy strings to buffer */
676 110 ts = luaS_newlstr(L, buff, tl);
677 }
678 else { /* long string; copy strings directly to final result */
679 8 ts = luaS_createlngstrobj(L, tl);
680 8 copy2buff(top, n, getlngstr(ts));
681 }
682 118 setsvalue2s(L, top - n, ts); /* create result */
683 }
684 152 total -= n - 1; /* got 'n' strings to create one new */
685 152 L->top.p -= n - 1; /* popped 'n' strings and pushed one */
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
152 } while (total > 1); /* repeat until only 1 result left */
687 }
688
689
690 /*
691 ** Main operation 'ra = #rb'.
692 */
693 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
694 const TValue *tm;
695 switch (ttypetag(rb)) {
696 case LUA_VTABLE: {
697 Table *h = hvalue(rb);
698 tm = fasttm(L, h->metatable, TM_LEN);
699 if (tm) break; /* metamethod? break switch to call it */
700 setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */
701 return;
702 }
703 case LUA_VSHRSTR: {
704 setivalue(s2v(ra), tsvalue(rb)->shrlen);
705 return;
706 }
707 case LUA_VLNGSTR: {
708 setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
709 return;
710 }
711 default: { /* try metamethod */
712 tm = luaT_gettmbyobj(L, rb, TM_LEN);
713 if (l_unlikely(notm(tm))) /* no metamethod? */
714 luaG_typeerror(L, rb, "get length of");
715 break;
716 }
717 }
718 luaT_callTMres(L, tm, rb, rb, ra);
719 }
720
721
722 /*
723 ** Integer division; return 'm // n', that is, floor(m/n).
724 ** C division truncates its result (rounds towards zero).
725 ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
726 ** otherwise 'floor(q) == trunc(q) - 1'.
727 */
728 lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
729 if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
730 if (n == 0)
731 luaG_runerror(L, "attempt to divide by zero");
732 return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
733 }
734 else {
735 lua_Integer q = m / n; /* perform C division */
736 if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
737 q -= 1; /* correct result for different rounding */
738 return q;
739 }
740 }
741
742
743 /*
744 ** Integer modulus; return 'm % n'. (Assume that C '%' with
745 ** negative operands follows C99 behavior. See previous comment
746 ** about luaV_idiv.)
747 */
748 81 lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
750 if (n == 0)
751 luaG_runerror(L, "attempt to perform 'n%%0'");
752 return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
753 }
754 else {
755 81 lua_Integer r = m % n;
756
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 67 times.
81 if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */
757 1 r += n; /* correct result for different rounding */
758 81 return r;
759 }
760 }
761
762
763 /*
764 ** Float modulus
765 */
766 lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
767 lua_Number r;
768 luai_nummod(L, m, n, r);
769 return r;
770 }
771
772
773 /* number of bits in an integer */
774 #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
775
776
777 /*
778 ** Shift left operation. (Shift right just negates 'y'.)
779 */
780 220 lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
781
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 139 times.
220 if (y < 0) { /* shift right? */
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (y <= -NBITS) return 0;
783 81 else return intop(>>, x, -y);
784 }
785 else { /* shift left */
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (y >= NBITS) return 0;
787 139 else return intop(<<, x, y);
788 }
789 }
790
791
792 /*
793 ** create a new Lua closure, push it in the stack, and initialize
794 ** its upvalues.
795 */
796 215 static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
797 StkId ra) {
798 215 int nup = p->sizeupvalues;
799 215 Upvaldesc *uv = p->upvalues;
800 int i;
801 215 LClosure *ncl = luaF_newLclosure(L, nup);
802 215 ncl->p = p;
803 215 setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */
804
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 215 times.
365 for (i = 0; i < nup; i++) { /* fill in its upvalues */
805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (uv[i].instack) /* upvalue refers to local variable? */
806 ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
807 else /* get upvalue from enclosing function */
808 150 ncl->upvals[i] = encup[uv[i].idx];
809
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
150 luaC_objbarrier(L, ncl, ncl->upvals[i]);
810 }
811 215 }
812
813
814 /*
815 ** finish execution of an opcode interrupted by a yield
816 */
817 void luaV_finishOp (lua_State *L) {
818 CallInfo *ci = L->ci;
819 StkId base = ci->func.p + 1;
820 Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
821 OpCode op = GET_OPCODE(inst);
822 switch (op) { /* finish its execution */
823 case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
824 setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p);
825 break;
826 }
827 case OP_UNM: case OP_BNOT: case OP_LEN:
828 case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
829 case OP_GETFIELD: case OP_SELF: {
830 setobjs2s(L, base + GETARG_A(inst), --L->top.p);
831 break;
832 }
833 case OP_LT: case OP_LE:
834 case OP_LTI: case OP_LEI:
835 case OP_GTI: case OP_GEI:
836 case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
837 int res = !l_isfalse(s2v(L->top.p - 1));
838 L->top.p--;
839 #if defined(LUA_COMPAT_LT_LE)
840 if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
841 ci->callstatus ^= CIST_LEQ; /* clear mark */
842 res = !res; /* negate result */
843 }
844 #endif
845 lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
846 if (res != GETARG_k(inst)) /* condition failed? */
847 ci->u.l.savedpc++; /* skip jump instruction */
848 break;
849 }
850 case OP_CONCAT: {
851 StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */
852 int a = GETARG_A(inst); /* first element to concatenate */
853 int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */
854 setobjs2s(L, top - 2, top); /* put TM result in proper position */
855 L->top.p = top - 1; /* top is one after last element (at top-2) */
856 luaV_concat(L, total); /* concat them (may yield again) */
857 break;
858 }
859 case OP_CLOSE: { /* yielded closing variables */
860 ci->u.l.savedpc--; /* repeat instruction to close other vars. */
861 break;
862 }
863 case OP_RETURN: { /* yielded closing variables */
864 StkId ra = base + GETARG_A(inst);
865 /* adjust top to signal correct number of returns, in case the
866 return is "up to top" ('isIT') */
867 L->top.p = ra + ci->u2.nres;
868 /* repeat instruction to close other vars. and complete the return */
869 ci->u.l.savedpc--;
870 break;
871 }
872 default: {
873 /* only these other opcodes can yield */
874 lua_assert(op == OP_TFORCALL || op == OP_CALL ||
875 op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE ||
876 op == OP_SETI || op == OP_SETFIELD);
877 break;
878 }
879 }
880 }
881
882
883
884
885 /*
886 ** {==================================================================
887 ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute'
888 ** ===================================================================
889 */
890
891 #define l_addi(L,a,b) intop(+, a, b)
892 #define l_subi(L,a,b) intop(-, a, b)
893 #define l_muli(L,a,b) intop(*, a, b)
894 #define l_band(a,b) intop(&, a, b)
895 #define l_bor(a,b) intop(|, a, b)
896 #define l_bxor(a,b) intop(^, a, b)
897
898 #define l_lti(a,b) (a < b)
899 #define l_lei(a,b) (a <= b)
900 #define l_gti(a,b) (a > b)
901 #define l_gei(a,b) (a >= b)
902
903
904 /*
905 ** Arithmetic operations with immediate operands. 'iop' is the integer
906 ** operation, 'fop' is the float operation.
907 */
908 #define op_arithI(L,iop,fop) { \
909 StkId ra = RA(i); \
910 TValue *v1 = vRB(i); \
911 int imm = GETARG_sC(i); \
912 if (ttisinteger(v1)) { \
913 lua_Integer iv1 = ivalue(v1); \
914 pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \
915 } \
916 else if (ttisfloat(v1)) { \
917 lua_Number nb = fltvalue(v1); \
918 lua_Number fimm = cast_num(imm); \
919 pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
920 }}
921
922
923 /*
924 ** Auxiliary function for arithmetic operations over floats and others
925 ** with two register operands.
926 */
927 #define op_arithf_aux(L,v1,v2,fop) { \
928 lua_Number n1; lua_Number n2; \
929 if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
930 pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
931 }}
932
933
934 /*
935 ** Arithmetic operations over floats and others with register operands.
936 */
937 #define op_arithf(L,fop) { \
938 StkId ra = RA(i); \
939 TValue *v1 = vRB(i); \
940 TValue *v2 = vRC(i); \
941 op_arithf_aux(L, v1, v2, fop); }
942
943
944 /*
945 ** Arithmetic operations with K operands for floats.
946 */
947 #define op_arithfK(L,fop) { \
948 StkId ra = RA(i); \
949 TValue *v1 = vRB(i); \
950 TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \
951 op_arithf_aux(L, v1, v2, fop); }
952
953
954 /*
955 ** Arithmetic operations over integers and floats.
956 */
957 #define op_arith_aux(L,v1,v2,iop,fop) { \
958 StkId ra = RA(i); \
959 if (ttisinteger(v1) && ttisinteger(v2)) { \
960 lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
961 pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
962 } \
963 else op_arithf_aux(L, v1, v2, fop); }
964
965
966 /*
967 ** Arithmetic operations with register operands.
968 */
969 #define op_arith(L,iop,fop) { \
970 TValue *v1 = vRB(i); \
971 TValue *v2 = vRC(i); \
972 op_arith_aux(L, v1, v2, iop, fop); }
973
974
975 /*
976 ** Arithmetic operations with K operands.
977 */
978 #define op_arithK(L,iop,fop) { \
979 TValue *v1 = vRB(i); \
980 TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \
981 op_arith_aux(L, v1, v2, iop, fop); }
982
983
984 /*
985 ** Bitwise operations with constant operand.
986 */
987 #define op_bitwiseK(L,op) { \
988 StkId ra = RA(i); \
989 TValue *v1 = vRB(i); \
990 TValue *v2 = KC(i); \
991 lua_Integer i1; \
992 lua_Integer i2 = ivalue(v2); \
993 if (tointegerns(v1, &i1)) { \
994 pc++; setivalue(s2v(ra), op(i1, i2)); \
995 }}
996
997
998 /*
999 ** Bitwise operations with register operands.
1000 */
1001 #define op_bitwise(L,op) { \
1002 StkId ra = RA(i); \
1003 TValue *v1 = vRB(i); \
1004 TValue *v2 = vRC(i); \
1005 lua_Integer i1; lua_Integer i2; \
1006 if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \
1007 pc++; setivalue(s2v(ra), op(i1, i2)); \
1008 }}
1009
1010
1011 /*
1012 ** Order operations with register operands. 'opn' actually works
1013 ** for all numbers, but the fast track improves performance for
1014 ** integers.
1015 */
1016 #define op_order(L,opi,opn,other) { \
1017 StkId ra = RA(i); \
1018 int cond; \
1019 TValue *rb = vRB(i); \
1020 if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \
1021 lua_Integer ia = ivalue(s2v(ra)); \
1022 lua_Integer ib = ivalue(rb); \
1023 cond = opi(ia, ib); \
1024 } \
1025 else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \
1026 cond = opn(s2v(ra), rb); \
1027 else \
1028 Protect(cond = other(L, s2v(ra), rb)); \
1029 docondjump(); }
1030
1031
1032 /*
1033 ** Order operations with immediate operand. (Immediate operand is
1034 ** always small enough to have an exact representation as a float.)
1035 */
1036 #define op_orderI(L,opi,opf,inv,tm) { \
1037 StkId ra = RA(i); \
1038 int cond; \
1039 int im = GETARG_sB(i); \
1040 if (ttisinteger(s2v(ra))) \
1041 cond = opi(ivalue(s2v(ra)), im); \
1042 else if (ttisfloat(s2v(ra))) { \
1043 lua_Number fa = fltvalue(s2v(ra)); \
1044 lua_Number fim = cast_num(im); \
1045 cond = opf(fa, fim); \
1046 } \
1047 else { \
1048 int isf = GETARG_C(i); \
1049 Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \
1050 } \
1051 docondjump(); }
1052
1053 /* }================================================================== */
1054
1055
1056 /*
1057 ** {==================================================================
1058 ** Function 'luaV_execute': main interpreter loop
1059 ** ===================================================================
1060 */
1061
1062 /*
1063 ** some macros for common tasks in 'luaV_execute'
1064 */
1065
1066
1067 #define RA(i) (base+GETARG_A(i))
1068 #define RB(i) (base+GETARG_B(i))
1069 #define vRB(i) s2v(RB(i))
1070 #define KB(i) (k+GETARG_B(i))
1071 #define RC(i) (base+GETARG_C(i))
1072 #define vRC(i) s2v(RC(i))
1073 #define KC(i) (k+GETARG_C(i))
1074 #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
1075
1076
1077
1078 #define updatetrap(ci) (trap = ci->u.l.trap)
1079
1080 #define updatebase(ci) (base = ci->func.p + 1)
1081
1082
1083 #define updatestack(ci) \
1084 { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } }
1085
1086
1087 /*
1088 ** Execute a jump instruction. The 'updatetrap' allows signals to stop
1089 ** tight loops. (Without it, the local copy of 'trap' could never change.)
1090 */
1091 #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); }
1092
1093
1094 /* for test instructions, execute the jump instruction that follows it */
1095 #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); }
1096
1097 /*
1098 ** do a conditional jump: skip next instruction if 'cond' is not what
1099 ** was expected (parameter 'k'), else do next instruction, which must
1100 ** be a jump.
1101 */
1102 #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci);
1103
1104
1105 /*
1106 ** Correct global 'pc'.
1107 */
1108 #define savepc(L) (ci->u.l.savedpc = pc)
1109
1110
1111 /*
1112 ** Whenever code can raise errors, the global 'pc' and the global
1113 ** 'top' must be correct to report occasional errors.
1114 */
1115 #define savestate(L,ci) (savepc(L), L->top.p = ci->top.p)
1116
1117
1118 /*
1119 ** Protect code that, in general, can raise errors, reallocate the
1120 ** stack, and change the hooks.
1121 */
1122 #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci))
1123
1124 /* special version that does not change the top */
1125 #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci))
1126
1127 /*
1128 ** Protect code that can only raise errors. (That is, it cannot change
1129 ** the stack or hooks.)
1130 */
1131 #define halfProtect(exp) (savestate(L,ci), (exp))
1132
1133 /* 'c' is the limit of live values in the stack */
1134 #define checkGC(L,c) \
1135 { luaC_condGC(L, (savepc(L), L->top.p = (c)), \
1136 updatetrap(ci)); \
1137 luai_threadyield(L); }
1138
1139
1140 /* fetch an instruction and prepare its execution */
1141 #define vmfetch() { \
1142 if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \
1143 trap = luaG_traceexec(L, pc); /* handle hooks */ \
1144 updatebase(ci); /* correct stack */ \
1145 } \
1146 i = *(pc++); \
1147 }
1148
1149 #define vmdispatch(o) switch(o)
1150 #define vmcase(l) case l:
1151 #define vmbreak break
1152
1153
1154 394 void luaV_execute (lua_State *L, CallInfo *ci) {
1155 LClosure *cl;
1156 TValue *k;
1157 StkId base;
1158 const Instruction *pc;
1159 int trap;
1160 #if LUA_USE_JUMPTABLE
1161 #include "ljumptab.h"
1162 #endif
1163 191 startfunc:
1164 585 trap = L->hookmask;
1165 735 returning: /* trap already set */
1166 735 cl = ci_func(ci);
1167 735 k = cl->p->k;
1168 735 pc = ci->u.l.savedpc;
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 735 times.
735 if (l_unlikely(trap))
1170 trap = luaG_tracecall(L);
1171 735 base = ci->func.p + 1;
1172 /* main loop of interpreter */
1173 for (;;) {
1174 Instruction i; /* instruction being executed */
1175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 735 times.
735 vmfetch();
1176 #if 0
1177 /* low-level line tracing for debugging Lua */
1178 printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p)));
1179 #endif
1180 lua_assert(base == ci->func.p + 1);
1181 lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p);
1182 /* invalidate top for instructions not expecting it */
1183 lua_assert(isIT(i) || (cast_void(L->top.p = base), 1));
1184 735 vmdispatch (GET_OPCODE(i)) {
1185 356 vmcase(OP_MOVE) {
1186 356 StkId ra = RA(i);
1187 356 setobjs2s(L, ra, RB(i));
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 356 times.
356 vmbreak;
1189 }
1190 1035 vmcase(OP_LOADI) {
1191 1035 StkId ra = RA(i);
1192 1035 lua_Integer b = GETARG_sBx(i);
1193 1035 setivalue(s2v(ra), b);
1194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1035 times.
1035 vmbreak;
1195 }
1196 4 vmcase(OP_LOADF) {
1197 4 StkId ra = RA(i);
1198 4 int b = GETARG_sBx(i);
1199 4 setfltvalue(s2v(ra), cast_num(b));
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 vmbreak;
1201 }
1202 280 vmcase(OP_LOADK) {
1203 280 StkId ra = RA(i);
1204 280 TValue *rb = k + GETARG_Bx(i);
1205 280 setobj2s(L, ra, rb);
1206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 vmbreak;
1207 }
1208 vmcase(OP_LOADKX) {
1209 StkId ra = RA(i);
1210 TValue *rb;
1211 rb = k + GETARG_Ax(*pc); pc++;
1212 setobj2s(L, ra, rb);
1213 vmbreak;
1214 }
1215 91 vmcase(OP_LOADFALSE) {
1216 91 StkId ra = RA(i);
1217 91 setbfvalue(s2v(ra));
1218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91 times.
91 vmbreak;
1219 }
1220 6 vmcase(OP_LFALSESKIP) {
1221 6 StkId ra = RA(i);
1222 6 setbfvalue(s2v(ra));
1223 6 pc++; /* skip next instruction */
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 vmbreak;
1225 }
1226 96 vmcase(OP_LOADTRUE) {
1227 96 StkId ra = RA(i);
1228 96 setbtvalue(s2v(ra));
1229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 vmbreak;
1230 }
1231 2 vmcase(OP_LOADNIL) {
1232 2 StkId ra = RA(i);
1233 2 int b = GETARG_B(i);
1234 do {
1235 2 setnilvalue(s2v(ra++));
1236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } while (b--);
1237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 vmbreak;
1238 }
1239 vmcase(OP_GETUPVAL) {
1240 StkId ra = RA(i);
1241 int b = GETARG_B(i);
1242 setobj2s(L, ra, cl->upvals[b]->v.p);
1243 vmbreak;
1244 }
1245 vmcase(OP_SETUPVAL) {
1246 StkId ra = RA(i);
1247 UpVal *uv = cl->upvals[GETARG_B(i)];
1248 setobj(L, uv->v.p, s2v(ra));
1249 luaC_barrier(L, uv, s2v(ra));
1250 vmbreak;
1251 }
1252 1096 vmcase(OP_GETTABUP) {
1253 1096 StkId ra = RA(i);
1254 const TValue *slot;
1255 1096 TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
1256 1096 TValue *rc = KC(i);
1257 1096 TString *key = tsvalue(rc); /* key must be a short string */
1258
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1096 times.
✓ Branch 3 taken 1096 times.
✗ Branch 4 not taken.
1096 if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
1259 1096 setobj2s(L, ra, slot);
1260 }
1261 else
1262 Protect(luaV_finishget(L, upval, rc, ra, slot));
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1096 times.
1096 vmbreak;
1264 }
1265 689 vmcase(OP_GETTABLE) {
1266 689 StkId ra = RA(i);
1267 const TValue *slot;
1268 689 TValue *rb = vRB(i);
1269 689 TValue *rc = vRC(i);
1270 lua_Unsigned n;
1271
3/4
✓ Branch 0 taken 689 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 644 times.
✓ Branch 3 taken 45 times.
1378 if (ttisinteger(rc) /* fast track for integers? */
1272
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
✓ Branch 2 taken 644 times.
✓ Branch 3 taken 45 times.
689 ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot))
1273 : luaV_fastget(L, rb, rc, slot, luaH_get)) {
1274 644 setobj2s(L, ra, slot);
1275 }
1276 else
1277 45 Protect(luaV_finishget(L, rb, rc, ra, slot));
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
689 vmbreak;
1279 }
1280 3 vmcase(OP_GETI) {
1281 3 StkId ra = RA(i);
1282 const TValue *slot;
1283 3 TValue *rb = vRB(i);
1284 3 int c = GETARG_C(i);
1285
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
3 if (luaV_fastgeti(L, rb, c, slot)) {
1286 3 setobj2s(L, ra, slot);
1287 }
1288 else {
1289 TValue key;
1290 setivalue(&key, c);
1291 Protect(luaV_finishget(L, rb, &key, ra, slot));
1292 }
1293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 vmbreak;
1294 }
1295 292 vmcase(OP_GETFIELD) {
1296 292 StkId ra = RA(i);
1297 const TValue *slot;
1298 292 TValue *rb = vRB(i);
1299 292 TValue *rc = KC(i);
1300 292 TString *key = tsvalue(rc); /* key must be a short string */
1301
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 292 times.
✓ Branch 3 taken 292 times.
✗ Branch 4 not taken.
292 if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
1302 292 setobj2s(L, ra, slot);
1303 }
1304 else
1305 Protect(luaV_finishget(L, rb, rc, ra, slot));
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 292 times.
292 vmbreak;
1307 }
1308 397 vmcase(OP_SETTABUP) {
1309 const TValue *slot;
1310 397 TValue *upval = cl->upvals[GETARG_A(i)]->v.p;
1311 397 TValue *rb = KB(i);
1312
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 369 times.
397 TValue *rc = RKC(i);
1313 397 TString *key = tsvalue(rb); /* key must be a short string */
1314
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 397 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 388 times.
397 if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
1315
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
9 luaV_finishfastset(L, upval, slot, rc);
1316 }
1317 else
1318 388 Protect(luaV_finishset(L, upval, rb, rc, slot));
1319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 397 times.
397 vmbreak;
1320 }
1321 68 vmcase(OP_SETTABLE) {
1322 68 StkId ra = RA(i);
1323 const TValue *slot;
1324 68 TValue *rb = vRB(i); /* key (table is in 'ra') */
1325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 TValue *rc = RKC(i); /* value */
1326 lua_Unsigned n;
1327
2/4
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
136 if (ttisinteger(rb) /* fast track for integers? */
1328
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
68 ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot))
1329 : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
1330
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
68 luaV_finishfastset(L, s2v(ra), slot, rc);
1331 }
1332 else
1333 Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
1334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 vmbreak;
1335 }
1336 14 vmcase(OP_SETI) {
1337 14 StkId ra = RA(i);
1338 const TValue *slot;
1339 14 int c = GETARG_B(i);
1340
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13 times.
14 TValue *rc = RKC(i);
1341
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14 times.
✗ Branch 6 not taken.
14 if (luaV_fastgeti(L, s2v(ra), c, slot)) {
1342
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
14 luaV_finishfastset(L, s2v(ra), slot, rc);
1343 }
1344 else {
1345 TValue key;
1346 setivalue(&key, c);
1347 Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
1348 }
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 vmbreak;
1350 }
1351 vmcase(OP_SETFIELD) {
1352 StkId ra = RA(i);
1353 const TValue *slot;
1354 TValue *rb = KB(i);
1355 TValue *rc = RKC(i);
1356 TString *key = tsvalue(rb); /* key must be a short string */
1357 if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
1358 luaV_finishfastset(L, s2v(ra), slot, rc);
1359 }
1360 else
1361 Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
1362 vmbreak;
1363 }
1364 106 vmcase(OP_NEWTABLE) {
1365 106 StkId ra = RA(i);
1366 106 int b = GETARG_B(i); /* log2(hash size) + 1 */
1367 106 int c = GETARG_C(i); /* array size */
1368 Table *t;
1369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (b > 0)
1370 b = 1 << (b - 1); /* size is 2^(b - 1) */
1371 lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0));
1372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (TESTARG_k(i)) /* non-zero extra argument? */
1373 c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */
1374 106 pc++; /* skip extra argument */
1375 106 L->top.p = ra + 1; /* correct top in case of emergency GC */
1376 106 t = luaH_new(L); /* memory allocation */
1377 106 sethvalue2s(L, ra, t);
1378
3/4
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
✓ Branch 3 taken 1 time.
106 if (b != 0 || c != 0)
1379 105 luaH_resize(L, t, c, b); /* idem */
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 checkGC(L, ra + 1);
1381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 vmbreak;
1382 }
1383 40 vmcase(OP_SELF) {
1384 40 StkId ra = RA(i);
1385 const TValue *slot;
1386 40 TValue *rb = vRB(i);
1387
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 TValue *rc = RKC(i);
1388 40 TString *key = tsvalue(rc); /* key must be a string */
1389 40 setobj2s(L, ra + 1, rb);
1390
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
40 if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
1391 setobj2s(L, ra, slot);
1392 }
1393 else
1394 40 Protect(luaV_finishget(L, rb, rc, ra, slot));
1395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 vmbreak;
1396 }
1397 518 vmcase(OP_ADDI) {
1398
3/4
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
518 op_arithI(L, l_addi, luai_numadd);
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 vmbreak;
1400 }
1401 vmcase(OP_ADDK) {
1402 op_arithK(L, l_addi, luai_numadd);
1403 vmbreak;
1404 }
1405 vmcase(OP_SUBK) {
1406 op_arithK(L, l_subi, luai_numsub);
1407 vmbreak;
1408 }
1409 136 vmcase(OP_MULK) {
1410
12/16
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 38 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 17 times.
✓ Branch 11 taken 21 times.
✓ Branch 12 taken 21 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 38 times.
✗ Branch 15 not taken.
136 op_arithK(L, l_muli, luai_nummul);
1411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 vmbreak;
1412 }
1413 81 vmcase(OP_MODK) {
1414 81 savestate(L, ci); /* in case of division by 0 */
1415
2/16
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
81 op_arithK(L, luaV_mod, luaV_modf);
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 vmbreak;
1417 }
1418 vmcase(OP_POWK) {
1419 op_arithfK(L, luai_numpow);
1420 vmbreak;
1421 }
1422 83 vmcase(OP_DIVK) {
1423
8/12
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 69 times.
✓ Branch 8 taken 69 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 83 times.
✗ Branch 11 not taken.
83 op_arithfK(L, luai_numdiv);
1424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 vmbreak;
1425 }
1426 vmcase(OP_IDIVK) {
1427 savestate(L, ci); /* in case of division by 0 */
1428 op_arithK(L, luaV_idiv, luai_numidiv);
1429 vmbreak;
1430 }
1431 4 vmcase(OP_BANDK) {
1432
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 op_bitwiseK(L, l_band);
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 vmbreak;
1434 }
1435 vmcase(OP_BORK) {
1436 op_bitwiseK(L, l_bor);
1437 vmbreak;
1438 }
1439 vmcase(OP_BXORK) {
1440 op_bitwiseK(L, l_bxor);
1441 vmbreak;
1442 }
1443 62 vmcase(OP_SHRI) {
1444 62 StkId ra = RA(i);
1445 62 TValue *rb = vRB(i);
1446 62 int ic = GETARG_sC(i);
1447 lua_Integer ib;
1448
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 62 times.
✗ Branch 4 not taken.
62 if (tointegerns(rb, &ib)) {
1449 62 pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
1450 }
1451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 vmbreak;
1452 }
1453 58 vmcase(OP_SHLI) {
1454 58 StkId ra = RA(i);
1455 58 TValue *rb = vRB(i);
1456 58 int ic = GETARG_sC(i);
1457 lua_Integer ib;
1458
2/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 58 times.
✗ Branch 4 not taken.
58 if (tointegerns(rb, &ib)) {
1459 58 pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
1460 }
1461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 vmbreak;
1462 }
1463 117 vmcase(OP_ADD) {
1464
7/16
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
117 op_arith(L, l_addi, luai_numadd);
1465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 vmbreak;
1466 }
1467 90 vmcase(OP_SUB) {
1468
10/16
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 5 times.
✗ Branch 15 not taken.
90 op_arith(L, l_subi, luai_numsub);
1469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 vmbreak;
1470 }
1471 35 vmcase(OP_MUL) {
1472
10/16
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 27 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 27 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 27 times.
✗ Branch 15 not taken.
35 op_arith(L, l_muli, luai_nummul);
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 vmbreak;
1474 }
1475 vmcase(OP_MOD) {
1476 savestate(L, ci); /* in case of division by 0 */
1477 op_arith(L, luaV_mod, luaV_modf);
1478 vmbreak;
1479 }
1480 vmcase(OP_POW) {
1481 op_arithf(L, luai_numpow);
1482 vmbreak;
1483 }
1484 16 vmcase(OP_DIV) { /* float division (always with floats) */
1485
8/12
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
16 op_arithf(L, luai_numdiv);
1486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 vmbreak;
1487 }
1488 vmcase(OP_IDIV) { /* floor division */
1489 savestate(L, ci); /* in case of division by 0 */
1490 op_arith(L, luaV_idiv, luai_numidiv);
1491 vmbreak;
1492 }
1493 87 vmcase(OP_BAND) {
1494
4/8
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 87 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 87 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 87 times.
✗ Branch 9 not taken.
87 op_bitwise(L, l_band);
1495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 vmbreak;
1496 }
1497 29 vmcase(OP_BOR) {
1498
4/8
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 29 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
29 op_bitwise(L, l_bor);
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 vmbreak;
1500 }
1501 42 vmcase(OP_BXOR) {
1502
4/8
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 42 times.
✗ Branch 9 not taken.
42 op_bitwise(L, l_bxor);
1503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 vmbreak;
1504 }
1505 42 vmcase(OP_SHR) {
1506
4/8
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 42 times.
✗ Branch 9 not taken.
42 op_bitwise(L, luaV_shiftr);
1507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 vmbreak;
1508 }
1509 58 vmcase(OP_SHL) {
1510
4/8
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 58 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 58 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 58 times.
✗ Branch 9 not taken.
58 op_bitwise(L, luaV_shiftl);
1511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 vmbreak;
1512 }
1513 vmcase(OP_MMBIN) {
1514 StkId ra = RA(i);
1515 Instruction pi = *(pc - 2); /* original arith. expression */
1516 TValue *rb = vRB(i);
1517 TMS tm = (TMS)GETARG_C(i);
1518 StkId result = RA(pi);
1519 lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
1520 Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
1521 vmbreak;
1522 }
1523 vmcase(OP_MMBINI) {
1524 StkId ra = RA(i);
1525 Instruction pi = *(pc - 2); /* original arith. expression */
1526 int imm = GETARG_sB(i);
1527 TMS tm = (TMS)GETARG_C(i);
1528 int flip = GETARG_k(i);
1529 StkId result = RA(pi);
1530 Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
1531 vmbreak;
1532 }
1533 vmcase(OP_MMBINK) {
1534 StkId ra = RA(i);
1535 Instruction pi = *(pc - 2); /* original arith. expression */
1536 TValue *imm = KB(i);
1537 TMS tm = (TMS)GETARG_C(i);
1538 int flip = GETARG_k(i);
1539 StkId result = RA(pi);
1540 Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
1541 vmbreak;
1542 }
1543 vmcase(OP_UNM) {
1544 StkId ra = RA(i);
1545 TValue *rb = vRB(i);
1546 lua_Number nb;
1547 if (ttisinteger(rb)) {
1548 lua_Integer ib = ivalue(rb);
1549 setivalue(s2v(ra), intop(-, 0, ib));
1550 }
1551 else if (tonumberns(rb, nb)) {
1552 setfltvalue(s2v(ra), luai_numunm(L, nb));
1553 }
1554 else
1555 Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
1556 vmbreak;
1557 }
1558 29 vmcase(OP_BNOT) {
1559 29 StkId ra = RA(i);
1560 29 TValue *rb = vRB(i);
1561 lua_Integer ib;
1562
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 29 times.
✗ Branch 4 not taken.
29 if (tointegerns(rb, &ib)) {
1563 29 setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
1564 }
1565 else
1566 Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
1567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 vmbreak;
1568 }
1569 vmcase(OP_NOT) {
1570 StkId ra = RA(i);
1571 TValue *rb = vRB(i);
1572 if (l_isfalse(rb))
1573 setbtvalue(s2v(ra));
1574 else
1575 setbfvalue(s2v(ra));
1576 vmbreak;
1577 }
1578 vmcase(OP_LEN) {
1579 StkId ra = RA(i);
1580 Protect(luaV_objlen(L, ra, vRB(i)));
1581 vmbreak;
1582 }
1583 146 vmcase(OP_CONCAT) {
1584 146 StkId ra = RA(i);
1585 146 int n = GETARG_B(i); /* number of elements to concatenate */
1586 146 L->top.p = ra + n; /* mark the end of concat operands */
1587 146 ProtectNT(luaV_concat(L, n));
1588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 vmbreak;
1590 }
1591 vmcase(OP_CLOSE) {
1592 StkId ra = RA(i);
1593 Protect(luaF_close(L, ra, LUA_OK, 1));
1594 vmbreak;
1595 }
1596 vmcase(OP_TBC) {
1597 StkId ra = RA(i);
1598 /* create new to-be-closed upvalue */
1599 halfProtect(luaF_newtbcupval(L, ra));
1600 vmbreak;
1601 }
1602 247 vmcase(OP_JMP) {
1603 247 dojump(ci, i, 0);
1604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 247 times.
247 vmbreak;
1605 }
1606 131 vmcase(OP_EQ) {
1607 131 StkId ra = RA(i);
1608 int cond;
1609 131 TValue *rb = vRB(i);
1610 131 Protect(cond = luaV_equalobj(L, s2v(ra), rb));
1611
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 89 times.
131 docondjump();
1612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 vmbreak;
1613 }
1614 vmcase(OP_LT) {
1615 op_order(L, l_lti, LTnum, lessthanothers);
1616 vmbreak;
1617 }
1618 vmcase(OP_LE) {
1619 op_order(L, l_lei, LEnum, lessequalothers);
1620 vmbreak;
1621 }
1622 243 vmcase(OP_EQK) {
1623 243 StkId ra = RA(i);
1624 243 TValue *rb = KB(i);
1625 /* basic types do not use '__eq'; we can use raw equality */
1626 243 int cond = luaV_rawequalobj(s2v(ra), rb);
1627
2/2
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 58 times.
243 docondjump();
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 243 times.
243 vmbreak;
1629 }
1630 81 vmcase(OP_EQI) {
1631 81 StkId ra = RA(i);
1632 int cond;
1633 81 int im = GETARG_sB(i);
1634
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 5 times.
81 if (ttisinteger(s2v(ra)))
1635 76 cond = (ivalue(s2v(ra)) == im);
1636
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 else if (ttisfloat(s2v(ra)))
1637 5 cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
1638 else
1639 cond = 0; /* other types cannot be equal to a number */
1640
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 44 times.
81 docondjump();
1641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 vmbreak;
1642 }
1643 vmcase(OP_LTI) {
1644 op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
1645 vmbreak;
1646 }
1647 vmcase(OP_LEI) {
1648 op_orderI(L, l_lei, luai_numle, 0, TM_LE);
1649 vmbreak;
1650 }
1651 154 vmcase(OP_GTI) {
1652
3/6
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 5 taken 90 times.
✓ Branch 6 taken 64 times.
154 op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
1653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 vmbreak;
1654 }
1655 vmcase(OP_GEI) {
1656 op_orderI(L, l_gei, luai_numge, 1, TM_LE);
1657 vmbreak;
1658 }
1659 3 vmcase(OP_TEST) {
1660 3 StkId ra = RA(i);
1661
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
3 int cond = !l_isfalse(s2v(ra));
1662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 docondjump();
1663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 vmbreak;
1664 }
1665 vmcase(OP_TESTSET) {
1666 StkId ra = RA(i);
1667 TValue *rb = vRB(i);
1668 if (l_isfalse(rb) == GETARG_k(i))
1669 pc++;
1670 else {
1671 setobj2s(L, ra, rb);
1672 donextjump(ci);
1673 }
1674 vmbreak;
1675 }
1676 724 vmcase(OP_CALL) {
1677 724 StkId ra = RA(i);
1678 CallInfo *newci;
1679 724 int b = GETARG_B(i);
1680 724 int nresults = GETARG_C(i) - 1;
1681
2/2
✓ Branch 0 taken 645 times.
✓ Branch 1 taken 79 times.
724 if (b != 0) /* fixed number of arguments? */
1682 645 L->top.p = ra + b; /* top signals number of arguments */
1683 /* else previous instruction set top */
1684 724 savepc(L); /* in case of errors */
1685
2/2
✓ Branch 1 taken 568 times.
✓ Branch 2 taken 150 times.
724 if ((newci = luaD_precall(L, ra, nresults)) == NULL)
1686 568 updatetrap(ci); /* C call; nothing else to be done */
1687 else { /* Lua call: run function in this same C frame */
1688 150 ci = newci;
1689 150 goto startfunc;
1690 }
1691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 568 times.
568 vmbreak;
1692 }
1693 59 vmcase(OP_TAILCALL) {
1694 59 StkId ra = RA(i);
1695 59 int b = GETARG_B(i); /* number of arguments + 1 (function) */
1696 int n; /* number of results when calling a C function */
1697 59 int nparams1 = GETARG_C(i);
1698 /* delta is virtual 'func' - real 'func' (vararg functions) */
1699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
1700
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 if (b != 0)
1701 59 L->top.p = ra + b;
1702 else /* previous instruction set top */
1703 b = cast_int(L->top.p - ra);
1704 59 savepc(ci); /* several calls here can raise errors */
1705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (TESTARG_k(i)) {
1706 luaF_closeupval(L, base); /* close upvalues from current call */
1707 lua_assert(L->tbclist.p < base); /* no pending tbc variables */
1708 lua_assert(base == ci->func.p + 1);
1709 }
1710
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 18 times.
59 if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */
1711 41 goto startfunc; /* execute the callee */
1712 else { /* C function? */
1713 18 ci->func.p -= delta; /* restore 'func' (if vararg) */
1714 18 luaD_poscall(L, ci, n); /* finish caller */
1715 18 updatetrap(ci); /* 'luaD_poscall' can change hooks */
1716 18 goto ret; /* caller returns after the tail call */
1717 }
1718 }
1719 290 vmcase(OP_RETURN) {
1720 290 StkId ra = RA(i);
1721 290 int n = GETARG_B(i) - 1; /* number of results */
1722 290 int nparams1 = GETARG_C(i);
1723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (n < 0) /* not fixed? */
1724 n = cast_int(L->top.p - ra); /* get what is available */
1725 290 savepc(ci);
1726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (TESTARG_k(i)) { /* may there be open upvalues? */
1727 ci->u2.nres = n; /* save number of returns */
1728 if (L->top.p < ci->top.p)
1729 L->top.p = ci->top.p;
1730 luaF_close(L, base, CLOSEKTOP, 1);
1731 updatetrap(ci);
1732 updatestack(ci);
1733 }
1734
1/2
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
290 if (nparams1) /* vararg function? */
1735 290 ci->func.p -= ci->u.l.nextraargs + nparams1;
1736 290 L->top.p = ra + n; /* set call for 'luaD_poscall' */
1737 290 luaD_poscall(L, ci, n);
1738 290 updatetrap(ci); /* 'luaD_poscall' can change hooks */
1739 290 goto ret;
1740 }
1741 47 vmcase(OP_RETURN0) {
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (l_unlikely(L->hookmask)) {
1743 StkId ra = RA(i);
1744 L->top.p = ra;
1745 savepc(ci);
1746 luaD_poscall(L, ci, 0); /* no hurry... */
1747 trap = 1;
1748 }
1749 else { /* do the 'poscall' here */
1750 int nres;
1751 47 L->ci = ci->previous; /* back to caller */
1752 47 L->top.p = base - 1;
1753
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 47 times.
50 for (nres = ci->nresults; l_unlikely(nres > 0); nres--)
1754 3 setnilvalue(s2v(L->top.p++)); /* all results are nil */
1755 }
1756 47 goto ret;
1757 }
1758 183 vmcase(OP_RETURN1) {
1759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (l_unlikely(L->hookmask)) {
1760 StkId ra = RA(i);
1761 L->top.p = ra + 1;
1762 savepc(ci);
1763 luaD_poscall(L, ci, 1); /* no hurry... */
1764 trap = 1;
1765 }
1766 else { /* do the 'poscall' here */
1767 183 int nres = ci->nresults;
1768 183 L->ci = ci->previous; /* back to caller */
1769
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 182 times.
183 if (nres == 0)
1770 1 L->top.p = base - 1; /* asked for no results */
1771 else {
1772 182 StkId ra = RA(i);
1773 182 setobjs2s(L, base - 1, ra); /* at least this result */
1774 182 L->top.p = base;
1775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
182 for (; l_unlikely(nres > 1); nres--)
1776 setnilvalue(s2v(L->top.p++)); /* complete missing results */
1777 }
1778 }
1779 182 ret: /* return from a Lua function */
1780
2/2
✓ Branch 0 taken 388 times.
✓ Branch 1 taken 150 times.
538 if (ci->callstatus & CIST_FRESH)
1781 388 return; /* end this frame */
1782 else {
1783 150 ci = ci->previous;
1784 150 goto returning; /* continue running caller in this frame */
1785 }
1786 }
1787 31 vmcase(OP_FORLOOP) {
1788 31 StkId ra = RA(i);
1789
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (ttisinteger(s2v(ra + 2))) { /* integer loop? */
1790 31 lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1)));
1791
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 7 times.
31 if (count > 0) { /* still more iterations? */
1792 24 lua_Integer step = ivalue(s2v(ra + 2));
1793 24 lua_Integer idx = ivalue(s2v(ra)); /* internal index */
1794 24 chgivalue(s2v(ra + 1), count - 1); /* update counter */
1795 24 idx = intop(+, idx, step); /* add step to index */
1796 24 chgivalue(s2v(ra), idx); /* update internal index */
1797 24 setivalue(s2v(ra + 3), idx); /* and control variable */
1798 24 pc -= GETARG_Bx(i); /* jump back */
1799 }
1800 }
1801 else if (floatforloop(ra)) /* float loop */
1802 pc -= GETARG_Bx(i); /* jump back */
1803 31 updatetrap(ci); /* allows a signal to break the loop */
1804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 vmbreak;
1805 }
1806 7 vmcase(OP_FORPREP) {
1807 7 StkId ra = RA(i);
1808 7 savestate(L, ci); /* in case of errors */
1809
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (forprep(L, ra))
1810 pc += GETARG_Bx(i) + 1; /* skip the loop */
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 vmbreak;
1812 }
1813 vmcase(OP_TFORPREP) {
1814 StkId ra = RA(i);
1815 /* create to-be-closed upvalue (if needed) */
1816 halfProtect(luaF_newtbcupval(L, ra + 3));
1817 pc += GETARG_Bx(i);
1818 i = *(pc++); /* go to next instruction */
1819 lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
1820 goto l_tforcall;
1821 }
1822 vmcase(OP_TFORCALL) {
1823 l_tforcall: {
1824 StkId ra = RA(i);
1825 /* 'ra' has the iterator function, 'ra + 1' has the state,
1826 'ra + 2' has the control variable, and 'ra + 3' has the
1827 to-be-closed variable. The call will use the stack after
1828 these values (starting at 'ra + 4')
1829 */
1830 /* push function, state, and control variable */
1831 memcpy(ra + 4, ra, 3 * sizeof(*ra));
1832 L->top.p = ra + 4 + 3;
1833 ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */
1834 updatestack(ci); /* stack may have changed */
1835 i = *(pc++); /* go to next instruction */
1836 lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
1837 goto l_tforloop;
1838 }}
1839 vmcase(OP_TFORLOOP) {
1840 l_tforloop: {
1841 StkId ra = RA(i);
1842 if (!ttisnil(s2v(ra + 4))) { /* continue loop? */
1843 setobjs2s(L, ra + 2, ra + 4); /* save control variable */
1844 pc -= GETARG_Bx(i); /* jump back */
1845 }
1846 vmbreak;
1847 }}
1848 105 vmcase(OP_SETLIST) {
1849 105 StkId ra = RA(i);
1850 105 int n = GETARG_B(i);
1851 105 unsigned int last = GETARG_C(i);
1852 105 Table *h = hvalue(s2v(ra));
1853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (n == 0)
1854 n = cast_int(L->top.p - ra) - 1; /* get up to the top */
1855 else
1856 105 L->top.p = ci->top.p; /* correct top in case of emergency GC */
1857 105 last += n;
1858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (TESTARG_k(i)) {
1859 last += GETARG_Ax(*pc) * (MAXARG_C + 1);
1860 pc++;
1861 }
1862
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
105 if (last > luaH_realasize(h)) /* needs more space? */
1863 luaH_resizearray(L, h, last); /* preallocate it at once */
1864
2/2
✓ Branch 0 taken 796 times.
✓ Branch 1 taken 105 times.
901 for (; n > 0; n--) {
1865 796 TValue *val = s2v(ra + n);
1866 796 setobj2t(L, &h->array[last - 1], val);
1867 796 last--;
1868
3/6
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 723 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 73 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
796 luaC_barrierback(L, obj2gco(h), val);
1869 }
1870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 vmbreak;
1871 }
1872 215 vmcase(OP_CLOSURE) {
1873 215 StkId ra = RA(i);
1874 215 Proto *p = cl->p->p[GETARG_Bx(i)];
1875 215 halfProtect(pushclosure(L, p, cl->upvals, base, ra));
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 checkGC(L, ra + 1);
1877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 vmbreak;
1878 }
1879 vmcase(OP_VARARG) {
1880 StkId ra = RA(i);
1881 int n = GETARG_C(i) - 1; /* required results */
1882 Protect(luaT_getvarargs(L, ci, ra, n));
1883 vmbreak;
1884 }
1885 296 vmcase(OP_VARARGPREP) {
1886 296 ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p));
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296 times.
296 if (l_unlikely(trap)) { /* previous "Protect" updated trap */
1888 luaD_hookcall(L, ci);
1889 L->oldpc = 1; /* next opcode will be seen as a "new" line */
1890 }
1891 296 updatebase(ci); /* function has new base after adjustment */
1892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296 times.
296 vmbreak;
1893 }
1894 vmcase(OP_EXTRAARG) {
1895 lua_assert(0);
1896 vmbreak;
1897 }
1898 }
1899 }
1900 }
1901
1902 /* }================================================================== */
1903