opcode.c 20 KB

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