A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 534 lines 15 kB view raw
1/* 2** $Id: ldo.c,v 2.38.1.4 2012/01/18 02:27:10 roberto Exp $ 3** Stack and Call structure of Lua 4** See Copyright Notice in lua.h 5*/ 6 7 8/* #include <setjmp.h> */ 9#include <stdlib.h> 10#include <string.h> 11 12#define ldo_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 "lmem.h" 22#include "lobject.h" 23#include "lopcodes.h" 24#include "lparser.h" 25#include "lstate.h" 26#include "lstring.h" 27#include "ltable.h" 28#include "ltm.h" 29#include "lundump.h" 30#include "lvm.h" 31#include "lzio.h" 32 33 34 35 36/* 37** {====================================================== 38** Error-recovery functions 39** ======================================================= 40*/ 41 42 43/* chain list of long jump buffers */ 44struct lua_longjmp { 45 struct lua_longjmp *previous; 46 luai_jmpbuf b; 47 volatile int status; /* error code */ 48}; 49 50 51void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { 52 switch (errcode) { 53 case LUA_ERRMEM: { 54 ptrdiff_t oldtopr = savestack(L, oldtop); 55 setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, MEMERRMSG)); 56 break; 57 } 58 case LUA_ERRERR: { 59 ptrdiff_t oldtopr = savestack(L, oldtop); 60 setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, "error in error handling")); 61 break; 62 } 63 case LUA_ERRSYNTAX: 64 case LUA_ERRRUN: { 65 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ 66 break; 67 } 68 } 69 L->top = oldtop + 1; 70} 71 72 73static void restore_stack_limit (lua_State *L) { 74 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); 75 if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */ 76 int inuse = cast_int(L->ci - L->base_ci); 77 if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */ 78 luaD_reallocCI(L, LUAI_MAXCALLS); 79 } 80} 81 82 83static void resetstack (lua_State *L, int status) { 84 L->ci = L->base_ci; 85 L->base = L->ci->base; 86 luaF_close(L, L->base); /* close eventual pending closures */ 87 luaD_seterrorobj(L, status, L->base); 88 L->nCcalls = L->baseCcalls; 89 L->allowhook = 1; 90 restore_stack_limit(L); 91 L->errfunc = 0; 92 L->errorJmp = NULL; 93} 94 95 96void luaD_throw (lua_State *L, int errcode) { 97 unfixedstack(L); /* make sure the fixedstack & block_gc flags get reset. */ 98 unset_block_gc(L); 99 if (L->errorJmp) { 100 L->errorJmp->status = errcode; 101 LUAI_THROW(L, L->errorJmp); 102 } 103 else { 104 L->status = cast_byte(errcode); 105 if (G(L)->panic) { 106 resetstack(L, errcode); 107 lua_unlock(L); 108 G(L)->panic(L); 109 } 110 exit(EXIT_FAILURE); 111 } 112} 113 114 115int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { 116 struct lua_longjmp lj; 117 lj.status = 0; 118 lj.previous = L->errorJmp; /* chain new error handler */ 119 L->errorJmp = &lj; 120 LUAI_TRY(L, &lj, 121 (*f)(L, ud); 122 ); 123 L->errorJmp = lj.previous; /* restore old error handler */ 124 return lj.status; 125} 126 127/* }====================================================== */ 128 129 130static void correctstack (lua_State *L, TValue *oldstack) { 131 CallInfo *ci; 132 GCObject *up; 133 L->top = (L->top - oldstack) + L->stack; 134 for (up = L->openupval; up != NULL; up = up->gch.next) 135 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; 136 for (ci = L->base_ci; ci <= L->ci; ci++) { 137 ci->top = (ci->top - oldstack) + L->stack; 138 ci->base = (ci->base - oldstack) + L->stack; 139 ci->func = (ci->func - oldstack) + L->stack; 140 } 141 L->base = (L->base - oldstack) + L->stack; 142} 143 144 145void luaD_reallocstack (lua_State *L, int newsize) { 146 TValue *oldstack = L->stack; 147 int realsize = newsize + 1 + EXTRA_STACK; 148 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); 149 luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue); 150 L->stacksize = realsize; 151 L->stack_last = L->stack+newsize; 152 correctstack(L, oldstack); 153} 154 155 156void luaD_reallocCI (lua_State *L, int newsize) { 157 CallInfo *oldci = L->base_ci; 158 luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo); 159 L->size_ci = newsize; 160 L->ci = (L->ci - oldci) + L->base_ci; 161 L->end_ci = L->base_ci + L->size_ci - 1; 162} 163 164 165void luaD_growstack (lua_State *L, int n) { 166 if (n <= L->stacksize) /* double size is enough? */ 167 luaD_reallocstack(L, 2*L->stacksize); 168 else 169 luaD_reallocstack(L, L->stacksize + n); 170} 171 172 173static CallInfo *growCI (lua_State *L) { 174 if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */ 175 luaD_throw(L, LUA_ERRERR); 176 else { 177 luaD_reallocCI(L, 2*L->size_ci); 178 if (L->size_ci > LUAI_MAXCALLS) 179 luaG_runerror(L, "stack overflow"); 180 } 181 return ++L->ci; 182} 183 184 185void luaD_callhook (lua_State *L, int event, int line) { 186 lua_Hook hook = L->hook; 187 if (hook && L->allowhook) { 188 ptrdiff_t top = savestack(L, L->top); 189 ptrdiff_t ci_top = savestack(L, L->ci->top); 190 lua_Debug ar; 191 ar.event = event; 192 ar.currentline = line; 193 if (event == LUA_HOOKTAILRET) 194 ar.i_ci = 0; /* tail call; no debug information about it */ 195 else 196 ar.i_ci = cast_int(L->ci - L->base_ci); 197 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 198 L->ci->top = L->top + LUA_MINSTACK; 199 lua_assert(L->ci->top <= L->stack_last); 200 L->allowhook = 0; /* cannot call hooks inside a hook */ 201 lua_unlock(L); 202 (*hook)(L, &ar); 203 lua_lock(L); 204 lua_assert(!L->allowhook); 205 L->allowhook = 1; 206 L->ci->top = restorestack(L, ci_top); 207 L->top = restorestack(L, top); 208 } 209} 210 211 212static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { 213 int i; 214 int nfixargs = p->numparams; 215#if defined(LUA_COMPAT_VARARG) 216 Table *htab = NULL; 217#endif 218 StkId base, fixed; 219 for (; actual < nfixargs; ++actual) 220 setnilvalue(L->top++); 221#if defined(LUA_COMPAT_VARARG) 222 if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */ 223 int nvar = actual - nfixargs; /* number of extra arguments */ 224 lua_assert(p->is_vararg & VARARG_HASARG); 225 luaC_checkGC(L); 226 luaD_checkstack(L, p->maxstacksize); 227 htab = luaH_new(L, nvar, 1); /* create `arg' table */ 228 sethvalue2s(L, L->top, htab); /* put table on stack */ 229 incr_top(L); 230 fixedstack(L); 231 for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */ 232 setobj2n(L, luaH_setnum(L, htab, i+1), L->top - 1 - nvar + i); 233 unfixedstack(L); 234 /* store counter in field `n' */ 235 setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar)); 236 L->top--; /* remove table from stack */ 237 } 238#endif 239 /* move fixed parameters to final position */ 240 fixed = L->top - actual; /* first fixed argument */ 241 base = L->top; /* final position of first argument */ 242 for (i=0; i<nfixargs; i++) { 243 setobjs2s(L, L->top++, fixed+i); 244 setnilvalue(fixed+i); 245 } 246#if defined(LUA_COMPAT_VARARG) 247 /* add `arg' parameter */ 248 if (htab) { 249 sethvalue(L, L->top++, htab); 250 lua_assert(iswhite(obj2gco(htab))); 251 } 252#endif 253 return base; 254} 255 256 257static StkId tryfuncTM (lua_State *L, StkId func) { 258 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); 259 StkId p; 260 ptrdiff_t funcr = savestack(L, func); 261 if (!ttisfunction(tm)) 262 luaG_typeerror(L, func, "call"); 263 /* Open a hole inside the stack at `func' */ 264 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); 265 incr_top(L); 266 func = restorestack(L, funcr); /* previous call may change stack */ 267 setobj2s(L, func, tm); /* tag method is the new function to be called */ 268 return func; 269} 270 271 272 273#define inc_ci(L) \ 274 ((L->ci == L->end_ci) ? growCI(L) : \ 275 (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci)) 276 277 278int luaD_precall (lua_State *L, StkId func, int nresults) { 279 LClosure *cl; 280 ptrdiff_t funcr; 281 if (!ttisfunction(func)) /* `func' is not a function? */ 282 func = tryfuncTM(L, func); /* check the `function' tag method */ 283 funcr = savestack(L, func); 284 cl = &clvalue(func)->l; 285 L->ci->savedpc = L->savedpc; 286 if (!cl->isC) { /* Lua function? prepare its call */ 287 CallInfo *ci; 288 StkId st, base; 289 Proto *p = cl->p; 290 luaD_checkstack(L, p->maxstacksize); 291 func = restorestack(L, funcr); 292 if (!p->is_vararg) { /* no varargs? */ 293 base = func + 1; 294 if (L->top > base + p->numparams) 295 L->top = base + p->numparams; 296 } 297 else { /* vararg function */ 298 int nargs = cast_int(L->top - func) - 1; 299 base = adjust_varargs(L, p, nargs); 300 func = restorestack(L, funcr); /* previous call may change the stack */ 301 } 302 ci = inc_ci(L); /* now `enter' new function */ 303 ci->func = func; 304 L->base = ci->base = base; 305 ci->top = L->base + p->maxstacksize; 306 lua_assert(ci->top <= L->stack_last); 307 L->savedpc = p->code; /* starting point */ 308 ci->tailcalls = 0; 309 ci->nresults = nresults; 310 for (st = L->top; st < ci->top; st++) 311 setnilvalue(st); 312 L->top = ci->top; 313 if (L->hookmask & LUA_MASKCALL) { 314 L->savedpc++; /* hooks assume 'pc' is already incremented */ 315 luaD_callhook(L, LUA_HOOKCALL, -1); 316 L->savedpc--; /* correct 'pc' */ 317 } 318 return PCRLUA; 319 } 320 else { /* if is a C function, call it */ 321 CallInfo *ci; 322 int n; 323 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 324 ci = inc_ci(L); /* now `enter' new function */ 325 ci->func = restorestack(L, funcr); 326 L->base = ci->base = ci->func + 1; 327 ci->top = L->top + LUA_MINSTACK; 328 lua_assert(ci->top <= L->stack_last); 329 ci->nresults = nresults; 330 if (L->hookmask & LUA_MASKCALL) 331 luaD_callhook(L, LUA_HOOKCALL, -1); 332 lua_unlock(L); 333 n = (*curr_func(L)->c.f)(L); /* do the actual call */ 334 lua_lock(L); 335 if (n < 0) /* yielding? */ 336 return PCRYIELD; 337 else { 338 luaD_poscall(L, L->top - n); 339 return PCRC; 340 } 341 } 342} 343 344 345static StkId callrethooks (lua_State *L, StkId firstResult) { 346 ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ 347 luaD_callhook(L, LUA_HOOKRET, -1); 348 if (f_isLua(L->ci)) { /* Lua function? */ 349 while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */ 350 luaD_callhook(L, LUA_HOOKTAILRET, -1); 351 } 352 return restorestack(L, fr); 353} 354 355 356int luaD_poscall (lua_State *L, StkId firstResult) { 357 StkId res; 358 int wanted, i; 359 CallInfo *ci; 360 if (L->hookmask & LUA_MASKRET) 361 firstResult = callrethooks(L, firstResult); 362 ci = L->ci--; 363 res = ci->func; /* res == final position of 1st result */ 364 wanted = ci->nresults; 365 L->base = (ci - 1)->base; /* restore base */ 366 L->savedpc = (ci - 1)->savedpc; /* restore savedpc */ 367 /* move results to correct place */ 368 for (i = wanted; i != 0 && firstResult < L->top; i--) 369 setobjs2s(L, res++, firstResult++); 370 while (i-- > 0) 371 setnilvalue(res++); 372 L->top = res; 373 return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ 374} 375 376 377/* 378** Call a function (C or Lua). The function to be called is at *func. 379** The arguments are on the stack, right after the function. 380** When returns, all the results are on the stack, starting at the original 381** function position. 382*/ 383void luaD_call (lua_State *L, StkId func, int nResults) { 384 if (++L->nCcalls >= LUAI_MAXCCALLS) { 385 if (L->nCcalls == LUAI_MAXCCALLS) 386 luaG_runerror(L, "C stack overflow"); 387 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) 388 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ 389 } 390 if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ 391 luaV_execute(L, 1); /* call it */ 392 L->nCcalls--; 393 luaC_checkGC(L); 394} 395 396 397static void resume (lua_State *L, void *ud) { 398 StkId firstArg = cast(StkId, ud); 399 CallInfo *ci = L->ci; 400 if (L->status == 0) { /* start coroutine? */ 401 lua_assert(ci == L->base_ci && firstArg > L->base); 402 if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA) 403 return; 404 } 405 else { /* resuming from previous yield */ 406 lua_assert(L->status == LUA_YIELD); 407 L->status = 0; 408 if (!f_isLua(ci)) { /* `common' yield? */ 409 /* finish interrupted execution of `OP_CALL' */ 410 lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || 411 GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); 412 if (luaD_poscall(L, firstArg)) /* complete it... */ 413 L->top = L->ci->top; /* and correct top if not multiple results */ 414 } 415 else /* yielded inside a hook: just continue its execution */ 416 L->base = L->ci->base; 417 } 418 luaV_execute(L, cast_int(L->ci - L->base_ci)); 419} 420 421 422static int resume_error (lua_State *L, const char *msg) { 423 L->top = L->ci->base; 424 setsvalue2s(L, L->top, luaS_new(L, msg)); 425 incr_top(L); 426 lua_unlock(L); 427 return LUA_ERRRUN; 428} 429 430 431LUA_API int lua_resume (lua_State *L, int nargs) { 432 int status; 433 lua_lock(L); 434 if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci)) 435 return resume_error(L, "cannot resume non-suspended coroutine"); 436 if (L->nCcalls >= LUAI_MAXCCALLS) 437 return resume_error(L, "C stack overflow"); 438 luai_userstateresume(L, nargs); 439 lua_assert(L->errfunc == 0); 440 L->baseCcalls = ++L->nCcalls; 441 status = luaD_rawrunprotected(L, resume, L->top - nargs); 442 if (status != 0) { /* error? */ 443 L->status = cast_byte(status); /* mark thread as `dead' */ 444 luaD_seterrorobj(L, status, L->top); 445 L->ci->top = L->top; 446 } 447 else { 448 lua_assert(L->nCcalls == L->baseCcalls); 449 status = L->status; 450 } 451 --L->nCcalls; 452 lua_unlock(L); 453 return status; 454} 455 456 457LUA_API int lua_yield (lua_State *L, int nresults) { 458 luai_userstateyield(L, nresults); 459 lua_lock(L); 460 if (L->nCcalls > L->baseCcalls) 461 luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); 462 L->base = L->top - nresults; /* protect stack slots below */ 463 L->status = LUA_YIELD; 464 lua_unlock(L); 465 return -1; 466} 467 468 469int luaD_pcall (lua_State *L, Pfunc func, void *u, 470 ptrdiff_t old_top, ptrdiff_t ef) { 471 int status; 472 unsigned short oldnCcalls = L->nCcalls; 473 ptrdiff_t old_ci = saveci(L, L->ci); 474 lu_byte old_allowhooks = L->allowhook; 475 ptrdiff_t old_errfunc = L->errfunc; 476 L->errfunc = ef; 477 status = luaD_rawrunprotected(L, func, u); 478 if (status != 0) { /* an error occurred? */ 479 StkId oldtop = restorestack(L, old_top); 480 luaF_close(L, oldtop); /* close eventual pending closures */ 481 luaD_seterrorobj(L, status, oldtop); 482 L->nCcalls = oldnCcalls; 483 L->ci = restoreci(L, old_ci); 484 L->base = L->ci->base; 485 L->savedpc = L->ci->savedpc; 486 L->allowhook = old_allowhooks; 487 restore_stack_limit(L); 488 } 489 L->errfunc = old_errfunc; 490 return status; 491} 492 493 494 495/* 496** Execute a protected parser. 497*/ 498struct SParser { /* data to `f_parser' */ 499 ZIO *z; 500 Mbuffer buff; /* buffer to be used by the scanner */ 501 const char *name; 502}; 503 504static void f_parser (lua_State *L, void *ud) { 505 int i; 506 Proto *tf; 507 Closure *cl; 508 struct SParser *p = cast(struct SParser *, ud); 509 int c = luaZ_lookahead(p->z); 510 luaC_checkGC(L); 511 set_block_gc(L); /* stop collector during parsing */ 512 tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, 513 &p->buff, p->name); 514 cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); 515 cl->l.p = tf; 516 for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ 517 cl->l.upvals[i] = luaF_newupval(L); 518 setclvalue(L, L->top, cl); 519 incr_top(L); 520 unset_block_gc(L); 521} 522 523 524int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { 525 struct SParser p; 526 int status; 527 p.z = z; p.name = name; 528 luaZ_initbuffer(L, &p.buff); 529 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); 530 luaZ_freebuffer(L, &p.buff); 531 return status; 532} 533 534