ldo.c 9.0 KB

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