ldo.c 15 KB

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