ldo.c 13 KB

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