ldo.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. ** $Id: ldo.c,v 1.55 1999/12/02 16:24:45 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. #define LUA_REENTRANT
  11. #include "lauxlib.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 "ltm.h"
  20. #include "lua.h"
  21. #include "luadebug.h"
  22. #include "lundump.h"
  23. #include "lvm.h"
  24. #include "lzio.h"
  25. #ifndef DEFAULT_STACK_SIZE
  26. #define DEFAULT_STACK_SIZE 1024
  27. #endif
  28. #define EXTRA_STACK 32 /* space to handle stack overflow errors */
  29. /*
  30. ** typical numer of recursive calls that fit in the stack
  31. ** (only for error messages)
  32. */
  33. #define REC_DEEP (DEFAULT_STACK_SIZE/20)
  34. void luaD_init (lua_State *L) {
  35. L->stack = luaM_newvector(L, DEFAULT_STACK_SIZE+EXTRA_STACK, TObject);
  36. L->stack_last = L->stack+(DEFAULT_STACK_SIZE-1);
  37. L->Cstack.base = L->Cstack.lua2C = L->top = L->stack;
  38. L->Cstack.num = 0;
  39. }
  40. void luaD_checkstack (lua_State *L, int n) {
  41. if (L->stack_last-L->top <= n) { /* stack overflow? */
  42. if (L->stack_last-L->stack > (DEFAULT_STACK_SIZE-1)) {
  43. /* overflow while handling overflow: do what?? */
  44. L->top -= EXTRA_STACK;
  45. lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!!");
  46. }
  47. else {
  48. L->stack_last += EXTRA_STACK; /* to be used by error message */
  49. if (lua_stackedfunction(L, REC_DEEP) == LUA_NOOBJECT) {
  50. /* less than REC_DEEP funcs on stack: doesn't look like a rec. loop */
  51. lua_error(L, "Lua2C - C2Lua overflow");
  52. }
  53. else
  54. lua_error(L, "stack size overflow");
  55. }
  56. }
  57. }
  58. static void restore_stack_limit (lua_State *L) {
  59. if (L->top-L->stack < DEFAULT_STACK_SIZE-1)
  60. L->stack_last = L->stack+(DEFAULT_STACK_SIZE-1);
  61. }
  62. /*
  63. ** Adjust stack. Set top to base+extra, pushing NILs if needed.
  64. ** (we cannot add base+extra unless we are sure it fits in the stack;
  65. ** otherwise the result of such operation on pointers is undefined)
  66. */
  67. void luaD_adjusttop (lua_State *L, StkId base, int extra) {
  68. int diff = extra-(L->top-base);
  69. if (diff <= 0)
  70. L->top = base+extra;
  71. else {
  72. luaD_checkstack(L, diff);
  73. while (diff--)
  74. ttype(L->top++) = LUA_T_NIL;
  75. }
  76. }
  77. /*
  78. ** Open a hole inside the stack at `pos'
  79. */
  80. void luaD_openstack (lua_State *L, StkId pos) {
  81. luaO_memup(pos+1, pos, (L->top-pos)*sizeof(TObject));
  82. incr_top;
  83. }
  84. void luaD_lineHook (lua_State *L, int line) {
  85. struct C_Lua_Stack oldCLS = L->Cstack;
  86. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
  87. L->Cstack.num = 0;
  88. (*L->linehook)(L, line);
  89. L->top = old_top;
  90. L->Cstack = oldCLS;
  91. }
  92. void luaD_callHook (lua_State *L, StkId base, const TProtoFunc *tf,
  93. int isreturn) {
  94. struct C_Lua_Stack oldCLS = L->Cstack;
  95. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
  96. L->Cstack.num = 0;
  97. if (isreturn)
  98. (*L->callhook)(L, LUA_NOOBJECT, "(return)", 0);
  99. else {
  100. TObject *f = base-1;
  101. if (tf)
  102. v 1.3 1997/10/16, f, tf->source->str, tf->lineDefined);
  103. else (*L->callhook)(L, f, "(C)", -1);
  104. }
  105. L->top = old_top;
  106. L->Cstack = oldCLS;
  107. }
  108. /*
  109. ** Call a C function.
  110. ** Cstack.num is the number of arguments; Cstack.lua2C points to the
  111. ** first argument. Returns an index to the first result from C.
  112. */
  113. static StkId callC (lua_State *L, lua_CFunction f, StkId base) {
  114. struct C_Lua_Stack oldCLS = L->Cstack;
  115. StkId firstResult;
  116. int numarg = L->top - base;
  117. L->Cstack.num = numarg;
  118. L->Cstack.lua2C = base;
  119. L->Cstack.base = L->top;
  120. if (L->callhook)
  121. luaD_callHook(L, base, NULL, 0);
  122. (*f)(L); /* do the actual call */
  123. if (L->callhook) /* test again: `func' may have changed callhook */
  124. luaD_callHook(L, base, NULL, 1);
  125. firstResult = L->Cstack.base;
  126. L->Cstack = oldCLS;
  127. return firstResult;
  128. }
  129. static StkId callCclosure (lua_State *L, const struct Closure *cl,
  130. lua_CFunction f, StkId base) {
  131. int nup = cl->nelems; /* number of upvalues */
  132. luaD_checkstack(L, nup);
  133. /* open space for upvalues as extra arguments */
  134. luaO_memup(base+nup, base, (L->top-base)*sizeof(TObject));
  135. /* copy upvalues into stack */
  136. memcpy(base, cl->consts+1, nup*sizeof(TObject));
  137. L->top += nup;
  138. return callC(L, f, base);
  139. }
  140. void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
  141. StkId base = L->top - nParams;
  142. luaD_openstack(L, base);
  143. *base = *f;
  144. luaD_call(L, base, nResults);
  145. }
  146. /*
  147. ** Call a function (C or Lua). The function to be called is at *func.
  148. ** The arguments are on the stack, right after the function.
  149. ** When returns, the results are on the stack, starting at the original
  150. ** function position.
  151. ** The number of results is nResults, unless nResults=MULT_RET.
  152. */
  153. void luaD_call (lua_State *L, StkId func, int nResults) {
  154. StkId firstResult;
  155. switch (ttype(func)) {
  156. case LUA_T_CPROTO:
  157. ttype(func) = LUA_T_CMARK;
  158. firstResult = callC(L, fvalue(func), func+1);
  159. break;
  160. case LUA_T_PROTO:
  161. ttype(func) = LUA_T_PMARK;
  162. firstResult = luaV_execute(L, NULL, tfvalue(func), func+1);
  163. break;
  164. case LUA_T_CLOSURE: {
  165. Closure *c = clvalue(func);
  166. TObject *proto = c->consts;
  167. ttype(func) = LUA_T_CLMARK;
  168. firstResult = (ttype(proto) == LUA_T_CPROTO) ?
  169. callCclosure(L, c, fvalue(proto), func+1) :
  170. luaV_execute(L, c, tfvalue(proto), func+1);
  171. break;
  172. }
  173. default: { /* func is not a function */
  174. /* Check the tag method for invalid functions */
  175. const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
  176. if (ttype(im) == LUA_T_NIL)
  177. lua_error(L, "call expression not a function");
  178. luaD_callTM(L, im, L->top-func, nResults);
  179. return;
  180. }
  181. }
  182. /* adjust the number of results */
  183. if (nResults == MULT_RET)
  184. nResults = L->top - firstResult;
  185. else
  186. luaD_adjusttop(L, firstResult, nResults);
  187. /* move results to func (to erase parameters and function) */
  188. luaO_memdown(func, firstResult, nResults*sizeof(TObject));
  189. L->top = func+nResults;
  190. }
  191. static void message (lua_State *L, const char *s) {
  192. const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
  193. if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO ||
  194. ttype(em) == LUA_T_CLOSURE) {
  195. *L->top = *em;
  196. incr_top;
  197. lua_pushstring(L, s);
  198. luaD_call(L, L->top-2, 0);
  199. }
  200. }
  201. /*
  202. ** Reports an error, and jumps up to the available recover label
  203. */
  204. void lua_error (lua_State *L, const char *s) {
  205. if (s) message(L, s);
  206. if (L->errorJmp)
  207. longjmp(L->errorJmp->b, 1);
  208. else {
  209. message(L, "exit(1). Unable to recover.\n");
  210. exit(1);
  211. }
  212. }
  213. /*
  214. ** Execute a protected call. Assumes that function is at L->Cstack.base and
  215. ** parameters are on top of it. Leave nResults on the stack.
  216. */
  217. int luaD_protectedrun (lua_State *L) {
  218. volatile struct C_Lua_Stack oldCLS = L->Cstack;
  219. struct lua_longjmp myErrorJmp;
  220. volatile int status;
  221. struct lua_longjmp *volatile oldErr = L->errorJmp;
  222. L->errorJmp = &myErrorJmp;
  223. if (setjmp(myErrorJmp.b) == 0) {
  224. StkId base = L->Cstack.base;
  225. luaD_call(L, base, MULT_RET);
  226. L->Cstack.lua2C = base; /* position of the new results */
  227. L->Cstack.num = L->top - base;
  228. L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
  229. status = 0;
  230. }
  231. else { /* an error occurred: restore the stack */
  232. L->Cstack = oldCLS;
  233. L->top = L->Cstack.base;
  234. restore_stack_limit(L);
  235. status = 1;
  236. }
  237. L->errorJmp = oldErr;
  238. return status;
  239. }
  240. /*
  241. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  242. */
  243. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  244. volatile struct C_Lua_Stack oldCLS = L->Cstack;
  245. struct lua_longjmp myErrorJmp;
  246. volatile int status;
  247. TProtoFunc *volatile tf;
  248. struct lua_longjmp *volatile oldErr = L->errorJmp;
  249. L->errorJmp = &myErrorJmp;
  250. if (setjmp(myErrorJmp.b) == 0) {
  251. tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
  252. status = 0;
  253. }
  254. else { /* an error occurred: restore L->Cstack and L->top */
  255. L->Cstack = oldCLS;
  256. L->top = L->Cstack.base;
  257. tf = NULL;
  258. status = 1;
  259. }
  260. L->errorJmp = oldErr;
  261. if (status) return 1; /* error code */
  262. if (tf == NULL) return 2; /* `natural' end */
  263. luaD_adjusttop(L, L->Cstack.base, 1); /* one slot for the pseudo-function */
  264. L->Cstack.base->ttype = LUA_T_PROTO;
  265. L->Cstack.base->value.tf = tf;
  266. luaV_closure(L, 0);
  267. return 0;
  268. }
  269. static int do_main (lua_State *L, ZIO *z, int bin) {
  270. int status;
  271. int debug = L->debug; /* save debug status */
  272. do {
  273. long old_blocks = (luaC_checkGC(L), L->nblocks);
  274. status = protectedparser(L, z, bin);
  275. if (status == 1) return 1; /* error */
  276. else if (status == 2) return 0; /* 'natural' end */
  277. else {
  278. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  279. L->GCthreshold += newelems2;
  280. status = luaD_protectedrun(L);
  281. L->GCthreshold -= newelems2;
  282. }
  283. } while (bin && status == 0);
  284. L->debug = debug; /* restore debug status */
  285. return status;
  286. }
  287. void luaD_gcIM (lua_State *L, const TObject *o) {
  288. const TObject *im = luaT_getimbyObj(L, o, IM_GC);
  289. if (ttype(im) != LUA_T_NIL) {
  290. *L->top = *o;
  291. incr_top;
  292. luaD_callTM(L, im, 1, 0);
  293. }
  294. }
  295. #define MAXFILENAME 260 /* maximum part of a file name kept */
  296. int lua_dofile (lua_State *L, const char *filename) {
  297. ZIO z;
  298. int status;
  299. int c;
  300. int bin;
  301. char source[MAXFILENAME];
  302. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  303. if (f == NULL)
  304. return 2;
  305. c = fgetc(f);
  306. ungetc(c, f);
  307. bin = (c == ID_CHUNK);
  308. if (bin)
  309. f = freopen(filename, "rb", f); /* set binary mode */
  310. luaL_filesource(source, filename, sizeof(source));
  311. luaZ_Fopen(&z, f, source);
  312. status = do_main(L, &z, bin);
  313. if (f != stdin)
  314. fclose(f);
  315. return status;
  316. }
  317. int lua_dostring (lua_State *L, const char *str) {
  318. return lua_dobuffer(L, str, strlen(str), str);
  319. }
  320. int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) {
  321. ZIO z;
  322. if (!name) name = "?";
  323. luaZ_mopen(&z, buff, size, name);
  324. return do_main(L, &z, buff[0]==ID_CHUNK);
  325. }