opcode.c 22 KB

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