ldo.c 13 KB

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