Coverage Report

Created: 2025-08-25 06:57

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