ldo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. ** $Id: ldo.c,v 1.42 1999/05/10 13:54:01 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
  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. {
  123. TObject *pbase;
  124. int nup = cl->nelems; /* number of upvalues */
  125. luaD_checkstack(nup);
  126. pbase = L->stack.stack+base; /* care: previous call may change this */
  127. /* open space for upvalues as extra arguments */
  128. luaO_memup(pbase+nup, pbase, (L->stack.top-pbase)*sizeof(TObject));
  129. /* copy upvalues into stack */
  130. memcpy(pbase, cl->consts+1, nup*sizeof(TObject));
  131. L->stack.top += nup;
  132. return callC(f, base);
  133. }
  134. void luaD_callTM (TObject *f, int nParams, int nResults) {
  135. luaD_openstack(nParams);
  136. *(L->stack.top-nParams-1) = *f;
  137. luaD_calln(nParams, nResults);
  138. }
  139. /*
  140. ** Call a function (C or Lua). The parameters must be on the L->stack.stack,
  141. ** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
  142. ** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
  143. ** The number of results is nResults, unless nResults=MULT_RET.
  144. */
  145. void luaD_call (StkId base, int nResults)
  146. {
  147. StkId firstResult;
  148. TObject *func = L->stack.stack+base-1;
  149. int i;
  150. switch (ttype(func)) {
  151. case LUA_T_CPROTO:
  152. ttype(func) = LUA_T_CMARK;
  153. firstResult = callC(fvalue(func), base);
  154. break;
  155. case LUA_T_PROTO:
  156. ttype(func) = LUA_T_PMARK;
  157. firstResult = luaV_execute(NULL, tfvalue(func), base);
  158. break;
  159. case LUA_T_CLOSURE: {
  160. Closure *c = clvalue(func);
  161. TObject *proto = &(c->consts[0]);
  162. ttype(func) = LUA_T_CLMARK;
  163. firstResult = (ttype(proto) == LUA_T_CPROTO) ?
  164. callCclosure(c, fvalue(proto), base) :
  165. luaV_execute(c, tfvalue(proto), base);
  166. break;
  167. }
  168. default: { /* func is not a function */
  169. /* Check the tag method for invalid functions */
  170. TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
  171. if (ttype(im) == LUA_T_NIL)
  172. lua_error("call expression not a function");
  173. luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults);
  174. return;
  175. }
  176. }
  177. /* adjust the number of results */
  178. if (nResults != MULT_RET)
  179. luaD_adjusttop(firstResult+nResults);
  180. /* move results to base-1 (to erase parameters and function) */
  181. base--;
  182. nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
  183. for (i=0; i<nResults; i++)
  184. *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
  185. L->stack.top -= firstResult-base;
  186. }
  187. void luaD_calln (int nArgs, int nResults) {
  188. luaD_call((L->stack.top-L->stack.stack)-nArgs, nResults);
  189. }
  190. /*
  191. ** Traverse all objects on L->stack.stack
  192. */
  193. void luaD_travstack (int (*fn)(TObject *))
  194. {
  195. StkId i;
  196. for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
  197. fn(L->stack.stack+i);
  198. }
  199. static void message (char *s) {
  200. TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval);
  201. if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO ||
  202. ttype(em) == LUA_T_CLOSURE) {
  203. *L->stack.top = *em;
  204. incr_top;
  205. lua_pushstring(s);
  206. luaD_calln(1, 0);
  207. }
  208. }
  209. /*
  210. ** Reports an error, and jumps up to the available recover label
  211. */
  212. void lua_error (char *s) {
  213. if (s) message(s);
  214. if (L->errorJmp)
  215. longjmp(L->errorJmp->b, 1);
  216. else {
  217. message("exit(1). Unable to recover.\n");
  218. exit(1);
  219. }
  220. }
  221. /*
  222. ** Call the function at L->Cstack.base, and incorporate results on
  223. ** the Lua2C structure.
  224. */
  225. static void do_callinc (int nResults)
  226. {
  227. StkId base = L->Cstack.base;
  228. luaD_call(base+1, nResults);
  229. L->Cstack.lua2C = base; /* position of the new results */
  230. L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
  231. L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
  232. }
  233. /*
  234. ** Execute a protected call. Assumes that function is at L->Cstack.base and
  235. ** parameters are on top of it. Leave nResults on the stack.
  236. */
  237. int luaD_protectedrun (int nResults) {
  238. volatile struct C_Lua_Stack oldCLS = L->Cstack;
  239. struct lua_longjmp myErrorJmp;
  240. volatile int status;
  241. struct lua_longjmp *volatile oldErr = L->errorJmp;
  242. L->errorJmp = &myErrorJmp;
  243. if (setjmp(myErrorJmp.b) == 0) {
  244. do_callinc(nResults);
  245. status = 0;
  246. }
  247. else { /* an error occurred: restore L->Cstack and L->stack.top */
  248. L->Cstack = oldCLS;
  249. L->stack.top = L->stack.stack+L->Cstack.base;
  250. status = 1;
  251. }
  252. L->errorJmp = oldErr;
  253. return status;
  254. }
  255. /*
  256. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  257. */
  258. static int protectedparser (ZIO *z, int bin) {
  259. volatile struct C_Lua_Stack oldCLS = L->Cstack;
  260. struct lua_longjmp myErrorJmp;
  261. volatile int status;
  262. TProtoFunc *volatile tf;
  263. struct lua_longjmp *volatile oldErr = L->errorJmp;
  264. L->errorJmp = &myErrorJmp;
  265. if (setjmp(myErrorJmp.b) == 0) {
  266. tf = bin ? luaU_undump1(z) : luaY_parser(z);
  267. status = 0;
  268. }
  269. else { /* an error occurred: restore L->Cstack and L->stack.top */
  270. L->Cstack = oldCLS;
  271. L->stack.top = L->stack.stack+L->Cstack.base;
  272. tf = NULL;
  273. status = 1;
  274. }
  275. L->errorJmp = oldErr;
  276. if (status) return 1; /* error code */
  277. if (tf == NULL) return 2; /* 'natural' end */
  278. luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
  279. L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
  280. L->stack.stack[L->Cstack.base].value.tf = tf;
  281. luaV_closure(0);
  282. return 0;
  283. }
  284. static int do_main (ZIO *z, int bin) {
  285. int status;
  286. int debug = L->debug; /* save debug status */
  287. do {
  288. long old_blocks = (luaC_checkGC(), L->nblocks);
  289. status = protectedparser(z, bin);
  290. if (status == 1) return 1; /* error */
  291. else if (status == 2) return 0; /* 'natural' end */
  292. else {
  293. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  294. L->GCthreshold += newelems2;
  295. status = luaD_protectedrun(MULT_RET);
  296. L->GCthreshold -= newelems2;
  297. }
  298. } while (bin && status == 0);
  299. L->debug = debug; /* restore debug status */
  300. return status;
  301. }
  302. void luaD_gcIM (TObject *o)
  303. {
  304. TObject *im = luaT_getimbyObj(o, IM_GC);
  305. if (ttype(im) != LUA_T_NIL) {
  306. *L->stack.top = *o;
  307. incr_top;
  308. luaD_callTM(im, 1, 0);
  309. }
  310. }
  311. #define MAXFILENAME 260 /* maximum part of a file name kept */
  312. int lua_dofile (char *filename) {
  313. ZIO z;
  314. int status;
  315. int c;
  316. int bin;
  317. char source[MAXFILENAME];
  318. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  319. if (f == NULL)
  320. return 2;
  321. c = fgetc(f);
  322. ungetc(c, f);
  323. bin = (c == ID_CHUNK);
  324. if (bin)
  325. f = freopen(filename, "rb", f); /* set binary mode */
  326. luaL_filesource(source, filename, sizeof(source));
  327. luaZ_Fopen(&z, f, source);
  328. status = do_main(&z, bin);
  329. if (f != stdin)
  330. fclose(f);
  331. return status;
  332. }
  333. int lua_dostring (char *str) {
  334. return lua_dobuffer(str, strlen(str), str);
  335. }
  336. int lua_dobuffer (char *buff, int size, char *name) {
  337. ZIO z;
  338. if (!name) name = "?";
  339. luaZ_mopen(&z, buff, size, name);
  340. return do_main(&z, buff[0]==ID_CHUNK);
  341. }