opcode.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 2.9 1994/10/11 14:38:17 celes Exp $";
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10. #ifdef __GNUC__
  11. #include <floatingpoint.h>
  12. #endif
  13. #include "mm.h"
  14. #include "opcode.h"
  15. #include "hash.h"
  16. #include "inout.h"
  17. #include "table.h"
  18. #include "lua.h"
  19. #define tonumber(o) ((tag(o) != T_NUMBER) && (lua_tonumber(o) != 0))
  20. #define tostring(o) ((tag(o) != T_STRING) && (lua_tostring(o) != 0))
  21. #define STACK_BUFFER (STACKGAP+128)
  22. static Long maxstack;
  23. static Object *stack=NULL;
  24. static Object *top, *base;
  25. /*
  26. ** Init stack
  27. */
  28. static int lua_initstack (void)
  29. {
  30. maxstack = STACK_BUFFER;
  31. stack = (Object *)calloc(maxstack, sizeof(Object));
  32. if (stack == NULL)
  33. {
  34. lua_error("stack - not enough memory");
  35. return 1;
  36. }
  37. tag(stack) = T_MARK;
  38. top = base = stack+1;
  39. return 0;
  40. }
  41. /*
  42. ** Check stack overflow and, if necessary, realloc vector
  43. */
  44. static int lua_checkstack (Word n)
  45. {
  46. if (stack == NULL)
  47. return lua_initstack();
  48. if (n > maxstack)
  49. {
  50. Word t = top-stack;
  51. Word b = base-stack;
  52. maxstack *= 2;
  53. stack = (Object *)realloc(stack, maxstack*sizeof(Object));
  54. if (stack == NULL)
  55. {
  56. lua_error("stack - not enough memory");
  57. return 1;
  58. }
  59. top = stack + t;
  60. base = stack + b;
  61. }
  62. return 0;
  63. }
  64. /*
  65. ** Concatenate two given string, creating a mark space at the beginning.
  66. ** Return the new string pointer.
  67. */
  68. static char *lua_strconc (char *l, char *r)
  69. {
  70. static char buffer[1024];
  71. int n = strlen(l)+strlen(r)+1;
  72. if (n > 1024)
  73. {
  74. lua_error ("string too large");
  75. return NULL;
  76. }
  77. return strcat(strcpy(buffer,l),r);
  78. }
  79. static int ToReal (char* s, float* f)
  80. {
  81. char c;
  82. float t;
  83. if (sscanf(s,"%f %c",&t,&c) == 1) { *f=t; return 1; } else return 0;
  84. }
  85. /*
  86. ** Convert, if possible, to a number object.
  87. ** Return 0 if success, not 0 if error.
  88. */
  89. static int lua_tonumber (Object *obj)
  90. {
  91. if (tag(obj) != T_STRING)
  92. {
  93. lua_reportbug ("unexpected type at conversion to number");
  94. return 1;
  95. }
  96. if (!ToReal(svalue(obj), &nvalue(obj)))
  97. {
  98. lua_reportbug ("string to number convertion failed");
  99. return 2;
  100. }
  101. tag(obj) = T_NUMBER;
  102. return 0;
  103. }
  104. /*
  105. ** Test if is possible to convert an object to a number object.
  106. ** If possible, return the converted object, otherwise return nil object.
  107. */
  108. static Object *lua_convtonumber (Object *obj)
  109. {
  110. static Object cvt;
  111. if (tag(obj) == T_NUMBER)
  112. {
  113. cvt = *obj;
  114. return &cvt;
  115. }
  116. if (tag(obj) == T_STRING && ToReal(svalue(obj), &nvalue(&cvt)))
  117. tag(&cvt) = T_NUMBER;
  118. else
  119. tag(&cvt) = T_NIL;
  120. return &cvt;
  121. }
  122. /*
  123. ** Convert, if possible, to a string tag
  124. ** Return 0 in success or not 0 on error.
  125. */
  126. static int lua_tostring (Object *obj)
  127. {
  128. static char s[256];
  129. if (tag(obj) != T_NUMBER)
  130. {
  131. lua_reportbug ("unexpected type at conversion to string");
  132. return 1;
  133. }
  134. if ((int) nvalue(obj) == nvalue(obj))
  135. sprintf (s, "%d", (int) nvalue(obj));
  136. else
  137. sprintf (s, "%g", nvalue(obj));
  138. svalue(obj) = lua_createstring(s);
  139. if (svalue(obj) == NULL)
  140. return 1;
  141. tag(obj) = T_STRING;
  142. return 0;
  143. }
  144. /*
  145. ** Execute the given opcode. Return 0 in success or 1 on error.
  146. */
  147. int lua_execute (Byte *pc)
  148. {
  149. Word oldbase;
  150. if (stack == NULL)
  151. lua_initstack();
  152. oldbase = base-stack;
  153. base = top;
  154. while (1)
  155. {
  156. OpCode opcode;
  157. switch (opcode = (OpCode)*pc++)
  158. {
  159. case PUSHNIL: tag(top++) = T_NIL; break;
  160. case PUSH0: tag(top) = T_NUMBER; nvalue(top++) = 0; break;
  161. case PUSH1: tag(top) = T_NUMBER; nvalue(top++) = 1; break;
  162. case PUSH2: tag(top) = T_NUMBER; nvalue(top++) = 2; break;
  163. case PUSHBYTE: tag(top) = T_NUMBER; nvalue(top++) = *pc++; break;
  164. case PUSHWORD:
  165. {
  166. CodeWord code;
  167. get_word(code,pc);
  168. tag(top) = T_NUMBER; nvalue(top++) = code.w;
  169. }
  170. break;
  171. case PUSHFLOAT:
  172. {
  173. CodeFloat code;
  174. get_float(code,pc);
  175. tag(top) = T_NUMBER; nvalue(top++) = code.f;
  176. }
  177. break;
  178. case PUSHSTRING:
  179. {
  180. CodeWord code;
  181. get_word(code,pc);
  182. tag(top) = T_STRING; svalue(top++) = lua_constant[code.w];
  183. }
  184. break;
  185. case PUSHFUNCTION:
  186. {
  187. CodeCode code;
  188. get_code(code,pc);
  189. tag(top) = T_FUNCTION; bvalue(top++) = code.b;
  190. }
  191. break;
  192. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  193. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  194. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  195. case PUSHLOCAL9: *top++ = *(base + (int)(opcode-PUSHLOCAL0)); break;
  196. case PUSHLOCAL: *top++ = *(base + (*pc++)); break;
  197. case PUSHGLOBAL:
  198. {
  199. CodeWord code;
  200. get_word(code,pc);
  201. *top++ = s_object(code.w);
  202. }
  203. break;
  204. case PUSHINDEXED:
  205. {
  206. int s = lua_pushsubscript();
  207. if (s == 1) return 1;
  208. }
  209. break;
  210. case PUSHMARK: tag(top++) = T_MARK; break;
  211. case PUSHMARKMET:
  212. {
  213. Object receiver = *(top-2);
  214. if (lua_pushsubscript() == 1) return 1;
  215. tag(top++) = T_MARK;
  216. *(top++) = receiver;
  217. break;
  218. }
  219. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  220. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  221. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  222. case STORELOCAL9: *(base + (int)(opcode-STORELOCAL0)) = *(--top); break;
  223. case STORELOCAL: *(base + (*pc++)) = *(--top); break;
  224. case STOREGLOBAL:
  225. {
  226. CodeWord code;
  227. get_word(code,pc);
  228. s_object(code.w) = *(--top);
  229. }
  230. break;
  231. case STOREINDEXED0:
  232. {
  233. int s = lua_storesubscript();
  234. if (s == 1) return 1;
  235. }
  236. break;
  237. case STOREINDEXED:
  238. {
  239. int n = *pc++;
  240. if (tag(top-3-n) != T_ARRAY)
  241. {
  242. lua_reportbug ("indexed expression not a table");
  243. return 1;
  244. }
  245. {
  246. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  247. if (h == NULL) return 1;
  248. *h = *(top-1);
  249. }
  250. top--;
  251. }
  252. break;
  253. case STORELIST0:
  254. case STORELIST:
  255. {
  256. int m, n;
  257. Object *arr;
  258. if (opcode == STORELIST0) m = 0;
  259. else m = *(pc++) * FIELDS_PER_FLUSH;
  260. n = *(pc++);
  261. arr = top-n-1;
  262. if (tag(arr) != T_ARRAY)
  263. {
  264. lua_reportbug ("internal error - table expected");
  265. return 1;
  266. }
  267. while (n)
  268. {
  269. tag(top) = T_NUMBER; nvalue(top) = n+m;
  270. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  271. top--;
  272. n--;
  273. }
  274. }
  275. break;
  276. case STORERECORD:
  277. {
  278. int n = *(pc++);
  279. Object *arr = top-n-1;
  280. if (tag(arr) != T_ARRAY)
  281. {
  282. lua_reportbug ("internal error - table expected");
  283. return 1;
  284. }
  285. while (n)
  286. {
  287. CodeWord code;
  288. get_word(code,pc);
  289. tag(top) = T_STRING; svalue(top) = lua_constant[code.w];
  290. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  291. top--;
  292. n--;
  293. }
  294. }
  295. break;
  296. case ADJUST:
  297. {
  298. Object *newtop = base + *(pc++);
  299. while (top < newtop) tag(top++) = T_NIL;
  300. top = newtop; /* top could be bigger than newtop */
  301. }
  302. break;
  303. case CREATEARRAY:
  304. if (tag(top-1) == T_NIL)
  305. nvalue(top-1) = 1;
  306. else
  307. {
  308. if (tonumber(top-1)) return 1;
  309. if (nvalue(top-1) <= 0) nvalue(top-1) = 1;
  310. }
  311. avalue(top-1) = lua_createarray(nvalue(top-1));
  312. if (avalue(top-1) == NULL)
  313. return 1;
  314. tag(top-1) = T_ARRAY;
  315. break;
  316. case EQOP:
  317. {
  318. Object *l = top-2;
  319. Object *r = top-1;
  320. --top;
  321. if (tag(l) != tag(r))
  322. tag(top-1) = T_NIL;
  323. else
  324. {
  325. switch (tag(l))
  326. {
  327. case T_NIL: tag(top-1) = T_NUMBER; break;
  328. case T_NUMBER: tag(top-1) = (nvalue(l) == nvalue(r)) ? T_NUMBER : T_NIL; break;
  329. case T_ARRAY: tag(top-1) = (avalue(l) == avalue(r)) ? T_NUMBER : T_NIL; break;
  330. case T_FUNCTION: tag(top-1) = (bvalue(l) == bvalue(r)) ? T_NUMBER : T_NIL; break;
  331. case T_CFUNCTION: tag(top-1) = (fvalue(l) == fvalue(r)) ? T_NUMBER : T_NIL; break;
  332. case T_USERDATA: tag(top-1) = (uvalue(l) == uvalue(r)) ? T_NUMBER : T_NIL; break;
  333. case T_STRING: tag(top-1) = (strcmp (svalue(l), svalue(r)) == 0) ? T_NUMBER : T_NIL; break;
  334. case T_MARK: return 1;
  335. }
  336. }
  337. nvalue(top-1) = 1;
  338. }
  339. break;
  340. case LTOP:
  341. {
  342. Object *l = top-2;
  343. Object *r = top-1;
  344. --top;
  345. if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)
  346. tag(top-1) = (nvalue(l) < nvalue(r)) ? T_NUMBER : T_NIL;
  347. else
  348. {
  349. if (tostring(l) || tostring(r))
  350. return 1;
  351. tag(top-1) = (strcmp (svalue(l), svalue(r)) < 0) ? T_NUMBER : T_NIL;
  352. }
  353. nvalue(top-1) = 1;
  354. }
  355. break;
  356. case LEOP:
  357. {
  358. Object *l = top-2;
  359. Object *r = top-1;
  360. --top;
  361. if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)
  362. tag(top-1) = (nvalue(l) <= nvalue(r)) ? T_NUMBER : T_NIL;
  363. else
  364. {
  365. if (tostring(l) || tostring(r))
  366. return 1;
  367. tag(top-1) = (strcmp (svalue(l), svalue(r)) <= 0) ? T_NUMBER : T_NIL;
  368. }
  369. nvalue(top-1) = 1;
  370. }
  371. break;
  372. case ADDOP:
  373. {
  374. Object *l = top-2;
  375. Object *r = top-1;
  376. if (tonumber(r) || tonumber(l))
  377. return 1;
  378. nvalue(l) += nvalue(r);
  379. --top;
  380. }
  381. break;
  382. case SUBOP:
  383. {
  384. Object *l = top-2;
  385. Object *r = top-1;
  386. if (tonumber(r) || tonumber(l))
  387. return 1;
  388. nvalue(l) -= nvalue(r);
  389. --top;
  390. }
  391. break;
  392. case MULTOP:
  393. {
  394. Object *l = top-2;
  395. Object *r = top-1;
  396. if (tonumber(r) || tonumber(l))
  397. return 1;
  398. nvalue(l) *= nvalue(r);
  399. --top;
  400. }
  401. break;
  402. case DIVOP:
  403. {
  404. Object *l = top-2;
  405. Object *r = top-1;
  406. if (tonumber(r) || tonumber(l))
  407. return 1;
  408. nvalue(l) /= nvalue(r);
  409. --top;
  410. }
  411. break;
  412. case POWOP:
  413. {
  414. Object *l = top-2;
  415. Object *r = top-1;
  416. if (tonumber(r) || tonumber(l))
  417. return 1;
  418. nvalue(l) = pow(nvalue(l), nvalue(r));
  419. --top;
  420. }
  421. break;
  422. case CONCOP:
  423. {
  424. Object *l = top-2;
  425. Object *r = top-1;
  426. if (tostring(r) || tostring(l))
  427. return 1;
  428. svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  429. if (svalue(l) == NULL)
  430. return 1;
  431. --top;
  432. }
  433. break;
  434. case MINUSOP:
  435. if (tonumber(top-1))
  436. return 1;
  437. nvalue(top-1) = - nvalue(top-1);
  438. break;
  439. case NOTOP:
  440. tag(top-1) = tag(top-1) == T_NIL ? T_NUMBER : T_NIL;
  441. break;
  442. case ONTJMP:
  443. {
  444. CodeWord code;
  445. get_word(code,pc);
  446. if (tag(top-1) != T_NIL) pc += code.w;
  447. }
  448. break;
  449. case ONFJMP:
  450. {
  451. CodeWord code;
  452. get_word(code,pc);
  453. if (tag(top-1) == T_NIL) pc += code.w;
  454. }
  455. break;
  456. case JMP:
  457. {
  458. CodeWord code;
  459. get_word(code,pc);
  460. pc += code.w;
  461. }
  462. break;
  463. case UPJMP:
  464. {
  465. CodeWord code;
  466. get_word(code,pc);
  467. pc -= code.w;
  468. }
  469. break;
  470. case IFFJMP:
  471. {
  472. CodeWord code;
  473. get_word(code,pc);
  474. top--;
  475. if (tag(top) == T_NIL) pc += code.w;
  476. }
  477. break;
  478. case IFFUPJMP:
  479. {
  480. CodeWord code;
  481. get_word(code,pc);
  482. top--;
  483. if (tag(top) == T_NIL) pc -= code.w;
  484. }
  485. break;
  486. case POP: --top; break;
  487. case CALLFUNC:
  488. {
  489. Byte *newpc;
  490. Object *b = top-1;
  491. while (tag(b) != T_MARK) b--;
  492. if (tag(b-1) == T_FUNCTION)
  493. {
  494. lua_debugline = 0; /* always reset debug flag */
  495. newpc = bvalue(b-1);
  496. bvalue(b-1) = pc; /* store return code */
  497. nvalue(b) = (base-stack); /* store base value */
  498. base = b+1;
  499. pc = newpc;
  500. if (lua_checkstack(STACKGAP+(base-stack)))
  501. return 1;
  502. }
  503. else if (tag(b-1) == T_CFUNCTION)
  504. {
  505. int nparam;
  506. lua_debugline = 0; /* always reset debug flag */
  507. nvalue(b) = (base-stack); /* store base value */
  508. base = b+1;
  509. nparam = top-base; /* number of parameters */
  510. (fvalue(b-1))(); /* call C function */
  511. /* shift returned values */
  512. {
  513. int i;
  514. int nretval = top - base - nparam;
  515. top = base - 2;
  516. base = stack + (int) nvalue(base-1);
  517. for (i=0; i<nretval; i++)
  518. {
  519. *top = *(top+nparam+2);
  520. ++top;
  521. }
  522. }
  523. }
  524. else
  525. {
  526. lua_reportbug ("call expression not a function");
  527. return 1;
  528. }
  529. }
  530. break;
  531. case RETCODE:
  532. {
  533. int i;
  534. int shift = *pc++;
  535. int nretval = top - base - shift;
  536. top = base - 2;
  537. pc = bvalue(base-2);
  538. base = stack + (int) nvalue(base-1);
  539. for (i=0; i<nretval; i++)
  540. {
  541. *top = *(top+shift+2);
  542. ++top;
  543. }
  544. }
  545. break;
  546. case HALT:
  547. base = stack+oldbase;
  548. return 0; /* success */
  549. case SETFUNCTION:
  550. {
  551. CodeCode file;
  552. CodeWord func;
  553. get_code(file,pc);
  554. get_word(func,pc);
  555. if (lua_pushfunction ((char *)file.b, func.w))
  556. return 1;
  557. }
  558. break;
  559. case SETLINE:
  560. {
  561. CodeWord code;
  562. get_word(code,pc);
  563. lua_debugline = code.w;
  564. }
  565. break;
  566. case RESET:
  567. lua_popfunction ();
  568. break;
  569. default:
  570. lua_error ("internal error - opcode didn't match");
  571. return 1;
  572. }
  573. }
  574. }
  575. /*
  576. ** Function to indexed the values on the top
  577. */
  578. int lua_pushsubscript (void)
  579. {
  580. --top;
  581. if (tag(top-1) != T_ARRAY)
  582. {
  583. lua_reportbug ("indexed expression not a table");
  584. return 1;
  585. }
  586. {
  587. Object *h = lua_hashget (avalue(top-1), top);
  588. if (h == NULL) return 1;
  589. *(top-1) = *h;
  590. }
  591. return 0;
  592. }
  593. /*
  594. ** Function to store indexed based on values at the top
  595. */
  596. int lua_storesubscript (void)
  597. {
  598. if (tag(top-3) != T_ARRAY)
  599. {
  600. lua_reportbug ("indexed expression not a table");
  601. return 1;
  602. }
  603. {
  604. Object *h = lua_hashdefine (avalue(top-3), top-2);
  605. if (h == NULL) return 1;
  606. *h = *(top-1);
  607. }
  608. top -= 3;
  609. return 0;
  610. }
  611. /*
  612. ** Traverse all objects on stack
  613. */
  614. void lua_travstack (void (*fn)(Object *))
  615. {
  616. Object *o;
  617. for (o = top-1; o >= stack; o--)
  618. fn (o);
  619. }
  620. /*
  621. ** Open file, generate opcode and execute global statement. Return 0 on
  622. ** success or 1 on error.
  623. */
  624. int lua_dofile (char *filename)
  625. {
  626. if (lua_openfile (filename)) return 1;
  627. if (lua_parse ()) { lua_closefile (); return 1; }
  628. lua_closefile ();
  629. return 0;
  630. }
  631. /*
  632. ** Generate opcode stored on string and execute global statement. Return 0 on
  633. ** success or 1 on error.
  634. */
  635. int lua_dostring (char *string)
  636. {
  637. if (lua_openstring (string)) return 1;
  638. if (lua_parse ()) return 1;
  639. lua_closestring();
  640. return 0;
  641. }
  642. /*
  643. ** Execute the given function. Return 0 on success or 1 on error.
  644. */
  645. int lua_call (char *functionname, int nparam)
  646. {
  647. static Byte startcode[] = {CALLFUNC, HALT};
  648. int i;
  649. Object func = s_object(lua_findsymbol(functionname));
  650. if (tag(&func) != T_FUNCTION) return 1;
  651. for (i=1; i<=nparam; i++)
  652. *(top-i+2) = *(top-i);
  653. top += 2;
  654. tag(top-nparam-1) = T_MARK;
  655. *(top-nparam-2) = func;
  656. return (lua_execute (startcode));
  657. }
  658. /*
  659. ** Execute the given lua function. Return 0 on success or 1 on error.
  660. */
  661. int lua_callfunction (Object *function, int nparam)
  662. {
  663. static Byte startcode[] = {CALLFUNC, HALT};
  664. int i;
  665. if (tag(function) != T_FUNCTION) return 1;
  666. for (i=1; i<=nparam; i++)
  667. *(top-i+2) = *(top-i);
  668. top += 2;
  669. tag(top-nparam-1) = T_MARK;
  670. *(top-nparam-2) = *function;
  671. return (lua_execute (startcode));
  672. }
  673. /*
  674. ** Get a parameter, returning the object handle or NULL on error.
  675. ** 'number' must be 1 to get the first parameter.
  676. */
  677. Object *lua_getparam (int number)
  678. {
  679. if (number <= 0 || number > top-base) return NULL;
  680. return (base+number-1);
  681. }
  682. /*
  683. ** Given an object handle, return its number value. On error, return 0.0.
  684. */
  685. real lua_getnumber (Object *object)
  686. {
  687. if (object == NULL || tag(object) == T_NIL) return 0.0;
  688. if (tonumber (object)) return 0.0;
  689. else return (nvalue(object));
  690. }
  691. /*
  692. ** Given an object handle, return its string pointer. On error, return NULL.
  693. */
  694. char *lua_getstring (Object *object)
  695. {
  696. if (object == NULL || tag(object) == T_NIL) return NULL;
  697. if (tostring (object)) return NULL;
  698. else return (svalue(object));
  699. }
  700. /*
  701. ** Given an object handle, return a copy of its string. On error, return NULL.
  702. */
  703. char *lua_copystring (Object *object)
  704. {
  705. if (object == NULL || tag(object) == T_NIL) return NULL;
  706. if (tostring (object)) return NULL;
  707. else return (strdup(svalue(object)));
  708. }
  709. /*
  710. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  711. */
  712. lua_CFunction lua_getcfunction (Object *object)
  713. {
  714. if (object == NULL) return NULL;
  715. if (tag(object) != T_CFUNCTION) return NULL;
  716. else return (fvalue(object));
  717. }
  718. /*
  719. ** Given an object handle, return its user data. On error, return NULL.
  720. */
  721. void *lua_getuserdata (Object *object)
  722. {
  723. if (object == NULL) return NULL;
  724. if (tag(object) != T_USERDATA) return NULL;
  725. else return (uvalue(object));
  726. }
  727. /*
  728. ** Given an object handle, return its table. On error, return NULL.
  729. */
  730. void *lua_gettable (Object *object)
  731. {
  732. if (object == NULL) return NULL;
  733. if (tag(object) != T_ARRAY) return NULL;
  734. else return (avalue(object));
  735. }
  736. /*
  737. ** Given an object handle and a field name, return its field object.
  738. ** On error, return NULL.
  739. */
  740. Object *lua_getfield (Object *object, char *field)
  741. {
  742. if (object == NULL) return NULL;
  743. if (tag(object) != T_ARRAY)
  744. return NULL;
  745. else
  746. {
  747. Object ref;
  748. tag(&ref) = T_STRING;
  749. svalue(&ref) = lua_constant[lua_findconstant(field)];
  750. return (lua_hashget(avalue(object), &ref));
  751. }
  752. }
  753. /*
  754. ** Given an object handle and an index, return its indexed object.
  755. ** On error, return NULL.
  756. */
  757. Object *lua_getindexed (Object *object, float index)
  758. {
  759. if (object == NULL) return NULL;
  760. if (tag(object) != T_ARRAY)
  761. return NULL;
  762. else
  763. {
  764. Object ref;
  765. tag(&ref) = T_NUMBER;
  766. nvalue(&ref) = index;
  767. return (lua_hashget(avalue(object), &ref));
  768. }
  769. }
  770. /*
  771. ** Get a global object. Return the object handle or NULL on error.
  772. */
  773. Object *lua_getglobal (char *name)
  774. {
  775. int n = lua_findsymbol(name);
  776. if (n < 0) return NULL;
  777. return &s_object(n);
  778. }
  779. /*
  780. ** Pop and return an object
  781. */
  782. Object *lua_pop (void)
  783. {
  784. if (top <= base) return NULL;
  785. top--;
  786. return top;
  787. }
  788. /*
  789. ** Push a nil object
  790. */
  791. int lua_pushnil (void)
  792. {
  793. if (lua_checkstack(top-stack+1) == 1)
  794. return 1;
  795. tag(top++) = T_NIL;
  796. return 0;
  797. }
  798. /*
  799. ** Push an object (tag=number) to stack. Return 0 on success or 1 on error.
  800. */
  801. int lua_pushnumber (real n)
  802. {
  803. if (lua_checkstack(top-stack+1) == 1)
  804. return 1;
  805. tag(top) = T_NUMBER; nvalue(top++) = n;
  806. return 0;
  807. }
  808. /*
  809. ** Push an object (tag=string) to stack. Return 0 on success or 1 on error.
  810. */
  811. int lua_pushstring (char *s)
  812. {
  813. if (lua_checkstack(top-stack+1) == 1)
  814. return 1;
  815. tag(top) = T_STRING;
  816. svalue(top++) = lua_createstring(s);
  817. return 0;
  818. }
  819. /*
  820. ** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
  821. */
  822. int lua_pushcfunction (lua_CFunction fn)
  823. {
  824. if (lua_checkstack(top-stack+1) == 1)
  825. return 1;
  826. tag(top) = T_CFUNCTION; fvalue(top++) = fn;
  827. return 0;
  828. }
  829. /*
  830. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  831. */
  832. int lua_pushuserdata (void *u)
  833. {
  834. if (lua_checkstack(top-stack+1) == 1)
  835. return 1;
  836. tag(top) = T_USERDATA; uvalue(top++) = u;
  837. return 0;
  838. }
  839. /*
  840. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  841. */
  842. int lua_pushtable (void *t)
  843. {
  844. if (lua_checkstack(top-stack+1) == 1)
  845. return 1;
  846. tag(top) = T_ARRAY; avalue(top++) = t;
  847. return 0;
  848. }
  849. /*
  850. ** Push an object to stack.
  851. */
  852. int lua_pushobject (Object *o)
  853. {
  854. if (lua_checkstack(top-stack+1) == 1)
  855. return 1;
  856. *top++ = *o;
  857. return 0;
  858. }
  859. /*
  860. ** Store top of the stack at a global variable array field.
  861. ** Return 1 on error, 0 on success.
  862. */
  863. int lua_storeglobal (char *name)
  864. {
  865. int n = lua_findsymbol (name);
  866. if (n < 0) return 1;
  867. if (tag(top-1) == T_MARK) return 1;
  868. s_object(n) = *(--top);
  869. return 0;
  870. }
  871. /*
  872. ** Store top of the stack at an array field. Return 1 on error, 0 on success.
  873. */
  874. int lua_storefield (lua_Object object, char *field)
  875. {
  876. if (tag(object) != T_ARRAY)
  877. return 1;
  878. else
  879. {
  880. Object ref, *h;
  881. tag(&ref) = T_STRING;
  882. svalue(&ref) = lua_createstring(field);
  883. h = lua_hashdefine(avalue(object), &ref);
  884. if (h == NULL) return 1;
  885. if (tag(top-1) == T_MARK) return 1;
  886. *h = *(--top);
  887. }
  888. return 0;
  889. }
  890. /*
  891. ** Store top of the stack at an array index. Return 1 on error, 0 on success.
  892. */
  893. int lua_storeindexed (lua_Object object, float index)
  894. {
  895. if (tag(object) != T_ARRAY)
  896. return 1;
  897. else
  898. {
  899. Object ref, *h;
  900. tag(&ref) = T_NUMBER;
  901. nvalue(&ref) = index;
  902. h = lua_hashdefine(avalue(object), &ref);
  903. if (h == NULL) return 1;
  904. if (tag(top-1) == T_MARK) return 1;
  905. *h = *(--top);
  906. }
  907. return 0;
  908. }
  909. /*
  910. ** Given an object handle, return if it is nil.
  911. */
  912. int lua_isnil (Object *object)
  913. {
  914. return (object != NULL && tag(object) == T_NIL);
  915. }
  916. /*
  917. ** Given an object handle, return if it is a number one.
  918. */
  919. int lua_isnumber (Object *object)
  920. {
  921. return (object != NULL && tag(object) == T_NUMBER);
  922. }
  923. /*
  924. ** Given an object handle, return if it is a string one.
  925. */
  926. int lua_isstring (Object *object)
  927. {
  928. return (object != NULL && tag(object) == T_STRING);
  929. }
  930. /*
  931. ** Given an object handle, return if it is an array one.
  932. */
  933. int lua_istable (Object *object)
  934. {
  935. return (object != NULL && tag(object) == T_ARRAY);
  936. }
  937. /*
  938. ** Given an object handle, return if it is a lua function.
  939. */
  940. int lua_isfunction (Object *object)
  941. {
  942. return (object != NULL && tag(object) == T_FUNCTION);
  943. }
  944. /*
  945. ** Given an object handle, return if it is a cfunction one.
  946. */
  947. int lua_iscfunction (Object *object)
  948. {
  949. return (object != NULL && tag(object) == T_CFUNCTION);
  950. }
  951. /*
  952. ** Given an object handle, return if it is an user data one.
  953. */
  954. int lua_isuserdata (Object *object)
  955. {
  956. return (object != NULL && tag(object) == T_USERDATA);
  957. }
  958. /*
  959. ** Internal function: return an object type.
  960. */
  961. void lua_type (void)
  962. {
  963. Object *o = lua_getparam(1);
  964. if (lua_constant == NULL)
  965. lua_initconstant();
  966. lua_pushstring (lua_constant[tag(o)]);
  967. }
  968. /*
  969. ** Internal function: convert an object to a number
  970. */
  971. void lua_obj2number (void)
  972. {
  973. Object *o = lua_getparam(1);
  974. lua_pushobject (lua_convtonumber(o));
  975. }
  976. /*
  977. ** Internal function: print object values
  978. */
  979. void lua_print (void)
  980. {
  981. int i=1;
  982. Object *obj;
  983. while ((obj=lua_getparam (i++)) != NULL)
  984. {
  985. if (lua_isnumber(obj)) printf("%g\n",lua_getnumber (obj));
  986. else if (lua_isstring(obj)) printf("%s\n",lua_getstring (obj));
  987. else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(obj));
  988. else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj));
  989. else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata (obj));
  990. else if (lua_istable(obj)) printf("table: %p\n",obj);
  991. else if (lua_isnil(obj)) printf("nil\n");
  992. else printf("invalid value to print\n");
  993. }
  994. }
  995. /*
  996. ** Internal function: do a file
  997. */
  998. void lua_internaldofile (void)
  999. {
  1000. lua_Object obj = lua_getparam (1);
  1001. if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
  1002. lua_pushnumber(1);
  1003. else
  1004. lua_pushnil();
  1005. }
  1006. /*
  1007. ** Internal function: do a string
  1008. */
  1009. void lua_internaldostring (void)
  1010. {
  1011. lua_Object obj = lua_getparam (1);
  1012. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  1013. lua_pushnumber(1);
  1014. else
  1015. lua_pushnil();
  1016. }