opcode.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 2.8 1994/09/27 21:43:30 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. static int ToReal (char* s, float* f)
  79. {
  80. char c;
  81. float t;
  82. if (sscanf(s,"%f %c",&t,&c) == 1) { *f=t; return 1; } else return 0;
  83. }
  84. /*
  85. ** Convert, if possible, to a number object.
  86. ** Return 0 if success, not 0 if error.
  87. */
  88. static int lua_tonumber (Object *obj)
  89. {
  90. if (tag(obj) != T_STRING)
  91. {
  92. lua_reportbug ("unexpected type at conversion to number");
  93. return 1;
  94. }
  95. if (!ToReal(svalue(obj), &nvalue(obj)))
  96. {
  97. lua_reportbug ("string to number convertion failed");
  98. return 2;
  99. }
  100. tag(obj) = T_NUMBER;
  101. return 0;
  102. }
  103. /*
  104. ** Test if is possible to convert an object to a number object.
  105. ** If possible, return the converted object, otherwise return nil object.
  106. */
  107. static Object *lua_convtonumber (Object *obj)
  108. {
  109. static Object cvt;
  110. if (tag(obj) == T_NUMBER)
  111. {
  112. cvt = *obj;
  113. return &cvt;
  114. }
  115. if (tag(obj) == T_STRING && ToReal(svalue(obj), &nvalue(&cvt)))
  116. tag(&cvt) = T_NUMBER;
  117. else
  118. tag(&cvt) = T_NIL;
  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. CodeCode file;
  534. CodeWord func;
  535. get_code(file,pc);
  536. get_word(func,pc);
  537. if (lua_pushfunction ((char *)file.b, func.w))
  538. return 1;
  539. }
  540. break;
  541. case SETLINE:
  542. {
  543. CodeWord code;
  544. get_word(code,pc);
  545. lua_debugline = code.w;
  546. }
  547. break;
  548. case RESET:
  549. lua_popfunction ();
  550. break;
  551. default:
  552. lua_error ("internal error - opcode didn't match");
  553. return 1;
  554. }
  555. }
  556. }
  557. /*
  558. ** Function to indexed the values on the top
  559. */
  560. int lua_pushsubscript (void)
  561. {
  562. --top;
  563. if (tag(top-1) != T_ARRAY)
  564. {
  565. lua_reportbug ("indexed expression not a table");
  566. return 1;
  567. }
  568. {
  569. Object *h = lua_hashget (avalue(top-1), top);
  570. if (h == NULL) return 1;
  571. *(top-1) = *h;
  572. }
  573. return 0;
  574. }
  575. /*
  576. ** Function to store indexed based on values at the top
  577. */
  578. int lua_storesubscript (void)
  579. {
  580. if (tag(top-3) != T_ARRAY)
  581. {
  582. lua_reportbug ("indexed expression not a table");
  583. return 1;
  584. }
  585. {
  586. Object *h = lua_hashdefine (avalue(top-3), top-2);
  587. if (h == NULL) return 1;
  588. *h = *(top-1);
  589. }
  590. top -= 3;
  591. return 0;
  592. }
  593. /*
  594. ** Traverse all objects on stack
  595. */
  596. void lua_travstack (void (*fn)(Object *))
  597. {
  598. Object *o;
  599. for (o = top-1; o >= stack; o--)
  600. fn (o);
  601. }
  602. /*
  603. ** Open file, generate opcode and execute global statement. Return 0 on
  604. ** success or 1 on error.
  605. */
  606. int lua_dofile (char *filename)
  607. {
  608. if (lua_openfile (filename)) return 1;
  609. if (lua_parse ()) { lua_closefile (); return 1; }
  610. lua_closefile ();
  611. return 0;
  612. }
  613. /*
  614. ** Generate opcode stored on string and execute global statement. Return 0 on
  615. ** success or 1 on error.
  616. */
  617. int lua_dostring (char *string)
  618. {
  619. if (lua_openstring (string)) return 1;
  620. if (lua_parse ()) return 1;
  621. lua_closestring();
  622. return 0;
  623. }
  624. /*
  625. ** Execute the given function. Return 0 on success or 1 on error.
  626. */
  627. int lua_call (char *functionname, int nparam)
  628. {
  629. static Byte startcode[] = {CALLFUNC, HALT};
  630. int i;
  631. Object func = s_object(lua_findsymbol(functionname));
  632. if (tag(&func) != T_FUNCTION) return 1;
  633. for (i=1; i<=nparam; i++)
  634. *(top-i+2) = *(top-i);
  635. top += 2;
  636. tag(top-nparam-1) = T_MARK;
  637. *(top-nparam-2) = func;
  638. return (lua_execute (startcode));
  639. }
  640. /*
  641. ** Execute the given lua function. Return 0 on success or 1 on error.
  642. */
  643. int lua_callfunction (Object *function, int nparam)
  644. {
  645. static Byte startcode[] = {CALLFUNC, HALT};
  646. int i;
  647. if (tag(function) != T_FUNCTION) return 1;
  648. for (i=1; i<=nparam; i++)
  649. *(top-i+2) = *(top-i);
  650. top += 2;
  651. tag(top-nparam-1) = T_MARK;
  652. *(top-nparam-2) = *function;
  653. return (lua_execute (startcode));
  654. }
  655. /*
  656. ** Get a parameter, returning the object handle or NULL on error.
  657. ** 'number' must be 1 to get the first parameter.
  658. */
  659. Object *lua_getparam (int number)
  660. {
  661. if (number <= 0 || number > top-base) return NULL;
  662. return (base+number-1);
  663. }
  664. /*
  665. ** Given an object handle, return its number value. On error, return 0.0.
  666. */
  667. real lua_getnumber (Object *object)
  668. {
  669. if (object == NULL || tag(object) == T_NIL) return 0.0;
  670. if (tonumber (object)) return 0.0;
  671. else return (nvalue(object));
  672. }
  673. /*
  674. ** Given an object handle, return its string pointer. On error, return NULL.
  675. */
  676. char *lua_getstring (Object *object)
  677. {
  678. if (object == NULL || tag(object) == T_NIL) return NULL;
  679. if (tostring (object)) return NULL;
  680. else return (svalue(object));
  681. }
  682. /*
  683. ** Given an object handle, return a copy of its string. On error, return NULL.
  684. */
  685. char *lua_copystring (Object *object)
  686. {
  687. if (object == NULL || tag(object) == T_NIL) return NULL;
  688. if (tostring (object)) return NULL;
  689. else return (strdup(svalue(object)));
  690. }
  691. /*
  692. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  693. */
  694. lua_CFunction lua_getcfunction (Object *object)
  695. {
  696. if (object == NULL) return NULL;
  697. if (tag(object) != T_CFUNCTION) return NULL;
  698. else return (fvalue(object));
  699. }
  700. /*
  701. ** Given an object handle, return its user data. On error, return NULL.
  702. */
  703. void *lua_getuserdata (Object *object)
  704. {
  705. if (object == NULL) return NULL;
  706. if (tag(object) != T_USERDATA) return NULL;
  707. else return (uvalue(object));
  708. }
  709. /*
  710. ** Given an object handle, return its table. On error, return NULL.
  711. */
  712. void *lua_gettable (Object *object)
  713. {
  714. if (object == NULL) return NULL;
  715. if (tag(object) != T_ARRAY) return NULL;
  716. else return (avalue(object));
  717. }
  718. /*
  719. ** Given an object handle and a field name, return its field object.
  720. ** On error, return NULL.
  721. */
  722. Object *lua_getfield (Object *object, char *field)
  723. {
  724. if (object == NULL) return NULL;
  725. if (tag(object) != T_ARRAY)
  726. return NULL;
  727. else
  728. {
  729. Object ref;
  730. tag(&ref) = T_STRING;
  731. svalue(&ref) = lua_createstring(field);
  732. return (lua_hashget(avalue(object), &ref));
  733. }
  734. }
  735. /*
  736. ** Given an object handle and an index, return its indexed object.
  737. ** On error, return NULL.
  738. */
  739. Object *lua_getindexed (Object *object, float index)
  740. {
  741. if (object == NULL) return NULL;
  742. if (tag(object) != T_ARRAY)
  743. return NULL;
  744. else
  745. {
  746. Object ref;
  747. tag(&ref) = T_NUMBER;
  748. nvalue(&ref) = index;
  749. return (lua_hashget(avalue(object), &ref));
  750. }
  751. }
  752. /*
  753. ** Get a global object. Return the object handle or NULL on error.
  754. */
  755. Object *lua_getglobal (char *name)
  756. {
  757. int n = lua_findsymbol(name);
  758. if (n < 0) return NULL;
  759. return &s_object(n);
  760. }
  761. /*
  762. ** Pop and return an object
  763. */
  764. Object *lua_pop (void)
  765. {
  766. if (top <= base) return NULL;
  767. top--;
  768. return top;
  769. }
  770. /*
  771. ** Push a nil object
  772. */
  773. int lua_pushnil (void)
  774. {
  775. if (lua_checkstack(top-stack+1) == 1)
  776. return 1;
  777. tag(top++) = T_NIL;
  778. return 0;
  779. }
  780. /*
  781. ** Push an object (tag=number) to stack. Return 0 on success or 1 on error.
  782. */
  783. int lua_pushnumber (real n)
  784. {
  785. if (lua_checkstack(top-stack+1) == 1)
  786. return 1;
  787. tag(top) = T_NUMBER; nvalue(top++) = n;
  788. return 0;
  789. }
  790. /*
  791. ** Push an object (tag=string) to stack. Return 0 on success or 1 on error.
  792. */
  793. int lua_pushstring (char *s)
  794. {
  795. if (lua_checkstack(top-stack+1) == 1)
  796. return 1;
  797. tag(top) = T_STRING;
  798. svalue(top++) = lua_createstring(s);
  799. return 0;
  800. }
  801. /*
  802. ** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
  803. */
  804. int lua_pushcfunction (lua_CFunction fn)
  805. {
  806. if (lua_checkstack(top-stack+1) == 1)
  807. return 1;
  808. tag(top) = T_CFUNCTION; fvalue(top++) = fn;
  809. return 0;
  810. }
  811. /*
  812. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  813. */
  814. int lua_pushuserdata (void *u)
  815. {
  816. if (lua_checkstack(top-stack+1) == 1)
  817. return 1;
  818. tag(top) = T_USERDATA; uvalue(top++) = u;
  819. return 0;
  820. }
  821. /*
  822. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  823. */
  824. int lua_pushtable (void *t)
  825. {
  826. if (lua_checkstack(top-stack+1) == 1)
  827. return 1;
  828. tag(top) = T_ARRAY; avalue(top++) = t;
  829. return 0;
  830. }
  831. /*
  832. ** Push an object to stack.
  833. */
  834. int lua_pushobject (Object *o)
  835. {
  836. if (lua_checkstack(top-stack+1) == 1)
  837. return 1;
  838. *top++ = *o;
  839. return 0;
  840. }
  841. /*
  842. ** Store top of the stack at a global variable array field.
  843. ** Return 1 on error, 0 on success.
  844. */
  845. int lua_storeglobal (char *name)
  846. {
  847. int n = lua_findsymbol (name);
  848. if (n < 0) return 1;
  849. if (tag(top-1) == T_MARK) return 1;
  850. s_object(n) = *(--top);
  851. return 0;
  852. }
  853. /*
  854. ** Store top of the stack at an array field. Return 1 on error, 0 on success.
  855. */
  856. int lua_storefield (lua_Object object, char *field)
  857. {
  858. if (tag(object) != T_ARRAY)
  859. return 1;
  860. else
  861. {
  862. Object ref, *h;
  863. tag(&ref) = T_STRING;
  864. svalue(&ref) = lua_createstring(field);
  865. h = lua_hashdefine(avalue(object), &ref);
  866. if (h == NULL) return 1;
  867. if (tag(top-1) == T_MARK) return 1;
  868. *h = *(--top);
  869. }
  870. return 0;
  871. }
  872. /*
  873. ** Store top of the stack at an array index. Return 1 on error, 0 on success.
  874. */
  875. int lua_storeindexed (lua_Object object, float index)
  876. {
  877. if (tag(object) != T_ARRAY)
  878. return 1;
  879. else
  880. {
  881. Object ref, *h;
  882. tag(&ref) = T_NUMBER;
  883. nvalue(&ref) = index;
  884. h = lua_hashdefine(avalue(object), &ref);
  885. if (h == NULL) return 1;
  886. if (tag(top-1) == T_MARK) return 1;
  887. *h = *(--top);
  888. }
  889. return 0;
  890. }
  891. /*
  892. ** Given an object handle, return if it is nil.
  893. */
  894. int lua_isnil (Object *object)
  895. {
  896. return (object != NULL && tag(object) == T_NIL);
  897. }
  898. /*
  899. ** Given an object handle, return if it is a number one.
  900. */
  901. int lua_isnumber (Object *object)
  902. {
  903. return (object != NULL && tag(object) == T_NUMBER);
  904. }
  905. /*
  906. ** Given an object handle, return if it is a string one.
  907. */
  908. int lua_isstring (Object *object)
  909. {
  910. return (object != NULL && tag(object) == T_STRING);
  911. }
  912. /*
  913. ** Given an object handle, return if it is an array one.
  914. */
  915. int lua_istable (Object *object)
  916. {
  917. return (object != NULL && tag(object) == T_ARRAY);
  918. }
  919. /*
  920. ** Given an object handle, return if it is a lua function.
  921. */
  922. int lua_isfunction (Object *object)
  923. {
  924. return (object != NULL && tag(object) == T_FUNCTION);
  925. }
  926. /*
  927. ** Given an object handle, return if it is a cfunction one.
  928. */
  929. int lua_iscfunction (Object *object)
  930. {
  931. return (object != NULL && tag(object) == T_CFUNCTION);
  932. }
  933. /*
  934. ** Given an object handle, return if it is an user data one.
  935. */
  936. int lua_isuserdata (Object *object)
  937. {
  938. return (object != NULL && tag(object) == T_USERDATA);
  939. }
  940. /*
  941. ** Internal function: return an object type.
  942. */
  943. void lua_type (void)
  944. {
  945. Object *o = lua_getparam(1);
  946. if (lua_constant == NULL)
  947. lua_initconstant();
  948. lua_pushstring (lua_constant[tag(o)]);
  949. }
  950. /*
  951. ** Internal function: convert an object to a number
  952. */
  953. void lua_obj2number (void)
  954. {
  955. Object *o = lua_getparam(1);
  956. lua_pushobject (lua_convtonumber(o));
  957. }
  958. /*
  959. ** Internal function: print object values
  960. */
  961. void lua_print (void)
  962. {
  963. int i=1;
  964. Object *obj;
  965. while ((obj=lua_getparam (i++)) != NULL)
  966. {
  967. if (lua_isnumber(obj)) printf("%g\n",lua_getnumber (obj));
  968. else if (lua_isstring(obj)) printf("%s\n",lua_getstring (obj));
  969. else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(obj));
  970. else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj));
  971. else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata (obj));
  972. else if (lua_istable(obj)) printf("table: %p\n",obj);
  973. else if (lua_isnil(obj)) printf("nil\n");
  974. else printf("invalid value to print\n");
  975. }
  976. }
  977. /*
  978. ** Internal function: do a file
  979. */
  980. void lua_internaldofile (void)
  981. {
  982. lua_Object obj = lua_getparam (1);
  983. if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
  984. lua_pushnumber(1);
  985. else
  986. lua_pushnil();
  987. }
  988. /*
  989. ** Internal function: do a string
  990. */
  991. void lua_internaldostring (void)
  992. {
  993. lua_Object obj = lua_getparam (1);
  994. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  995. lua_pushnumber(1);
  996. else
  997. lua_pushnil();
  998. }