ldo.c 16 KB

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