ldo.c 14 KB

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