opcode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 2.7 1994/09/20 15:11:11 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. 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. }