ldo.c 10 KB

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