/script_binding/lua/lua/ldo.c

http://ftk.googlecode.com/ · C · 518 lines · 424 code · 67 blank · 27 comment · 83 complexity · 6ac8b3237c6186ca13f786e546545868 MD5 · raw file

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