opcode.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 2.10 1994/10/17 19:00:40 celes 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. if (tag(top-1) == T_NIL)
  304. nvalue(top-1) = 1;
  305. else
  306. {
  307. if (tonumber(top-1)) return 1;
  308. if (nvalue(top-1) <= 0) nvalue(top-1) = 1;
  309. }
  310. avalue(top-1) = lua_createarray(nvalue(top-1));
  311. if (avalue(top-1) == NULL)
  312. return 1;
  313. tag(top-1) = T_ARRAY;
  314. break;
  315. case EQOP:
  316. {
  317. Object *l = top-2;
  318. Object *r = top-1;
  319. --top;
  320. if (tag(l) != tag(r))
  321. tag(top-1) = T_NIL;
  322. else
  323. {
  324. switch (tag(l))
  325. {
  326. case T_NIL: tag(top-1) = T_NUMBER; break;
  327. case T_NUMBER: tag(top-1) = (nvalue(l) == nvalue(r)) ? T_NUMBER : T_NIL; break;
  328. case T_ARRAY: tag(top-1) = (avalue(l) == avalue(r)) ? T_NUMBER : T_NIL; break;
  329. case T_FUNCTION: tag(top-1) = (bvalue(l) == bvalue(r)) ? T_NUMBER : T_NIL; break;
  330. case T_CFUNCTION: tag(top-1) = (fvalue(l) == fvalue(r)) ? T_NUMBER : T_NIL; break;
  331. case T_USERDATA: tag(top-1) = (uvalue(l) == uvalue(r)) ? T_NUMBER : T_NIL; break;
  332. case T_STRING: tag(top-1) = (strcmp (svalue(l), svalue(r)) == 0) ? T_NUMBER : T_NIL; break;
  333. case T_MARK: return 1;
  334. }
  335. }
  336. nvalue(top-1) = 1;
  337. }
  338. break;
  339. case LTOP:
  340. {
  341. Object *l = top-2;
  342. Object *r = top-1;
  343. --top;
  344. if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)
  345. tag(top-1) = (nvalue(l) < nvalue(r)) ? T_NUMBER : T_NIL;
  346. else
  347. {
  348. if (tostring(l) || tostring(r))
  349. return 1;
  350. tag(top-1) = (strcmp (svalue(l), svalue(r)) < 0) ? T_NUMBER : T_NIL;
  351. }
  352. nvalue(top-1) = 1;
  353. }
  354. break;
  355. case LEOP:
  356. {
  357. Object *l = top-2;
  358. Object *r = top-1;
  359. --top;
  360. if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)
  361. tag(top-1) = (nvalue(l) <= nvalue(r)) ? T_NUMBER : T_NIL;
  362. else
  363. {
  364. if (tostring(l) || tostring(r))
  365. return 1;
  366. tag(top-1) = (strcmp (svalue(l), svalue(r)) <= 0) ? T_NUMBER : T_NIL;
  367. }
  368. nvalue(top-1) = 1;
  369. }
  370. break;
  371. case ADDOP:
  372. {
  373. Object *l = top-2;
  374. Object *r = top-1;
  375. if (tonumber(r) || tonumber(l))
  376. return 1;
  377. nvalue(l) += nvalue(r);
  378. --top;
  379. }
  380. break;
  381. case SUBOP:
  382. {
  383. Object *l = top-2;
  384. Object *r = top-1;
  385. if (tonumber(r) || tonumber(l))
  386. return 1;
  387. nvalue(l) -= nvalue(r);
  388. --top;
  389. }
  390. break;
  391. case MULTOP:
  392. {
  393. Object *l = top-2;
  394. Object *r = top-1;
  395. if (tonumber(r) || tonumber(l))
  396. return 1;
  397. nvalue(l) *= nvalue(r);
  398. --top;
  399. }
  400. break;
  401. case DIVOP:
  402. {
  403. Object *l = top-2;
  404. Object *r = top-1;
  405. if (tonumber(r) || tonumber(l))
  406. return 1;
  407. nvalue(l) /= nvalue(r);
  408. --top;
  409. }
  410. break;
  411. case POWOP:
  412. {
  413. Object *l = top-2;
  414. Object *r = top-1;
  415. if (tonumber(r) || tonumber(l))
  416. return 1;
  417. nvalue(l) = pow(nvalue(l), nvalue(r));
  418. --top;
  419. }
  420. break;
  421. case CONCOP:
  422. {
  423. Object *l = top-2;
  424. Object *r = top-1;
  425. if (tostring(r) || tostring(l))
  426. return 1;
  427. svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  428. if (svalue(l) == NULL)
  429. return 1;
  430. --top;
  431. }
  432. break;
  433. case MINUSOP:
  434. if (tonumber(top-1))
  435. return 1;
  436. nvalue(top-1) = - nvalue(top-1);
  437. break;
  438. case NOTOP:
  439. tag(top-1) = tag(top-1) == T_NIL ? T_NUMBER : T_NIL;
  440. break;
  441. case ONTJMP:
  442. {
  443. CodeWord code;
  444. get_word(code,pc);
  445. if (tag(top-1) != T_NIL) pc += code.w;
  446. }
  447. break;
  448. case ONFJMP:
  449. {
  450. CodeWord code;
  451. get_word(code,pc);
  452. if (tag(top-1) == T_NIL) pc += code.w;
  453. }
  454. break;
  455. case JMP:
  456. {
  457. CodeWord code;
  458. get_word(code,pc);
  459. pc += code.w;
  460. }
  461. break;
  462. case UPJMP:
  463. {
  464. CodeWord code;
  465. get_word(code,pc);
  466. pc -= code.w;
  467. }
  468. break;
  469. case IFFJMP:
  470. {
  471. CodeWord code;
  472. get_word(code,pc);
  473. top--;
  474. if (tag(top) == T_NIL) pc += code.w;
  475. }
  476. break;
  477. case IFFUPJMP:
  478. {
  479. CodeWord code;
  480. get_word(code,pc);
  481. top--;
  482. if (tag(top) == T_NIL) pc -= code.w;
  483. }
  484. break;
  485. case POP: --top; break;
  486. case CALLFUNC:
  487. {
  488. Byte *newpc;
  489. Object *b = top-1;
  490. while (tag(b) != T_MARK) b--;
  491. if (tag(b-1) == T_FUNCTION)
  492. {
  493. lua_debugline = 0; /* always reset debug flag */
  494. newpc = bvalue(b-1);
  495. bvalue(b-1) = pc; /* store return code */
  496. nvalue(b) = (base-stack); /* store base value */
  497. base = b+1;
  498. pc = newpc;
  499. if (lua_checkstack(STACKGAP+(base-stack)))
  500. return 1;
  501. }
  502. else if (tag(b-1) == T_CFUNCTION)
  503. {
  504. int nparam;
  505. lua_debugline = 0; /* always reset debug flag */
  506. nvalue(b) = (base-stack); /* store base value */
  507. base = b+1;
  508. nparam = top-base; /* number of parameters */
  509. (fvalue(b-1))(); /* call C function */
  510. /* shift returned values */
  511. {
  512. int i;
  513. int nretval = top - base - nparam;
  514. top = base - 2;
  515. base = stack + (int) nvalue(base-1);
  516. for (i=0; i<nretval; i++)
  517. {
  518. *top = *(top+nparam+2);
  519. ++top;
  520. }
  521. }
  522. }
  523. else
  524. {
  525. lua_reportbug ("call expression not a function");
  526. return 1;
  527. }
  528. }
  529. break;
  530. case RETCODE:
  531. {
  532. int i;
  533. int shift = *pc++;
  534. int nretval = top - base - shift;
  535. top = base - 2;
  536. pc = bvalue(base-2);
  537. base = stack + (int) nvalue(base-1);
  538. for (i=0; i<nretval; i++)
  539. {
  540. *top = *(top+shift+2);
  541. ++top;
  542. }
  543. }
  544. break;
  545. case HALT:
  546. base = stack+oldbase;
  547. return 0; /* success */
  548. case SETFUNCTION:
  549. {
  550. CodeCode file;
  551. CodeWord func;
  552. get_code(file,pc);
  553. get_word(func,pc);
  554. if (lua_pushfunction ((char *)file.b, func.w))
  555. return 1;
  556. }
  557. break;
  558. case SETLINE:
  559. {
  560. CodeWord code;
  561. get_word(code,pc);
  562. lua_debugline = code.w;
  563. }
  564. break;
  565. case RESET:
  566. lua_popfunction ();
  567. break;
  568. default:
  569. lua_error ("internal error - opcode didn't match");
  570. return 1;
  571. }
  572. }
  573. }
  574. /*
  575. ** Function to index the values on the top
  576. */
  577. int lua_pushsubscript (void)
  578. {
  579. --top;
  580. if (tag(top-1) != T_ARRAY)
  581. {
  582. lua_reportbug ("indexed expression not a table");
  583. return 1;
  584. }
  585. {
  586. Object *h = lua_hashget (avalue(top-1), top);
  587. if (h == NULL) return 1;
  588. *(top-1) = *h;
  589. }
  590. return 0;
  591. }
  592. /*
  593. ** Function to store indexed based on values at the top
  594. */
  595. int lua_storesubscript (void)
  596. {
  597. if (tag(top-3) != T_ARRAY)
  598. {
  599. lua_reportbug ("indexed expression not a table");
  600. return 1;
  601. }
  602. {
  603. Object *h = lua_hashdefine (avalue(top-3), top-2);
  604. if (h == NULL) return 1;
  605. *h = *(top-1);
  606. }
  607. top -= 3;
  608. return 0;
  609. }
  610. /*
  611. ** Traverse all objects on stack
  612. */
  613. void lua_travstack (void (*fn)(Object *))
  614. {
  615. Object *o;
  616. for (o = top-1; o >= stack; o--)
  617. fn (o);
  618. }
  619. /*
  620. ** Open file, generate opcode and execute global statement. Return 0 on
  621. ** success or 1 on error.
  622. */
  623. int lua_dofile (char *filename)
  624. {
  625. if (lua_openfile (filename)) return 1;
  626. if (lua_parse ()) { lua_closefile (); return 1; }
  627. lua_closefile ();
  628. return 0;
  629. }
  630. /*
  631. ** Generate opcode stored on string and execute global statement. Return 0 on
  632. ** success or 1 on error.
  633. */
  634. int lua_dostring (char *string)
  635. {
  636. if (lua_openstring (string)) return 1;
  637. if (lua_parse ()) return 1;
  638. lua_closestring();
  639. return 0;
  640. }
  641. /*
  642. ** Execute the given function. Return 0 on success or 1 on error.
  643. */
  644. int lua_call (char *functionname, int nparam)
  645. {
  646. static Byte startcode[] = {CALLFUNC, HALT};
  647. int i;
  648. Object func = s_object(lua_findsymbol(functionname));
  649. if (tag(&func) != T_FUNCTION) return 1;
  650. for (i=1; i<=nparam; i++)
  651. *(top-i+2) = *(top-i);
  652. top += 2;
  653. tag(top-nparam-1) = T_MARK;
  654. *(top-nparam-2) = func;
  655. return (lua_execute (startcode));
  656. }
  657. /*
  658. ** Execute the given lua function. Return 0 on success or 1 on error.
  659. */
  660. int lua_callfunction (Object *function, int nparam)
  661. {
  662. static Byte startcode[] = {CALLFUNC, HALT};
  663. int i;
  664. if (tag(function) != T_FUNCTION) return 1;
  665. for (i=1; i<=nparam; i++)
  666. *(top-i+2) = *(top-i);
  667. top += 2;
  668. tag(top-nparam-1) = T_MARK;
  669. *(top-nparam-2) = *function;
  670. return (lua_execute (startcode));
  671. }
  672. /*
  673. ** Get a parameter, returning the object handle or NULL on error.
  674. ** 'number' must be 1 to get the first parameter.
  675. */
  676. Object *lua_getparam (int number)
  677. {
  678. if (number <= 0 || number > top-base) return NULL;
  679. return (base+number-1);
  680. }
  681. /*
  682. ** Given an object handle, return its number value. On error, return 0.0.
  683. */
  684. real lua_getnumber (Object *object)
  685. {
  686. if (object == NULL || tag(object) == T_NIL) return 0.0;
  687. if (tonumber (object)) return 0.0;
  688. else return (nvalue(object));
  689. }
  690. /*
  691. ** Given an object handle, return its string pointer. On error, return NULL.
  692. */
  693. char *lua_getstring (Object *object)
  694. {
  695. if (object == NULL || tag(object) == T_NIL) return NULL;
  696. if (tostring (object)) return NULL;
  697. else return (svalue(object));
  698. }
  699. /*
  700. ** Given an object handle, return a copy of its string. On error, return NULL.
  701. */
  702. char *lua_copystring (Object *object)
  703. {
  704. if (object == NULL || tag(object) == T_NIL) return NULL;
  705. if (tostring (object)) return NULL;
  706. else return (strdup(svalue(object)));
  707. }
  708. /*
  709. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  710. */
  711. lua_CFunction lua_getcfunction (Object *object)
  712. {
  713. if (object == NULL) return NULL;
  714. if (tag(object) != T_CFUNCTION) return NULL;
  715. else return (fvalue(object));
  716. }
  717. /*
  718. ** Given an object handle, return its user data. On error, return NULL.
  719. */
  720. void *lua_getuserdata (Object *object)
  721. {
  722. if (object == NULL) return NULL;
  723. if (tag(object) != T_USERDATA) return NULL;
  724. else return (uvalue(object));
  725. }
  726. /*
  727. ** Given an object handle, return its table. On error, return NULL.
  728. */
  729. void *lua_gettable (Object *object)
  730. {
  731. if (object == NULL) return NULL;
  732. if (tag(object) != T_ARRAY) return NULL;
  733. else return (avalue(object));
  734. }
  735. /*
  736. ** Given an object handle and a field name, return its field object.
  737. ** On error, return NULL.
  738. */
  739. Object *lua_getfield (Object *object, char *field)
  740. {
  741. if (object == NULL) return NULL;
  742. if (tag(object) != T_ARRAY)
  743. return NULL;
  744. else
  745. {
  746. Object ref;
  747. tag(&ref) = T_STRING;
  748. svalue(&ref) = lua_constant[lua_findconstant(field)];
  749. return (lua_hashget(avalue(object), &ref));
  750. }
  751. }
  752. /*
  753. ** Given an object handle and an index, return its indexed object.
  754. ** On error, return NULL.
  755. */
  756. Object *lua_getindexed (Object *object, float index)
  757. {
  758. if (object == NULL) return NULL;
  759. if (tag(object) != T_ARRAY)
  760. return NULL;
  761. else
  762. {
  763. Object ref;
  764. tag(&ref) = T_NUMBER;
  765. nvalue(&ref) = index;
  766. return (lua_hashget(avalue(object), &ref));
  767. }
  768. }
  769. /*
  770. ** Get a global object. Return the object handle or NULL on error.
  771. */
  772. Object *lua_getglobal (char *name)
  773. {
  774. int n = lua_findsymbol(name);
  775. if (n < 0) return NULL;
  776. return &s_object(n);
  777. }
  778. /*
  779. ** Pop and return an object
  780. */
  781. Object *lua_pop (void)
  782. {
  783. if (top <= base) return NULL;
  784. top--;
  785. return top;
  786. }
  787. /*
  788. ** Push a nil object
  789. */
  790. int lua_pushnil (void)
  791. {
  792. if (lua_checkstack(top-stack+1) == 1)
  793. return 1;
  794. tag(top++) = T_NIL;
  795. return 0;
  796. }
  797. /*
  798. ** Push an object (tag=number) to stack. Return 0 on success or 1 on error.
  799. */
  800. int lua_pushnumber (real n)
  801. {
  802. if (lua_checkstack(top-stack+1) == 1)
  803. return 1;
  804. tag(top) = T_NUMBER; nvalue(top++) = n;
  805. return 0;
  806. }
  807. /*
  808. ** Push an object (tag=string) to stack. Return 0 on success or 1 on error.
  809. */
  810. int lua_pushstring (char *s)
  811. {
  812. if (lua_checkstack(top-stack+1) == 1)
  813. return 1;
  814. tag(top) = T_STRING;
  815. svalue(top++) = lua_createstring(s);
  816. return 0;
  817. }
  818. /*
  819. ** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
  820. */
  821. int lua_pushcfunction (lua_CFunction fn)
  822. {
  823. if (lua_checkstack(top-stack+1) == 1)
  824. return 1;
  825. tag(top) = T_CFUNCTION; fvalue(top++) = fn;
  826. return 0;
  827. }
  828. /*
  829. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  830. */
  831. int lua_pushuserdata (void *u)
  832. {
  833. if (lua_checkstack(top-stack+1) == 1)
  834. return 1;
  835. tag(top) = T_USERDATA; uvalue(top++) = u;
  836. return 0;
  837. }
  838. /*
  839. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  840. */
  841. int lua_pushtable (void *t)
  842. {
  843. if (lua_checkstack(top-stack+1) == 1)
  844. return 1;
  845. tag(top) = T_ARRAY; avalue(top++) = t;
  846. return 0;
  847. }
  848. /*
  849. ** Push an object to stack.
  850. */
  851. int lua_pushobject (Object *o)
  852. {
  853. if (lua_checkstack(top-stack+1) == 1)
  854. return 1;
  855. *top++ = *o;
  856. return 0;
  857. }
  858. /*
  859. ** Store top of the stack at a global variable array field.
  860. ** Return 1 on error, 0 on success.
  861. */
  862. int lua_storeglobal (char *name)
  863. {
  864. int n = lua_findsymbol (name);
  865. if (n < 0) return 1;
  866. if (tag(top-1) == T_MARK) return 1;
  867. s_object(n) = *(--top);
  868. return 0;
  869. }
  870. /*
  871. ** Store top of the stack at an array field. Return 1 on error, 0 on success.
  872. */
  873. int lua_storefield (lua_Object object, char *field)
  874. {
  875. if (tag(object) != T_ARRAY)
  876. return 1;
  877. else
  878. {
  879. Object ref, *h;
  880. tag(&ref) = T_STRING;
  881. svalue(&ref) = lua_createstring(field);
  882. h = lua_hashdefine(avalue(object), &ref);
  883. if (h == NULL) return 1;
  884. if (tag(top-1) == T_MARK) return 1;
  885. *h = *(--top);
  886. }
  887. return 0;
  888. }
  889. /*
  890. ** Store top of the stack at an array index. Return 1 on error, 0 on success.
  891. */
  892. int lua_storeindexed (lua_Object object, float index)
  893. {
  894. if (tag(object) != T_ARRAY)
  895. return 1;
  896. else
  897. {
  898. Object ref, *h;
  899. tag(&ref) = T_NUMBER;
  900. nvalue(&ref) = index;
  901. h = lua_hashdefine(avalue(object), &ref);
  902. if (h == NULL) return 1;
  903. if (tag(top-1) == T_MARK) return 1;
  904. *h = *(--top);
  905. }
  906. return 0;
  907. }
  908. /*
  909. ** Given an object handle, return if it is nil.
  910. */
  911. int lua_isnil (Object *object)
  912. {
  913. return (object != NULL && tag(object) == T_NIL);
  914. }
  915. /*
  916. ** Given an object handle, return if it is a number one.
  917. */
  918. int lua_isnumber (Object *object)
  919. {
  920. return (object != NULL && tag(object) == T_NUMBER);
  921. }
  922. /*
  923. ** Given an object handle, return if it is a string one.
  924. */
  925. int lua_isstring (Object *object)
  926. {
  927. return (object != NULL && tag(object) == T_STRING);
  928. }
  929. /*
  930. ** Given an object handle, return if it is an array one.
  931. */
  932. int lua_istable (Object *object)
  933. {
  934. return (object != NULL && tag(object) == T_ARRAY);
  935. }
  936. /*
  937. ** Given an object handle, return if it is a lua function.
  938. */
  939. int lua_isfunction (Object *object)
  940. {
  941. return (object != NULL && tag(object) == T_FUNCTION);
  942. }
  943. /*
  944. ** Given an object handle, return if it is a cfunction one.
  945. */
  946. int lua_iscfunction (Object *object)
  947. {
  948. return (object != NULL && tag(object) == T_CFUNCTION);
  949. }
  950. /*
  951. ** Given an object handle, return if it is an user data one.
  952. */
  953. int lua_isuserdata (Object *object)
  954. {
  955. return (object != NULL && tag(object) == T_USERDATA);
  956. }
  957. /*
  958. ** Internal function: return an object type.
  959. */
  960. void lua_type (void)
  961. {
  962. Object *o = lua_getparam(1);
  963. if (lua_constant == NULL)
  964. lua_initconstant();
  965. lua_pushstring (lua_constant[tag(o)]);
  966. }
  967. /*
  968. ** Internal function: convert an object to a number
  969. */
  970. void lua_obj2number (void)
  971. {
  972. Object *o = lua_getparam(1);
  973. lua_pushobject (lua_convtonumber(o));
  974. }
  975. /*
  976. ** Internal function: print object values
  977. */
  978. void lua_print (void)
  979. {
  980. int i=1;
  981. Object *obj;
  982. while ((obj=lua_getparam (i++)) != NULL)
  983. {
  984. if (lua_isnumber(obj)) printf("%g\n",lua_getnumber (obj));
  985. else if (lua_isstring(obj)) printf("%s\n",lua_getstring (obj));
  986. else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(obj));
  987. else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj));
  988. else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata (obj));
  989. else if (lua_istable(obj)) printf("table: %p\n",obj);
  990. else if (lua_isnil(obj)) printf("nil\n");
  991. else printf("invalid value to print\n");
  992. }
  993. }
  994. /*
  995. ** Internal function: do a file
  996. */
  997. void lua_internaldofile (void)
  998. {
  999. lua_Object obj = lua_getparam (1);
  1000. if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
  1001. lua_pushnumber(1);
  1002. else
  1003. lua_pushnil();
  1004. }
  1005. /*
  1006. ** Internal function: do a string
  1007. */
  1008. void lua_internaldostring (void)
  1009. {
  1010. lua_Object obj = lua_getparam (1);
  1011. if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  1012. lua_pushnumber(1);
  1013. else
  1014. lua_pushnil();
  1015. }