ldo.c 9.8 KB

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