ldo.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. ** $Id: ldo.c,v 1.87 2000/08/29 14:33:31 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. static 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 n;
  117. L->Cbase = base; /* new base for C function */
  118. luaD_checkstack(L, nup);
  119. for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
  120. *(L->top++) = cl->upvalue[n];
  121. n = (*cl->f.c)(L); /* do the actual call */
  122. L->Cbase = old_Cbase; /* restore old C base */
  123. return L->top - n; /* return index of first result */
  124. }
  125. void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
  126. StkId base = L->top - nParams;
  127. luaD_openstack(L, base);
  128. *base = *f;
  129. luaD_call(L, base, nResults);
  130. }
  131. /*
  132. ** Call a function (C or Lua). The function to be called is at *func.
  133. ** The arguments are on the stack, right after the function.
  134. ** When returns, the results are on the stack, starting at the original
  135. ** function position.
  136. ** The number of results is nResults, unless nResults=LUA_MULTRET.
  137. */
  138. void luaD_call (lua_State *L, StkId func, int nResults) {
  139. StkId firstResult;
  140. lua_Hook callhook = L->callhook;
  141. retry: /* for `function' tag method */
  142. switch (ttype(func)) {
  143. case TAG_LCLOSURE: {
  144. CallInfo ci;
  145. ci.func = clvalue(func);
  146. ci.line = 0;
  147. ttype(func) = TAG_LMARK;
  148. infovalue(func) = &ci;
  149. if (callhook)
  150. luaD_callHook(L, func, callhook, "call");
  151. firstResult = luaV_execute(L, ci.func, func+1);
  152. break;
  153. }
  154. case TAG_CCLOSURE: {
  155. ttype(func) = TAG_CMARK;
  156. if (callhook)
  157. luaD_callHook(L, func, callhook, "call");
  158. firstResult = callCclosure(L, clvalue(func), func+1);
  159. break;
  160. }
  161. default: { /* `func' is not a function; check the `function' tag method */
  162. const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
  163. if (ttype(im) == TAG_NIL)
  164. luaG_typeerror(L, func, "call");
  165. luaD_openstack(L, func);
  166. *func = *im; /* tag method is the new function to be called */
  167. goto retry; /* retry the call */
  168. }
  169. }
  170. if (callhook) /* same hook that was active at entry */
  171. luaD_callHook(L, func, callhook, "return");
  172. /* adjust the number of results */
  173. if (nResults == LUA_MULTRET)
  174. nResults = L->top - firstResult;
  175. else
  176. luaD_adjusttop(L, firstResult, nResults);
  177. /* move results to `func' (to erase parameters and function) */
  178. while (nResults) {
  179. *func++ = *(L->top - nResults);
  180. nResults--;
  181. }
  182. L->top = func;
  183. }
  184. static void message (lua_State *L, const char *s) {
  185. const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
  186. if (*luaO_typename(em) == 'f') {
  187. *L->top = *em;
  188. incr_top;
  189. lua_pushstring(L, s);
  190. luaD_call(L, L->top-2, 0);
  191. }
  192. }
  193. void luaD_breakrun (lua_State *L, int errcode) {
  194. if (L->errorJmp) {
  195. L->errorJmp->status = errcode;
  196. longjmp(L->errorJmp->b, 1);
  197. }
  198. else {
  199. if (errcode != LUA_ERRMEM)
  200. message(L, "unable to recover; exiting\n");
  201. exit(1);
  202. }
  203. }
  204. /*
  205. ** Reports an error, and jumps up to the available recovery label
  206. */
  207. void lua_error (lua_State *L, const char *s) {
  208. if (s) message(L, s);
  209. luaD_breakrun(L, LUA_ERRRUN);
  210. }
  211. static void chain_longjmp (lua_State *L, struct lua_longjmp *lj) {
  212. lj->status = 0;
  213. lj->base = L->Cbase;
  214. lj->previous = L->errorJmp;
  215. L->errorJmp = lj;
  216. }
  217. static int restore_longjmp (lua_State *L, struct lua_longjmp *lj) {
  218. L->Cbase = lj->base;
  219. L->errorJmp = lj->previous;
  220. return lj->status;
  221. }
  222. /*
  223. ** Execute a protected call.
  224. */
  225. int lua_call (lua_State *L, int nargs, int nresults) {
  226. StkId func = L->top - (nargs+1); /* function to be called */
  227. struct lua_longjmp myErrorJmp;
  228. chain_longjmp(L, &myErrorJmp);
  229. if (setjmp(myErrorJmp.b) == 0) {
  230. luaD_call(L, func, nresults);
  231. }
  232. else { /* an error occurred: restore the state */
  233. L->top = func; /* remove garbage from the stack */
  234. restore_stack_limit(L);
  235. }
  236. return restore_longjmp(L, &myErrorJmp);
  237. }
  238. /*
  239. ** returns 0 = chunk loaded; >0 : error; -1 = no more chunks to load
  240. */
  241. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  242. struct lua_longjmp myErrorJmp;
  243. chain_longjmp(L, &myErrorJmp);
  244. if (setjmp(myErrorJmp.b) == 0) {
  245. Proto *tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
  246. if (tf == NULL)
  247. myErrorJmp.status = -1; /* `natural' end */
  248. luaV_Lclosure(L, tf, 0);
  249. }
  250. else { /* an error occurred: correct error code */
  251. if (myErrorJmp.status == LUA_ERRRUN)
  252. myErrorJmp.status = LUA_ERRSYNTAX;
  253. }
  254. return restore_longjmp(L, &myErrorJmp); /* error code */
  255. }
  256. static int do_main (lua_State *L, ZIO *z, int bin) {
  257. int status;
  258. do {
  259. unsigned long old_blocks;
  260. luaC_checkGC(L);
  261. old_blocks = L->nblocks;
  262. status = protectedparser(L, z, bin);
  263. if (status > 0) return status; /* error */
  264. else if (status < 0) return 0; /* `natural' end */
  265. else {
  266. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  267. L->GCthreshold += newelems2;
  268. status = lua_call(L, 0, LUA_MULTRET);
  269. L->GCthreshold -= newelems2;
  270. }
  271. } while (bin && status == 0);
  272. return status;
  273. }
  274. int lua_dofile (lua_State *L, const char *filename) {
  275. ZIO z;
  276. int status;
  277. int bin; /* flag for file mode */
  278. int c; /* look ahead char */
  279. char source[MAXFILENAME];
  280. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  281. if (f == NULL) return 2; /* unable to open file */
  282. luaL_filesource(source, filename, sizeof(source));
  283. c = fgetc(f);
  284. ungetc(c, f);
  285. bin = (c == ID_CHUNK);
  286. if (bin && f != stdin) {
  287. f = freopen(filename, "rb", f); /* set binary mode */
  288. if (f == NULL) return 2; /* unable to reopen file */
  289. }
  290. luaZ_Fopen(&z, f, source);
  291. status = do_main(L, &z, bin);
  292. if (f != stdin)
  293. fclose(f);
  294. return status;
  295. }
  296. int lua_dostring (lua_State *L, const char *str) {
  297. return lua_dobuffer(L, str, strlen(str), str);
  298. }
  299. int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  300. const char *name) {
  301. ZIO z;
  302. if (!name) name = "?";
  303. luaZ_mopen(&z, buff, size, name);
  304. return do_main(L, &z, buff[0]==ID_CHUNK);
  305. }