ldo.c 14 KB

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