ldo.c 15 KB

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