opcode.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.83 1997/03/06 17:30:55 roberto Exp roberto $";
  6. #include <setjmp.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "luadebug.h"
  11. #include "mem.h"
  12. #include "opcode.h"
  13. #include "hash.h"
  14. #include "inout.h"
  15. #include "table.h"
  16. #include "lua.h"
  17. #include "fallback.h"
  18. #include "undump.h"
  19. #define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
  20. #define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
  21. #define STACK_SIZE 128
  22. #ifndef STACK_LIMIT
  23. #define STACK_LIMIT 6000
  24. #endif
  25. typedef int StkId; /* index to stack elements */
  26. static Object initial_stack;
  27. static Object *stackLimit = &initial_stack+1;
  28. static Object *stack = &initial_stack;
  29. static Object *top = &initial_stack;
  30. /* macros to convert from lua_Object to (Object *) and back */
  31. #define Address(lo) ((lo)+stack-1)
  32. #define Ref(st) ((st)-stack+1)
  33. /* macro to increment stack top. There must be always an empty slot in
  34. * the stack
  35. */
  36. #define incr_top if (++top >= stackLimit) growstack()
  37. struct C_Lua_Stack {
  38. StkId base; /* when Lua calls C or C calls Lua, points to */
  39. /* the first slot after the last parameter. */
  40. int num; /* when Lua calls C, has the number of parameters; */
  41. /* when C calls Lua, has the number of results. */
  42. };
  43. static struct C_Lua_Stack CLS_current = {0, 0};
  44. static jmp_buf *errorJmp = NULL; /* current error recover point */
  45. /* Hooks */
  46. lua_LHFunction lua_linehook = NULL;
  47. lua_CHFunction lua_callhook = NULL;
  48. static StkId lua_execute (Byte *pc, StkId base);
  49. static void do_call (StkId base, int nResults);
  50. Object *luaI_Address (lua_Object o)
  51. {
  52. return Address(o);
  53. }
  54. /*
  55. ** Init stack
  56. */
  57. static void lua_initstack (void)
  58. {
  59. Long maxstack = STACK_SIZE;
  60. stack = newvector(maxstack, Object);
  61. stackLimit = stack+maxstack;
  62. top = stack;
  63. *(top++) = initial_stack;
  64. }
  65. /*
  66. ** Check stack overflow and, if necessary, realloc vector
  67. */
  68. #define lua_checkstack(nt) if ((nt) >= stackLimit) growstack()
  69. static void growstack (void)
  70. {
  71. if (stack == &initial_stack)
  72. lua_initstack();
  73. else
  74. {
  75. static int limit = STACK_LIMIT;
  76. StkId t = top-stack;
  77. Long stacksize = stackLimit - stack;
  78. stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100);
  79. stackLimit = stack+stacksize;
  80. top = stack + t;
  81. if (stacksize >= limit)
  82. {
  83. limit = stacksize;
  84. lua_error(stackEM);
  85. }
  86. }
  87. }
  88. /*
  89. ** Concatenate two given strings. Return the new string pointer.
  90. */
  91. static char *lua_strconc (char *l, char *r)
  92. {
  93. size_t nl = strlen(l);
  94. char *buffer = luaI_buffer(nl+strlen(r)+1);
  95. strcpy(buffer, l);
  96. strcpy(buffer+nl, r);
  97. return buffer;
  98. }
  99. /*
  100. ** Convert, if possible, to a number object.
  101. ** Return 0 if success, not 0 if error.
  102. */
  103. static int lua_tonumber (Object *obj)
  104. {
  105. float t;
  106. char c;
  107. if (ttype(obj) != LUA_T_STRING)
  108. return 1;
  109. else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
  110. {
  111. nvalue(obj) = t;
  112. ttype(obj) = LUA_T_NUMBER;
  113. return 0;
  114. }
  115. else
  116. return 2;
  117. }
  118. /*
  119. ** Convert, if possible, to a string ttype
  120. ** Return 0 in success or not 0 on error.
  121. */
  122. static int lua_tostring (Object *obj)
  123. {
  124. if (ttype(obj) != LUA_T_NUMBER)
  125. return 1;
  126. else {
  127. char s[60];
  128. real f = nvalue(obj);
  129. int i;
  130. if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f)
  131. sprintf (s, "%d", i);
  132. else
  133. sprintf (s, "%g", nvalue(obj));
  134. tsvalue(obj) = lua_createstring(s);
  135. ttype(obj) = LUA_T_STRING;
  136. return 0;
  137. }
  138. }
  139. /*
  140. ** Adjust stack. Set top to the given value, pushing NILs if needed.
  141. */
  142. static void adjust_top (StkId newtop)
  143. {
  144. Object *nt;
  145. lua_checkstack(stack+newtop);
  146. nt = stack+newtop; /* warning: previous call may change stack */
  147. while (top < nt) ttype(top++) = LUA_T_NIL;
  148. top = nt; /* top could be bigger than newtop */
  149. }
  150. #define adjustC(nParams) adjust_top(CLS_current.base+nParams)
  151. /*
  152. ** Open a hole below "nelems" from the top.
  153. */
  154. static void open_stack (int nelems)
  155. {
  156. int i;
  157. for (i=0; i<nelems; i++)
  158. *(top-i) = *(top-i-1);
  159. incr_top;
  160. }
  161. /*
  162. ** call Line hook
  163. */
  164. static void lineHook (int line)
  165. {
  166. struct C_Lua_Stack oldCLS = CLS_current;
  167. StkId old_top = CLS_current.base = top-stack;
  168. CLS_current.num = 0;
  169. (*lua_linehook)(line);
  170. top = stack+old_top;
  171. CLS_current = oldCLS;
  172. }
  173. /*
  174. ** Call hook
  175. ** The function being called is in [stack+base-1]
  176. */
  177. static void callHook (StkId base, lua_Type type, int isreturn)
  178. {
  179. struct C_Lua_Stack oldCLS = CLS_current;
  180. StkId old_top = CLS_current.base = top-stack;
  181. CLS_current.num = 0;
  182. if (isreturn)
  183. (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
  184. else
  185. {
  186. Object *f = stack+base-1;
  187. if (type == LUA_T_MARK)
  188. (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
  189. else
  190. (*lua_callhook)(Ref(f), "(C)", -1);
  191. }
  192. top = stack+old_top;
  193. CLS_current = oldCLS;
  194. }
  195. /*
  196. ** Call a C function. CLS_current.base will point to the top of the stack,
  197. ** and CLS_current.num is the number of parameters. Returns an index
  198. ** to the first result from C.
  199. */
  200. static StkId callC (lua_CFunction func, StkId base)
  201. {
  202. struct C_Lua_Stack oldCLS = CLS_current;
  203. StkId firstResult;
  204. CLS_current.num = (top-stack) - base;
  205. /* incorporate parameters on the stack */
  206. CLS_current.base = base+CLS_current.num; /* == top-stack */
  207. if (lua_callhook)
  208. callHook(base, LUA_T_CMARK, 0);
  209. (*func)();
  210. if (lua_callhook) /* func may have changed lua_callhook */
  211. callHook(base, LUA_T_CMARK, 1);
  212. firstResult = CLS_current.base;
  213. CLS_current = oldCLS;
  214. return firstResult;
  215. }
  216. static void callIM (Object *f, int nParams, int nResults)
  217. {
  218. open_stack(nParams);
  219. *(top-nParams-1) = *f;
  220. do_call((top-stack)-nParams, nResults);
  221. }
  222. /*
  223. ** Call the specified fallback, putting it on the stack below its arguments
  224. */
  225. static void callFB (int fb)
  226. {
  227. callIM(&luaI_fallBacks[fb].function, luaI_fallBacks[fb].nParams,
  228. luaI_fallBacks[fb].nResults);
  229. }
  230. /*
  231. ** Call a function (C or Lua). The parameters must be on the stack,
  232. ** between [stack+base,top). The function to be called is at stack+base-1.
  233. ** When returns, the results are on the stack, between [stack+base-1,top).
  234. ** The number of results is nResults, unless nResults=MULT_RET.
  235. */
  236. static void do_call (StkId base, int nResults)
  237. {
  238. StkId firstResult;
  239. Object *func = stack+base-1;
  240. int i;
  241. if (ttype(func) == LUA_T_CFUNCTION)
  242. {
  243. ttype(func) = LUA_T_CMARK;
  244. firstResult = callC(fvalue(func), base);
  245. }
  246. else if (ttype(func) == LUA_T_FUNCTION)
  247. {
  248. ttype(func) = LUA_T_MARK;
  249. firstResult = lua_execute(func->value.tf->code, base);
  250. }
  251. else
  252. { /* func is not a function */
  253. /* Call the fallback for invalid functions */
  254. open_stack((top-stack)-(base-1));
  255. stack[base-1] = luaI_fallBacks[FB_FUNCTION].function;
  256. do_call(base, nResults);
  257. return;
  258. }
  259. /* adjust the number of results */
  260. if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
  261. adjust_top(firstResult+nResults);
  262. /* move results to base-1 (to erase parameters and function) */
  263. base--;
  264. nResults = top - (stack+firstResult); /* actual number of results */
  265. for (i=0; i<nResults; i++)
  266. *(stack+base+i) = *(stack+firstResult+i);
  267. top -= firstResult-base;
  268. }
  269. /*
  270. ** Function to index a table. Receives the table at top-2 and the index
  271. ** at top-1.
  272. */
  273. static void pushsubscript (void)
  274. {
  275. int tg = luaI_tag(top-2);
  276. Object *im = luaI_getim(tg, FB_GETTABLE);
  277. if (ttype(top-2) == LUA_T_ARRAY && im == NULL) {
  278. Object *h = lua_hashget(avalue(top-2), top-1);
  279. if (h != NULL && ttype(h) != LUA_T_NIL) {
  280. --top;
  281. *(top-1) = *h;
  282. }
  283. else if (tg == LUA_T_ARRAY &&
  284. (im=luaI_getim(0, FB_INDEX)) != NULL)
  285. callIM(im, 2, 1);
  286. else {
  287. --top;
  288. ttype(top-1) = LUA_T_NIL;
  289. }
  290. }
  291. else { /* object is not a table, and/or has a specific "gettable" method */
  292. if (im)
  293. callIM(im, 2, 1);
  294. else
  295. lua_error("indexed expression not a table");
  296. }
  297. }
  298. lua_Object lua_basicindex (void)
  299. {
  300. adjustC(2);
  301. if (ttype(top-2) != LUA_T_ARRAY)
  302. lua_error("indexed expression not a table in basic indexing");
  303. else {
  304. Object *h = lua_hashget(avalue(top-2), top-1);
  305. --top;
  306. if (h != NULL)
  307. *(top-1) = *h;
  308. else
  309. ttype(top-1) = LUA_T_NIL;
  310. }
  311. CLS_current.base++; /* incorporate object in the stack */
  312. return (Ref(top-1));
  313. }
  314. /*
  315. ** Function to store indexed based on values at the top
  316. ** mode = 0: basic store (without internal methods)
  317. ** mode = 1: normal store (with internal methods)
  318. ** mode = 2: "deep stack" store (with internal methods)
  319. */
  320. static void storesubscript (Object *t, int mode)
  321. {
  322. Object *im = (mode == 0) ? NULL : luaI_getim(luaI_tag(t), FB_SETTABLE);
  323. if (ttype(t) == LUA_T_ARRAY && im == NULL) {
  324. Object *h = lua_hashdefine(avalue(t), t+1);
  325. *h = *(top-1);
  326. top -= (mode == 2) ? 1 : 3;
  327. }
  328. else { /* object is not a table, and/or has a specific "settable" method */
  329. if (im) {
  330. if (mode == 2) {
  331. lua_checkstack(top+2);
  332. *(top+1) = *(top-1);
  333. *(top) = *(t+1);
  334. *(top-1) = *t;
  335. top += 2;
  336. }
  337. callIM(im, 3, 0);
  338. }
  339. else
  340. lua_error("indexed expression not a table");
  341. }
  342. }
  343. static void getglobal (Word n)
  344. {
  345. *top = lua_table[n].object;
  346. incr_top;
  347. if (ttype(top-1) == LUA_T_NIL)
  348. { /* must call getglobal fallback */
  349. ttype(top-1) = LUA_T_STRING;
  350. tsvalue(top-1) = lua_table[n].varname;
  351. callFB(FB_GETGLOBAL);
  352. }
  353. }
  354. /*
  355. ** Traverse all objects on stack
  356. */
  357. void lua_travstack (int (*fn)(Object *))
  358. {
  359. Object *o;
  360. for (o = top-1; o >= stack; o--)
  361. fn (o);
  362. }
  363. /*
  364. ** Error messages and debug functions
  365. */
  366. static void lua_message (char *s)
  367. {
  368. lua_pushstring(s);
  369. callFB(FB_ERROR);
  370. }
  371. /*
  372. ** Reports an error, and jumps up to the available recover label
  373. */
  374. void lua_error (char *s)
  375. {
  376. if (s) lua_message(s);
  377. if (errorJmp)
  378. longjmp(*errorJmp, 1);
  379. else
  380. {
  381. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  382. exit(1);
  383. }
  384. }
  385. lua_Function lua_stackedfunction (int level)
  386. {
  387. StkId i;
  388. for (i = (top-1)-stack; i>=0; i--)
  389. if (stack[i].ttype == LUA_T_MARK || stack[i].ttype == LUA_T_CMARK)
  390. if (level-- == 0)
  391. return Ref(stack+i);
  392. return LUA_NOOBJECT;
  393. }
  394. int lua_currentline (lua_Function func)
  395. {
  396. Object *f = Address(func);
  397. return (f+1 < top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
  398. }
  399. lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
  400. {
  401. Object *f = luaI_Address(func);
  402. *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  403. if (*name)
  404. {
  405. /* if "*name", there must be a LUA_T_LINE */
  406. /* therefore, f+2 points to function base */
  407. return Ref((f+2)+(local_number-1));
  408. }
  409. else
  410. return LUA_NOOBJECT;
  411. }
  412. int lua_setlocal (lua_Function func, int local_number)
  413. {
  414. Object *f = Address(func);
  415. char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  416. adjustC(1);
  417. --top;
  418. if (name)
  419. {
  420. /* if "name", there must be a LUA_T_LINE */
  421. /* therefore, f+2 points to function base */
  422. *((f+2)+(local_number-1)) = *top;
  423. return 1;
  424. }
  425. else
  426. return 0;
  427. }
  428. /*
  429. ** Call the function at CLS_current.base, and incorporate results on
  430. ** the Lua2C structure.
  431. */
  432. static void do_callinc (int nResults)
  433. {
  434. do_call(CLS_current.base+1, nResults);
  435. CLS_current.num = (top-stack) - CLS_current.base; /* number of results */
  436. CLS_current.base += CLS_current.num; /* incorporate results on the stack */
  437. }
  438. static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
  439. {
  440. adjustC(nParams);
  441. open_stack((top-stack)-CLS_current.base);
  442. stack[CLS_current.base].ttype = LUA_T_CFUNCTION;
  443. stack[CLS_current.base].value.f = f;
  444. do_callinc(nResults);
  445. }
  446. /*
  447. ** Execute a protected call. Assumes that function is at CLS_current.base and
  448. ** parameters are on top of it. Leave nResults on the stack.
  449. */
  450. static int do_protectedrun (int nResults)
  451. {
  452. jmp_buf myErrorJmp;
  453. int status;
  454. struct C_Lua_Stack oldCLS = CLS_current;
  455. jmp_buf *oldErr = errorJmp;
  456. errorJmp = &myErrorJmp;
  457. if (setjmp(myErrorJmp) == 0) {
  458. do_callinc(nResults);
  459. status = 0;
  460. }
  461. else { /* an error occurred: restore CLS_current and top */
  462. CLS_current = oldCLS;
  463. top = stack+CLS_current.base;
  464. status = 1;
  465. }
  466. errorJmp = oldErr;
  467. return status;
  468. }
  469. int luaI_dorun (TFunc *tf)
  470. {
  471. int status;
  472. adjustC(1); /* one slot for the pseudo-function */
  473. stack[CLS_current.base].ttype = LUA_T_FUNCTION;
  474. stack[CLS_current.base].value.tf = tf;
  475. status = do_protectedrun(MULT_RET);
  476. return status;
  477. }
  478. static int do_protectedmain (void)
  479. {
  480. TFunc tf;
  481. int status;
  482. jmp_buf myErrorJmp;
  483. jmp_buf *oldErr = errorJmp;
  484. errorJmp = &myErrorJmp;
  485. luaI_initTFunc(&tf);
  486. tf.fileName = lua_parsedfile;
  487. if (setjmp(myErrorJmp) == 0)
  488. {
  489. lua_parse(&tf);
  490. status = luaI_dorun(&tf);
  491. }
  492. else
  493. {
  494. status = 1;
  495. adjustC(0); /* erase extra slot */
  496. }
  497. errorJmp = oldErr;
  498. luaI_free(tf.code);
  499. return status;
  500. }
  501. /*
  502. ** Execute the given lua function. Return 0 on success or 1 on error.
  503. */
  504. int lua_callfunction (lua_Object function)
  505. {
  506. if (function == LUA_NOOBJECT)
  507. return 1;
  508. else
  509. {
  510. open_stack((top-stack)-CLS_current.base);
  511. stack[CLS_current.base] = *Address(function);
  512. return do_protectedrun (MULT_RET);
  513. }
  514. }
  515. int lua_call (char *funcname)
  516. {
  517. Word n = luaI_findsymbolbyname(funcname);
  518. open_stack((top-stack)-CLS_current.base);
  519. stack[CLS_current.base] = s_object(n);
  520. return do_protectedrun(MULT_RET);
  521. }
  522. /*
  523. ** Open file, generate opcode and execute global statement. Return 0 on
  524. ** success or non 0 on error.
  525. */
  526. int lua_dofile (char *filename)
  527. {
  528. int status;
  529. int c;
  530. FILE *f = lua_openfile(filename);
  531. if (f == NULL)
  532. return 2;
  533. c = fgetc(f);
  534. ungetc(c, f);
  535. if (c == ID_CHUNK) {
  536. f = freopen(filename, "rb", f); /* set binary mode */
  537. status = luaI_undump(f);
  538. }
  539. else {
  540. if (c == '#')
  541. while ((c=fgetc(f)) != '\n') /* skip first line */;
  542. status = do_protectedmain();
  543. }
  544. lua_closefile();
  545. return status;
  546. }
  547. /*
  548. ** Generate opcode stored on string and execute global statement. Return 0 on
  549. ** success or non 0 on error.
  550. */
  551. int lua_dostring (char *str)
  552. {
  553. int status;
  554. if (str == NULL)
  555. return 1;
  556. lua_openstring(str);
  557. status = do_protectedmain();
  558. lua_closestring();
  559. return status;
  560. }
  561. /*
  562. ** API: set a function as a fallback
  563. */
  564. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  565. {
  566. lua_pushstring(name);
  567. lua_pushcfunction(fallback);
  568. do_unprotectedrun(luaI_setfallback, 2, 1);
  569. return (Ref(top-1));
  570. }
  571. void lua_setintmethod (int tag, char *event, lua_CFunction method)
  572. {
  573. lua_pushnumber(tag);
  574. lua_pushstring(event);
  575. lua_pushcfunction (method);
  576. do_unprotectedrun(luaI_setintmethod, 3, 0);
  577. }
  578. /*
  579. ** API: receives on the stack the table and the index.
  580. ** returns the value.
  581. */
  582. lua_Object lua_getsubscript (void)
  583. {
  584. adjustC(2);
  585. pushsubscript();
  586. CLS_current.base++; /* incorporate object in the stack */
  587. return (Ref(top-1));
  588. }
  589. #define MAX_C_BLOCKS 10
  590. static int numCblocks = 0;
  591. static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
  592. /*
  593. ** API: starts a new block
  594. */
  595. void lua_beginblock (void)
  596. {
  597. if (numCblocks >= MAX_C_BLOCKS)
  598. lua_error("`lua_beginblock': too many nested blocks");
  599. Cblocks[numCblocks] = CLS_current;
  600. numCblocks++;
  601. }
  602. /*
  603. ** API: ends a block
  604. */
  605. void lua_endblock (void)
  606. {
  607. --numCblocks;
  608. CLS_current = Cblocks[numCblocks];
  609. adjustC(0);
  610. }
  611. void lua_settag (int tag)
  612. {
  613. adjustC(1);
  614. luaI_settag(tag, --top);
  615. }
  616. /*
  617. ** API: receives on the stack the table, the index, and the new value.
  618. */
  619. void lua_storesubscript (void)
  620. {
  621. adjustC(3);
  622. storesubscript(top-3, 1);
  623. }
  624. void lua_basicstoreindex (void)
  625. {
  626. adjustC(3);
  627. storesubscript(top-3, 0);
  628. }
  629. /*
  630. ** API: creates a new table
  631. */
  632. lua_Object lua_createtable (void)
  633. {
  634. adjustC(0);
  635. avalue(top) = lua_createarray(0);
  636. ttype(top) = LUA_T_ARRAY;
  637. incr_top;
  638. CLS_current.base++; /* incorporate object in the stack */
  639. return Ref(top-1);
  640. }
  641. /*
  642. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  643. ** 'number' must be 1 to get the first parameter.
  644. */
  645. lua_Object lua_getparam (int number)
  646. {
  647. if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
  648. /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
  649. stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
  650. return CLS_current.base-CLS_current.num+number;
  651. }
  652. int lua_isnil (lua_Object o)
  653. {
  654. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
  655. }
  656. int lua_istable (lua_Object o)
  657. {
  658. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
  659. }
  660. int lua_isuserdata (lua_Object o)
  661. {
  662. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
  663. }
  664. int lua_iscfunction (lua_Object o)
  665. {
  666. int t = lua_tag(o);
  667. return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
  668. }
  669. int lua_isnumber (lua_Object o)
  670. {
  671. return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
  672. }
  673. int lua_isstring (lua_Object o)
  674. {
  675. int t = lua_tag(o);
  676. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  677. }
  678. int lua_isfunction (lua_Object o)
  679. {
  680. int t = lua_tag(o);
  681. return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
  682. (t == LUA_T_MARK) || (t == LUA_T_CMARK);
  683. }
  684. /*
  685. ** Given an object handle, return its number value. On error, return 0.0.
  686. */
  687. real lua_getnumber (lua_Object object)
  688. {
  689. if (object == LUA_NOOBJECT) return 0.0;
  690. if (tonumber (Address(object))) return 0.0;
  691. else return (nvalue(Address(object)));
  692. }
  693. /*
  694. ** Given an object handle, return its string pointer. On error, return NULL.
  695. */
  696. char *lua_getstring (lua_Object object)
  697. {
  698. if (object == LUA_NOOBJECT) return NULL;
  699. if (tostring (Address(object))) return NULL;
  700. else return (svalue(Address(object)));
  701. }
  702. void *lua_getbinarydata (lua_Object object)
  703. {
  704. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  705. lua_error("getbinarydata: object is not binary data");
  706. return svalue(Address(object));
  707. }
  708. int lua_getbindatasize (lua_Object object)
  709. {
  710. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  711. return 0;
  712. else return (Address(object))->value.ts->size;
  713. }
  714. /*
  715. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  716. */
  717. lua_CFunction lua_getcfunction (lua_Object object)
  718. {
  719. if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) &&
  720. (ttype(Address(object)) != LUA_T_CMARK)))
  721. return NULL;
  722. else return (fvalue(Address(object)));
  723. }
  724. lua_Object lua_getref (int ref)
  725. {
  726. Object *o = luaI_getref(ref);
  727. if (o == NULL)
  728. return LUA_NOOBJECT;
  729. adjustC(0);
  730. luaI_pushobject(o);
  731. CLS_current.base++; /* incorporate object in the stack */
  732. return Ref(top-1);
  733. }
  734. void lua_pushref (int ref)
  735. {
  736. Object *o = luaI_getref(ref);
  737. if (o == NULL)
  738. lua_error("access to invalid (possibly garbage collected) reference");
  739. luaI_pushobject(o);
  740. }
  741. int lua_ref (int lock)
  742. {
  743. adjustC(1);
  744. return luaI_ref(--top, lock);
  745. }
  746. /*
  747. ** Get a global object.
  748. */
  749. lua_Object lua_getglobal (char *name)
  750. {
  751. adjustC(0);
  752. getglobal(luaI_findsymbolbyname(name));
  753. CLS_current.base++; /* incorporate object in the stack */
  754. return Ref(top-1);
  755. }
  756. /*
  757. ** Store top of the stack at a global variable array field.
  758. */
  759. void lua_storeglobal (char *name)
  760. {
  761. Word n = luaI_findsymbolbyname(name);
  762. adjustC(1);
  763. s_object(n) = *(--top);
  764. }
  765. /*
  766. ** Push a nil object
  767. */
  768. void lua_pushnil (void)
  769. {
  770. ttype(top) = LUA_T_NIL;
  771. incr_top;
  772. }
  773. /*
  774. ** Push an object (ttype=number) to stack.
  775. */
  776. void lua_pushnumber (real n)
  777. {
  778. ttype(top) = LUA_T_NUMBER; nvalue(top) = n;
  779. incr_top;
  780. }
  781. /*
  782. ** Push an object (ttype=string) to stack.
  783. */
  784. void lua_pushstring (char *s)
  785. {
  786. if (s == NULL)
  787. ttype(top) = LUA_T_NIL;
  788. else
  789. {
  790. tsvalue(top) = lua_createstring(s);
  791. ttype(top) = LUA_T_STRING;
  792. }
  793. incr_top;
  794. }
  795. /*>>>>>>>>>#undef lua_pushliteral
  796. void lua_pushliteral(char *s) { lua_pushstring(s); }*/
  797. /*
  798. ** Push an object (ttype=cfunction) to stack.
  799. */
  800. void lua_pushcfunction (lua_CFunction fn)
  801. {
  802. ttype(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  803. incr_top;
  804. }
  805. void lua_pushbinarydata (void *buff, int size, int tag)
  806. {
  807. if (buff == NULL)
  808. ttype(top) = LUA_T_NIL;
  809. else {
  810. tsvalue(top) = luaI_createuserdata(buff, size, tag);
  811. ttype(top) = LUA_T_USERDATA;
  812. }
  813. incr_top;
  814. }
  815. /*
  816. ** Push an object (ttype=userdata) to stack.
  817. */
  818. void lua_pushusertag (void *u, int tag)
  819. {
  820. if (tag < LUA_T_USERDATA)
  821. lua_error("invalid tag in `lua_pushusertag'");
  822. lua_pushbinarydata(&u, sizeof(void *), tag);
  823. }
  824. /*
  825. ** Push an object on the stack.
  826. */
  827. void luaI_pushobject (Object *o)
  828. {
  829. *top = *o;
  830. incr_top;
  831. }
  832. /*
  833. ** Push a lua_Object on stack.
  834. */
  835. void lua_pushobject (lua_Object o)
  836. {
  837. if (o == LUA_NOOBJECT)
  838. lua_error("attempt to push a NOOBJECT");
  839. *top = *Address(o);
  840. if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
  841. else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
  842. incr_top;
  843. }
  844. int lua_tag (lua_Object o)
  845. {
  846. return (o == LUA_NOOBJECT) ? LUA_T_NIL : luaI_tag(Address(o));
  847. }
  848. void luaI_gcFB (Object *o)
  849. {
  850. *top = *o;
  851. incr_top;
  852. callFB(FB_GC);
  853. }
  854. static void call_arith (char *op)
  855. {
  856. lua_pushstring(op);
  857. callFB(FB_ARITH);
  858. }
  859. static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
  860. lua_Type ttype_great, char *op)
  861. {
  862. Object *l = top-2;
  863. Object *r = top-1;
  864. int result;
  865. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  866. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  867. else if (tostring(l) || tostring(r))
  868. {
  869. lua_pushstring(op);
  870. callFB(FB_ORDER);
  871. return;
  872. }
  873. else
  874. result = strcmp(svalue(l), svalue(r));
  875. top--;
  876. nvalue(top-1) = 1;
  877. ttype(top-1) = (result < 0) ? ttype_less :
  878. (result == 0) ? ttype_equal : ttype_great;
  879. }
  880. static void adjust_varargs (StkId first_extra_arg)
  881. {
  882. Object arg;
  883. Object *firstelem = stack+first_extra_arg;
  884. int nvararg = top-firstelem;
  885. int i;
  886. if (nvararg < 0) nvararg = 0;
  887. avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
  888. ttype(&arg) = LUA_T_ARRAY;
  889. for (i=0; i<nvararg; i++) {
  890. Object index;
  891. ttype(&index) = LUA_T_NUMBER;
  892. nvalue(&index) = i+1;
  893. *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
  894. }
  895. /* store counter in field "n" */ {
  896. Object index, extra;
  897. ttype(&index) = LUA_T_STRING;
  898. tsvalue(&index) = lua_createstring("n");
  899. ttype(&extra) = LUA_T_NUMBER;
  900. nvalue(&extra) = nvararg;
  901. *(lua_hashdefine(avalue(&arg), &index)) = extra;
  902. }
  903. adjust_top(first_extra_arg);
  904. *top = arg; incr_top;
  905. }
  906. /*
  907. ** Execute the given opcode, until a RET. Parameters are between
  908. ** [stack+base,top). Returns n such that the the results are between
  909. ** [stack+n,top).
  910. */
  911. static StkId lua_execute (Byte *pc, StkId base)
  912. {
  913. if (lua_callhook)
  914. callHook (base, LUA_T_MARK, 0);
  915. while (1)
  916. {
  917. OpCode opcode;
  918. switch (opcode = (OpCode)*pc++)
  919. {
  920. case PUSHNIL: ttype(top) = LUA_T_NIL; incr_top; break;
  921. case PUSH0: case PUSH1: case PUSH2:
  922. ttype(top) = LUA_T_NUMBER;
  923. nvalue(top) = opcode-PUSH0;
  924. incr_top;
  925. break;
  926. case PUSHBYTE:
  927. ttype(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  928. case PUSHWORD:
  929. {
  930. Word w;
  931. get_word(w,pc);
  932. ttype(top) = LUA_T_NUMBER; nvalue(top) = w;
  933. incr_top;
  934. }
  935. break;
  936. case PUSHFLOAT:
  937. {
  938. real num;
  939. get_float(num,pc);
  940. ttype(top) = LUA_T_NUMBER; nvalue(top) = num;
  941. incr_top;
  942. }
  943. break;
  944. case PUSHSTRING:
  945. {
  946. Word w;
  947. get_word(w,pc);
  948. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  949. incr_top;
  950. }
  951. break;
  952. case PUSHFUNCTION:
  953. {
  954. TFunc *f;
  955. get_code(f,pc);
  956. luaI_insertfunction(f); /* may take part in GC */
  957. top->ttype = LUA_T_FUNCTION;
  958. top->value.tf = f;
  959. incr_top;
  960. }
  961. break;
  962. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  963. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  964. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  965. case PUSHLOCAL9:
  966. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  967. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  968. case PUSHGLOBAL:
  969. {
  970. Word w;
  971. get_word(w,pc);
  972. getglobal(w);
  973. }
  974. break;
  975. case PUSHINDEXED:
  976. pushsubscript();
  977. break;
  978. case PUSHSELF:
  979. {
  980. Object receiver = *(top-1);
  981. Word w;
  982. get_word(w,pc);
  983. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  984. incr_top;
  985. pushsubscript();
  986. *top = receiver;
  987. incr_top;
  988. break;
  989. }
  990. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  991. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  992. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  993. case STORELOCAL9:
  994. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  995. break;
  996. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  997. case STOREGLOBAL:
  998. {
  999. Word w;
  1000. get_word(w,pc);
  1001. s_object(w) = *(--top);
  1002. }
  1003. break;
  1004. case STOREINDEXED0:
  1005. storesubscript(top-3, 1);
  1006. break;
  1007. case STOREINDEXED: {
  1008. int n = *pc++;
  1009. storesubscript(top-3-n, 2);
  1010. break;
  1011. }
  1012. case STORELIST0:
  1013. case STORELIST:
  1014. {
  1015. int m, n;
  1016. Object *arr;
  1017. if (opcode == STORELIST0) m = 0;
  1018. else m = *(pc++) * FIELDS_PER_FLUSH;
  1019. n = *(pc++);
  1020. arr = top-n-1;
  1021. while (n)
  1022. {
  1023. ttype(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  1024. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1025. top--;
  1026. n--;
  1027. }
  1028. }
  1029. break;
  1030. case STORERECORD: /* opcode obsolete: supersed by STOREMAP */
  1031. {
  1032. int n = *(pc++);
  1033. Object *arr = top-n-1;
  1034. while (n)
  1035. {
  1036. Word w;
  1037. get_word(w,pc);
  1038. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1039. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1040. top--;
  1041. n--;
  1042. }
  1043. }
  1044. break;
  1045. case STOREMAP: {
  1046. int n = *(pc++);
  1047. Object *arr = top-(2*n)-1;
  1048. while (n--) {
  1049. *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
  1050. top-=2;
  1051. }
  1052. }
  1053. break;
  1054. case ADJUST0:
  1055. adjust_top(base);
  1056. break;
  1057. case ADJUST:
  1058. adjust_top(base + *(pc++));
  1059. break;
  1060. case VARARGS:
  1061. adjust_varargs(base + *(pc++));
  1062. break;
  1063. case CREATEARRAY:
  1064. {
  1065. Word size;
  1066. get_word(size,pc);
  1067. avalue(top) = lua_createarray(size);
  1068. ttype(top) = LUA_T_ARRAY;
  1069. incr_top;
  1070. }
  1071. break;
  1072. case EQOP:
  1073. {
  1074. int res = lua_equalObj(top-2, top-1);
  1075. --top;
  1076. ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  1077. nvalue(top-1) = 1;
  1078. }
  1079. break;
  1080. case LTOP:
  1081. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
  1082. break;
  1083. case LEOP:
  1084. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
  1085. break;
  1086. case GTOP:
  1087. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
  1088. break;
  1089. case GEOP:
  1090. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
  1091. break;
  1092. case ADDOP:
  1093. {
  1094. Object *l = top-2;
  1095. Object *r = top-1;
  1096. if (tonumber(r) || tonumber(l))
  1097. call_arith("add");
  1098. else
  1099. {
  1100. nvalue(l) += nvalue(r);
  1101. --top;
  1102. }
  1103. }
  1104. break;
  1105. case SUBOP:
  1106. {
  1107. Object *l = top-2;
  1108. Object *r = top-1;
  1109. if (tonumber(r) || tonumber(l))
  1110. call_arith("sub");
  1111. else
  1112. {
  1113. nvalue(l) -= nvalue(r);
  1114. --top;
  1115. }
  1116. }
  1117. break;
  1118. case MULTOP:
  1119. {
  1120. Object *l = top-2;
  1121. Object *r = top-1;
  1122. if (tonumber(r) || tonumber(l))
  1123. call_arith("mul");
  1124. else
  1125. {
  1126. nvalue(l) *= nvalue(r);
  1127. --top;
  1128. }
  1129. }
  1130. break;
  1131. case DIVOP:
  1132. {
  1133. Object *l = top-2;
  1134. Object *r = top-1;
  1135. if (tonumber(r) || tonumber(l))
  1136. call_arith("div");
  1137. else
  1138. {
  1139. nvalue(l) /= nvalue(r);
  1140. --top;
  1141. }
  1142. }
  1143. break;
  1144. case POWOP:
  1145. call_arith("pow");
  1146. break;
  1147. case CONCOP:
  1148. {
  1149. Object *l = top-2;
  1150. Object *r = top-1;
  1151. if (tostring(r) || tostring(l))
  1152. callFB(FB_CONCAT);
  1153. else
  1154. {
  1155. tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  1156. --top;
  1157. }
  1158. }
  1159. break;
  1160. case MINUSOP:
  1161. if (tonumber(top-1))
  1162. {
  1163. ttype(top) = LUA_T_NIL;
  1164. incr_top;
  1165. call_arith("unm");
  1166. }
  1167. else
  1168. nvalue(top-1) = - nvalue(top-1);
  1169. break;
  1170. case NOTOP:
  1171. ttype(top-1) = (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1172. nvalue(top-1) = 1;
  1173. break;
  1174. case ONTJMP:
  1175. {
  1176. Word w;
  1177. get_word(w,pc);
  1178. if (ttype(top-1) != LUA_T_NIL) pc += w;
  1179. }
  1180. break;
  1181. case ONFJMP:
  1182. {
  1183. Word w;
  1184. get_word(w,pc);
  1185. if (ttype(top-1) == LUA_T_NIL) pc += w;
  1186. }
  1187. break;
  1188. case JMP:
  1189. {
  1190. Word w;
  1191. get_word(w,pc);
  1192. pc += w;
  1193. }
  1194. break;
  1195. case UPJMP:
  1196. {
  1197. Word w;
  1198. get_word(w,pc);
  1199. pc -= w;
  1200. }
  1201. break;
  1202. case IFFJMP:
  1203. {
  1204. Word w;
  1205. get_word(w,pc);
  1206. top--;
  1207. if (ttype(top) == LUA_T_NIL) pc += w;
  1208. }
  1209. break;
  1210. case IFFUPJMP:
  1211. {
  1212. Word w;
  1213. get_word(w,pc);
  1214. top--;
  1215. if (ttype(top) == LUA_T_NIL) pc -= w;
  1216. }
  1217. break;
  1218. case POP: --top; break;
  1219. case CALLFUNC:
  1220. {
  1221. int nParams = *(pc++);
  1222. int nResults = *(pc++);
  1223. StkId newBase = (top-stack)-nParams;
  1224. do_call(newBase, nResults);
  1225. }
  1226. break;
  1227. case RETCODE0:
  1228. case RETCODE:
  1229. if (lua_callhook)
  1230. callHook (base, LUA_T_MARK, 1);
  1231. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1232. case SETLINE:
  1233. {
  1234. Word line;
  1235. get_word(line,pc);
  1236. if ((stack+base-1)->ttype != LUA_T_LINE)
  1237. {
  1238. /* open space for LINE value */
  1239. open_stack((top-stack)-base);
  1240. base++;
  1241. (stack+base-1)->ttype = LUA_T_LINE;
  1242. }
  1243. (stack+base-1)->value.i = line;
  1244. if (lua_linehook)
  1245. lineHook (line);
  1246. break;
  1247. }
  1248. default:
  1249. lua_error ("internal error - opcode doesn't match");
  1250. }
  1251. }
  1252. }