ldo.c 8.9 KB

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