ldo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. ** $Id: ldo.c,v 1.154 2002/01/11 20:27:41 roberto Exp $
  3. ** Stack and Call structure of Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <setjmp.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lua.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. static void restore_stack_limit (lua_State *L) {
  27. StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1;
  28. if (L->top < limit)
  29. L->stack_last = limit;
  30. }
  31. void luaD_stackerror (lua_State *L) {
  32. if (L->stack_last == L->stack+L->stacksize-1) {
  33. /* overflow while handling overflow */
  34. luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
  35. }
  36. else {
  37. L->stack_last += EXTRA_STACK; /* to be used by error message */
  38. lua_assert(L->stack_last == L->stack+L->stacksize-1);
  39. luaD_error(L, "stack overflow");
  40. }
  41. }
  42. /*
  43. ** Open a hole inside the stack at `pos'
  44. */
  45. static void luaD_openstack (lua_State *L, StkId pos) {
  46. int i = L->top-pos;
  47. while (i--) setobj(pos+i+1, pos+i);
  48. incr_top;
  49. }
  50. static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
  51. StkId old_top = L->top;
  52. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  53. L->allowhooks = 0; /* cannot call hooks inside a hook */
  54. lua_unlock(L);
  55. (*hook)(L, ar);
  56. lua_lock(L);
  57. lua_assert(L->allowhooks == 0);
  58. L->allowhooks = 1;
  59. L->top = old_top;
  60. }
  61. void luaD_lineHook (lua_State *L, int line, lua_Hook linehook) {
  62. if (L->allowhooks) {
  63. lua_Debug ar;
  64. ar.event = "line";
  65. ar._ci = L->ci - L->base_ci;
  66. ar.currentline = line;
  67. dohook(L, &ar, linehook);
  68. }
  69. }
  70. void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event) {
  71. if (L->allowhooks) {
  72. lua_Debug ar;
  73. ar.event = event;
  74. ar._ci = L->ci - L->base_ci;
  75. L->ci->pc = NULL; /* function is not active */
  76. dohook(L, &ar, callhook);
  77. }
  78. }
  79. static CallInfo *growci (lua_State *L) {
  80. lua_assert(L->ci == L->end_ci);
  81. luaM_reallocvector(L, L->base_ci, L->size_ci, 2*L->size_ci, CallInfo);
  82. L->ci = L->base_ci + L->size_ci;
  83. L->size_ci *= 2;
  84. L->end_ci = L->base_ci + L->size_ci;
  85. return L->ci;
  86. }
  87. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  88. int i;
  89. Table *htab;
  90. TObject n, nname;
  91. StkId firstvar = base + nfixargs; /* position of first vararg */
  92. if (L->top < firstvar) {
  93. luaD_checkstack(L, firstvar - L->top);
  94. while (L->top < firstvar)
  95. setnilvalue(L->top++);
  96. }
  97. htab = luaH_new(L, 0, 0);
  98. for (i=0; firstvar+i<L->top; i++)
  99. luaH_setnum(L, htab, i+1, firstvar+i);
  100. /* store counter in field `n' */
  101. setnvalue(&n, i);
  102. setsvalue(&nname, luaS_newliteral(L, "n"));
  103. luaH_set(L, htab, &nname, &n);
  104. L->top = firstvar; /* remove elements from the stack */
  105. sethvalue(L->top, htab);
  106. incr_top;
  107. }
  108. StkId luaD_precall (lua_State *L, StkId func) {
  109. CallInfo *ci;
  110. LClosure *cl;
  111. if (ttype(func) != LUA_TFUNCTION) {
  112. /* `func' is not a function; check the `function' tag method */
  113. const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL);
  114. if (ttype(tm) != LUA_TFUNCTION)
  115. luaG_typeerror(L, func, "call");
  116. luaD_openstack(L, func);
  117. setobj(func, tm); /* tag method is the new function to be called */
  118. }
  119. ci = ++L->ci;
  120. if (L->ci == L->end_ci) ci = growci(L);
  121. ci->base = func+1;
  122. if (L->callhook)
  123. luaD_callHook(L, L->callhook, "call");
  124. cl = &clvalue(func)->l;
  125. if (!cl->isC) { /* Lua function? prepare its call */
  126. StkId base = func+1;
  127. Proto *p = cl->p;
  128. ci->savedpc = p->code; /* starting point */
  129. if (p->is_vararg) /* varargs? */
  130. adjust_varargs(L, base, p->numparams);
  131. if (base > L->stack_last - p->maxstacksize)
  132. luaD_stackerror(L);
  133. ci->line = 0;
  134. ci->top = base + p->maxstacksize;
  135. while (L->top < ci->top)
  136. setnilvalue(L->top++);
  137. L->top = ci->top;
  138. return NULL;
  139. }
  140. else { /* if is a C function, call it */
  141. int n;
  142. ci->savedpc = NULL;
  143. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  144. lua_unlock(L);
  145. #if LUA_COMPATUPVALUES
  146. lua_pushupvalues(L);
  147. #endif
  148. n = (*clvalue(func)->c.f)(L); /* do the actual call */
  149. lua_lock(L);
  150. return L->top - n;
  151. }
  152. }
  153. void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
  154. StkId res;
  155. if (L->callhook)
  156. luaD_callHook(L, L->callhook, "return");
  157. res = L->ci->base - 1; /* `func' = final position of 1st result */
  158. L->ci--;
  159. /* move results to correct place */
  160. while (wanted != 0 && firstResult < L->top) {
  161. setobj(res++, firstResult++);
  162. wanted--;
  163. }
  164. while (wanted-- > 0)
  165. setnilvalue(res++);
  166. L->top = res;
  167. luaC_checkGC(L);
  168. }
  169. /*
  170. ** Call a function (C or Lua). The function to be called is at *func.
  171. ** The arguments are on the stack, right after the function.
  172. ** When returns, all the results are on the stack, starting at the original
  173. ** function position.
  174. */
  175. void luaD_call (lua_State *L, StkId func, int nResults) {
  176. StkId firstResult = luaD_precall(L, func);
  177. if (firstResult == NULL) { /* is a Lua function? */
  178. firstResult = luaV_execute(L); /* call it */
  179. if (firstResult == NULL) {
  180. luaD_poscall(L, 0, L->top);
  181. luaD_error(L, "attempt to `yield' across tag-method/C-call boundary");
  182. }
  183. }
  184. luaD_poscall(L, nResults, firstResult);
  185. }
  186. LUA_API void lua_cobegin (lua_State *L, int nargs) {
  187. lua_lock(L);
  188. luaD_precall(L, L->top - (nargs+1));
  189. lua_unlock(L);
  190. }
  191. static void resume_results (lua_State *L, lua_State *from, int numresults) {
  192. while (numresults) {
  193. setobj(L->top, from->top - numresults);
  194. numresults--;
  195. incr_top;
  196. }
  197. }
  198. LUA_API void lua_resume (lua_State *L, lua_State *co) {
  199. StkId firstResult;
  200. lua_lock(L);
  201. if (co->ci->savedpc == NULL) /* no activation record? */
  202. luaD_error(L, "thread is dead - cannot be resumed");
  203. lua_assert(co->errorJmp == NULL);
  204. co->errorJmp = L->errorJmp;
  205. firstResult = luaV_execute(co);
  206. if (firstResult != NULL) { /* `return'? */
  207. resume_results(L, co, co->top - firstResult);
  208. luaD_poscall(co, 0, firstResult); /* ends this coroutine */
  209. }
  210. else { /* `yield' */
  211. int nresults = GETARG_C(*((co->ci-1)->savedpc - 1)) - 1;
  212. resume_results(L, co, co->ci->yield_results);
  213. luaD_poscall(co, nresults, co->top); /* complete it */
  214. if (nresults >= 0) co->top = co->ci->top;
  215. }
  216. co->errorJmp = NULL;
  217. lua_unlock(L);
  218. }
  219. LUA_API int lua_yield (lua_State *L, int nresults) {
  220. CallInfo *ci;
  221. int ibase;
  222. lua_lock(L);
  223. ci = L->ci;
  224. if (ci_func(ci-1)->c.isC)
  225. luaD_error(L, "cannot `yield' a C function");
  226. ci->yield_results = nresults; /* very dirty trick! */
  227. ibase = L->top - (ci-1)->base;
  228. lua_unlock(L);
  229. return ibase;
  230. }
  231. /*
  232. ** Execute a protected call.
  233. */
  234. struct CallS { /* data to `f_call' */
  235. StkId func;
  236. int nresults;
  237. };
  238. static void f_call (lua_State *L, void *ud) {
  239. struct CallS *c = cast(struct CallS *, ud);
  240. luaD_call(L, c->func, c->nresults);
  241. }
  242. LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
  243. StkId func;
  244. struct CallS c;
  245. int status;
  246. lua_lock(L);
  247. func = L->top - (nargs+1); /* function to be called */
  248. c.func = func; c.nresults = nresults;
  249. status = luaD_runprotected(L, f_call, &c);
  250. if (status != 0) { /* an error occurred? */
  251. luaF_close(L, func); /* close eventual pending closures */
  252. L->top = func; /* remove parameters from the stack */
  253. }
  254. lua_unlock(L);
  255. return status;
  256. }
  257. /*
  258. ** Execute a protected parser.
  259. */
  260. struct SParser { /* data to `f_parser' */
  261. ZIO *z;
  262. int bin;
  263. };
  264. static void f_parser (lua_State *L, void *ud) {
  265. struct SParser *p = cast(struct SParser *, ud);
  266. Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
  267. Closure *cl = luaF_newLclosure(L, 0);
  268. cl->l.p = tf;
  269. setclvalue(L->top, cl);
  270. incr_top;
  271. }
  272. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  273. struct SParser p;
  274. lu_mem old_blocks;
  275. int status;
  276. lua_lock(L);
  277. p.z = z; p.bin = bin;
  278. /* before parsing, give a (good) chance to GC */
  279. if (G(L)->nblocks/8 >= G(L)->GCthreshold/10)
  280. luaC_collectgarbage(L);
  281. old_blocks = G(L)->nblocks;
  282. status = luaD_runprotected(L, f_parser, &p);
  283. if (status == 0) {
  284. /* add new memory to threshold (as it probably will stay) */
  285. lua_assert(G(L)->nblocks >= old_blocks);
  286. G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
  287. }
  288. else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
  289. status = LUA_ERRSYNTAX;
  290. lua_unlock(L);
  291. return status;
  292. }
  293. LUA_API int lua_loadfile (lua_State *L, const char *filename) {
  294. ZIO z;
  295. int status;
  296. int bin; /* flag for file mode */
  297. int nlevel; /* level on the stack of filename */
  298. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  299. if (f == NULL) return LUA_ERRFILE; /* unable to open file */
  300. bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
  301. if (bin && f != stdin) {
  302. fclose(f);
  303. f = fopen(filename, "rb"); /* reopen in binary mode */
  304. if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
  305. }
  306. lua_pushliteral(L, "@");
  307. lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename);
  308. lua_concat(L, 2);
  309. nlevel = lua_gettop(L);
  310. filename = lua_tostring(L, -1); /* filename = `@'..filename */
  311. luaZ_Fopen(&z, f, filename);
  312. status = protectedparser(L, &z, bin);
  313. lua_remove(L, nlevel); /* remove filename */
  314. if (f != stdin)
  315. fclose(f);
  316. return status;
  317. }
  318. LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
  319. const char *name) {
  320. ZIO z;
  321. if (!name) name = "?";
  322. luaZ_mopen(&z, buff, size, name);
  323. return protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
  324. }
  325. /*
  326. ** {======================================================
  327. ** Error-recovery functions (based on long jumps)
  328. ** =======================================================
  329. */
  330. /* chain list of long jump buffers */
  331. struct lua_longjmp {
  332. jmp_buf b;
  333. struct lua_longjmp *previous;
  334. volatile int status; /* error code */
  335. int ci; /* index of call info of active function that set protection */
  336. StkId top; /* top stack when protection was set */
  337. int allowhooks; /* `allowhook' state when protection was set */
  338. };
  339. static void message (lua_State *L, const char *s) {
  340. TObject o, m;
  341. setsvalue(&o, luaS_newliteral(L, LUA_ERRORMESSAGE));
  342. luaV_gettable(L, gt(L), &o, &m);
  343. if (ttype(&m) == LUA_TFUNCTION) {
  344. StkId top = L->top;
  345. setobj(top, &m);
  346. incr_top;
  347. setsvalue(top+1, luaS_new(L, s));
  348. incr_top;
  349. luaD_call(L, top, 0);
  350. }
  351. }
  352. /*
  353. ** Reports an error, and jumps up to the available recovery label
  354. */
  355. void luaD_error (lua_State *L, const char *s) {
  356. if (s) {
  357. if (L->ci->savedpc) { /* error in Lua function preamble? */
  358. L->ci->savedpc = NULL; /* pretend function was already running */
  359. L->ci->pc = NULL;
  360. }
  361. message(L, s);
  362. }
  363. luaD_breakrun(L, LUA_ERRRUN);
  364. }
  365. void luaD_breakrun (lua_State *L, int errcode) {
  366. if (L->errorJmp) {
  367. L->errorJmp->status = errcode;
  368. longjmp(L->errorJmp->b, 1);
  369. }
  370. else {
  371. if (errcode != LUA_ERRMEM && errcode != LUA_ERRERR)
  372. message(L, "unable to recover; exiting\n");
  373. exit(EXIT_FAILURE);
  374. }
  375. }
  376. int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
  377. struct lua_longjmp lj;
  378. lj.ci = L->ci - L->base_ci;
  379. lj.top = L->top;
  380. lj.allowhooks = L->allowhooks;
  381. lj.status = 0;
  382. lj.previous = L->errorJmp; /* chain new error handler */
  383. L->errorJmp = &lj;
  384. if (setjmp(lj.b) == 0)
  385. (*f)(L, ud);
  386. else { /* an error occurred: restore the state */
  387. L->ci = L->base_ci + lj.ci;
  388. L->top = lj.top;
  389. L->allowhooks = lj.allowhooks;
  390. restore_stack_limit(L);
  391. }
  392. L->errorJmp = lj.previous; /* restore old error handler */
  393. return lj.status;
  394. }
  395. /* }====================================================== */