A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2** $Id: lvm.c,v 2.63.1.5 2011/08/17 20:43:11 roberto Exp $
3** Lua virtual machine
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define lvm_c
13#define LUA_CORE
14
15#include "lua.h"
16
17#include "ldebug.h"
18#include "ldo.h"
19#include "lfunc.h"
20#include "lgc.h"
21#include "lobject.h"
22#include "lopcodes.h"
23#include "lstate.h"
24#include "lstring.h"
25#include "ltable.h"
26#include "ltm.h"
27#include "lvm.h"
28
29
30
31/* limit for table tag-method chains (to avoid loops) */
32#define MAXTAGLOOP 100
33
34
35const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
36 lua_Number num;
37 if (ttisnumber(obj)) return obj;
38 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
39 setnvalue(n, num);
40 return n;
41 }
42 else
43 return NULL;
44}
45
46
47int luaV_tostring (lua_State *L, StkId obj) {
48 if (!ttisnumber(obj))
49 return 0;
50 else {
51 char s[LUAI_MAXNUMBER2STR];
52 ptrdiff_t objr = savestack(L, obj);
53 lua_Number n = nvalue(obj);
54 lua_number2str(s, n);
55 setsvalue2s(L, restorestack(L, objr), luaS_new(L, s));
56 return 1;
57 }
58}
59
60
61static void traceexec (lua_State *L, const Instruction *pc) {
62 lu_byte mask = L->hookmask;
63 const Instruction *oldpc = L->savedpc;
64 L->savedpc = pc;
65 if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
66 resethookcount(L);
67 luaD_callhook(L, LUA_HOOKCOUNT, -1);
68 }
69 if (mask & LUA_MASKLINE) {
70 Proto *p = ci_func(L->ci)->l.p;
71 int npc = pcRel(pc, p);
72 int newline = getline(p, npc);
73 /* call linehook when enter a new function, when jump back (loop),
74 or when enter a new line */
75 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
76 luaD_callhook(L, LUA_HOOKLINE, newline);
77 }
78}
79
80
81static void callTMres (lua_State *L, StkId res, const TValue *f,
82 const TValue *p1, const TValue *p2) {
83 ptrdiff_t result = savestack(L, res);
84 setobj2s(L, L->top, f); /* push function */
85 setobj2s(L, L->top+1, p1); /* 1st argument */
86 setobj2s(L, L->top+2, p2); /* 2nd argument */
87 luaD_checkstack(L, 3);
88 L->top += 3;
89 luaD_call(L, L->top - 3, 1);
90 res = restorestack(L, result);
91 L->top--;
92 setobjs2s(L, res, L->top);
93}
94
95
96
97static void callTM (lua_State *L, const TValue *f, const TValue *p1,
98 const TValue *p2, const TValue *p3) {
99 setobj2s(L, L->top, f); /* push function */
100 setobj2s(L, L->top+1, p1); /* 1st argument */
101 setobj2s(L, L->top+2, p2); /* 2nd argument */
102 setobj2s(L, L->top+3, p3); /* 3th argument */
103 luaD_checkstack(L, 4);
104 L->top += 4;
105 luaD_call(L, L->top - 4, 0);
106}
107
108
109void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
110 int loop;
111 for (loop = 0; loop < MAXTAGLOOP; loop++) {
112 const TValue *tm;
113 if (ttistable(t)) { /* `t' is a table? */
114 Table *h = hvalue(t);
115 const TValue *res = luaH_get(h, key); /* do a primitive get */
116 if (!ttisnil(res) || /* result is no nil? */
117 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
118 setobj2s(L, val, res);
119 return;
120 }
121 /* else will try the tag method */
122 }
123 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
124 luaG_typeerror(L, t, "index");
125 if (ttisfunction(tm)) {
126 callTMres(L, val, tm, t, key);
127 return;
128 }
129 t = tm; /* else repeat with `tm' */
130 }
131 luaG_runerror(L, "loop in gettable");
132}
133
134
135void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
136 int loop;
137 TValue temp;
138 setnilvalue(L->top);
139 L->top++;
140 fixedstack(L);
141 for (loop = 0; loop < MAXTAGLOOP; loop++) {
142 const TValue *tm;
143 if (ttistable(t)) { /* `t' is a table? */
144 Table *h = hvalue(t);
145 TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
146 if (!ttisnil(oldval) || /* result is no nil? */
147 (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
148 L->top--;
149 unfixedstack(L);
150 setobj2t(L, oldval, val);
151 h->flags = 0;
152 luaC_barriert(L, h, val);
153 return;
154 }
155 /* else will try the tag method */
156 }
157 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
158 luaG_typeerror(L, t, "index");
159 if (ttisfunction(tm)) {
160 L->top--;
161 unfixedstack(L);
162 callTM(L, tm, t, key, val);
163 return;
164 }
165 /* else repeat with `tm' */
166 setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */
167 t = &temp;
168 setobj2s(L, L->top-1, t); /* need to protect value from EGC. */
169 }
170 luaG_runerror(L, "loop in settable");
171}
172
173
174static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
175 StkId res, TMS event) {
176 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
177 if (ttisnil(tm))
178 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
179 if (ttisnil(tm)) return 0;
180 callTMres(L, res, tm, p1, p2);
181 return 1;
182}
183
184
185static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
186 TMS event) {
187 const TValue *tm1 = fasttm(L, mt1, event);
188 const TValue *tm2;
189 if (tm1 == NULL) return NULL; /* no metamethod */
190 if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
191 tm2 = fasttm(L, mt2, event);
192 if (tm2 == NULL) return NULL; /* no metamethod */
193 if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
194 return tm1;
195 return NULL;
196}
197
198
199static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
200 TMS event) {
201 const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
202 const TValue *tm2;
203 if (ttisnil(tm1)) return -1; /* no metamethod? */
204 tm2 = luaT_gettmbyobj(L, p2, event);
205 if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
206 return -1;
207 callTMres(L, L->top, tm1, p1, p2);
208 return !l_isfalse(L->top);
209}
210
211
212static int l_strcmp (const TString *ls, const TString *rs) {
213 const char *l = getstr(ls);
214 size_t ll = ls->tsv.len;
215 const char *r = getstr(rs);
216 size_t lr = rs->tsv.len;
217 for (;;) {
218 int temp = strcoll(l, r);
219 if (temp != 0) return temp;
220 else { /* strings are equal up to a `\0' */
221 size_t len = strlen(l); /* index of first `\0' in both strings */
222 if (len == lr) /* r is finished? */
223 return (len == ll) ? 0 : 1;
224 else if (len == ll) /* l is finished? */
225 return -1; /* l is smaller than r (because r is not finished) */
226 /* both strings longer than `len'; go on comparing (after the `\0') */
227 len++;
228 l += len; ll -= len; r += len; lr -= len;
229 }
230 }
231}
232
233
234int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
235 int res;
236 if (ttype(l) != ttype(r))
237 return luaG_ordererror(L, l, r);
238 else if (ttisnumber(l))
239 return luai_numlt(nvalue(l), nvalue(r));
240 else if (ttisstring(l))
241 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
242 else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
243 return res;
244 return luaG_ordererror(L, l, r);
245}
246
247
248static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
249 int res;
250 if (ttype(l) != ttype(r))
251 return luaG_ordererror(L, l, r);
252 else if (ttisnumber(l))
253 return luai_numle(nvalue(l), nvalue(r));
254 else if (ttisstring(l))
255 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
256 else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
257 return res;
258 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
259 return !res;
260 return luaG_ordererror(L, l, r);
261}
262
263
264int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
265 const TValue *tm;
266 lua_assert(ttype(t1) == ttype(t2));
267 switch (ttype(t1)) {
268 case LUA_TNIL: return 1;
269 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
270 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
271 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
272 case LUA_TUSERDATA: {
273 if (uvalue(t1) == uvalue(t2)) return 1;
274 tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
275 TM_EQ);
276 break; /* will try TM */
277 }
278 case LUA_TTABLE: {
279 if (hvalue(t1) == hvalue(t2)) return 1;
280 tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
281 break; /* will try TM */
282 }
283 default: return gcvalue(t1) == gcvalue(t2);
284 }
285 if (tm == NULL) return 0; /* no TM? */
286 callTMres(L, L->top, tm, t1, t2); /* call TM */
287 return !l_isfalse(L->top);
288}
289
290
291void luaV_concat (lua_State *L, int total, int last) {
292 do {
293 /* Any call which does a memory allocation may trim the stack,
294 invalidating top unless the stack is fixed during the allocation */
295 StkId top = L->base + last + 1;
296 fixedstack(L);
297 int n = 2; /* number of elements handled in this pass (at least 2) */
298 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
299 unfixedstack(L);
300 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) {
301 /* restore 'top' pointer, since stack might have been reallocted */
302 top = L->base + last + 1;
303 luaG_concaterror(L, top-2, top-1);
304 }
305 } else if (tsvalue(top-1)->len == 0) { /* second op is empty? */
306 (void)tostring(L, top - 2); /* result is first op (as string) */
307 } else {
308 /* at least two string values; get as many as possible */
309 size_t tl = tsvalue(top-1)->len;
310 char *buffer;
311 int i;
312 /* collect total length */
313 for (n = 1; n < total && tostring(L, top-n-1); n++) {
314 size_t l = tsvalue(top-n-1)->len;
315 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
316 tl += l;
317 }
318 G(L)->buff.n = tl;
319 buffer = luaZ_openspace(L, &G(L)->buff, tl);
320 tl = 0;
321 for (i=n; i>0; i--) { /* concat all strings */
322 size_t l = tsvalue(top-i)->len;
323 memcpy(buffer+tl, svalue(top-i), l);
324 tl += l;
325 }
326 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
327 luaZ_resetbuffer(&G(L)->buff);
328 }
329 total -= n-1; /* got `n' strings to create 1 new */
330 last -= n-1;
331 unfixedstack(L);
332 } while (total > 1); /* repeat until only 1 result left */
333}
334
335
336static void Arith (lua_State *L, StkId ra, const TValue *rb,
337 const TValue *rc, TMS op) {
338 TValue tempb, tempc;
339 const TValue *b, *c;
340 if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
341 (c = luaV_tonumber(rc, &tempc)) != NULL) {
342 lua_Number nb = nvalue(b), nc = nvalue(c);
343 switch (op) {
344 case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
345 case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
346 case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
347 case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
348 case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
349 case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
350 case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
351 default: lua_assert(0); break;
352 }
353 }
354 else {
355 ptrdiff_t br = savestack(L, rb);
356 ptrdiff_t cr = savestack(L, rc);
357 if (!call_binTM(L, rb, rc, ra, op)) {
358 luaG_aritherror(L, restorestack(L, br), restorestack(L, cr));
359 }
360 }
361}
362
363
364
365/*
366** some macros for common tasks in `luaV_execute'
367*/
368
369#define runtime_check(L, c) { if (!(c)) break; }
370
371#define RA(i) (base+GETARG_A(i))
372/* to be used after possible stack reallocation */
373#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
374#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
375#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
376 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
377#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
378 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
379#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
380
381
382#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
383
384
385#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
386
387
388#define arith_op(op,tm) { \
389 TValue *rb = RKB(i); \
390 TValue *rc = RKC(i); \
391 if (ttisnumber(rb) && ttisnumber(rc)) { \
392 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
393 setnvalue(ra, op(nb, nc)); \
394 } \
395 else \
396 Protect(Arith(L, ra, rb, rc, tm)); \
397 }
398
399
400
401void luaV_execute (lua_State *L, int nexeccalls) {
402 LClosure *cl;
403 StkId base;
404 TValue *k;
405 const Instruction *pc;
406 reentry: /* entry point */
407 lua_assert(isLua(L->ci));
408 pc = L->savedpc;
409 cl = &clvalue(L->ci->func)->l;
410 base = L->base;
411 k = cl->p->k;
412 /* main loop of interpreter */
413 for (;;) {
414 const Instruction i = *pc++;
415 StkId ra;
416 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
417 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
418 traceexec(L, pc);
419 if (L->status == LUA_YIELD) { /* did hook yield? */
420 L->savedpc = pc - 1;
421 return;
422 }
423 base = L->base;
424 }
425 /* warning!! several calls may realloc the stack and invalidate `ra' */
426 ra = RA(i);
427 lua_assert(base == L->base && L->base == L->ci->base);
428 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
429 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
430 switch (GET_OPCODE(i)) {
431 case OP_MOVE: {
432 setobjs2s(L, ra, RB(i));
433 continue;
434 }
435 case OP_LOADK: {
436 setobj2s(L, ra, KBx(i));
437 continue;
438 }
439 case OP_LOADBOOL: {
440 setbvalue(ra, GETARG_B(i));
441 if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
442 continue;
443 }
444 case OP_LOADNIL: {
445 TValue *rb = RB(i);
446 do {
447 setnilvalue(rb--);
448 } while (rb >= ra);
449 continue;
450 }
451 case OP_GETUPVAL: {
452 int b = GETARG_B(i);
453 setobj2s(L, ra, cl->upvals[b]->v);
454 continue;
455 }
456 case OP_GETGLOBAL: {
457 TValue g;
458 TValue *rb = KBx(i);
459 sethvalue(L, &g, cl->env);
460 lua_assert(ttisstring(rb));
461 Protect(luaV_gettable(L, &g, rb, ra));
462 continue;
463 }
464 case OP_GETTABLE: {
465 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
466 continue;
467 }
468 case OP_SETGLOBAL: {
469 TValue g;
470 sethvalue(L, &g, cl->env);
471 lua_assert(ttisstring(KBx(i)));
472 Protect(luaV_settable(L, &g, KBx(i), ra));
473 continue;
474 }
475 case OP_SETUPVAL: {
476 UpVal *uv = cl->upvals[GETARG_B(i)];
477 setobj(L, uv->v, ra);
478 luaC_barrier(L, uv, ra);
479 continue;
480 }
481 case OP_SETTABLE: {
482 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
483 continue;
484 }
485 case OP_NEWTABLE: {
486 int b = GETARG_B(i);
487 int c = GETARG_C(i);
488 Table *h;
489 Protect(h = luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
490 sethvalue(L, RA(i), h);
491 Protect(luaC_checkGC(L));
492 continue;
493 }
494 case OP_SELF: {
495 StkId rb = RB(i);
496 setobjs2s(L, ra+1, rb);
497 Protect(luaV_gettable(L, rb, RKC(i), ra));
498 continue;
499 }
500 case OP_ADD: {
501 arith_op(luai_numadd, TM_ADD);
502 continue;
503 }
504 case OP_SUB: {
505 arith_op(luai_numsub, TM_SUB);
506 continue;
507 }
508 case OP_MUL: {
509 arith_op(luai_nummul, TM_MUL);
510 continue;
511 }
512 case OP_DIV: {
513 /* ROCKLUA INTEGER BUGFIX */
514 TValue *rb = RKB(i);
515 TValue *rc = RKC(i);
516 if (ttisnumber(rb) && ttisnumber(rc)) {
517 lua_Number nb = nvalue(rb), nc = nvalue(rc);
518 if (nc == 0)
519 luaG_typeerror(L, rc, "divide by zero");
520
521 setnvalue(ra, luai_numdiv(nb, nc));
522 }
523 else
524 Protect(Arith(L, ra, rb, rc, TM_DIV));
525
526 continue;
527 }
528 case OP_MOD: {
529 /* ROCKLUA INTEGER BUGFIX */
530 TValue *rb = RKB(i);
531 TValue *rc = RKC(i);
532 if (ttisnumber(rb) && ttisnumber(rc)) {
533 lua_Number nb = nvalue(rb), nc = nvalue(rc);
534 if (nc == 0)
535 luaG_typeerror(L, rc, "perform 'n%0'");
536
537 setnvalue(ra, luai_nummod(nb, nc));
538 }
539 else
540 Protect(Arith(L, ra, rb, rc, TM_MOD));
541
542 continue;
543 }
544 case OP_POW: {
545 arith_op(luai_numpow, TM_POW);
546 continue;
547 }
548 case OP_UNM: {
549 TValue *rb = RB(i);
550 if (ttisnumber(rb)) {
551 lua_Number nb = nvalue(rb);
552 setnvalue(ra, luai_numunm(nb));
553 }
554 else {
555 Protect(Arith(L, ra, rb, rb, TM_UNM));
556 }
557 continue;
558 }
559 case OP_NOT: {
560 int res = l_isfalse(RB(i)); /* next assignment may change this value */
561 setbvalue(ra, res);
562 continue;
563 }
564 case OP_LEN: {
565 const TValue *rb = RB(i);
566 switch (ttype(rb)) {
567 case LUA_TTABLE: {
568 setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
569 break;
570 }
571 case LUA_TSTRING: {
572 setnvalue(ra, cast_num(tsvalue(rb)->len));
573 break;
574 }
575 default: { /* try metamethod */
576 ptrdiff_t br = savestack(L, rb);
577 Protect(
578 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
579 luaG_typeerror(L, restorestack(L, br), "get length of");
580 )
581 }
582 }
583 continue;
584 }
585 case OP_CONCAT: {
586 int b = GETARG_B(i);
587 int c = GETARG_C(i);
588 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
589 setobjs2s(L, RA(i), base+b);
590 continue;
591 }
592 case OP_JMP: {
593 dojump(L, pc, GETARG_sBx(i));
594 continue;
595 }
596 case OP_EQ: {
597 TValue *rb = RKB(i);
598 TValue *rc = RKC(i);
599 Protect(
600 if (equalobj(L, rb, rc) == GETARG_A(i))
601 dojump(L, pc, GETARG_sBx(*pc));
602 )
603 pc++;
604 continue;
605 }
606 case OP_LT: {
607 Protect(
608 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
609 dojump(L, pc, GETARG_sBx(*pc));
610 )
611 pc++;
612 continue;
613 }
614 case OP_LE: {
615 Protect(
616 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
617 dojump(L, pc, GETARG_sBx(*pc));
618 )
619 pc++;
620 continue;
621 }
622 case OP_TEST: {
623 if (l_isfalse(ra) != GETARG_C(i))
624 dojump(L, pc, GETARG_sBx(*pc));
625 pc++;
626 continue;
627 }
628 case OP_TESTSET: {
629 TValue *rb = RB(i);
630 if (l_isfalse(rb) != GETARG_C(i)) {
631 setobjs2s(L, ra, rb);
632 dojump(L, pc, GETARG_sBx(*pc));
633 }
634 pc++;
635 continue;
636 }
637 case OP_CALL: {
638 int b = GETARG_B(i);
639 int nresults = GETARG_C(i) - 1;
640 if (b != 0) L->top = ra+b; /* else previous instruction set top */
641 L->savedpc = pc;
642 switch (luaD_precall(L, ra, nresults)) {
643 case PCRLUA: {
644 nexeccalls++;
645 goto reentry; /* restart luaV_execute over new Lua function */
646 }
647 case PCRC: {
648 /* it was a C function (`precall' called it); adjust results */
649 if (nresults >= 0) L->top = L->ci->top;
650 base = L->base;
651 continue;
652 }
653 default: {
654 return; /* yield */
655 }
656 }
657 }
658 case OP_TAILCALL: {
659 int b = GETARG_B(i);
660 if (b != 0) L->top = ra+b; /* else previous instruction set top */
661 L->savedpc = pc;
662 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
663 switch (luaD_precall(L, ra, LUA_MULTRET)) {
664 case PCRLUA: {
665 /* tail call: put new frame in place of previous one */
666 CallInfo *ci = L->ci - 1; /* previous frame */
667 int aux;
668 StkId func = ci->func;
669 StkId pfunc = (ci+1)->func; /* previous function index */
670 if (L->openupval) luaF_close(L, ci->base);
671 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
672 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
673 setobjs2s(L, func+aux, pfunc+aux);
674 ci->top = L->top = func+aux; /* correct top */
675 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
676 ci->savedpc = L->savedpc;
677 ci->tailcalls++; /* one more call lost */
678 L->ci--; /* remove new frame */
679 goto reentry;
680 }
681 case PCRC: { /* it was a C function (`precall' called it) */
682 base = L->base;
683 continue;
684 }
685 default: {
686 return; /* yield */
687 }
688 }
689 }
690 case OP_RETURN: {
691 int b = GETARG_B(i);
692 if (b != 0) L->top = ra+b-1;
693 if (L->openupval) luaF_close(L, base);
694 L->savedpc = pc;
695 b = luaD_poscall(L, ra);
696 if (--nexeccalls == 0) /* was previous function running `here'? */
697 return; /* no: return */
698 else { /* yes: continue its execution */
699 if (b) L->top = L->ci->top;
700 lua_assert(isLua(L->ci));
701 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
702 goto reentry;
703 }
704 }
705 case OP_FORLOOP: {
706 lua_Number step = nvalue(ra+2);
707 lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
708 lua_Number limit = nvalue(ra+1);
709 if (luai_numlt(0, step) ? luai_numle(idx, limit)
710 : luai_numle(limit, idx)) {
711 dojump(L, pc, GETARG_sBx(i)); /* jump back */
712 setnvalue(ra, idx); /* update internal index... */
713 setnvalue(ra+3, idx); /* ...and external index */
714 }
715 continue;
716 }
717 case OP_FORPREP: {
718 const TValue *init = ra;
719 const TValue *plimit = ra+1;
720 const TValue *pstep = ra+2;
721 L->savedpc = pc; /* next steps may throw errors */
722 if (!tonumber(init, ra))
723 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
724 else if (!tonumber(plimit, ra+1))
725 luaG_runerror(L, LUA_QL("for") " limit must be a number");
726 else if (!tonumber(pstep, ra+2))
727 luaG_runerror(L, LUA_QL("for") " step must be a number");
728 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
729 dojump(L, pc, GETARG_sBx(i));
730 continue;
731 }
732 case OP_TFORLOOP: {
733 StkId cb = ra + 3; /* call base */
734 setobjs2s(L, cb+2, ra+2);
735 setobjs2s(L, cb+1, ra+1);
736 setobjs2s(L, cb, ra);
737 L->top = cb+3; /* func. + 2 args (state and index) */
738 Protect(luaD_call(L, cb, GETARG_C(i)));
739 L->top = L->ci->top;
740 cb = RA(i) + 3; /* previous call may change the stack */
741 if (!ttisnil(cb)) { /* continue loop? */
742 setobjs2s(L, cb-1, cb); /* save control variable */
743 dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
744 }
745 pc++;
746 continue;
747 }
748 case OP_SETLIST: {
749 int n = GETARG_B(i);
750 int c = GETARG_C(i);
751 int last;
752 Table *h;
753 fixedstack(L);
754 if (n == 0) {
755 n = cast_int(L->top - ra) - 1;
756 L->top = L->ci->top;
757 }
758 if (c == 0) c = cast_int(*pc++);
759 runtime_check(L, ttistable(ra));
760 h = hvalue(ra);
761 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
762 if (last > h->sizearray) /* needs more space? */
763 luaH_resizearray(L, h, last); /* pre-alloc it at once */
764 for (; n > 0; n--) {
765 TValue *val = ra+n;
766 setobj2t(L, luaH_setnum(L, h, last--), val);
767 luaC_barriert(L, h, val);
768 }
769 unfixedstack(L);
770 continue;
771 }
772 case OP_CLOSE: {
773 luaF_close(L, ra);
774 continue;
775 }
776 case OP_CLOSURE: {
777 Proto *p;
778 Closure *ncl;
779 int nup, j;
780 p = cl->p->p[GETARG_Bx(i)];
781 nup = p->nups;
782 fixedstack(L);
783 ncl = luaF_newLclosure(L, nup, cl->env);
784 setclvalue(L, ra, ncl);
785 ncl->l.p = p;
786 for (j=0; j<nup; j++, pc++) {
787 if (GET_OPCODE(*pc) == OP_GETUPVAL)
788 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
789 else {
790 lua_assert(GET_OPCODE(*pc) == OP_MOVE);
791 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
792 }
793 }
794 unfixedstack(L);
795 Protect(luaC_checkGC(L));
796 continue;
797 }
798 case OP_VARARG: {
799 int b = GETARG_B(i) - 1;
800 int j;
801 CallInfo *ci = L->ci;
802 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
803 if (b == LUA_MULTRET) {
804 Protect(luaD_checkstack(L, n));
805 ra = RA(i); /* previous call may change the stack */
806 b = n;
807 L->top = ra + n;
808 }
809 for (j = 0; j < b; j++) {
810 if (j < n) {
811 setobjs2s(L, ra + j, ci->base - n + j);
812 }
813 else {
814 setnilvalue(ra + j);
815 }
816 }
817 continue;
818 }
819 }
820 }
821}
822