ldo.c 13 KB

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