ldo.c 14 KB

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