ldo.c 8.8 KB

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