ldo.c 9.7 KB

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