opcode.c 20 KB

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