annotate src/lua/ldo.c @ 613:e1dcad3ce967

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