ldo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. ** $Id: ldo.c,v 1.198 2002/11/06 19:08:00 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. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lopcodes.h"
  17. #include "lparser.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lundump.h"
  23. #include "lvm.h"
  24. #include "lzio.h"
  25. /*
  26. ** {======================================================
  27. ** Error-recovery functions (based on long jumps)
  28. ** =======================================================
  29. */
  30. /* chain list of long jump buffers */
  31. struct lua_longjmp {
  32. struct lua_longjmp *previous;
  33. jmp_buf b;
  34. volatile int status; /* error code */
  35. };
  36. static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
  37. switch (errcode) {
  38. case LUA_ERRMEM: {
  39. setsvalue2s(oldtop, luaS_new(L, MEMERRMSG));
  40. break;
  41. }
  42. case LUA_ERRERR: {
  43. setsvalue2s(oldtop, luaS_new(L, "error in error handling"));
  44. break;
  45. }
  46. case LUA_ERRSYNTAX:
  47. case LUA_ERRRUN: {
  48. setobjs2s(oldtop, L->top - 1); /* error message on current top */
  49. break;
  50. }
  51. }
  52. L->top = oldtop + 1;
  53. }
  54. void luaD_throw (lua_State *L, int errcode) {
  55. if (L->errorJmp) {
  56. L->errorJmp->status = errcode;
  57. longjmp(L->errorJmp->b, 1);
  58. }
  59. else {
  60. G(L)->panic(L);
  61. exit(EXIT_FAILURE);
  62. }
  63. }
  64. int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
  65. struct lua_longjmp lj;
  66. lj.status = 0;
  67. lj.previous = L->errorJmp; /* chain new error handler */
  68. L->errorJmp = &lj;
  69. if (setjmp(lj.b) == 0)
  70. (*f)(L, ud);
  71. L->errorJmp = lj.previous; /* restore old error handler */
  72. return lj.status;
  73. }
  74. static void restore_stack_limit (lua_State *L) {
  75. L->stack_last = L->stack+L->stacksize-1;
  76. if (L->size_ci > LUA_MAXCALLS) { /* there was an overflow? */
  77. int inuse = (L->ci - L->base_ci);
  78. if (inuse + 1 < LUA_MAXCALLS) /* can `undo' overflow? */
  79. luaD_reallocCI(L, LUA_MAXCALLS);
  80. }
  81. }
  82. /* }====================================================== */
  83. static void correctstack (lua_State *L, TObject *oldstack) {
  84. CallInfo *ci;
  85. GCObject *up;
  86. L->top = (L->top - oldstack) + L->stack;
  87. for (up = L->openupval; up != NULL; up = up->gch.next)
  88. (&up->uv)->v = ((&up->uv)->v - oldstack) + L->stack;
  89. for (ci = L->base_ci; ci <= L->ci; ci++) {
  90. ci->base = (ci->base - oldstack) + L->stack;
  91. ci->top = (ci->top - oldstack) + L->stack;
  92. if (ci->state & CI_HASFRAME) /* Lua function with active frame? */
  93. *ci->u.l.pb = (*ci->u.l.pb - oldstack) + L->stack; /* correct frame */
  94. }
  95. }
  96. void luaD_reallocstack (lua_State *L, int newsize) {
  97. TObject *oldstack = L->stack;
  98. luaM_reallocvector(L, L->stack, L->stacksize, newsize, TObject);
  99. L->stacksize = newsize;
  100. L->stack_last = L->stack+newsize-1-EXTRA_STACK;
  101. correctstack(L, oldstack);
  102. }
  103. void luaD_reallocCI (lua_State *L, int newsize) {
  104. CallInfo *oldci = L->base_ci;
  105. luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
  106. L->size_ci = newsize;
  107. L->ci = (L->ci - oldci) + L->base_ci;
  108. L->end_ci = L->base_ci + L->size_ci;
  109. }
  110. void luaD_growstack (lua_State *L, int n) {
  111. if (n <= L->stacksize) /* double size is enough? */
  112. luaD_reallocstack(L, 2*L->stacksize);
  113. else
  114. luaD_reallocstack(L, L->stacksize + n + EXTRA_STACK);
  115. }
  116. static void luaD_growCI (lua_State *L) {
  117. if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */
  118. luaD_throw(L, LUA_ERRERR);
  119. else {
  120. luaD_reallocCI(L, 2*L->size_ci);
  121. if (L->size_ci > LUA_MAXCALLS)
  122. luaG_runerror(L, "stack overflow");
  123. }
  124. }
  125. void luaD_callhook (lua_State *L, int event, int line) {
  126. lua_Hook hook = L->hook;
  127. if (hook && allowhook(L)) {
  128. ptrdiff_t top = savestack(L, L->top);
  129. ptrdiff_t ci_top = savestack(L, L->ci->top);
  130. lua_Debug ar;
  131. ar.event = event;
  132. ar.currentline = line;
  133. ar.i_ci = L->ci - L->base_ci;
  134. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  135. L->ci->top = L->top + LUA_MINSTACK;
  136. setallowhook(L, 0); /* cannot call hooks inside a hook */
  137. lua_unlock(L);
  138. (*hook)(L, &ar);
  139. lua_lock(L);
  140. lua_assert(!allowhook(L));
  141. setallowhook(L, 1);
  142. L->ci->top = restorestack(L, ci_top);
  143. L->top = restorestack(L, top);
  144. }
  145. }
  146. static void adjust_varargs (lua_State *L, int nfixargs, StkId base) {
  147. int i;
  148. Table *htab;
  149. TObject nname;
  150. int actual = L->top - base; /* actual number of arguments */
  151. if (actual < nfixargs) {
  152. luaD_checkstack(L, nfixargs - actual);
  153. for (; actual < nfixargs; ++actual)
  154. setnilvalue(L->top++);
  155. }
  156. actual -= nfixargs; /* number of extra arguments */
  157. htab = luaH_new(L, 0, 0); /* create `arg' table */
  158. for (i=0; i<actual; i++) /* put extra arguments into `arg' table */
  159. setobj2t(luaH_setnum(L, htab, i+1), L->top - actual + i);
  160. /* store counter in field `n' */
  161. setsvalue(&nname, luaS_newliteral(L, "n"));
  162. setnvalue(luaH_set(L, htab, &nname), actual);
  163. L->top -= actual; /* remove extra elements from the stack */
  164. sethvalue(L->top, htab);
  165. incr_top(L);
  166. }
  167. static StkId tryfuncTM (lua_State *L, StkId func) {
  168. const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL);
  169. StkId p;
  170. ptrdiff_t funcr = savestack(L, func);
  171. if (!ttisfunction(tm))
  172. luaG_typeerror(L, func, "call");
  173. /* Open a hole inside the stack at `func' */
  174. for (p = L->top; p > func; p--) setobjs2s(p, p-1);
  175. incr_top(L);
  176. func = restorestack(L, funcr); /* previous call may change stack */
  177. setobj2s(func, tm); /* tag method is the new function to be called */
  178. return func;
  179. }
  180. StkId luaD_precall (lua_State *L, StkId func) {
  181. LClosure *cl;
  182. ptrdiff_t funcr = savestack(L, func);
  183. if (!ttisfunction(func)) /* `func' is not a function? */
  184. func = tryfuncTM(L, func); /* check the `function' tag method */
  185. if (L->ci + 1 == L->end_ci) luaD_growCI(L);
  186. cl = &clvalue(func)->l;
  187. if (!cl->isC) { /* Lua function? prepare its call */
  188. CallInfo *ci;
  189. Proto *p = cl->p;
  190. if (p->is_vararg) /* varargs? */
  191. adjust_varargs(L, p->numparams, func+1);
  192. luaD_checkstack(L, p->maxstacksize);
  193. ci = ++L->ci; /* now `enter' new function */
  194. ci->base = restorestack(L, funcr) + 1;
  195. ci->top = ci->base + p->maxstacksize;
  196. ci->u.l.savedpc = p->code; /* starting point */
  197. ci->state = CI_SAVEDPC;
  198. while (L->top < ci->top)
  199. setnilvalue(L->top++);
  200. L->top = ci->top;
  201. return NULL;
  202. }
  203. else { /* if is a C function, call it */
  204. CallInfo *ci;
  205. int n;
  206. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  207. ci = ++L->ci; /* now `enter' new function */
  208. ci->base = restorestack(L, funcr) + 1;
  209. ci->top = L->top + LUA_MINSTACK;
  210. ci->state = CI_C; /* a C function */
  211. if (L->hookmask & LUA_MASKCALL) {
  212. luaD_callhook(L, LUA_HOOKCALL, -1);
  213. ci = L->ci; /* previous call may realocate `ci' */
  214. }
  215. lua_unlock(L);
  216. #ifdef LUA_COMPATUPVALUES
  217. lua_pushupvalues(L);
  218. #endif
  219. n = (*clvalue(ci->base-1)->c.f)(L); /* do the actual call */
  220. lua_lock(L);
  221. return L->top - n;
  222. }
  223. }
  224. void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
  225. StkId res;
  226. if (L->hookmask & LUA_MASKRET) {
  227. ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
  228. luaD_callhook(L, LUA_HOOKRET, -1);
  229. firstResult = restorestack(L, fr);
  230. }
  231. res = L->ci->base - 1; /* res == final position of 1st result */
  232. L->ci--;
  233. /* move results to correct place */
  234. while (wanted != 0 && firstResult < L->top) {
  235. setobjs2s(res++, firstResult++);
  236. wanted--;
  237. }
  238. while (wanted-- > 0)
  239. setnilvalue(res++);
  240. L->top = res;
  241. luaC_checkGC(L);
  242. }
  243. /*
  244. ** Call a function (C or Lua). The function to be called is at *func.
  245. ** The arguments are on the stack, right after the function.
  246. ** When returns, all the results are on the stack, starting at the original
  247. ** function position.
  248. */
  249. void luaD_call (lua_State *L, StkId func, int nResults) {
  250. StkId firstResult = luaD_precall(L, func);
  251. if (firstResult == NULL) { /* is a Lua function? */
  252. firstResult = luaV_execute(L); /* call it */
  253. if (firstResult == NULL) {
  254. luaD_poscall(L, 0, L->top);
  255. luaG_runerror(L, "attempt to yield across tag-method/C-call boundary");
  256. }
  257. }
  258. luaD_poscall(L, nResults, firstResult);
  259. }
  260. static void resume (lua_State *L, void *ud) {
  261. StkId firstResult;
  262. int nargs = *cast(int *, ud);
  263. CallInfo *ci = L->ci;
  264. if (ci == L->base_ci) { /* no activation record? */
  265. if (nargs >= L->top - L->ci->base)
  266. luaG_runerror(L, "cannot resume dead coroutine");
  267. luaD_precall(L, L->top - (nargs + 1)); /* start coroutine */
  268. }
  269. else if (ci->state && CI_YIELD) { /* inside a yield? */
  270. /* finish interrupted execution of `OP_CALL' */
  271. int nresults;
  272. lua_assert((ci-1)->state & CI_SAVEDPC);
  273. lua_assert(GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_CALL ||
  274. GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_TAILCALL);
  275. nresults = GETARG_C(*((ci-1)->u.l.savedpc - 1)) - 1;
  276. luaD_poscall(L, nresults, L->top - nargs); /* complete it */
  277. if (nresults >= 0) L->top = L->ci->top;
  278. }
  279. else
  280. luaG_runerror(L, "cannot resume non-suspended coroutine");
  281. firstResult = luaV_execute(L);
  282. if (firstResult != NULL) /* return? */
  283. luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
  284. }
  285. LUA_API int lua_resume (lua_State *L, int nargs) {
  286. int status;
  287. int old_allowhooks;
  288. lua_lock(L);
  289. old_allowhooks = allowhook(L);
  290. lua_assert(L->errfunc == 0);
  291. status = luaD_rawrunprotected(L, resume, &nargs);
  292. if (status != 0) { /* error? */
  293. L->ci = L->base_ci; /* go back to initial level */
  294. luaF_close(L, L->ci->base); /* close eventual pending closures */
  295. seterrorobj(L, status, L->ci->base);
  296. setallowhook(L, old_allowhooks);
  297. restore_stack_limit(L);
  298. }
  299. lua_unlock(L);
  300. return status;
  301. }
  302. LUA_API int lua_yield (lua_State *L, int nresults) {
  303. CallInfo *ci;
  304. lua_lock(L);
  305. ci = L->ci;
  306. if ((ci-1)->state & CI_C)
  307. luaG_runerror(L, "cannot yield a C function");
  308. lua_assert(ci->state & CI_C); /* current function is not Lua */
  309. if (L->top - nresults > ci->base) { /* is there garbage in the stack? */
  310. int i;
  311. for (i=0; i<nresults; i++) /* move down results */
  312. setobjs2s(ci->base + i, L->top - nresults + i);
  313. L->top = ci->base + nresults;
  314. }
  315. lua_unlock(L);
  316. return -1;
  317. }
  318. /*
  319. ** Execute a protected call.
  320. */
  321. struct CallS { /* data to `f_call' */
  322. StkId func;
  323. int nresults;
  324. };
  325. static void f_call (lua_State *L, void *ud) {
  326. struct CallS *c = cast(struct CallS *, ud);
  327. luaD_call(L, c->func, c->nresults);
  328. }
  329. int luaD_pcall (lua_State *L, int nargs, int nresults, ptrdiff_t errfunc) {
  330. struct CallS c;
  331. int status;
  332. ptrdiff_t old_top = savestack(L, L->top);
  333. ptrdiff_t old_ci = saveci(L, L->ci);
  334. int old_allowhooks = allowhook(L);
  335. ptrdiff_t old_errfunc = L->errfunc;
  336. L->errfunc = errfunc;
  337. c.func = L->top - (nargs+1); /* function to be called */
  338. c.nresults = nresults;
  339. status = luaD_rawrunprotected(L, &f_call, &c);
  340. if (status != 0) { /* an error occurred? */
  341. StkId oldtop = restorestack(L, old_top) - (nargs+1);
  342. luaF_close(L, oldtop); /* close eventual pending closures */
  343. seterrorobj(L, status, oldtop);
  344. L->ci = restoreci(L, old_ci);
  345. setallowhook(L, old_allowhooks);
  346. restore_stack_limit(L);
  347. }
  348. L->errfunc = old_errfunc;
  349. return status;
  350. }
  351. /*
  352. ** Execute a protected parser.
  353. */
  354. struct SParser { /* data to `f_parser' */
  355. ZIO *z;
  356. Mbuffer buff; /* buffer to be used by the scanner */
  357. int bin;
  358. };
  359. static void f_parser (lua_State *L, void *ud) {
  360. struct SParser *p = cast(struct SParser *, ud);
  361. Proto *tf = p->bin ? luaU_undump(L, p->z, &p->buff) :
  362. luaY_parser(L, p->z, &p->buff);
  363. Closure *cl = luaF_newLclosure(L, 0, gt(L));
  364. cl->l.p = tf;
  365. setclvalue(L->top, cl);
  366. incr_top(L);
  367. }
  368. int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
  369. struct SParser p;
  370. lu_mem old_blocks;
  371. int status;
  372. ptrdiff_t oldtopr = savestack(L, L->top); /* save current top */
  373. p.z = z; p.bin = bin;
  374. luaZ_initbuffer(L, &p.buff);
  375. /* before parsing, give a (good) chance to GC */
  376. if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold)
  377. luaC_collectgarbage(L);
  378. old_blocks = G(L)->nblocks;
  379. status = luaD_rawrunprotected(L, f_parser, &p);
  380. luaZ_freebuffer(L, &p.buff);
  381. if (status == 0) {
  382. /* add new memory to threshold (as it probably will stay) */
  383. lua_assert(G(L)->nblocks >= old_blocks);
  384. G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
  385. }
  386. else { /* error */
  387. StkId oldtop = restorestack(L, oldtopr);
  388. seterrorobj(L, status, oldtop);
  389. }
  390. return status;
  391. }