opcode.c 22 KB

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