ldo.c 16 KB

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