ldo.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. ** $Id: ldo.c,v 1.85 2000/08/10 19:50:47 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 "lauxlib.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lgc.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lparser.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lundump.h"
  23. #include "lvm.h"
  24. #include "lzio.h"
  25. #define EXTRA_STACK 32 /* space to handle stack overflow errors */
  26. /*
  27. ** typical numer of stack slots used by a (big) function
  28. ** (this constant is used only for choosing error messages)
  29. */
  30. #define SLOTS_PER_F 20
  31. void luaD_init (lua_State *L, int stacksize) {
  32. L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
  33. L->stack_last = L->stack+(stacksize-1);
  34. L->stacksize = stacksize;
  35. L->Cbase = L->top = L->stack;
  36. }
  37. void luaD_checkstack (lua_State *L, int n) {
  38. if (L->stack_last-L->top <= n) { /* stack overflow? */
  39. if (L->stack_last-L->stack > (L->stacksize-1)) {
  40. /* overflow while handling overflow: do what?? */
  41. L->top -= EXTRA_STACK;
  42. lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
  43. }
  44. else {
  45. lua_Debug dummy;
  46. L->stack_last += EXTRA_STACK; /* to be used by error message */
  47. if (lua_getstack(L, L->stacksize/SLOTS_PER_F, &dummy) == 0) {
  48. /* too few funcs on stack: doesn't look like a recursion loop */
  49. lua_error(L, "Lua2C - C2Lua overflow");
  50. }
  51. else
  52. lua_error(L, "stack overflow; possible recursion loop");
  53. }
  54. }
  55. }
  56. static void restore_stack_limit (lua_State *L) {
  57. if (L->top - L->stack < L->stacksize - 1)
  58. L->stack_last = L->stack + (L->stacksize-1);
  59. }
  60. /*
  61. ** Adjust stack. Set top to base+extra, pushing NILs if needed.
  62. ** (we cannot add base+extra unless we are sure it fits in the stack;
  63. ** otherwise the result of such operation on pointers is undefined)
  64. */
  65. void luaD_adjusttop (lua_State *L, StkId base, int extra) {
  66. int diff = extra-(L->top-base);
  67. if (diff <= 0)
  68. L->top = base+extra;
  69. else {
  70. luaD_checkstack(L, diff);
  71. while (diff--)
  72. ttype(L->top++) = TAG_NIL;
  73. }
  74. }
  75. /*
  76. ** Open a hole inside the stack at `pos'
  77. */
  78. void luaD_openstack (lua_State *L, StkId pos) {
  79. int i = L->top-pos;
  80. while (i--) pos[i+1] = pos[i];
  81. incr_top;
  82. }
  83. void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
  84. if (L->allowhooks) {
  85. lua_Debug ar;
  86. StkId old_Cbase = L->Cbase;
  87. StkId old_top = L->Cbase = L->top;
  88. ar._func = func;
  89. ar.event = "line";
  90. ar.currentline = line;
  91. L->allowhooks = 0; /* cannot call hooks inside a hook */
  92. (*linehook)(L, &ar);
  93. L->allowhooks = 1;
  94. L->top = old_top;
  95. L->Cbase = old_Cbase;
  96. }
  97. }
  98. static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
  99. const char *event) {
  100. if (L->allowhooks) {
  101. lua_Debug ar;
  102. StkId old_Cbase = L->Cbase;
  103. StkId old_top = L->Cbase = L->top;
  104. ar._func = func;
  105. ar.event = event;
  106. L->allowhooks = 0; /* cannot call hooks inside a hook */
  107. callhook(L, &ar);
  108. L->allowhooks = 1;
  109. L->top = old_top;
  110. L->Cbase = old_Cbase;
  111. }
  112. }
  113. static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
  114. int nup = cl->nupvalues; /* number of upvalues */
  115. StkId old_Cbase = L->Cbase;
  116. int nres; /* number of results */
  117. if (nup > 0) {
  118. int n = L->top - base; /* number of arguments */
  119. luaD_checkstack(L, nup);
  120. /* open space for upvalues as extra arguments */
  121. while (n--) *(base+nup+n) = *(base+n);
  122. L->top += nup;
  123. /* copy upvalues into stack */
  124. while (nup--) *(base+nup) = cl->upvalue[nup];
  125. }
  126. L->Cbase = base; /* new base for C function */
  127. nres = (*cl->f.c)(L); /* do the actual call */
  128. L->Cbase = old_Cbase; /* restore old C base */
  129. return L->top - nres; /* return index of first result */
  130. }
  131. void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
  132. StkId base = L->top - nParams;
  133. luaD_openstack(L, base);
  134. *base = *f;
  135. luaD_call(L, base, nResults);
  136. }
  137. /*
  138. ** Call a function (C or Lua). The function to be called is at *func.
  139. ** The arguments are on the stack, right after the function.
  140. ** When returns, the results are on the stack, starting at the original
  141. ** function position.
  142. ** The number of results is nResults, unless nResults=MULT_RET.
  143. */
  144. void luaD_call (lua_State *L, StkId func, int nResults) {
  145. StkId firstResult;
  146. lua_Hook callhook = L->callhook;
  147. retry: /* for `function' tag method */
  148. switch (ttype(func)) {
  149. case TAG_LCLOSURE: {
  150. CallInfo ci;
  151. ci.func = clvalue(func);
  152. ci.line = 0;
  153. ttype(func) = TAG_LMARK;
  154. infovalue(func) = &ci;
  155. if (callhook)
  156. luaD_callHook(L, func, callhook, "call");
  157. firstResult = luaV_execute(L, ci.func, func+1);
  158. break;
  159. }
  160. case TAG_CCLOSURE: {
  161. ttype(func) = TAG_CMARK;
  162. if (callhook)
  163. luaD_callHook(L, func, callhook, "call");
  164. firstResult = callCclosure(L, clvalue(func), func+1);
  165. break;
  166. }
  167. default: { /* `func' is not a function; check the `function' tag method */
  168. const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
  169. if (ttype(im) == TAG_NIL)
  170. luaG_typeerror(L, func, "call");
  171. luaD_openstack(L, func);
  172. *func = *im; /* tag method is the new function to be called */
  173. goto retry; /* retry the call */
  174. }
  175. }
  176. if (callhook) /* same hook that was active at entry */
  177. luaD_callHook(L, func, callhook, "return");
  178. /* adjust the number of results */
  179. if (nResults == MULT_RET)
  180. nResults = L->top - firstResult;
  181. else
  182. luaD_adjusttop(L, firstResult, nResults);
  183. /* move results to `func' (to erase parameters and function) */
  184. while (nResults) {
  185. *func++ = *(L->top - nResults);
  186. nResults--;
  187. }
  188. L->top = func;
  189. }
  190. static void message (lua_State *L, const char *s) {
  191. const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
  192. if (*luaO_typename(em) == 'f') {
  193. *L->top = *em;
  194. incr_top;
  195. lua_pushstring(L, s);
  196. luaD_call(L, L->top-2, 0);
  197. }
  198. }
  199. void luaD_breakrun (lua_State *L, int errcode) {
  200. if (L->errorJmp) {
  201. L->errorJmp->status = errcode;
  202. longjmp(L->errorJmp->b, 1);
  203. }
  204. else {
  205. if (errcode != LUA_ERRMEM)
  206. message(L, "unable to recover; exiting\n");
  207. exit(1);
  208. }
  209. }
  210. /*
  211. ** Reports an error, and jumps up to the available recovery label
  212. */
  213. void lua_error (lua_State *L, const char *s) {
  214. if (s) message(L, s);
  215. luaD_breakrun(L, LUA_ERRRUN);
  216. }
  217. static void chain_longjmp (lua_State *L, struct lua_longjmp *lj) {
  218. lj->status = 0;
  219. lj->base = L->Cbase;
  220. lj->previous = L->errorJmp;
  221. L->errorJmp = lj;
  222. }
  223. static int restore_longjmp (lua_State *L, struct lua_longjmp *lj) {
  224. L->Cbase = lj->base;
  225. L->errorJmp = lj->previous;
  226. return lj->status;
  227. }
  228. /*
  229. ** Execute a protected call.
  230. */
  231. int lua_call (lua_State *L, int nargs, int nresults) {
  232. StkId func = L->top - (nargs+1); /* function to be called */
  233. struct lua_longjmp myErrorJmp;
  234. chain_longjmp(L, &myErrorJmp);
  235. if (nresults == LUA_MULTRET) nresults = MULT_RET; /* internal code */
  236. if (setjmp(myErrorJmp.b) == 0) {
  237. luaD_call(L, func, nresults);
  238. }
  239. else { /* an error occurred: restore the state */
  240. L->top = func; /* remove garbage from the stack */
  241. restore_stack_limit(L);
  242. }
  243. return restore_longjmp(L, &myErrorJmp);
  244. }
  245. /*
  246. ** returns 0 = chunk loaded; >0 : error; -1 = no more chunks to load
  247. */
  248. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  249. struct lua_longjmp myErrorJmp;
  250. chain_longjmp(L, &myErrorJmp);
  251. if (setjmp(myErrorJmp.b) == 0) {
  252. Proto *tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
  253. if (tf == NULL)
  254. myErrorJmp.status = -1; /* `natural' end */
  255. luaV_Lclosure(L, tf, 0);
  256. }
  257. else { /* an error occurred: correct error code */
  258. if (myErrorJmp.status == LUA_ERRRUN)
  259. myErrorJmp.status = LUA_ERRSYNTAX;
  260. }
  261. return restore_longjmp(L, &myErrorJmp); /* error code */
  262. }
  263. static int do_main (lua_State *L, ZIO *z, int bin) {
  264. int status;
  265. do {
  266. unsigned long old_blocks;
  267. luaC_checkGC(L);
  268. old_blocks = L->nblocks;
  269. status = protectedparser(L, z, bin);
  270. if (status > 0) return status; /* error */
  271. else if (status < 0) return 0; /* `natural' end */
  272. else {
  273. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  274. L->GCthreshold += newelems2;
  275. status = lua_call(L, 0, LUA_MULTRET);
  276. L->GCthreshold -= newelems2;
  277. }
  278. } while (bin && status == 0);
  279. return status;
  280. }
  281. int lua_dofile (lua_State *L, const char *filename) {
  282. ZIO z;
  283. int status;
  284. int bin; /* flag for file mode */
  285. int c; /* look ahead char */
  286. char source[MAXFILENAME];
  287. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  288. if (f == NULL) return 2; /* unable to open file */
  289. luaL_filesource(source, filename, sizeof(source));
  290. c = fgetc(f);
  291. ungetc(c, f);
  292. bin = (c == ID_CHUNK);
  293. if (bin && f != stdin) {
  294. f = freopen(filename, "rb", f); /* set binary mode */
  295. if (f == NULL) return 2; /* unable to reopen file */
  296. }
  297. luaZ_Fopen(&z, f, source);
  298. status = do_main(L, &z, bin);
  299. if (f != stdin)
  300. fclose(f);
  301. return status;
  302. }
  303. int lua_dostring (lua_State *L, const char *str) {
  304. return lua_dobuffer(L, str, strlen(str), str);
  305. }
  306. int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  307. const char *name) {
  308. ZIO z;
  309. if (!name) name = "?";
  310. luaZ_mopen(&z, buff, size, name);
  311. return do_main(L, &z, buff[0]==ID_CHUNK);
  312. }