ldo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. #include "lua.h"
  10. #include <string.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. luai_jmpbuf b;
  35. volatile int status; /* error code */
  36. };
  37. void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
  38. switch (errcode) {
  39. case LUA_ERRMEM: {
  40. ptrdiff_t oldtopr = savestack(L, oldtop);
  41. setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, MEMERRMSG));
  42. break;
  43. }
  44. case LUA_ERRERR: {
  45. ptrdiff_t oldtopr = savestack(L, oldtop);
  46. setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, "error in error handling"));
  47. break;
  48. }
  49. case LUA_ERRSYNTAX:
  50. case LUA_ERRRUN: {
  51. setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
  52. break;
  53. }
  54. }
  55. L->top = oldtop + 1;
  56. }
  57. static void restore_stack_limit (lua_State *L) {
  58. lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
  59. if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */
  60. int inuse = cast_int(L->ci - L->base_ci);
  61. if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */
  62. luaD_reallocCI(L, LUAI_MAXCALLS);
  63. }
  64. }
  65. static void resetstack (lua_State *L, int status) {
  66. L->ci = L->base_ci;
  67. L->base = L->ci->base;
  68. luaF_close(L, L->base); /* close eventual pending closures */
  69. luaD_seterrorobj(L, status, L->base);
  70. L->nCcalls = L->baseCcalls;
  71. L->allowhook = 1;
  72. restore_stack_limit(L);
  73. L->errfunc = 0;
  74. L->errorJmp = NULL;
  75. }
  76. void luaD_throw (lua_State *L, int errcode) {
  77. unfixedstack(L); /* make sure the fixedstack & block_gc flags get reset. */
  78. unset_block_gc(L);
  79. if (L->errorJmp) {
  80. L->errorJmp->status = errcode;
  81. LUAI_THROW(L, L->errorJmp);
  82. }
  83. else {
  84. L->status = cast_byte(errcode);
  85. if (G(L)->panic) {
  86. resetstack(L, errcode);
  87. lua_unlock(L);
  88. G(L)->panic(L);
  89. }
  90. // c_exit(EXIT_FAILURE);
  91. }
  92. }
  93. int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
  94. struct lua_longjmp lj;
  95. lj.status = 0;
  96. lj.previous = L->errorJmp; /* chain new error handler */
  97. L->errorJmp = &lj;
  98. LUAI_TRY(L, &lj,
  99. (*f)(L, ud);
  100. );
  101. L->errorJmp = lj.previous; /* restore old error handler */
  102. return lj.status;
  103. }
  104. /* }====================================================== */
  105. static void correctstack (lua_State *L, TValue *oldstack) {
  106. CallInfo *ci;
  107. GCObject *up;
  108. L->top = (L->top - oldstack) + L->stack;
  109. for (up = L->openupval; up != NULL; up = up->gch.next)
  110. gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
  111. for (ci = L->base_ci; ci <= L->ci; ci++) {
  112. ci->top = (ci->top - oldstack) + L->stack;
  113. ci->base = (ci->base - oldstack) + L->stack;
  114. ci->func = (ci->func - oldstack) + L->stack;
  115. }
  116. L->base = (L->base - oldstack) + L->stack;
  117. }
  118. void luaD_reallocstack (lua_State *L, int newsize) {
  119. TValue *oldstack = L->stack;
  120. int realsize = newsize + 1 + EXTRA_STACK;
  121. int block_status = is_block_gc(L);
  122. lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
  123. set_block_gc(L); /* The GC MUST be blocked during stack reallocaiton */
  124. luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
  125. if (!block_status) unset_block_gc(L); /* Honour the previous block status */
  126. L->stacksize = realsize;
  127. L->stack_last = L->stack+newsize;
  128. correctstack(L, oldstack);
  129. }
  130. void luaD_reallocCI (lua_State *L, int newsize) {
  131. CallInfo *oldci = L->base_ci;
  132. luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
  133. L->size_ci = newsize;
  134. L->ci = (L->ci - oldci) + L->base_ci;
  135. L->end_ci = L->base_ci + L->size_ci - 1;
  136. }
  137. void luaD_growstack (lua_State *L, int n) {
  138. if (n <= L->stacksize) /* double size is enough? */
  139. luaD_reallocstack(L, 2*L->stacksize);
  140. else
  141. luaD_reallocstack(L, L->stacksize + n);
  142. }
  143. static CallInfo *growCI (lua_State *L) {
  144. if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */
  145. luaD_throw(L, LUA_ERRERR);
  146. else {
  147. luaD_reallocCI(L, 2*L->size_ci);
  148. if (L->size_ci > LUAI_MAXCALLS)
  149. luaG_runerror(L, "stack overflow");
  150. }
  151. return ++L->ci;
  152. }
  153. void luaD_callhook (lua_State *L, int event, int line) {
  154. lua_Hook hook = L->hook;
  155. if (hook && L->allowhook) {
  156. ptrdiff_t top = savestack(L, L->top);
  157. ptrdiff_t ci_top = savestack(L, L->ci->top);
  158. lua_Debug ar;
  159. ar.event = event;
  160. ar.currentline = line;
  161. if (event == LUA_HOOKTAILRET)
  162. ar.i_ci = 0; /* tail call; no debug information about it */
  163. else
  164. ar.i_ci = cast_int(L->ci - L->base_ci);
  165. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  166. L->ci->top = L->top + LUA_MINSTACK;
  167. lua_assert(L->ci->top <= L->stack_last);
  168. L->allowhook = 0; /* cannot call hooks inside a hook */
  169. lua_unlock(L);
  170. (*hook)(L, &ar);
  171. lua_lock(L);
  172. lua_assert(!L->allowhook);
  173. L->allowhook = 1;
  174. L->ci->top = restorestack(L, ci_top);
  175. L->top = restorestack(L, top);
  176. }
  177. }
  178. static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
  179. int i;
  180. int nfixargs = p->numparams;
  181. #if defined(LUA_COMPAT_VARARG)
  182. Table *htab = NULL;
  183. #endif
  184. StkId base, fixed;
  185. for (; actual < nfixargs; ++actual)
  186. setnilvalue(L->top++);
  187. #if defined(LUA_COMPAT_VARARG)
  188. if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
  189. int nvar = actual - nfixargs; /* number of extra arguments */
  190. lua_assert(p->is_vararg & VARARG_HASARG);
  191. luaC_checkGC(L);
  192. htab = luaH_new(L, nvar, 1); /* create `arg' table */
  193. sethvalue2s(L, L->top, htab); /* put table on stack */
  194. incr_top(L);
  195. fixedstack(L);
  196. for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
  197. setobj2n(L, luaH_setnum(L, htab, i+1), L->top - 1 - nvar + i);
  198. unfixedstack(L);
  199. /* store counter in field `n' */
  200. setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
  201. L->top--; /* remove table from stack */
  202. }
  203. #endif
  204. /* move fixed parameters to final position */
  205. fixed = L->top - actual; /* first fixed argument */
  206. base = L->top; /* final position of first argument */
  207. for (i=0; i<nfixargs; i++) {
  208. setobjs2s(L, L->top++, fixed+i);
  209. setnilvalue(fixed+i);
  210. }
  211. #if defined(LUA_COMPAT_VARARG)
  212. /* add `arg' parameter */
  213. if (htab) {
  214. sethvalue(L, L->top++, htab);
  215. lua_assert(iswhite(obj2gco(htab)));
  216. }
  217. #endif
  218. return base;
  219. }
  220. static StkId tryfuncTM (lua_State *L, StkId func) {
  221. const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
  222. StkId p;
  223. ptrdiff_t funcr = savestack(L, func);
  224. if (!ttisfunction(tm)) {
  225. luaG_typeerror(L, func, "call");
  226. }
  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)) /* `func' is not a function? */
  241. func = tryfuncTM(L, func); /* check the `function' tag method */
  242. funcr = savestack(L, func);
  243. if (!ttislightfunction(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 (ttislightfunction(ci->func))
  294. n = ((lua_CFunction)fvalue(ci->func))(L); /* do the actual call */
  295. else
  296. n = (*curr_func(L)->c.f)(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. }