ldo.c 9.9 KB

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