ldo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. ** $Id: ldo.c 4115 2012-04-12 21:06:13Z olereinhardt $
  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. #include <stdio.h>
  10. #define ldo_c
  11. #define LUA_CORE
  12. #include <lua/lua.h>
  13. #include <lua/ldebug.h>
  14. #include <lua/ldo.h>
  15. #include <lua/lfunc.h>
  16. #include <lua/lgc.h>
  17. #include <lua/lmem.h>
  18. #include <lua/lobject.h>
  19. #include <lua/lopcodes.h>
  20. #include <lua/lparser.h>
  21. #include <lua/lstate.h>
  22. #include <lua/lstring.h>
  23. #include <lua/ltable.h>
  24. #include <lua/ltm.h>
  25. #include <lua/lundump.h>
  26. #include <lua/lvm.h>
  27. #include <lua/lzio.h>
  28. /*
  29. ** {======================================================
  30. ** Error-recovery functions
  31. ** =======================================================
  32. */
  33. /* chain list of long jump buffers */
  34. struct lua_longjmp {
  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. switch (errcode) {
  41. case LUA_ERRMEM: {
  42. setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
  43. break;
  44. }
  45. case LUA_ERRERR: {
  46. setsvalue2s(L, oldtop, 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. if (L->errorJmp) {
  78. L->errorJmp->status = errcode;
  79. LUAI_THROW(L, L->errorJmp);
  80. }
  81. else {
  82. L->status = cast_byte(errcode);
  83. if (G(L)->panic) {
  84. resetstack(L, errcode);
  85. lua_unlock(L);
  86. G(L)->panic(L);
  87. }
  88. printf("lua throw calling ");
  89. while(1);
  90. // todo ezt itt ki kell váltani vmivel
  91. //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. Table *htab = NULL;
  180. StkId base, fixed;
  181. for (; actual < nfixargs; ++actual)
  182. setnilvalue(L->top++);
  183. #if defined(LUA_COMPAT_VARARG)
  184. if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
  185. int nvar = actual - nfixargs; /* number of extra arguments */
  186. lua_assert(p->is_vararg & VARARG_HASARG);
  187. luaC_checkGC(L);
  188. htab = luaH_new(L, nvar, 1); /* create `arg' table */
  189. for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
  190. setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
  191. /* store counter in field `n' */
  192. setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
  193. }
  194. #endif
  195. /* move fixed parameters to final position */
  196. fixed = L->top - actual; /* first fixed argument */
  197. base = L->top; /* final position of first argument */
  198. for (i=0; i<nfixargs; i++) {
  199. setobjs2s(L, L->top++, fixed+i);
  200. setnilvalue(fixed+i);
  201. }
  202. /* add `arg' parameter */
  203. if (htab) {
  204. sethvalue(L, L->top++, htab);
  205. lua_assert(iswhite(obj2gco(htab)));
  206. }
  207. return base;
  208. }
  209. static StkId tryfuncTM (lua_State *L, StkId func) {
  210. const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
  211. StkId p;
  212. ptrdiff_t funcr = savestack(L, func);
  213. if (!ttisfunction(tm))
  214. luaG_typeerror(L, func, "call");
  215. /* Open a hole inside the stack at `func' */
  216. for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
  217. incr_top(L);
  218. func = restorestack(L, funcr); /* previous call may change stack */
  219. setobj2s(L, func, tm); /* tag method is the new function to be called */
  220. return func;
  221. }
  222. #define inc_ci(L) \
  223. ((L->ci == L->end_ci) ? growCI(L) : \
  224. (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
  225. int luaD_precall (lua_State *L, StkId func, int nresults) {
  226. ptrdiff_t funcr;
  227. LClosure *cl = NULL;
  228. if (!ttisfunction(func) && !ttislightfunction(func)) /* `func' is not a function? */
  229. func = tryfuncTM(L, func); /* check the `function' tag method */
  230. funcr = savestack(L, func);
  231. if (ttisfunction(func))
  232. cl = &clvalue(func)->l;
  233. L->ci->savedpc = L->savedpc;
  234. if (cl && !cl->isC) { /* Lua function? prepare its call */
  235. CallInfo *ci;
  236. StkId st, base;
  237. Proto *p = cl->p;
  238. luaD_checkstack(L, p->maxstacksize);
  239. func = restorestack(L, funcr);
  240. if (!p->is_vararg) { /* no varargs? */
  241. base = func + 1;
  242. if (L->top > base + p->numparams)
  243. L->top = base + p->numparams;
  244. }
  245. else { /* vararg function */
  246. int nargs = cast_int(L->top - func) - 1;
  247. base = adjust_varargs(L, p, nargs);
  248. func = restorestack(L, funcr); /* previous call may change the stack */
  249. }
  250. ci = inc_ci(L); /* now `enter' new function */
  251. ci->func = func;
  252. L->base = ci->base = base;
  253. ci->top = L->base + p->maxstacksize;
  254. lua_assert(ci->top <= L->stack_last);
  255. L->savedpc = p->code; /* starting point */
  256. ci->tailcalls = 0;
  257. ci->nresults = nresults;
  258. for (st = L->top; st < ci->top; st++)
  259. setnilvalue(st);
  260. L->top = ci->top;
  261. if (L->hookmask & LUA_MASKCALL) {
  262. L->savedpc++; /* hooks assume 'pc' is already incremented */
  263. luaD_callhook(L, LUA_HOOKCALL, -1);
  264. L->savedpc--; /* correct 'pc' */
  265. }
  266. return PCRLUA;
  267. }
  268. else { /* if is a C function, call it */
  269. CallInfo *ci;
  270. int n;
  271. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  272. ci = inc_ci(L); /* now `enter' new function */
  273. ci->func = restorestack(L, funcr);
  274. L->base = ci->base = ci->func + 1;
  275. ci->top = L->top + LUA_MINSTACK;
  276. lua_assert(ci->top <= L->stack_last);
  277. ci->nresults = nresults;
  278. if (L->hookmask & LUA_MASKCALL)
  279. luaD_callhook(L, LUA_HOOKCALL, -1);
  280. lua_unlock(L);
  281. if (ttisfunction(func))
  282. n = (*curr_func(L)->c.f)(L); /* do the actual call */
  283. else
  284. n = ((lua_CFunction)fvalue(func))(L); /* do the actual call */
  285. lua_lock(L);
  286. if (n < 0) /* yielding? */
  287. return PCRYIELD;
  288. else {
  289. luaD_poscall(L, L->top - n);
  290. return PCRC;
  291. }
  292. }
  293. }
  294. static StkId callrethooks (lua_State *L, StkId firstResult) {
  295. ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
  296. luaD_callhook(L, LUA_HOOKRET, -1);
  297. if (f_isLua(L->ci)) { /* Lua function? */
  298. while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
  299. luaD_callhook(L, LUA_HOOKTAILRET, -1);
  300. }
  301. return restorestack(L, fr);
  302. }
  303. int luaD_poscall (lua_State *L, StkId firstResult) {
  304. StkId res;
  305. int wanted, i;
  306. CallInfo *ci;
  307. if (L->hookmask & LUA_MASKRET)
  308. firstResult = callrethooks(L, firstResult);
  309. ci = L->ci--;
  310. res = ci->func; /* res == final position of 1st result */
  311. wanted = ci->nresults;
  312. L->base = (ci - 1)->base; /* restore base */
  313. L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
  314. /* move results to correct place */
  315. for (i = wanted; i != 0 && firstResult < L->top; i--)
  316. setobjs2s(L, res++, firstResult++);
  317. while (i-- > 0)
  318. setnilvalue(res++);
  319. L->top = res;
  320. return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
  321. }
  322. /*
  323. ** Call a function (C or Lua). The function to be called is at *func.
  324. ** The arguments are on the stack, right after the function.
  325. ** When returns, all the results are on the stack, starting at the original
  326. ** function position.
  327. */
  328. void luaD_call (lua_State *L, StkId func, int nResults) {
  329. if (++L->nCcalls >= LUAI_MAXCCALLS) {
  330. if (L->nCcalls == LUAI_MAXCCALLS)
  331. luaG_runerror(L, "C stack overflow");
  332. else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
  333. luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
  334. }
  335. if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
  336. luaV_execute(L, 1); /* call it */
  337. L->nCcalls--;
  338. luaC_checkGC(L);
  339. }
  340. static void resume (lua_State *L, void *ud) {
  341. StkId firstArg = cast(StkId, ud);
  342. CallInfo *ci = L->ci;
  343. if (L->status == 0) { /* start coroutine? */
  344. lua_assert(ci == L->base_ci && firstArg > L->base);
  345. if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
  346. return;
  347. }
  348. else { /* resuming from previous yield */
  349. lua_assert(L->status == LUA_YIELD);
  350. L->status = 0;
  351. if (!f_isLua(ci)) { /* `common' yield? */
  352. /* finish interrupted execution of `OP_CALL' */
  353. lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
  354. GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
  355. if (luaD_poscall(L, firstArg)) /* complete it... */
  356. L->top = L->ci->top; /* and correct top if not multiple results */
  357. }
  358. else /* yielded inside a hook: just continue its execution */
  359. L->base = L->ci->base;
  360. }
  361. luaV_execute(L, cast_int(L->ci - L->base_ci));
  362. }
  363. static int resume_error (lua_State *L, const char *msg) {
  364. L->top = L->ci->base;
  365. setsvalue2s(L, L->top, luaS_new(L, msg));
  366. incr_top(L);
  367. lua_unlock(L);
  368. return LUA_ERRRUN;
  369. }
  370. LUA_API int lua_resume (lua_State *L, int nargs) {
  371. int status;
  372. lua_lock(L);
  373. if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
  374. return resume_error(L, "cannot resume non-suspended coroutine");
  375. if (L->nCcalls >= LUAI_MAXCCALLS)
  376. return resume_error(L, "C stack overflow");
  377. luai_userstateresume(L, nargs);
  378. lua_assert(L->errfunc == 0);
  379. L->baseCcalls = ++L->nCcalls;
  380. status = luaD_rawrunprotected(L, resume, L->top - nargs);
  381. if (status != 0) { /* error? */
  382. L->status = cast_byte(status); /* mark thread as `dead' */
  383. luaD_seterrorobj(L, status, L->top);
  384. L->ci->top = L->top;
  385. }
  386. else {
  387. lua_assert(L->nCcalls == L->baseCcalls);
  388. status = L->status;
  389. }
  390. --L->nCcalls;
  391. lua_unlock(L);
  392. return status;
  393. }
  394. LUA_API int lua_yield (lua_State *L, int nresults) {
  395. luai_userstateyield(L, nresults);
  396. lua_lock(L);
  397. if (L->nCcalls > L->baseCcalls)
  398. luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
  399. L->base = L->top - nresults; /* protect stack slots below */
  400. L->status = LUA_YIELD;
  401. lua_unlock(L);
  402. return -1;
  403. }
  404. int luaD_pcall (lua_State *L, Pfunc func, void *u,
  405. ptrdiff_t old_top, ptrdiff_t ef) {
  406. int status;
  407. unsigned short oldnCcalls = L->nCcalls;
  408. ptrdiff_t old_ci = saveci(L, L->ci);
  409. lu_byte old_allowhooks = L->allowhook;
  410. ptrdiff_t old_errfunc = L->errfunc;
  411. L->errfunc = ef;
  412. status = luaD_rawrunprotected(L, func, u);
  413. if (status != 0) { /* an error occurred? */
  414. StkId oldtop = restorestack(L, old_top);
  415. luaF_close(L, oldtop); /* close eventual pending closures */
  416. luaD_seterrorobj(L, status, oldtop);
  417. L->nCcalls = oldnCcalls;
  418. L->ci = restoreci(L, old_ci);
  419. L->base = L->ci->base;
  420. L->savedpc = L->ci->savedpc;
  421. L->allowhook = old_allowhooks;
  422. restore_stack_limit(L);
  423. }
  424. L->errfunc = old_errfunc;
  425. return status;
  426. }
  427. /*
  428. ** Execute a protected parser.
  429. */
  430. struct SParser { /* data to `f_parser' */
  431. ZIO *z;
  432. Mbuffer buff; /* buffer to be used by the scanner */
  433. const char *name;
  434. };
  435. static void f_parser (lua_State *L, void *ud) {
  436. int i;
  437. Proto *tf;
  438. Closure *cl;
  439. struct SParser *p = cast(struct SParser *, ud);
  440. int c = luaZ_lookahead(p->z);
  441. luaC_checkGC(L);
  442. tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
  443. &p->buff, p->name);
  444. cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
  445. cl->l.p = tf;
  446. for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
  447. cl->l.upvals[i] = luaF_newupval(L);
  448. setclvalue(L, L->top, cl);
  449. incr_top(L);
  450. }
  451. int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
  452. struct SParser p;
  453. int status;
  454. p.z = z; p.name = name;
  455. luaZ_initbuffer(L, &p.buff);
  456. status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
  457. luaZ_freebuffer(L, &p.buff);
  458. return status;
  459. }