ldo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. ** $Id: ldo.c,v 1.210 2002/12/04 17:28:27 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. #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 (based on long jumps)
  29. ** =======================================================
  30. */
  31. /* chain list of long jump buffers */
  32. struct lua_longjmp {
  33. struct lua_longjmp *previous;
  34. jmp_buf 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(oldtop, luaS_new(L, MEMERRMSG));
  41. break;
  42. }
  43. case LUA_ERRERR: {
  44. setsvalue2s(oldtop, luaS_new(L, "error in error handling"));
  45. break;
  46. }
  47. case LUA_ERRSYNTAX:
  48. case LUA_ERRRUN: {
  49. setobjs2s(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. longjmp(L->errorJmp->b, 1);
  59. }
  60. else {
  61. 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. if (setjmp(lj.b) == 0)
  71. (*f)(L, ud);
  72. L->errorJmp = lj.previous; /* restore old error handler */
  73. return lj.status;
  74. }
  75. static void restore_stack_limit (lua_State *L) {
  76. L->stack_last = L->stack+L->stacksize-1;
  77. if (L->size_ci > LUA_MAXCALLS) { /* there was an overflow? */
  78. int inuse = (L->ci - L->base_ci);
  79. if (inuse + 1 < LUA_MAXCALLS) /* can `undo' overflow? */
  80. luaD_reallocCI(L, LUA_MAXCALLS);
  81. }
  82. }
  83. /* }====================================================== */
  84. static void correctstack (lua_State *L, TObject *oldstack) {
  85. CallInfo *ci;
  86. GCObject *up;
  87. L->top = (L->top - oldstack) + L->stack;
  88. for (up = L->openupval; up != NULL; up = up->gch.next)
  89. gcotouv(up)->v = (gcotouv(up)->v - oldstack) + L->stack;
  90. for (ci = L->base_ci; ci <= L->ci; ci++) {
  91. ci->top = (ci->top - oldstack) + L->stack;
  92. ci->base = (ci->base - oldstack) + L->stack;
  93. }
  94. L->base = L->ci->base;
  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 = cast(unsigned short, 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 && L->allowhook) {
  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. L->allowhook = 0; /* cannot call hooks inside a hook */
  137. lua_unlock(L);
  138. (*hook)(L, &ar);
  139. lua_lock(L);
  140. lua_assert(!L->allowhook);
  141. L->allowhook = 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. setobj2n(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. else condhardstacktests(luaD_reallocCI(L, L->size_ci));
  187. cl = &clvalue(func)->l;
  188. if (!cl->isC) { /* Lua function? prepare its call */
  189. CallInfo *ci;
  190. Proto *p = cl->p;
  191. if (p->is_vararg) /* varargs? */
  192. adjust_varargs(L, p->numparams, func+1);
  193. luaD_checkstack(L, p->maxstacksize);
  194. ci = ++L->ci; /* now `enter' new function */
  195. L->base = L->ci->base = restorestack(L, funcr) + 1;
  196. ci->top = L->base + p->maxstacksize;
  197. ci->u.l.savedpc = p->code; /* starting point */
  198. ci->state = CI_SAVEDPC;
  199. while (L->top < ci->top)
  200. setnilvalue(L->top++);
  201. L->top = ci->top;
  202. return NULL;
  203. }
  204. else { /* if is a C function, call it */
  205. CallInfo *ci;
  206. int n;
  207. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  208. ci = ++L->ci; /* now `enter' new function */
  209. L->base = L->ci->base = restorestack(L, funcr) + 1;
  210. ci->top = L->top + LUA_MINSTACK;
  211. ci->state = CI_C; /* a C function */
  212. if (L->hookmask & LUA_MASKCALL) {
  213. luaD_callhook(L, LUA_HOOKCALL, -1);
  214. ci = L->ci; /* previous call may reallocate `ci' */
  215. }
  216. lua_unlock(L);
  217. #ifdef LUA_COMPATUPVALUES
  218. lua_pushupvalues(L);
  219. #endif
  220. n = (*clvalue(L->base - 1)->c.f)(L); /* do the actual call */
  221. lua_lock(L);
  222. return L->top - n;
  223. }
  224. }
  225. void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
  226. StkId res;
  227. if (L->hookmask & LUA_MASKRET) {
  228. ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
  229. luaD_callhook(L, LUA_HOOKRET, -1);
  230. firstResult = restorestack(L, fr);
  231. }
  232. res = L->base - 1; /* res == final position of 1st result */
  233. L->ci--;
  234. L->base = L->ci->base; /* restore base */
  235. /* move results to correct place */
  236. while (wanted != 0 && firstResult < L->top) {
  237. setobjs2s(res++, firstResult++);
  238. wanted--;
  239. }
  240. while (wanted-- > 0)
  241. setnilvalue(res++);
  242. L->top = res;
  243. luaC_checkGC(L);
  244. }
  245. /*
  246. ** Call a function (C or Lua). The function to be called is at *func.
  247. ** The arguments are on the stack, right after the function.
  248. ** When returns, all the results are on the stack, starting at the original
  249. ** function position.
  250. */
  251. void luaD_call (lua_State *L, StkId func, int nResults) {
  252. StkId firstResult;
  253. if (++L->nCcalls >= LUA_MAXCCALLS) {
  254. if (L->nCcalls == LUA_MAXCCALLS)
  255. luaG_runerror(L, "stack overflow");
  256. else if (L->nCcalls >= (LUA_MAXCCALLS + (LUA_MAXCCALLS>>3)))
  257. luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
  258. }
  259. firstResult = luaD_precall(L, func);
  260. if (firstResult == NULL) /* is a Lua function? */
  261. firstResult = luaV_execute(L); /* call it */
  262. luaD_poscall(L, nResults, firstResult);
  263. L->nCcalls--;
  264. }
  265. static void resume (lua_State *L, void *ud) {
  266. StkId firstResult;
  267. int nargs = *cast(int *, ud);
  268. CallInfo *ci = L->ci;
  269. if (ci == L->base_ci) { /* no activation record? */
  270. if (nargs >= L->top - L->base)
  271. luaG_runerror(L, "cannot resume dead coroutine");
  272. luaD_precall(L, L->top - (nargs + 1)); /* start coroutine */
  273. }
  274. else if (ci->state & CI_YIELD) { /* inside a yield? */
  275. if (ci->state & CI_C) { /* `common' yield? */
  276. /* finish interrupted execution of `OP_CALL' */
  277. int nresults;
  278. lua_assert((ci-1)->state & CI_SAVEDPC);
  279. lua_assert(GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_CALL ||
  280. GET_OPCODE(*((ci-1)->u.l.savedpc - 1)) == OP_TAILCALL);
  281. nresults = GETARG_C(*((ci-1)->u.l.savedpc - 1)) - 1;
  282. luaD_poscall(L, nresults, L->top - nargs); /* complete it */
  283. if (nresults >= 0) L->top = L->ci->top;
  284. }
  285. else { /* yielded inside a hook: just continue its execution */
  286. ci->state &= ~CI_YIELD;
  287. }
  288. }
  289. else
  290. luaG_runerror(L, "cannot resume non-suspended coroutine");
  291. firstResult = luaV_execute(L);
  292. if (firstResult != NULL) /* return? */
  293. luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
  294. }
  295. LUA_API int lua_resume (lua_State *L, int nargs) {
  296. int status;
  297. lu_byte old_allowhooks;
  298. lua_lock(L);
  299. old_allowhooks = L->allowhook;
  300. lua_assert(L->errfunc == 0 && L->nCcalls == 0);
  301. status = luaD_rawrunprotected(L, resume, &nargs);
  302. if (status != 0) { /* error? */
  303. L->ci = L->base_ci; /* go back to initial level */
  304. L->base = L->ci->base;
  305. L->nCcalls = 0;
  306. luaF_close(L, L->base); /* close eventual pending closures */
  307. seterrorobj(L, status, L->base);
  308. L->allowhook = old_allowhooks;
  309. restore_stack_limit(L);
  310. }
  311. lua_unlock(L);
  312. return status;
  313. }
  314. LUA_API int lua_yield (lua_State *L, int nresults) {
  315. CallInfo *ci;
  316. lua_lock(L);
  317. ci = L->ci;
  318. if (L->nCcalls > 0)
  319. luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
  320. if (ci->state & CI_C) { /* usual yield */
  321. if ((ci-1)->state & CI_C)
  322. luaG_runerror(L, "cannot yield a C function");
  323. if (L->top - nresults > L->base) { /* is there garbage in the stack? */
  324. int i;
  325. for (i=0; i<nresults; i++) /* move down results */
  326. setobjs2s(L->base + i, L->top - nresults + i);
  327. L->top = L->base + nresults;
  328. }
  329. } /* else it's an yield inside a hook: nothing to do */
  330. ci->state |= CI_YIELD;
  331. lua_unlock(L);
  332. return -1;
  333. }
  334. int luaD_pcall (lua_State *L, Pfunc func, void *u,
  335. ptrdiff_t old_top, ptrdiff_t ef) {
  336. int status;
  337. unsigned short oldnCcalls = L->nCcalls;
  338. ptrdiff_t old_ci = saveci(L, L->ci);
  339. lu_byte old_allowhooks = L->allowhook;
  340. ptrdiff_t old_errfunc = L->errfunc;
  341. L->errfunc = ef;
  342. status = luaD_rawrunprotected(L, func, u);
  343. if (status != 0) { /* an error occurred? */
  344. StkId oldtop = restorestack(L, old_top);
  345. luaF_close(L, oldtop); /* close eventual pending closures */
  346. seterrorobj(L, status, oldtop);
  347. L->nCcalls = oldnCcalls;
  348. L->ci = restoreci(L, old_ci);
  349. L->base = L->ci->base;
  350. L->allowhook = old_allowhooks;
  351. restore_stack_limit(L);
  352. }
  353. L->errfunc = old_errfunc;
  354. return status;
  355. }
  356. /*
  357. ** Execute a protected parser.
  358. */
  359. struct SParser { /* data to `f_parser' */
  360. ZIO *z;
  361. Mbuffer buff; /* buffer to be used by the scanner */
  362. int bin;
  363. };
  364. static void f_parser (lua_State *L, void *ud) {
  365. struct SParser *p = cast(struct SParser *, ud);
  366. Proto *tf = p->bin ? luaU_undump(L, p->z, &p->buff) :
  367. luaY_parser(L, p->z, &p->buff);
  368. Closure *cl = luaF_newLclosure(L, 0, gt(L));
  369. cl->l.p = tf;
  370. setclvalue(L->top, cl);
  371. incr_top(L);
  372. }
  373. int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
  374. struct SParser p;
  375. lu_mem old_blocks;
  376. int status;
  377. ptrdiff_t oldtopr = savestack(L, L->top); /* save current top */
  378. p.z = z; p.bin = bin;
  379. luaZ_initbuffer(L, &p.buff);
  380. /* before parsing, give a (good) chance to GC */
  381. if (G(L)->nblocks + G(L)->nblocks/4 >= G(L)->GCthreshold)
  382. luaC_collectgarbage(L);
  383. old_blocks = G(L)->nblocks;
  384. status = luaD_rawrunprotected(L, f_parser, &p);
  385. luaZ_freebuffer(L, &p.buff);
  386. if (status == 0) {
  387. /* add new memory to threshold (as it probably will stay) */
  388. lua_assert(G(L)->nblocks >= old_blocks);
  389. G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
  390. }
  391. else { /* error */
  392. StkId oldtop = restorestack(L, oldtopr);
  393. seterrorobj(L, status, oldtop);
  394. }
  395. return status;
  396. }