opcode.c 19 KB

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