1/* 2** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $ 3** Stack and Call structure of Lua 4** See Copyright Notice in lua.h 5*/ 6 7 8#include <sys/zfs_context.h> 9 10#define ldo_c 11#define LUA_CORE 12 13#include "lua.h" 14 15#include "lapi.h" 16#include "ldebug.h" 17#include "ldo.h" 18#include "lfunc.h" 19#include "lgc.h" 20#include "lmem.h" 21#include "lobject.h" 22#include "lopcodes.h" 23#include "lparser.h" 24#include "lstate.h" 25#include "lstring.h" 26#include "ltable.h" 27#include "ltm.h" 28#include "lundump.h" 29#include "lvm.h" 30#include "lzio.h" 31 32 33 34 35/* 36** {====================================================== 37** Error-recovery functions 38** ======================================================= 39*/ 40 41/* 42** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By 43** default, Lua handles errors with exceptions when compiling as 44** C++ code, with _longjmp/_setjmp when asked to use them, and with 45** longjmp/setjmp otherwise. 46*/ 47#if !defined(LUAI_THROW) 48 49#ifdef _KERNEL 50#ifdef illumos 51#define LUAI_THROW(L,c) longjmp(&(c)->b) 52#define LUAI_TRY(L,c,a) if (setjmp(&(c)->b) == 0) { a } 53#define luai_jmpbuf label_t 54#else 55#define LUAI_THROW(L,c) longjmp((c)->b, 1) 56#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } 57#define luai_jmpbuf jmp_buf 58#endif 59#else 60#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) 61/* C++ exceptions */ 62#define LUAI_THROW(L,c) throw(c) 63#define LUAI_TRY(L,c,a) \ 64 try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } 65#define luai_jmpbuf int /* dummy variable */ 66 67#elif defined(LUA_USE_ULONGJMP) 68/* in Unix, try _longjmp/_setjmp (more efficient) */ 69#define LUAI_THROW(L,c) _longjmp((c)->b, 1) 70#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } 71#define luai_jmpbuf jmp_buf 72 73#else 74/* default handling with long jumps */ 75#define LUAI_THROW(L,c) longjmp((c)->b, 1) 76#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } 77#define luai_jmpbuf jmp_buf 78 79#endif 80 81#endif 82 83#endif 84 85 86/* chain list of long jump buffers */ 87struct lua_longjmp { 88 struct lua_longjmp *previous; 89 luai_jmpbuf b; 90 volatile int status; /* error code */ 91}; 92 93 94static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { 95 switch (errcode) { 96 case LUA_ERRMEM: { /* memory error? */ 97 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ 98 break; 99 } 100 case LUA_ERRERR: { 101 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); 102 break; 103 } 104 default: { 105 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ 106 break; 107 } 108 } 109 L->top = oldtop + 1; 110} 111 112 113l_noret luaD_throw (lua_State *L, int errcode) { 114 if (L->errorJmp) { /* thread has an error handler? */ 115 L->errorJmp->status = errcode; /* set status */ 116 LUAI_THROW(L, L->errorJmp); /* jump to it */ 117 } 118 else { /* thread has no error handler */ 119 L->status = cast_byte(errcode); /* mark it as dead */ 120 if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ 121 setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ 122 luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ 123 } 124 else { /* no handler at all; abort */ 125 if (G(L)->panic) { /* panic function? */ 126 lua_unlock(L); 127 G(L)->panic(L); /* call it (last chance to jump out) */ 128 } 129 panic("no error handler"); 130 } 131 } 132} 133 134 135int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { 136 unsigned short oldnCcalls = L->nCcalls; 137 struct lua_longjmp lj; 138 lj.status = LUA_OK; 139 lj.previous = L->errorJmp; /* chain new error handler */ 140 L->errorJmp = &lj; 141 LUAI_TRY(L, &lj, 142 (*f)(L, ud); 143 ); 144 L->errorJmp = lj.previous; /* restore old error handler */ 145 L->nCcalls = oldnCcalls; 146 return lj.status; 147} 148 149/* }====================================================== */ 150 151 152static void correctstack (lua_State *L, TValue *oldstack) { 153 CallInfo *ci; 154 GCObject *up; 155 L->top = (L->top - oldstack) + L->stack; 156 for (up = L->openupval; up != NULL; up = up->gch.next) 157 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; 158 for (ci = L->ci; ci != NULL; ci = ci->previous) { 159 ci->top = (ci->top - oldstack) + L->stack; 160 ci->func = (ci->func - oldstack) + L->stack; 161 if (isLua(ci)) 162 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; 163 } 164} 165 166 167/* some space for error handling */ 168#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) 169 170 171void luaD_reallocstack (lua_State *L, int newsize) { 172 TValue *oldstack = L->stack; 173 int lim = L->stacksize; 174 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); 175 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); 176 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); 177 for (; lim < newsize; lim++) 178 setnilvalue(L->stack + lim); /* erase new segment */ 179 L->stacksize = newsize; 180 L->stack_last = L->stack + newsize - EXTRA_STACK; 181 correctstack(L, oldstack); 182} 183 184 185void luaD_growstack (lua_State *L, int n) { 186 int size = L->stacksize; 187 if (size > LUAI_MAXSTACK) /* error after extra size? */ 188 luaD_throw(L, LUA_ERRERR); 189 else { 190 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; 191 int newsize = 2 * size; 192 if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; 193 if (newsize < needed) newsize = needed; 194 if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ 195 luaD_reallocstack(L, ERRORSTACKSIZE); 196 luaG_runerror(L, "stack overflow"); 197 } 198 else 199 luaD_reallocstack(L, newsize); 200 } 201} 202 203 204static int stackinuse (lua_State *L) { 205 CallInfo *ci; 206 StkId lim = L->top; 207 for (ci = L->ci; ci != NULL; ci = ci->previous) { 208 lua_assert(ci->top <= L->stack_last); 209 if (lim < ci->top) lim = ci->top; 210 } 211 return cast_int(lim - L->stack) + 1; /* part of stack in use */ 212} 213 214 215void luaD_shrinkstack (lua_State *L) { 216 int inuse = stackinuse(L); 217 int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; 218 if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; 219 if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */ 220 goodsize >= L->stacksize) /* would grow instead of shrink? */ 221 condmovestack(L); /* don't change stack (change only for debugging) */ 222 else 223 luaD_reallocstack(L, goodsize); /* shrink it */ 224} 225 226 227void luaD_hook (lua_State *L, int event, int line) { 228 lua_Hook hook = L->hook; 229 if (hook && L->allowhook) { 230 CallInfo *ci = L->ci; 231 ptrdiff_t top = savestack(L, L->top); 232 ptrdiff_t ci_top = savestack(L, ci->top); 233 lua_Debug ar; 234 ar.event = event; 235 ar.currentline = line; 236 ar.i_ci = ci; 237 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 238 ci->top = L->top + LUA_MINSTACK; 239 lua_assert(ci->top <= L->stack_last); 240 L->allowhook = 0; /* cannot call hooks inside a hook */ 241 ci->callstatus |= CIST_HOOKED; 242 lua_unlock(L); 243 (*hook)(L, &ar); 244 lua_lock(L); 245 lua_assert(!L->allowhook); 246 L->allowhook = 1; 247 ci->top = restorestack(L, ci_top); 248 L->top = restorestack(L, top); 249 ci->callstatus &= ~CIST_HOOKED; 250 } 251} 252 253 254static void callhook (lua_State *L, CallInfo *ci) { 255 int hook = LUA_HOOKCALL; 256 ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ 257 if (isLua(ci->previous) && 258 GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { 259 ci->callstatus |= CIST_TAIL; 260 hook = LUA_HOOKTAILCALL; 261 } 262 luaD_hook(L, hook, -1); 263 ci->u.l.savedpc--; /* correct 'pc' */ 264} 265 266 267static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { 268 int i; 269 int nfixargs = p->numparams; 270 StkId base, fixed; 271 lua_assert(actual >= nfixargs); 272 /* move fixed parameters to final position */ 273 luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ 274 fixed = L->top - actual; /* first fixed argument */ 275 base = L->top; /* final position of first argument */ 276 for (i=0; i<nfixargs; i++) { 277 setobjs2s(L, L->top++, fixed + i); 278 setnilvalue(fixed + i); 279 } 280 return base; 281} 282 283 284static StkId tryfuncTM (lua_State *L, StkId func) { 285 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); 286 StkId p; 287 ptrdiff_t funcr = savestack(L, func); 288 if (!ttisfunction(tm)) 289 luaG_typeerror(L, func, "call"); 290 /* Open a hole inside the stack at `func' */ 291 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); 292 incr_top(L); 293 func = restorestack(L, funcr); /* previous call may change stack */ 294 setobj2s(L, func, tm); /* tag method is the new function to be called */ 295 return func; 296} 297 298 299 300#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) 301 302 303/* 304** returns true if function has been executed (C function) 305*/ 306int luaD_precall (lua_State *L, StkId func, int nresults) { 307 lua_CFunction f; 308 CallInfo *ci; 309 int n; /* number of arguments (Lua) or returns (C) */ 310 ptrdiff_t funcr = savestack(L, func); 311 switch (ttype(func)) { 312 case LUA_TLCF: /* light C function */ 313 f = fvalue(func); 314 goto Cfunc; 315 case LUA_TCCL: { /* C closure */ 316 f = clCvalue(func)->f; 317 Cfunc: 318 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ 319 ci = next_ci(L); /* now 'enter' new function */ 320 ci->nresults = nresults; 321 ci->func = restorestack(L, funcr); 322 ci->top = L->top + LUA_MINSTACK; 323 lua_assert(ci->top <= L->stack_last); 324 ci->callstatus = 0; 325 luaC_checkGC(L); /* stack grow uses memory */ 326 if (L->hookmask & LUA_MASKCALL) 327 luaD_hook(L, LUA_HOOKCALL, -1); 328 lua_unlock(L); 329 n = (*f)(L); /* do the actual call */ 330 lua_lock(L); 331 api_checknelems(L, n); 332 luaD_poscall(L, L->top - n); 333 return 1; 334 } 335 case LUA_TLCL: { /* Lua function: prepare its call */ 336 StkId base; 337 Proto *p = clLvalue(func)->p; 338 n = cast_int(L->top - func) - 1; /* number of real arguments */ 339 luaD_checkstack(L, p->maxstacksize); 340 for (; n < p->numparams; n++) 341 setnilvalue(L->top++); /* complete missing arguments */ 342 if (!p->is_vararg) { 343 func = restorestack(L, funcr); 344 base = func + 1; 345 } 346 else { 347 base = adjust_varargs(L, p, n); 348 func = restorestack(L, funcr); /* previous call can change stack */ 349 } 350 ci = next_ci(L); /* now 'enter' new function */ 351 ci->nresults = nresults; 352 ci->func = func; 353 ci->u.l.base = base; 354 ci->top = base + p->maxstacksize; 355 lua_assert(ci->top <= L->stack_last); 356 ci->u.l.savedpc = p->code; /* starting point */ 357 ci->callstatus = CIST_LUA; 358 L->top = ci->top; 359 luaC_checkGC(L); /* stack grow uses memory */ 360 if (L->hookmask & LUA_MASKCALL) 361 callhook(L, ci); 362 return 0; 363 } 364 default: { /* not a function */ 365 func = tryfuncTM(L, func); /* retry with 'function' tag method */ 366 return luaD_precall(L, func, nresults); /* now it must be a function */ 367 } 368 } 369} 370 371 372int luaD_poscall (lua_State *L, StkId firstResult) { 373 StkId res; 374 int wanted, i; 375 CallInfo *ci = L->ci; 376 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { 377 if (L->hookmask & LUA_MASKRET) { 378 ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ 379 luaD_hook(L, LUA_HOOKRET, -1); 380 firstResult = restorestack(L, fr); 381 } 382 L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ 383 } 384 res = ci->func; /* res == final position of 1st result */ 385 wanted = ci->nresults; 386 L->ci = ci = ci->previous; /* back to caller */ 387 /* move results to correct place */ 388 for (i = wanted; i != 0 && firstResult < L->top; i--) 389 setobjs2s(L, res++, firstResult++); 390 while (i-- > 0) 391 setnilvalue(res++); 392 L->top = res; 393 return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ 394} 395 396 397/* 398** Call a function (C or Lua). The function to be called is at *func. 399** The arguments are on the stack, right after the function. 400** When returns, all the results are on the stack, starting at the original 401** function position. 402*/ 403void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { 404 if (++L->nCcalls >= LUAI_MAXCCALLS) { 405 if (L->nCcalls == LUAI_MAXCCALLS) 406 luaG_runerror(L, "C stack overflow"); 407 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) 408 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ 409 } 410 if (!allowyield) L->nny++; 411 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ 412 luaV_execute(L); /* call it */ 413 if (!allowyield) L->nny--; 414 L->nCcalls--; 415} 416 417 418static void finishCcall (lua_State *L) { 419 CallInfo *ci = L->ci; 420 int n; 421 lua_assert(ci->u.c.k != NULL); /* must have a continuation */ 422 lua_assert(L->nny == 0); 423 if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ 424 ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ 425 L->errfunc = ci->u.c.old_errfunc; 426 } 427 /* finish 'lua_callk'/'lua_pcall' */ 428 adjustresults(L, ci->nresults); 429 /* call continuation function */ 430 if (!(ci->callstatus & CIST_STAT)) /* no call status? */ 431 ci->u.c.status = LUA_YIELD; /* 'default' status */ 432 lua_assert(ci->u.c.status != LUA_OK); 433 ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; 434 lua_unlock(L); 435 n = (*ci->u.c.k)(L); 436 lua_lock(L); 437 api_checknelems(L, n); 438 /* finish 'luaD_precall' */ 439 luaD_poscall(L, L->top - n); 440} 441 442 443static void unroll (lua_State *L, void *ud) { 444 UNUSED(ud); 445 for (;;) { 446 if (L->ci == &L->base_ci) /* stack is empty? */ 447 return; /* coroutine finished normally */ 448 if (!isLua(L->ci)) /* C function? */ 449 finishCcall(L); 450 else { /* Lua function */ 451 luaV_finishOp(L); /* finish interrupted instruction */ 452 luaV_execute(L); /* execute down to higher C 'boundary' */ 453 } 454 } 455} 456 457 458/* 459** check whether thread has a suspended protected call 460*/ 461static CallInfo *findpcall (lua_State *L) { 462 CallInfo *ci; 463 for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ 464 if (ci->callstatus & CIST_YPCALL) 465 return ci; 466 } 467 return NULL; /* no pending pcall */ 468} 469 470 471static int recover (lua_State *L, int status) { 472 StkId oldtop; 473 CallInfo *ci = findpcall(L); 474 if (ci == NULL) return 0; /* no recovery point */ 475 /* "finish" luaD_pcall */ 476 oldtop = restorestack(L, ci->extra); 477 luaF_close(L, oldtop); 478 seterrorobj(L, status, oldtop); 479 L->ci = ci; 480 L->allowhook = ci->u.c.old_allowhook; 481 L->nny = 0; /* should be zero to be yieldable */ 482 luaD_shrinkstack(L); 483 L->errfunc = ci->u.c.old_errfunc; 484 ci->callstatus |= CIST_STAT; /* call has error status */ 485 ci->u.c.status = status; /* (here it is) */ 486 return 1; /* continue running the coroutine */ 487} 488 489 490/* 491** signal an error in the call to 'resume', not in the execution of the 492** coroutine itself. (Such errors should not be handled by any coroutine 493** error handler and should not kill the coroutine.) 494*/ 495static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { 496 L->top = firstArg; /* remove args from the stack */ 497 setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ 498 api_incr_top(L); 499 luaD_throw(L, -1); /* jump back to 'lua_resume' */ 500} 501 502 503/* 504** do the work for 'lua_resume' in protected mode 505*/ 506static void resume_cb (lua_State *L, void *ud) { 507 int nCcalls = L->nCcalls; 508 StkId firstArg = cast(StkId, ud); 509 CallInfo *ci = L->ci; 510 if (nCcalls >= LUAI_MAXCCALLS) 511 resume_error(L, "C stack overflow", firstArg); 512 if (L->status == LUA_OK) { /* may be starting a coroutine */ 513 if (ci != &L->base_ci) /* not in base level? */ 514 resume_error(L, "cannot resume non-suspended coroutine", firstArg); 515 /* coroutine is in base level; start running it */ 516 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ 517 luaV_execute(L); /* call it */ 518 } 519 else if (L->status != LUA_YIELD) 520 resume_error(L, "cannot resume dead coroutine", firstArg); 521 else { /* resuming from previous yield */ 522 L->status = LUA_OK; 523 ci->func = restorestack(L, ci->extra); 524 if (isLua(ci)) /* yielded inside a hook? */ 525 luaV_execute(L); /* just continue running Lua code */ 526 else { /* 'common' yield */ 527 if (ci->u.c.k != NULL) { /* does it have a continuation? */ 528 int n; 529 ci->u.c.status = LUA_YIELD; /* 'default' status */ 530 ci->callstatus |= CIST_YIELDED; 531 lua_unlock(L); 532 n = (*ci->u.c.k)(L); /* call continuation */ 533 lua_lock(L); 534 api_checknelems(L, n); 535 firstArg = L->top - n; /* yield results come from continuation */ 536 } 537 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ 538 } 539 unroll(L, NULL); 540 } 541 lua_assert(nCcalls == L->nCcalls); 542} 543 544 545LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { 546 int status; 547 int oldnny = L->nny; /* save 'nny' */ 548 lua_lock(L); 549 luai_userstateresume(L, nargs); 550 L->nCcalls = (from) ? from->nCcalls + 1 : 1; 551 L->nny = 0; /* allow yields */ 552 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); 553 status = luaD_rawrunprotected(L, resume_cb, L->top - nargs); 554 if (status == -1) /* error calling 'lua_resume'? */ 555 status = LUA_ERRRUN; 556 else { /* yield or regular error */ 557 while (status != LUA_OK && status != LUA_YIELD) { /* error? */ 558 if (recover(L, status)) /* recover point? */ 559 status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ 560 else { /* unrecoverable error */ 561 L->status = cast_byte(status); /* mark thread as `dead' */ 562 seterrorobj(L, status, L->top); 563 L->ci->top = L->top; 564 break; 565 } 566 } 567 lua_assert(status == L->status); 568 } 569 L->nny = oldnny; /* restore 'nny' */ 570 L->nCcalls--; 571 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); 572 lua_unlock(L); 573 return status; 574} 575 576 577LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { 578 CallInfo *ci = L->ci; 579 luai_userstateyield(L, nresults); 580 lua_lock(L); 581 api_checknelems(L, nresults); 582 if (L->nny > 0) { 583 if (L != G(L)->mainthread) 584 luaG_runerror(L, "attempt to yield across a C-call boundary"); 585 else 586 luaG_runerror(L, "attempt to yield from outside a coroutine"); 587 } 588 L->status = LUA_YIELD; 589 ci->extra = savestack(L, ci->func); /* save current 'func' */ 590 if (isLua(ci)) { /* inside a hook? */ 591 api_check(L, k == NULL, "hooks cannot continue after yielding"); 592 } 593 else { 594 if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ 595 ci->u.c.ctx = ctx; /* save context */ 596 ci->func = L->top - nresults - 1; /* protect stack below results */ 597 luaD_throw(L, LUA_YIELD); 598 } 599 lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ 600 lua_unlock(L); 601 return 0; /* return to 'luaD_hook' */ 602} 603 604 605int luaD_pcall (lua_State *L, Pfunc func, void *u, 606 ptrdiff_t old_top, ptrdiff_t ef) { 607 int status; 608 CallInfo *old_ci = L->ci; 609 lu_byte old_allowhooks = L->allowhook; 610 unsigned short old_nny = L->nny; 611 ptrdiff_t old_errfunc = L->errfunc; 612 L->errfunc = ef; 613 status = luaD_rawrunprotected(L, func, u); 614 if (status != LUA_OK) { /* an error occurred? */ 615 StkId oldtop = restorestack(L, old_top); 616 luaF_close(L, oldtop); /* close possible pending closures */ 617 seterrorobj(L, status, oldtop); 618 L->ci = old_ci; 619 L->allowhook = old_allowhooks; 620 L->nny = old_nny; 621 luaD_shrinkstack(L); 622 } 623 L->errfunc = old_errfunc; 624 return status; 625} 626 627 628 629/* 630** Execute a protected parser. 631*/ 632struct SParser { /* data to `f_parser' */ 633 ZIO *z; 634 Mbuffer buff; /* dynamic structure used by the scanner */ 635 Dyndata dyd; /* dynamic structures used by the parser */ 636 const char *mode; 637 const char *name; 638}; 639 640 641static void checkmode (lua_State *L, const char *mode, const char *x) { 642 if (mode && strchr(mode, x[0]) == NULL) { 643 luaO_pushfstring(L, 644 "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); 645 luaD_throw(L, LUA_ERRSYNTAX); 646 } 647} 648 649 650static void f_parser (lua_State *L, void *ud) { 651 int i; 652 Closure *cl; 653 struct SParser *p = cast(struct SParser *, ud); 654 int c = zgetc(p->z); /* read first character */ 655 if (c == LUA_SIGNATURE[0]) { 656 checkmode(L, p->mode, "binary"); 657 cl = luaU_undump(L, p->z, &p->buff, p->name); 658 } 659 else { 660 checkmode(L, p->mode, "text"); 661 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); 662 } 663 lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); 664 for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */ 665 UpVal *up = luaF_newupval(L); 666 cl->l.upvals[i] = up; 667 luaC_objbarrier(L, cl, up); 668 } 669} 670 671 672int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 673 const char *mode) { 674 struct SParser p; 675 int status; 676 L->nny++; /* cannot yield during parsing */ 677 p.z = z; p.name = name; p.mode = mode; 678 p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; 679 p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; 680 p.dyd.label.arr = NULL; p.dyd.label.size = 0; 681 luaZ_initbuffer(L, &p.buff); 682 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); 683 luaZ_freebuffer(L, &p.buff); 684 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); 685 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); 686 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); 687 L->nny--; 688 return status; 689} 690 691 692