ldo.c 10.0 KB

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