ldo.c 14 KB

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