ldo.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. ** $Id: ldo.c,v 1.9 1997/11/19 17:29:23 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 "ldo.h"
  11. #include "lfunc.h"
  12. #include "lgc.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lparser.h"
  16. #include "lstate.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
  25. #endif
  26. /*
  27. ** Error messages
  28. */
  29. static void stderrorim (void)
  30. {
  31. lua_Object s = lua_getparam(1);
  32. if (lua_isstring(s))
  33. fprintf(stderr, "lua error: %s\n", lua_getstring(s));
  34. }
  35. static void initCfunc (TObject *o, lua_CFunction f)
  36. {
  37. ttype(o) = LUA_T_CPROTO;
  38. fvalue(o) = f;
  39. luaF_simpleclosure(o);
  40. }
  41. #define STACK_EXTRA 32
  42. #define INIT_STACK_SIZE 32
  43. void luaD_init (void)
  44. {
  45. L->stacklimit = STACK_LIMIT;
  46. L->stack.stack = luaM_newvector(INIT_STACK_SIZE, TObject);
  47. L->stack.top = L->stack.stack;
  48. L->stack.last = L->stack.stack+(INIT_STACK_SIZE-1);
  49. initCfunc(&L->errorim, stderrorim);
  50. }
  51. void luaD_checkstack (int n)
  52. {
  53. if (L->stack.last-L->stack.top <= n) {
  54. StkId top = L->stack.top-L->stack.stack;
  55. int stacksize = (L->stack.last-L->stack.stack)+1+STACK_EXTRA+n;
  56. L->stack.stack = luaM_reallocvector(L->stack.stack, stacksize,TObject);
  57. L->stack.last = L->stack.stack+(stacksize-1);
  58. L->stack.top = L->stack.stack + top;
  59. if (stacksize >= L->stacklimit) {
  60. /* extra space to run error handler */
  61. L->stacklimit = stacksize+STACK_EXTRA;
  62. if (lua_stackedfunction(100) == LUA_NOOBJECT) {
  63. /* less than 100 functions on the stack: cannot be recursive loop */
  64. lua_error("Lua2C - C2Lua overflow");
  65. }
  66. else
  67. lua_error(stackEM);
  68. }
  69. }
  70. }
  71. /*
  72. ** Adjust stack. Set top to the given value, pushing NILs if needed.
  73. */
  74. void luaD_adjusttop (StkId newtop)
  75. {
  76. int diff = newtop-(L->stack.top-L->stack.stack);
  77. if (diff <= 0)
  78. L->stack.top += diff;
  79. else {
  80. luaD_checkstack(diff);
  81. while (diff--)
  82. ttype(L->stack.top++) = LUA_T_NIL;
  83. }
  84. }
  85. /*
  86. ** Open a hole below "nelems" from the L->stack.top.
  87. */
  88. void luaD_openstack (int nelems)
  89. {
  90. int i;
  91. for (i=0; i<nelems; i++)
  92. *(L->stack.top-i) = *(L->stack.top-i-1);
  93. incr_top;
  94. }
  95. void luaD_lineHook (int line)
  96. {
  97. struct C_Lua_Stack oldCLS = L->Cstack;
  98. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
  99. L->Cstack.num = 0;
  100. (*lua_linehook)(line);
  101. L->stack.top = L->stack.stack+old_top;
  102. L->Cstack = oldCLS;
  103. }
  104. void luaD_callHook (StkId base, lua_Type type, int isreturn)
  105. {
  106. struct C_Lua_Stack oldCLS = L->Cstack;
  107. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
  108. L->Cstack.num = 0;
  109. if (isreturn)
  110. (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
  111. else {
  112. TObject *f = L->stack.stack+base-1;
  113. if (type == LUA_T_PROTO)
  114. (*lua_callhook)(Ref(f), tfvalue(protovalue(f))->fileName->str,
  115. tfvalue(protovalue(f))->lineDefined);
  116. else
  117. (*lua_callhook)(Ref(f), "(C)", -1);
  118. }
  119. L->stack.top = L->stack.stack+old_top;
  120. L->Cstack = oldCLS;
  121. }
  122. /*
  123. ** Call a C function. L->Cstack.base will point to the top of the stack,
  124. ** and L->Cstack.num is the number of parameters. Returns an index
  125. ** to the first result from C.
  126. */
  127. static StkId callC (lua_CFunction func, StkId base)
  128. {
  129. struct C_Lua_Stack oldCLS = L->Cstack;
  130. StkId firstResult;
  131. L->Cstack.num = (L->stack.top-L->stack.stack) - base;
  132. /* incorporate parameters on the L->stack.stack */
  133. L->Cstack.lua2C = base;
  134. L->Cstack.base = base+L->Cstack.num; /* == top-stack */
  135. if (lua_callhook)
  136. luaD_callHook(base, LUA_T_CPROTO, 0);
  137. (*func)();
  138. if (lua_callhook) /* func may have changed lua_callhook */
  139. luaD_callHook(base, LUA_T_CPROTO, 1);
  140. firstResult = L->Cstack.base;
  141. L->Cstack = oldCLS;
  142. return firstResult;
  143. }
  144. void luaD_callTM (TObject *f, int nParams, int nResults)
  145. {
  146. luaD_openstack(nParams);
  147. *(L->stack.top-nParams-1) = *f;
  148. luaD_call((L->stack.top-L->stack.stack)-nParams, nResults);
  149. }
  150. /*
  151. ** Call a function (C or Lua). The parameters must be on the L->stack.stack,
  152. ** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
  153. ** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
  154. ** The number of results is nResults, unless nResults=MULT_RET.
  155. */
  156. void luaD_call (StkId base, int nResults)
  157. {
  158. StkId firstResult;
  159. TObject *func = L->stack.stack+base-1;
  160. int i;
  161. if (ttype(func) == LUA_T_FUNCTION) {
  162. TObject *proto = protovalue(func);
  163. ttype(func) = LUA_T_MARK;
  164. firstResult = (ttype(proto) == LUA_T_CPROTO) ? callC(fvalue(proto), base)
  165. : luaV_execute(func->value.cl, base);
  166. }
  167. else { /* 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. /* adjust the number of results */
  176. if (nResults != MULT_RET)
  177. luaD_adjusttop(firstResult+nResults);
  178. /* move results to base-1 (to erase parameters and function) */
  179. base--;
  180. nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
  181. for (i=0; i<nResults; i++)
  182. *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
  183. L->stack.top -= firstResult-base;
  184. }
  185. /*
  186. ** Traverse all objects on L->stack.stack
  187. */
  188. void luaD_travstack (int (*fn)(TObject *))
  189. {
  190. StkId i;
  191. for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
  192. fn(L->stack.stack+i);
  193. }
  194. static void message (char *s)
  195. {
  196. TObject im = L->errorim;
  197. if (ttype(&im) != LUA_T_NIL) {
  198. lua_pushstring(s);
  199. luaD_callTM(&im, 1, 0);
  200. }
  201. }
  202. /*
  203. ** Reports an error, and jumps up to the available recover label
  204. */
  205. void lua_error (char *s)
  206. {
  207. if (s) message(s);
  208. if (L->errorJmp)
  209. longjmp(*((jmp_buf *)L->errorJmp), 1);
  210. else {
  211. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  212. exit(1);
  213. }
  214. }
  215. /*
  216. ** Call the function at L->Cstack.base, and incorporate results on
  217. ** the Lua2C structure.
  218. */
  219. static void do_callinc (int nResults)
  220. {
  221. StkId base = L->Cstack.base;
  222. luaD_call(base+1, nResults);
  223. L->Cstack.lua2C = base; /* position of the luaM_new results */
  224. L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
  225. L->Cstack.base = base + L->Cstack.num; /* incorporate results on L->stack.stack */
  226. }
  227. /*
  228. ** Execute a protected call. Assumes that function is at L->Cstack.base and
  229. ** parameters are on top of it. Leave nResults on the stack.
  230. */
  231. int luaD_protectedrun (int nResults)
  232. {
  233. jmp_buf myErrorJmp;
  234. int status;
  235. struct C_Lua_Stack oldCLS = L->Cstack;
  236. jmp_buf *oldErr = L->errorJmp;
  237. L->errorJmp = &myErrorJmp;
  238. if (setjmp(myErrorJmp) == 0) {
  239. do_callinc(nResults);
  240. status = 0;
  241. }
  242. else { /* an error occurred: restore L->Cstack and L->stack.top */
  243. L->Cstack = oldCLS;
  244. L->stack.top = L->stack.stack+L->Cstack.base;
  245. status = 1;
  246. }
  247. L->errorJmp = oldErr;
  248. return status;
  249. }
  250. /*
  251. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  252. */
  253. static int protectedparser (ZIO *z, char *chunkname, int bin)
  254. {
  255. int status;
  256. TProtoFunc *tf;
  257. jmp_buf myErrorJmp;
  258. jmp_buf *oldErr = L->errorJmp;
  259. L->errorJmp = &myErrorJmp;
  260. if (setjmp(myErrorJmp) == 0) {
  261. tf = bin ? luaU_undump1(z, chunkname) : luaY_parser(z, chunkname);
  262. status = 0;
  263. }
  264. else {
  265. tf = NULL;
  266. status = 1;
  267. }
  268. L->errorJmp = oldErr;
  269. if (status) return 1; /* error code */
  270. if (tf == NULL) return 2; /* 'natural' end */
  271. luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
  272. L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
  273. L->stack.stack[L->Cstack.base].value.tf = tf;
  274. luaV_closure(0);
  275. return 0;
  276. }
  277. static int do_main (ZIO *z, char *chunkname, int bin)
  278. {
  279. int status;
  280. do {
  281. long old_blocks = (luaC_checkGC(), L->nblocks);
  282. status = protectedparser(z, chunkname, bin);
  283. if (status == 1) return 1; /* error */
  284. else if (status == 2) return 0; /* 'natural' end */
  285. else {
  286. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  287. L->GCthreshold += newelems2;
  288. status = luaD_protectedrun(MULT_RET);
  289. L->GCthreshold -= newelems2;
  290. }
  291. } while (bin && status == 0);
  292. return status;
  293. }
  294. void luaD_gcIM (TObject *o)
  295. {
  296. TObject *im = luaT_getimbyObj(o, IM_GC);
  297. if (ttype(im) != LUA_T_NIL) {
  298. *L->stack.top = *o;
  299. incr_top;
  300. luaD_callTM(im, 1, 0);
  301. }
  302. }
  303. int lua_dofile (char *filename)
  304. {
  305. ZIO z;
  306. int status;
  307. int c;
  308. int bin;
  309. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  310. if (f == NULL)
  311. return 2;
  312. if (filename == NULL)
  313. filename = "(stdin)";
  314. c = fgetc(f);
  315. ungetc(c, f);
  316. bin = (c == ID_CHUNK);
  317. if (bin)
  318. f = freopen(filename, "rb", f); /* set binary mode */
  319. luaZ_Fopen(&z, f);
  320. status = do_main(&z, filename, bin);
  321. if (f != stdin)
  322. fclose(f);
  323. return status;
  324. }
  325. #define SIZE_PREF 20 /* size of string prefix to appear in error messages */
  326. int lua_dostring (char *str)
  327. {
  328. int status;
  329. char buff[SIZE_PREF+25];
  330. char *temp;
  331. ZIO z;
  332. if (str == NULL) return 1;
  333. sprintf(buff, "(dostring) >> %.20s", str);
  334. temp = strchr(buff, '\n');
  335. if (temp) *temp = 0; /* end string after first line */
  336. luaZ_sopen(&z, str);
  337. status = do_main(&z, buff, 0);
  338. return status;
  339. }