ldo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. ** $Id: ldo.c,v 2.54 2009/03/04 13:32:29 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. Table *htab = NULL;
  175. StkId base, fixed;
  176. for (; actual < nfixargs; ++actual)
  177. setnilvalue(L->top++);
  178. #if defined(LUA_COMPAT_VARARG)
  179. if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
  180. int nvar = actual - nfixargs; /* number of extra arguments */
  181. lua_assert(p->is_vararg & VARARG_HASARG);
  182. luaC_checkGC(L);
  183. htab = luaH_new(L); /* create `arg' table */
  184. sethvalue(L, L->top++, htab);
  185. for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
  186. setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i - 1);
  187. /* store counter in field `n' */
  188. setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
  189. L->top--;
  190. }
  191. #endif
  192. /* move fixed parameters to final position */
  193. fixed = L->top - actual; /* first fixed argument */
  194. base = L->top; /* final position of first argument */
  195. for (i=0; i<nfixargs; i++) {
  196. setobjs2s(L, L->top++, fixed+i);
  197. setnilvalue(fixed+i);
  198. }
  199. /* add `arg' parameter */
  200. if (htab) {
  201. sethvalue(L, L->top++, htab);
  202. lua_assert(iswhite(obj2gco(htab)));
  203. }
  204. return base;
  205. }
  206. static StkId tryfuncTM (lua_State *L, StkId func) {
  207. const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
  208. StkId p;
  209. ptrdiff_t funcr = savestack(L, func);
  210. if (!ttisfunction(tm))
  211. luaG_typeerror(L, func, "call");
  212. /* Open a hole inside the stack at `func' */
  213. for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
  214. incr_top(L);
  215. func = restorestack(L, funcr); /* previous call may change stack */
  216. setobj2s(L, func, tm); /* tag method is the new function to be called */
  217. return func;
  218. }
  219. #define inc_ci(L) \
  220. ((L->ci == L->end_ci) ? growCI(L) : \
  221. (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
  222. /*
  223. ** returns true if function has been executed (C function)
  224. */
  225. int luaD_precall (lua_State *L, StkId func, int nresults) {
  226. LClosure *cl;
  227. ptrdiff_t funcr;
  228. if (!ttisfunction(func)) /* `func' is not a function? */
  229. func = tryfuncTM(L, func); /* check the `function' tag method */
  230. funcr = savestack(L, func);
  231. cl = &clvalue(func)->l;
  232. L->ci->savedpc = L->savedpc;
  233. if (!cl->isC) { /* Lua function? prepare its call */
  234. CallInfo *ci;
  235. StkId st, base;
  236. Proto *p = cl->p;
  237. luaD_checkstack(L, p->maxstacksize);
  238. func = restorestack(L, funcr);
  239. if (!p->is_vararg) /* no varargs? */
  240. base = func + 1;
  241. else { /* vararg function */
  242. int nargs = cast_int(L->top - func) - 1;
  243. base = adjust_varargs(L, p, nargs);
  244. func = restorestack(L, funcr); /* previous call may change the stack */
  245. }
  246. ci = inc_ci(L); /* now `enter' new function */
  247. ci->func = func;
  248. L->base = ci->base = base;
  249. ci->top = L->base + p->maxstacksize;
  250. lua_assert(ci->top <= L->stack_last);
  251. L->savedpc = p->code; /* starting point */
  252. ci->u.l.tailcalls = 0;
  253. ci->callstatus = CIST_LUA;
  254. ci->nresults = nresults;
  255. for (st = L->top; st < ci->top; st++)
  256. setnilvalue(st);
  257. L->top = ci->top;
  258. if (L->hookmask & LUA_MASKCALL) {
  259. L->savedpc++; /* hooks assume 'pc' is already incremented */
  260. luaD_callhook(L, LUA_HOOKCALL, -1);
  261. L->savedpc--; /* correct 'pc' */
  262. }
  263. return 0;
  264. }
  265. else { /* if is a C function, call it */
  266. CallInfo *ci;
  267. int n;
  268. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  269. ci = inc_ci(L); /* now `enter' new function */
  270. ci->func = restorestack(L, funcr);
  271. L->base = ci->base = ci->func + 1;
  272. ci->top = L->top + LUA_MINSTACK;
  273. lua_assert(ci->top <= L->stack_last);
  274. ci->nresults = nresults;
  275. ci->callstatus = 0;
  276. if (L->hookmask & LUA_MASKCALL)
  277. luaD_callhook(L, LUA_HOOKCALL, -1);
  278. lua_unlock(L);
  279. n = (*curr_func(L)->c.f)(L); /* do the actual call */
  280. lua_lock(L);
  281. luaD_poscall(L, L->top - n);
  282. return 1;
  283. }
  284. }
  285. static StkId callrethooks (lua_State *L, StkId firstResult) {
  286. ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
  287. luaD_callhook(L, LUA_HOOKRET, -1);
  288. if (isLua(L->ci)) { /* Lua function? */
  289. while ((L->hookmask & LUA_MASKRET) && L->ci->u.l.tailcalls--)
  290. luaD_callhook(L, LUA_HOOKTAILRET, -1); /* ret. hooks for tail calls */
  291. }
  292. return restorestack(L, fr);
  293. }
  294. int luaD_poscall (lua_State *L, StkId firstResult) {
  295. StkId res;
  296. int wanted, i;
  297. CallInfo *ci;
  298. if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
  299. if (L->hookmask & LUA_MASKRET)
  300. firstResult = callrethooks(L, firstResult);
  301. L->oldpc = (L->ci - 1)->savedpc; /* set 'oldpc' for returning function */
  302. }
  303. ci = L->ci--;
  304. res = ci->func; /* res == final position of 1st result */
  305. wanted = ci->nresults;
  306. L->base = (ci - 1)->base; /* restore base */
  307. L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
  308. /* move results to correct place */
  309. for (i = wanted; i != 0 && firstResult < L->top; i--)
  310. setobjs2s(L, res++, firstResult++);
  311. while (i-- > 0)
  312. setnilvalue(res++);
  313. L->top = res;
  314. return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
  315. }
  316. /*
  317. ** Call a function (C or Lua). The function to be called is at *func.
  318. ** The arguments are on the stack, right after the function.
  319. ** When returns, all the results are on the stack, starting at the original
  320. ** function position.
  321. */
  322. void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
  323. global_State *g = G(L);
  324. if (++g->nCcalls >= LUAI_MAXCCALLS) {
  325. if (g->nCcalls == LUAI_MAXCCALLS)
  326. luaG_runerror(L, "C stack overflow");
  327. else if (g->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
  328. luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
  329. }
  330. if (!allowyield) L->nny++;
  331. if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
  332. luaV_execute(L); /* call it */
  333. if (!allowyield) L->nny--;
  334. g->nCcalls--;
  335. luaC_checkGC(L);
  336. }
  337. static void finishCcall (lua_State *L) {
  338. int n;
  339. lua_assert(L->ci->u.c.cont != NULL); /* must have a continuation */
  340. lua_assert(L->nny == 0);
  341. /* finish 'luaD_call' */
  342. G(L)->nCcalls--;
  343. /* finish 'lua_callcont' */
  344. adjustresults(L, (L->ci + 1)->nresults);
  345. /* call continuation function */
  346. lua_unlock(L);
  347. n = (*L->ci->u.c.cont)(L);
  348. lua_lock(L);
  349. /* finish 'luaD_precall' */
  350. luaD_poscall(L, L->top - n);
  351. }
  352. static void unroll (lua_State *L) {
  353. for (;;) {
  354. if (L->ci == L->base_ci) /* stack is empty? */
  355. return; /* coroutine finished normally */
  356. if (!isLua(L->ci)) /* C function? */
  357. finishCcall(L);
  358. else { /* Lua function */
  359. luaV_finishOp(L); /* finish interrupted instruction */
  360. luaV_execute(L); /* execute down to higher C 'boundary' */
  361. }
  362. }
  363. }
  364. static void resume (lua_State *L, void *ud) {
  365. StkId firstArg = cast(StkId, ud);
  366. CallInfo *ci = L->ci;
  367. if (L->status == LUA_OK) { /* start coroutine? */
  368. lua_assert(ci == L->base_ci && firstArg > L->base);
  369. if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */
  370. luaV_execute(L); /* call it */
  371. }
  372. else { /* resuming from previous yield */
  373. lua_assert(L->status == LUA_YIELD);
  374. L->status = LUA_OK;
  375. if (isLua(ci)) { /* yielded inside a hook? */
  376. L->base = L->ci->base; /* just continue its execution */
  377. luaV_execute(L);
  378. }
  379. else { /* 'common' yield */
  380. G(L)->nCcalls--; /* finish 'luaD_call' */
  381. luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
  382. }
  383. unroll(L);
  384. }
  385. }
  386. static int resume_error (lua_State *L, const char *msg) {
  387. L->top = L->ci->base;
  388. setsvalue2s(L, L->top, luaS_new(L, msg));
  389. incr_top(L);
  390. lua_unlock(L);
  391. return LUA_ERRRUN;
  392. }
  393. LUA_API int lua_resume (lua_State *L, int nargs) {
  394. int status;
  395. lua_lock(L);
  396. if (L->status != LUA_YIELD) {
  397. if (L->status != LUA_OK)
  398. return resume_error(L, "cannot resume dead coroutine");
  399. else if (L->ci != L->base_ci)
  400. return resume_error(L, "cannot resume non-suspended coroutine");
  401. }
  402. luai_userstateresume(L, nargs);
  403. lua_assert(L->errfunc == 0);
  404. if (G(L)->nCcalls >= LUAI_MAXCCALLS)
  405. return resume_error(L, "C stack overflow");
  406. ++G(L)->nCcalls; /* count resume */
  407. L->nny = 0; /* allow yields */
  408. status = luaD_rawrunprotected(L, resume, L->top - nargs);
  409. if (status != LUA_OK && status != LUA_YIELD) { /* error? */
  410. L->status = cast_byte(status); /* mark thread as `dead' */
  411. luaD_seterrorobj(L, status, L->top);
  412. L->ci->top = L->top;
  413. }
  414. else {
  415. lua_assert(status == L->status);
  416. }
  417. L->nny = 1; /* do not allow yields */
  418. --G(L)->nCcalls;
  419. lua_unlock(L);
  420. return status;
  421. }
  422. LUA_API int lua_yield (lua_State *L, int nresults) {
  423. luai_userstateyield(L, nresults);
  424. lua_lock(L);
  425. if (L->nny > 0)
  426. luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
  427. L->base = L->top - nresults; /* protect stack slots below */
  428. L->status = LUA_YIELD;
  429. if (!isLua(L->ci)) /* not inside a hook? */
  430. luaD_throw(L, LUA_YIELD);
  431. lua_assert(L->ci->callstatus & CIST_HOOKED); /* must be inside a hook */
  432. lua_unlock(L);
  433. return 0; /* otherwise, return to 'luaD_callhook' */
  434. }
  435. int luaD_pcall (lua_State *L, Pfunc func, void *u,
  436. ptrdiff_t old_top, ptrdiff_t ef) {
  437. int status;
  438. ptrdiff_t old_ci = saveci(L, L->ci);
  439. lu_byte old_allowhooks = L->allowhook;
  440. unsigned short old_nny = L->nny;
  441. ptrdiff_t old_errfunc = L->errfunc;
  442. L->errfunc = ef;
  443. status = luaD_rawrunprotected(L, func, u);
  444. if (status != LUA_OK) { /* an error occurred? */
  445. StkId oldtop = restorestack(L, old_top);
  446. luaF_close(L, oldtop); /* close possible pending closures */
  447. luaD_seterrorobj(L, status, oldtop);
  448. L->ci = restoreci(L, old_ci);
  449. L->base = L->ci->base;
  450. L->savedpc = L->ci->savedpc;
  451. L->allowhook = old_allowhooks;
  452. L->nny = old_nny;
  453. restore_stack_limit(L);
  454. }
  455. L->errfunc = old_errfunc;
  456. return status;
  457. }
  458. /*
  459. ** Execute a protected parser.
  460. */
  461. struct SParser { /* data to `f_parser' */
  462. ZIO *z;
  463. Mbuffer buff; /* buffer to be used by the scanner */
  464. const char *name;
  465. };
  466. static void f_parser (lua_State *L, void *ud) {
  467. int i;
  468. Proto *tf;
  469. Closure *cl;
  470. struct SParser *p = cast(struct SParser *, ud);
  471. int c = luaZ_lookahead(p->z);
  472. luaC_checkGC(L);
  473. tf = (c == LUA_SIGNATURE[0]) ? luaU_undump(L, p->z, &p->buff, p->name)
  474. : luaY_parser(L, p->z, &p->buff, p->name);
  475. setptvalue2s(L, L->top, tf);
  476. incr_top(L);
  477. cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
  478. cl->l.p = tf;
  479. setclvalue(L, L->top - 1, cl);
  480. for (i = 0; i < tf->nups; i++) /* initialize upvalues */
  481. cl->l.upvals[i] = luaF_newupval(L);
  482. }
  483. int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
  484. struct SParser p;
  485. int status;
  486. p.z = z; p.name = name;
  487. luaZ_initbuffer(L, &p.buff);
  488. status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
  489. luaZ_freebuffer(L, &p.buff);
  490. return status;
  491. }