ldo.c 13 KB

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