opcode.c 20 KB

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