ldo.c 14 KB

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