opcode.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.81 1997/02/20 15:51:14 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) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
  20. #define tostring(o) ((tag(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 (tag(obj) != LUA_T_STRING)
  108. return 1;
  109. else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
  110. {
  111. nvalue(obj) = t;
  112. tag(obj) = LUA_T_NUMBER;
  113. return 0;
  114. }
  115. else
  116. return 2;
  117. }
  118. /*
  119. ** Convert, if possible, to a string tag
  120. ** Return 0 in success or not 0 on error.
  121. */
  122. static int lua_tostring (Object *obj)
  123. {
  124. if (tag(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. tag(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) tag(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 (tag(func) == LUA_T_CFUNCTION)
  242. {
  243. tag(func) = LUA_T_CMARK;
  244. firstResult = callC(fvalue(func), base);
  245. }
  246. else if (tag(func) == LUA_T_FUNCTION)
  247. {
  248. tag(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 (tag(top-2) == LUA_T_ARRAY && im == NULL) {
  278. Object *h = lua_hashget(avalue(top-2), top-1);
  279. if (h != NULL && tag(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. tag(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 (tag(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. tag(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 (tag(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 (tag(top-1) == LUA_T_NIL)
  348. { /* must call getglobal fallback */
  349. tag(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].tag == LUA_T_MARK || stack[i].tag == 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)->tag == 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].tag = 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].tag = 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. tag(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) && (tag(Address(o)) == LUA_T_NIL);
  655. }
  656. int lua_istable (lua_Object o)
  657. {
  658. return (o!= LUA_NOOBJECT) && (tag(Address(o)) == LUA_T_ARRAY);
  659. }
  660. int lua_isuserdata (lua_Object o)
  661. {
  662. return (o!= LUA_NOOBJECT) && (tag(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 || tag(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 || tag(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 || ((tag(Address(object)) != LUA_T_CFUNCTION) &&
  720. (tag(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. tag(top) = LUA_T_NIL;
  771. incr_top;
  772. }
  773. /*
  774. ** Push an object (tag=number) to stack.
  775. */
  776. void lua_pushnumber (real n)
  777. {
  778. tag(top) = LUA_T_NUMBER; nvalue(top) = n;
  779. incr_top;
  780. }
  781. /*
  782. ** Push an object (tag=string) to stack.
  783. */
  784. void lua_pushstring (char *s)
  785. {
  786. if (s == NULL)
  787. tag(top) = LUA_T_NIL;
  788. else
  789. {
  790. tsvalue(top) = lua_createstring(s);
  791. tag(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 (tag=cfunction) to stack.
  799. */
  800. void lua_pushcfunction (lua_CFunction fn)
  801. {
  802. tag(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. tag(top) = LUA_T_NIL;
  809. else {
  810. tsvalue(top) = luaI_createuserdata(buff, size, tag);
  811. tag(top) = LUA_T_USERDATA;
  812. }
  813. incr_top;
  814. }
  815. /*
  816. ** Push an object (tag=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 (tag(top) == LUA_T_MARK) tag(top) = LUA_T_FUNCTION;
  841. else if (tag(top) == LUA_T_CMARK) tag(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 tag_less, lua_Type tag_equal,
  860. lua_Type tag_great, char *op)
  861. {
  862. Object *l = top-2;
  863. Object *r = top-1;
  864. int result;
  865. if (tag(l) == LUA_T_NUMBER && tag(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. tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
  878. }
  879. static void adjust_varargs (StkId first_extra_arg)
  880. {
  881. Object arg;
  882. Object *firstelem = stack+first_extra_arg;
  883. int nvararg = top-firstelem;
  884. int i;
  885. if (nvararg < 0) nvararg = 0;
  886. avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
  887. tag(&arg) = LUA_T_ARRAY;
  888. for (i=0; i<nvararg; i++) {
  889. Object index;
  890. tag(&index) = LUA_T_NUMBER;
  891. nvalue(&index) = i+1;
  892. *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
  893. }
  894. /* store counter in field "n" */ {
  895. Object index, extra;
  896. tag(&index) = LUA_T_STRING;
  897. tsvalue(&index) = lua_createstring("n");
  898. tag(&extra) = LUA_T_NUMBER;
  899. nvalue(&extra) = nvararg;
  900. *(lua_hashdefine(avalue(&arg), &index)) = extra;
  901. }
  902. adjust_top(first_extra_arg);
  903. *top = arg; incr_top;
  904. }
  905. /*
  906. ** Execute the given opcode, until a RET. Parameters are between
  907. ** [stack+base,top). Returns n such that the the results are between
  908. ** [stack+n,top).
  909. */
  910. static StkId lua_execute (Byte *pc, StkId base)
  911. {
  912. if (lua_callhook)
  913. callHook (base, LUA_T_MARK, 0);
  914. while (1)
  915. {
  916. OpCode opcode;
  917. switch (opcode = (OpCode)*pc++)
  918. {
  919. case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
  920. case PUSH0: case PUSH1: case PUSH2:
  921. tag(top) = LUA_T_NUMBER;
  922. nvalue(top) = opcode-PUSH0;
  923. incr_top;
  924. break;
  925. case PUSHBYTE:
  926. tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  927. case PUSHWORD:
  928. {
  929. Word w;
  930. get_word(w,pc);
  931. tag(top) = LUA_T_NUMBER; nvalue(top) = w;
  932. incr_top;
  933. }
  934. break;
  935. case PUSHFLOAT:
  936. {
  937. real num;
  938. get_float(num,pc);
  939. tag(top) = LUA_T_NUMBER; nvalue(top) = num;
  940. incr_top;
  941. }
  942. break;
  943. case PUSHSTRING:
  944. {
  945. Word w;
  946. get_word(w,pc);
  947. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  948. incr_top;
  949. }
  950. break;
  951. case PUSHFUNCTION:
  952. {
  953. TFunc *f;
  954. get_code(f,pc);
  955. luaI_insertfunction(f); /* may take part in GC */
  956. top->tag = LUA_T_FUNCTION;
  957. top->value.tf = f;
  958. incr_top;
  959. }
  960. break;
  961. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  962. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  963. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  964. case PUSHLOCAL9:
  965. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  966. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  967. case PUSHGLOBAL:
  968. {
  969. Word w;
  970. get_word(w,pc);
  971. getglobal(w);
  972. }
  973. break;
  974. case PUSHINDEXED:
  975. pushsubscript();
  976. break;
  977. case PUSHSELF:
  978. {
  979. Object receiver = *(top-1);
  980. Word w;
  981. get_word(w,pc);
  982. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  983. incr_top;
  984. pushsubscript();
  985. *top = receiver;
  986. incr_top;
  987. break;
  988. }
  989. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  990. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  991. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  992. case STORELOCAL9:
  993. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  994. break;
  995. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  996. case STOREGLOBAL:
  997. {
  998. Word w;
  999. get_word(w,pc);
  1000. s_object(w) = *(--top);
  1001. }
  1002. break;
  1003. case STOREINDEXED0:
  1004. storesubscript(top-3, 1);
  1005. break;
  1006. case STOREINDEXED: {
  1007. int n = *pc++;
  1008. storesubscript(top-3-n, 2);
  1009. break;
  1010. }
  1011. case STORELIST0:
  1012. case STORELIST:
  1013. {
  1014. int m, n;
  1015. Object *arr;
  1016. if (opcode == STORELIST0) m = 0;
  1017. else m = *(pc++) * FIELDS_PER_FLUSH;
  1018. n = *(pc++);
  1019. arr = top-n-1;
  1020. while (n)
  1021. {
  1022. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  1023. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1024. top--;
  1025. n--;
  1026. }
  1027. }
  1028. break;
  1029. case STORERECORD:
  1030. {
  1031. int n = *(pc++);
  1032. Object *arr = top-n-1;
  1033. while (n)
  1034. {
  1035. Word w;
  1036. get_word(w,pc);
  1037. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1038. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1039. top--;
  1040. n--;
  1041. }
  1042. }
  1043. break;
  1044. case ADJUST0:
  1045. adjust_top(base);
  1046. break;
  1047. case ADJUST:
  1048. adjust_top(base + *(pc++));
  1049. break;
  1050. case VARARGS:
  1051. adjust_varargs(base + *(pc++));
  1052. break;
  1053. case CREATEARRAY:
  1054. {
  1055. Word size;
  1056. get_word(size,pc);
  1057. avalue(top) = lua_createarray(size);
  1058. tag(top) = LUA_T_ARRAY;
  1059. incr_top;
  1060. }
  1061. break;
  1062. case EQOP:
  1063. {
  1064. int res = lua_equalObj(top-2, top-1);
  1065. --top;
  1066. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  1067. nvalue(top-1) = 1;
  1068. }
  1069. break;
  1070. case LTOP:
  1071. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
  1072. break;
  1073. case LEOP:
  1074. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
  1075. break;
  1076. case GTOP:
  1077. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
  1078. break;
  1079. case GEOP:
  1080. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
  1081. break;
  1082. case ADDOP:
  1083. {
  1084. Object *l = top-2;
  1085. Object *r = top-1;
  1086. if (tonumber(r) || tonumber(l))
  1087. call_arith("add");
  1088. else
  1089. {
  1090. nvalue(l) += nvalue(r);
  1091. --top;
  1092. }
  1093. }
  1094. break;
  1095. case SUBOP:
  1096. {
  1097. Object *l = top-2;
  1098. Object *r = top-1;
  1099. if (tonumber(r) || tonumber(l))
  1100. call_arith("sub");
  1101. else
  1102. {
  1103. nvalue(l) -= nvalue(r);
  1104. --top;
  1105. }
  1106. }
  1107. break;
  1108. case MULTOP:
  1109. {
  1110. Object *l = top-2;
  1111. Object *r = top-1;
  1112. if (tonumber(r) || tonumber(l))
  1113. call_arith("mul");
  1114. else
  1115. {
  1116. nvalue(l) *= nvalue(r);
  1117. --top;
  1118. }
  1119. }
  1120. break;
  1121. case DIVOP:
  1122. {
  1123. Object *l = top-2;
  1124. Object *r = top-1;
  1125. if (tonumber(r) || tonumber(l))
  1126. call_arith("div");
  1127. else
  1128. {
  1129. nvalue(l) /= nvalue(r);
  1130. --top;
  1131. }
  1132. }
  1133. break;
  1134. case POWOP:
  1135. call_arith("pow");
  1136. break;
  1137. case CONCOP:
  1138. {
  1139. Object *l = top-2;
  1140. Object *r = top-1;
  1141. if (tostring(r) || tostring(l))
  1142. callFB(FB_CONCAT);
  1143. else
  1144. {
  1145. tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  1146. --top;
  1147. }
  1148. }
  1149. break;
  1150. case MINUSOP:
  1151. if (tonumber(top-1))
  1152. {
  1153. tag(top) = LUA_T_NIL;
  1154. incr_top;
  1155. call_arith("unm");
  1156. }
  1157. else
  1158. nvalue(top-1) = - nvalue(top-1);
  1159. break;
  1160. case NOTOP:
  1161. tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1162. nvalue(top-1) = 1;
  1163. break;
  1164. case ONTJMP:
  1165. {
  1166. Word w;
  1167. get_word(w,pc);
  1168. if (tag(top-1) != LUA_T_NIL) pc += w;
  1169. }
  1170. break;
  1171. case ONFJMP:
  1172. {
  1173. Word w;
  1174. get_word(w,pc);
  1175. if (tag(top-1) == LUA_T_NIL) pc += w;
  1176. }
  1177. break;
  1178. case JMP:
  1179. {
  1180. Word w;
  1181. get_word(w,pc);
  1182. pc += w;
  1183. }
  1184. break;
  1185. case UPJMP:
  1186. {
  1187. Word w;
  1188. get_word(w,pc);
  1189. pc -= w;
  1190. }
  1191. break;
  1192. case IFFJMP:
  1193. {
  1194. Word w;
  1195. get_word(w,pc);
  1196. top--;
  1197. if (tag(top) == LUA_T_NIL) pc += w;
  1198. }
  1199. break;
  1200. case IFFUPJMP:
  1201. {
  1202. Word w;
  1203. get_word(w,pc);
  1204. top--;
  1205. if (tag(top) == LUA_T_NIL) pc -= w;
  1206. }
  1207. break;
  1208. case POP: --top; break;
  1209. case CALLFUNC:
  1210. {
  1211. int nParams = *(pc++);
  1212. int nResults = *(pc++);
  1213. StkId newBase = (top-stack)-nParams;
  1214. do_call(newBase, nResults);
  1215. }
  1216. break;
  1217. case RETCODE0:
  1218. case RETCODE:
  1219. if (lua_callhook)
  1220. callHook (base, LUA_T_MARK, 1);
  1221. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1222. case SETLINE:
  1223. {
  1224. Word line;
  1225. get_word(line,pc);
  1226. if ((stack+base-1)->tag != LUA_T_LINE)
  1227. {
  1228. /* open space for LINE value */
  1229. open_stack((top-stack)-base);
  1230. base++;
  1231. (stack+base-1)->tag = LUA_T_LINE;
  1232. }
  1233. (stack+base-1)->value.i = line;
  1234. if (lua_linehook)
  1235. lineHook (line);
  1236. break;
  1237. }
  1238. default:
  1239. lua_error ("internal error - opcode doesn't match");
  1240. }
  1241. }
  1242. }