ldo.c 15 KB

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