ldo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. ** $Id: ldo.c,v 1.120 2001/02/01 17:40:48 roberto Exp roberto $
  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 "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lparser.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #include "lundump.h"
  22. #include "lvm.h"
  23. #include "lzio.h"
  24. /* space to handle stack overflow errors */
  25. #define EXTRA_STACK (2*LUA_MINSTACK)
  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_init (lua_State *L, int stacksize) {
  32. stacksize += EXTRA_STACK;
  33. L->stack = luaM_newvector(L, stacksize, TObject);
  34. L->stacksize = stacksize;
  35. L->Cbase = L->top = L->stack;
  36. restore_stack_limit(L);
  37. }
  38. void luaD_checkstack (lua_State *L, int n) {
  39. if (L->stack_last - L->top <= n) { /* stack overflow? */
  40. if (L->stack_last == L->stack+L->stacksize-1) {
  41. /* overflow while handling overflow */
  42. luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
  43. }
  44. else {
  45. L->stack_last += EXTRA_STACK; /* to be used by error message */
  46. lua_assert(L->stack_last == L->stack+L->stacksize-1);
  47. luaD_error(L, "stack overflow");
  48. }
  49. }
  50. }
  51. /*
  52. ** Adjust stack. Set top to base+extra, pushing NILs if needed.
  53. ** (we cannot add base+extra unless we are sure it fits in the stack;
  54. ** otherwise the result of such operation on pointers is undefined)
  55. */
  56. void luaD_adjusttop (lua_State *L, StkId base, int extra) {
  57. int diff = extra-(L->top-base);
  58. if (diff <= 0)
  59. L->top = base+extra;
  60. else {
  61. luaD_checkstack(L, diff);
  62. while (diff--)
  63. setnilvalue(L->top++);
  64. }
  65. }
  66. /*
  67. ** Open a hole inside the stack at `pos'
  68. */
  69. static void luaD_openstack (lua_State *L, StkId pos) {
  70. int i = L->top-pos;
  71. while (i--) setobj(pos+i+1, pos+i);
  72. incr_top;
  73. }
  74. static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
  75. StkId old_Cbase = L->Cbase;
  76. StkId old_top = L->Cbase = L->top;
  77. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  78. L->allowhooks = 0; /* cannot call hooks inside a hook */
  79. LUA_UNLOCK(L);
  80. (*hook)(L, ar);
  81. LUA_LOCK(L);
  82. lua_assert(L->allowhooks == 0);
  83. L->allowhooks = 1;
  84. L->top = old_top;
  85. L->Cbase = old_Cbase;
  86. }
  87. void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
  88. if (L->allowhooks) {
  89. lua_Debug ar;
  90. ar._func = func;
  91. ar.event = "line";
  92. ar.currentline = line;
  93. dohook(L, &ar, linehook);
  94. }
  95. }
  96. static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
  97. const char *event) {
  98. if (L->allowhooks) {
  99. lua_Debug ar;
  100. ar._func = func;
  101. ar.event = event;
  102. infovalue(func)->pc = NULL; /* function is not active */
  103. dohook(L, &ar, callhook);
  104. }
  105. }
  106. static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
  107. int nup = cl->nupvalues; /* number of upvalues */
  108. StkId old_Cbase = L->Cbase;
  109. int n;
  110. L->Cbase = base; /* new base for C function */
  111. luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
  112. for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
  113. setobj(L->top++, &cl->upvalue[n]);
  114. LUA_UNLOCK(L);
  115. n = (*cl->f.c)(L); /* do the actual call */
  116. LUA_LOCK(L);
  117. L->Cbase = old_Cbase; /* restore old C base */
  118. return L->top - n; /* return index of first result */
  119. }
  120. void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) {
  121. StkId base = L->top - nParams;
  122. luaD_openstack(L, base);
  123. setclvalue(base, f);
  124. luaD_call(L, base, nResults);
  125. }
  126. /*
  127. ** Call a function (C or Lua). The function to be called is at *func.
  128. ** The arguments are on the stack, right after the function.
  129. ** When returns, the results are on the stack, starting at the original
  130. ** function position.
  131. ** The number of results is nResults, unless nResults=LUA_MULTRET.
  132. */
  133. void luaD_call (lua_State *L, StkId func, int nResults) {
  134. lua_Hook callhook;
  135. StkId firstResult;
  136. CallInfo ci;
  137. Closure *cl;
  138. if (ttype(func) != LUA_TFUNCTION) {
  139. /* `func' is not a function; check the `function' tag method */
  140. Closure *tm = luaT_gettmbyObj(G(L), func, TM_FUNCTION);
  141. if (tm == NULL)
  142. luaG_typeerror(L, func, "call");
  143. luaD_openstack(L, func);
  144. setclvalue(func, tm); /* tag method is the new function to be called */
  145. }
  146. cl = clvalue(func);
  147. ci.func = cl;
  148. setivalue(func, &ci);
  149. callhook = L->callhook;
  150. if (callhook)
  151. luaD_callHook(L, func, callhook, "call");
  152. firstResult = (cl->isC ? callCclosure(L, cl, func+1) :
  153. luaV_execute(L, cl, func+1));
  154. if (callhook) /* same hook that was active at entry */
  155. luaD_callHook(L, func, callhook, "return");
  156. lua_assert(ttype(func) == LUA_TMARK);
  157. /* move results to `func' (to erase parameters and function) */
  158. if (nResults == LUA_MULTRET) {
  159. while (firstResult < L->top) /* copy all results */
  160. setobj(func++, firstResult++);
  161. L->top = func;
  162. }
  163. else { /* copy at most `nResults' */
  164. for (; nResults > 0 && firstResult < L->top; nResults--)
  165. setobj(func++, firstResult++);
  166. L->top = func;
  167. for (; nResults > 0; nResults--) { /* if there are not enough results */
  168. setnilvalue(L->top); /* adjust the stack */
  169. incr_top; /* must check stack space */
  170. }
  171. }
  172. luaC_checkGC(L);
  173. }
  174. /*
  175. ** Execute a protected call.
  176. */
  177. struct CallS { /* data to `f_call' */
  178. StkId func;
  179. int nresults;
  180. };
  181. static void f_call (lua_State *L, void *ud) {
  182. struct CallS *c = (struct CallS *)ud;
  183. luaD_call(L, c->func, c->nresults);
  184. }
  185. LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
  186. StkId func;
  187. struct CallS c;
  188. int status;
  189. LUA_LOCK(L);
  190. func = L->top - (nargs+1); /* function to be called */
  191. c.func = func; c.nresults = nresults;
  192. status = luaD_runprotected(L, f_call, &c);
  193. if (status != 0) /* an error occurred? */
  194. L->top = func; /* remove parameters from the stack */
  195. LUA_UNLOCK(L);
  196. return status;
  197. }
  198. /*
  199. ** Execute a protected parser.
  200. */
  201. struct SParser { /* data to `f_parser' */
  202. ZIO *z;
  203. int bin;
  204. };
  205. static void f_parser (lua_State *L, void *ud) {
  206. struct SParser *p = (struct SParser *)ud;
  207. Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
  208. luaV_Lclosure(L, tf, 0);
  209. }
  210. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  211. struct SParser p;
  212. mem_int old_blocks;
  213. int status;
  214. LUA_LOCK(L);
  215. p.z = z; p.bin = bin;
  216. luaC_checkGC(L);
  217. old_blocks = G(L)->nblocks;
  218. status = luaD_runprotected(L, f_parser, &p);
  219. if (status == 0) {
  220. /* add new memory to threshold (as it probably will stay) */
  221. lua_assert(G(L)->nblocks >= old_blocks);
  222. G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
  223. }
  224. else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
  225. status = LUA_ERRSYNTAX;
  226. LUA_UNLOCK(L);
  227. return status;
  228. }
  229. static int parse_file (lua_State *L, const char *filename) {
  230. ZIO z;
  231. int status;
  232. int bin; /* flag for file mode */
  233. int c; /* look ahead char */
  234. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  235. if (f == NULL) return LUA_ERRFILE; /* unable to open file */
  236. c = fgetc(f);
  237. ungetc(c, f);
  238. bin = (c == ID_CHUNK);
  239. if (bin && f != stdin) {
  240. fclose(f);
  241. f = fopen(filename, "rb"); /* reopen in binary mode */
  242. if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
  243. }
  244. lua_pushliteral(L, "@");
  245. lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename);
  246. lua_concat(L, 2);
  247. filename = lua_tostring(L, -1); /* filename = '@'..filename */
  248. luaZ_Fopen(&z, f, filename);
  249. status = protectedparser(L, &z, bin);
  250. lua_remove(L, -2); /* remove filename */
  251. if (f != stdin)
  252. fclose(f);
  253. return status;
  254. }
  255. LUA_API int lua_dofile (lua_State *L, const char *filename) {
  256. int status;
  257. status = parse_file(L, filename);
  258. if (status == 0) /* parse OK? */
  259. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  260. return status;
  261. }
  262. static int parse_buffer (lua_State *L, const char *buff, size_t size,
  263. const char *name) {
  264. ZIO z;
  265. int status;
  266. if (!name) name = "?";
  267. luaZ_mopen(&z, buff, size, name);
  268. status = protectedparser(L, &z, buff[0]==ID_CHUNK);
  269. return status;
  270. }
  271. LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name) {
  272. int status;
  273. status = parse_buffer(L, buff, size, name);
  274. if (status == 0) /* parse OK? */
  275. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  276. return status;
  277. }
  278. LUA_API int lua_dostring (lua_State *L, const char *str) {
  279. return lua_dobuffer(L, str, strlen(str), str);
  280. }
  281. /*
  282. ** {======================================================
  283. ** Error-recover functions (based on long jumps)
  284. ** =======================================================
  285. */
  286. /* chain list of long jump buffers */
  287. struct lua_longjmp {
  288. jmp_buf b;
  289. struct lua_longjmp *previous;
  290. volatile int status; /* error code */
  291. };
  292. static void message (lua_State *L, const char *s) {
  293. const TObject *em = luaH_getstr(L->gt, luaS_newliteral(L, LUA_ERRORMESSAGE));
  294. if (ttype(em) == LUA_TFUNCTION) {
  295. setobj(L->top, em);
  296. incr_top;
  297. setsvalue(L->top, luaS_new(L, s));
  298. incr_top;
  299. luaD_call(L, L->top-2, 0);
  300. }
  301. }
  302. /*
  303. ** Reports an error, and jumps up to the available recovery label
  304. */
  305. void luaD_error (lua_State *L, const char *s) {
  306. if (s) message(L, s);
  307. luaD_breakrun(L, LUA_ERRRUN);
  308. }
  309. void luaD_breakrun (lua_State *L, int errcode) {
  310. if (L->errorJmp) {
  311. L->errorJmp->status = errcode;
  312. longjmp(L->errorJmp->b, 1);
  313. }
  314. else {
  315. if (errcode != LUA_ERRMEM)
  316. message(L, "unable to recover; exiting\n");
  317. exit(EXIT_FAILURE);
  318. }
  319. }
  320. int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
  321. StkId oldCbase = L->Cbase;
  322. StkId oldtop = L->top;
  323. struct lua_longjmp lj;
  324. int allowhooks = L->allowhooks;
  325. lj.status = 0;
  326. lj.previous = L->errorJmp; /* chain new error handler */
  327. L->errorJmp = &lj;
  328. if (setjmp(lj.b) == 0)
  329. (*f)(L, ud);
  330. else { /* an error occurred: restore the state */
  331. L->allowhooks = allowhooks;
  332. L->Cbase = oldCbase;
  333. L->top = oldtop;
  334. restore_stack_limit(L);
  335. }
  336. L->errorJmp = lj.previous; /* restore old error handler */
  337. return lj.status;
  338. }
  339. /* }====================================================== */