opcode.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.78 1996/11/22 13:08:28 roberto Exp roberto $";
  6. #include <setjmp.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "luadebug.h"
  11. #include "mem.h"
  12. #include "opcode.h"
  13. #include "hash.h"
  14. #include "inout.h"
  15. #include "table.h"
  16. #include "lua.h"
  17. #include "fallback.h"
  18. #include "undump.h"
  19. #define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
  20. #define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
  21. #define STACK_SIZE 128
  22. #ifndef STACK_LIMIT
  23. #define STACK_LIMIT 6000
  24. #endif
  25. typedef int StkId; /* index to stack elements */
  26. static Object initial_stack;
  27. static Object *stackLimit = &initial_stack+1;
  28. static Object *stack = &initial_stack;
  29. static Object *top = &initial_stack;
  30. /* macros to convert from lua_Object to (Object *) and back */
  31. #define Address(lo) ((lo)+stack-1)
  32. #define Ref(st) ((st)-stack+1)
  33. /* macro to increment stack top. There must be always an empty slot in
  34. * the stack
  35. */
  36. #define incr_top if (++top >= stackLimit) growstack()
  37. struct C_Lua_Stack {
  38. StkId base; /* when Lua calls C or C calls Lua, points to */
  39. /* the first slot after the last parameter. */
  40. int num; /* when Lua calls C, has the number of parameters; */
  41. /* when C calls Lua, has the number of results. */
  42. };
  43. static struct C_Lua_Stack CLS_current = {0, 0};
  44. static jmp_buf *errorJmp = NULL; /* current error recover point */
  45. /* Hooks */
  46. lua_LHFunction lua_linehook = NULL;
  47. lua_CHFunction lua_callhook = NULL;
  48. static StkId lua_execute (Byte *pc, StkId base);
  49. static void do_call (StkId base, int nResults);
  50. Object *luaI_Address (lua_Object o)
  51. {
  52. return Address(o);
  53. }
  54. /*
  55. ** Init stack
  56. */
  57. static void lua_initstack (void)
  58. {
  59. Long maxstack = STACK_SIZE;
  60. stack = newvector(maxstack, Object);
  61. stackLimit = stack+maxstack;
  62. top = stack;
  63. *(top++) = initial_stack;
  64. }
  65. /*
  66. ** Check stack overflow and, if necessary, realloc vector
  67. */
  68. #define lua_checkstack(nt) if ((nt) >= stackLimit) growstack()
  69. static void growstack (void)
  70. {
  71. if (stack == &initial_stack)
  72. lua_initstack();
  73. else
  74. {
  75. static int limit = STACK_LIMIT;
  76. StkId t = top-stack;
  77. Long stacksize = stackLimit - stack;
  78. stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100);
  79. stackLimit = stack+stacksize;
  80. top = stack + t;
  81. if (stacksize >= limit)
  82. {
  83. limit = stacksize;
  84. lua_error(stackEM);
  85. }
  86. }
  87. }
  88. /*
  89. ** Concatenate two given strings. Return the new string pointer.
  90. */
  91. static char *lua_strconc (char *l, char *r)
  92. {
  93. size_t nl = strlen(l);
  94. char *buffer = luaI_buffer(nl+strlen(r)+1);
  95. strcpy(buffer, l);
  96. strcpy(buffer+nl, r);
  97. return buffer;
  98. }
  99. /*
  100. ** Convert, if possible, to a number object.
  101. ** Return 0 if success, not 0 if error.
  102. */
  103. static int lua_tonumber (Object *obj)
  104. {
  105. float t;
  106. char c;
  107. if (tag(obj) != LUA_T_STRING)
  108. return 1;
  109. else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
  110. {
  111. nvalue(obj) = t;
  112. tag(obj) = LUA_T_NUMBER;
  113. return 0;
  114. }
  115. else
  116. return 2;
  117. }
  118. /*
  119. ** Convert, if possible, to a string tag
  120. ** Return 0 in success or not 0 on error.
  121. */
  122. static int lua_tostring (Object *obj)
  123. {
  124. if (tag(obj) != LUA_T_NUMBER)
  125. return 1;
  126. else {
  127. char s[60];
  128. real f = nvalue(obj);
  129. int i;
  130. if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f)
  131. sprintf (s, "%d", i);
  132. else
  133. sprintf (s, "%g", nvalue(obj));
  134. tsvalue(obj) = lua_createstring(s);
  135. tag(obj) = LUA_T_STRING;
  136. return 0;
  137. }
  138. }
  139. /*
  140. ** Adjust stack. Set top to the given value, pushing NILs if needed.
  141. */
  142. static void adjust_top (StkId newtop)
  143. {
  144. Object *nt;
  145. lua_checkstack(stack+newtop);
  146. nt = stack+newtop; /* warning: previous call may change stack */
  147. while (top < nt) tag(top++) = LUA_T_NIL;
  148. top = nt; /* top could be bigger than newtop */
  149. }
  150. #define adjustC(nParams) adjust_top(CLS_current.base+nParams)
  151. /*
  152. ** Open a hole below "nelems" from the top.
  153. */
  154. static void open_stack (int nelems)
  155. {
  156. int i;
  157. for (i=0; i<nelems; i++)
  158. *(top-i) = *(top-i-1);
  159. incr_top;
  160. }
  161. /*
  162. ** call Line hook
  163. */
  164. static void lineHook (int line)
  165. {
  166. struct C_Lua_Stack oldCLS = CLS_current;
  167. StkId old_top = CLS_current.base = top-stack;
  168. CLS_current.num = 0;
  169. (*lua_linehook)(line);
  170. top = stack+old_top;
  171. CLS_current = oldCLS;
  172. }
  173. /*
  174. ** Call hook
  175. ** The function being called is in [stack+base-1]
  176. */
  177. static void callHook (StkId base, lua_Type type, int isreturn)
  178. {
  179. struct C_Lua_Stack oldCLS = CLS_current;
  180. StkId old_top = CLS_current.base = top-stack;
  181. CLS_current.num = 0;
  182. if (isreturn)
  183. (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
  184. else
  185. {
  186. Object *f = stack+base-1;
  187. if (type == LUA_T_MARK)
  188. (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
  189. else
  190. (*lua_callhook)(Ref(f), "(C)", -1);
  191. }
  192. top = stack+old_top;
  193. CLS_current = oldCLS;
  194. }
  195. /*
  196. ** Call a C function. CLS_current.base will point to the top of the stack,
  197. ** and CLS_current.num is the number of parameters. Returns an index
  198. ** to the first result from C.
  199. */
  200. static StkId callC (lua_CFunction func, StkId base)
  201. {
  202. struct C_Lua_Stack oldCLS = CLS_current;
  203. StkId firstResult;
  204. CLS_current.num = (top-stack) - base;
  205. /* incorporate parameters on the stack */
  206. CLS_current.base = base+CLS_current.num; /* == top-stack */
  207. if (lua_callhook)
  208. callHook(base, LUA_T_CMARK, 0);
  209. (*func)();
  210. if (lua_callhook) /* func may have changed lua_callhook */
  211. callHook(base, LUA_T_CMARK, 1);
  212. firstResult = CLS_current.base;
  213. CLS_current = oldCLS;
  214. return firstResult;
  215. }
  216. /*
  217. ** Call the specified fallback, putting it on the stack below its arguments
  218. */
  219. static void callFB (int fb)
  220. {
  221. int nParams = luaI_fallBacks[fb].nParams;
  222. open_stack(nParams);
  223. *(top-nParams-1) = luaI_fallBacks[fb].function;
  224. do_call((top-stack)-nParams, luaI_fallBacks[fb].nResults);
  225. }
  226. /*
  227. ** Call a function (C or Lua). The parameters must be on the stack,
  228. ** between [stack+base,top). The function to be called is at stack+base-1.
  229. ** When returns, the results are on the stack, between [stack+base-1,top).
  230. ** The number of results is nResults, unless nResults=MULT_RET.
  231. */
  232. static void do_call (StkId base, int nResults)
  233. {
  234. StkId firstResult;
  235. Object *func = stack+base-1;
  236. int i;
  237. if (tag(func) == LUA_T_CFUNCTION)
  238. {
  239. tag(func) = LUA_T_CMARK;
  240. firstResult = callC(fvalue(func), base);
  241. }
  242. else if (tag(func) == LUA_T_FUNCTION)
  243. {
  244. tag(func) = LUA_T_MARK;
  245. firstResult = lua_execute(func->value.tf->code, base);
  246. }
  247. else
  248. { /* func is not a function */
  249. /* Call the fallback for invalid functions */
  250. open_stack((top-stack)-(base-1));
  251. stack[base-1] = luaI_fallBacks[FB_FUNCTION].function;
  252. do_call(base, nResults);
  253. return;
  254. }
  255. /* adjust the number of results */
  256. if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
  257. adjust_top(firstResult+nResults);
  258. /* move results to base-1 (to erase parameters and function) */
  259. base--;
  260. nResults = top - (stack+firstResult); /* actual number of results */
  261. for (i=0; i<nResults; i++)
  262. *(stack+base+i) = *(stack+firstResult+i);
  263. top -= firstResult-base;
  264. }
  265. /*
  266. ** Function to index a table. Receives the table at top-2 and the index
  267. ** at top-1.
  268. */
  269. static void pushsubscript (void)
  270. {
  271. if (tag(top-2) != LUA_T_ARRAY)
  272. callFB(FB_GETTABLE);
  273. else
  274. {
  275. Object *h = lua_hashget(avalue(top-2), top-1);
  276. if (h == NULL || tag(h) == LUA_T_NIL)
  277. callFB(FB_INDEX);
  278. else
  279. {
  280. --top;
  281. *(top-1) = *h;
  282. }
  283. }
  284. }
  285. /*
  286. ** Function to store indexed based on values at the top
  287. */
  288. static void storesubscript (void)
  289. {
  290. if (tag(top-3) != LUA_T_ARRAY)
  291. callFB(FB_SETTABLE);
  292. else
  293. {
  294. Object *h = lua_hashdefine (avalue(top-3), top-2);
  295. *h = *(top-1);
  296. top -= 3;
  297. }
  298. }
  299. static void getglobal (Word n)
  300. {
  301. *top = lua_table[n].object;
  302. incr_top;
  303. if (tag(top-1) == LUA_T_NIL)
  304. { /* must call getglobal fallback */
  305. tag(top-1) = LUA_T_STRING;
  306. tsvalue(top-1) = lua_table[n].varname;
  307. callFB(FB_GETGLOBAL);
  308. }
  309. }
  310. /*
  311. ** Traverse all objects on stack
  312. */
  313. void lua_travstack (int (*fn)(Object *))
  314. {
  315. Object *o;
  316. for (o = top-1; o >= stack; o--)
  317. fn (o);
  318. }
  319. /*
  320. ** Error messages and debug functions
  321. */
  322. static void lua_message (char *s)
  323. {
  324. lua_pushstring(s);
  325. callFB(FB_ERROR);
  326. }
  327. /*
  328. ** Reports an error, and jumps up to the available recover label
  329. */
  330. void lua_error (char *s)
  331. {
  332. if (s) lua_message(s);
  333. if (errorJmp)
  334. longjmp(*errorJmp, 1);
  335. else
  336. {
  337. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  338. exit(1);
  339. }
  340. }
  341. lua_Function lua_stackedfunction (int level)
  342. {
  343. StkId i;
  344. for (i = (top-1)-stack; i>=0; i--)
  345. if (stack[i].tag == LUA_T_MARK || stack[i].tag == LUA_T_CMARK)
  346. if (level-- == 0)
  347. return Ref(stack+i);
  348. return LUA_NOOBJECT;
  349. }
  350. int lua_currentline (lua_Function func)
  351. {
  352. Object *f = Address(func);
  353. return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
  354. }
  355. lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
  356. {
  357. Object *f = luaI_Address(func);
  358. *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  359. if (*name)
  360. {
  361. /* if "*name", there must be a LUA_T_LINE */
  362. /* therefore, f+2 points to function base */
  363. return Ref((f+2)+(local_number-1));
  364. }
  365. else
  366. return LUA_NOOBJECT;
  367. }
  368. int lua_setlocal (lua_Function func, int local_number)
  369. {
  370. Object *f = Address(func);
  371. char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  372. adjustC(1);
  373. --top;
  374. if (name)
  375. {
  376. /* if "name", there must be a LUA_T_LINE */
  377. /* therefore, f+2 points to function base */
  378. *((f+2)+(local_number-1)) = *top;
  379. return 1;
  380. }
  381. else
  382. return 0;
  383. }
  384. /*
  385. ** Execute a protected call. Assumes that function is at CLS_current.base and
  386. ** parameters are on top of it. Leave nResults on the stack.
  387. */
  388. static int do_protectedrun (int nResults)
  389. {
  390. jmp_buf myErrorJmp;
  391. int status;
  392. struct C_Lua_Stack oldCLS = CLS_current;
  393. jmp_buf *oldErr = errorJmp;
  394. errorJmp = &myErrorJmp;
  395. if (setjmp(myErrorJmp) == 0)
  396. {
  397. do_call(CLS_current.base+1, nResults);
  398. CLS_current.num = (top-stack) - CLS_current.base; /* number of results */
  399. CLS_current.base += CLS_current.num; /* incorporate results on the stack */
  400. status = 0;
  401. }
  402. else
  403. { /* an error occurred: restore CLS_current and top */
  404. CLS_current = oldCLS;
  405. top = stack+CLS_current.base;
  406. status = 1;
  407. }
  408. errorJmp = oldErr;
  409. return status;
  410. }
  411. int luaI_dorun (TFunc *tf)
  412. {
  413. int status;
  414. adjustC(1); /* one slot for the pseudo-function */
  415. stack[CLS_current.base].tag = LUA_T_FUNCTION;
  416. stack[CLS_current.base].value.tf = tf;
  417. status = do_protectedrun(MULT_RET);
  418. return status;
  419. }
  420. static int do_protectedmain (void)
  421. {
  422. TFunc tf;
  423. int status;
  424. jmp_buf myErrorJmp;
  425. jmp_buf *oldErr = errorJmp;
  426. errorJmp = &myErrorJmp;
  427. luaI_initTFunc(&tf);
  428. tf.fileName = lua_parsedfile;
  429. if (setjmp(myErrorJmp) == 0)
  430. {
  431. lua_parse(&tf);
  432. status = luaI_dorun(&tf);
  433. }
  434. else
  435. {
  436. status = 1;
  437. adjustC(0); /* erase extra slot */
  438. }
  439. errorJmp = oldErr;
  440. luaI_free(tf.code);
  441. return status;
  442. }
  443. /*
  444. ** Execute the given lua function. Return 0 on success or 1 on error.
  445. */
  446. int lua_callfunction (lua_Object function)
  447. {
  448. if (function == LUA_NOOBJECT)
  449. return 1;
  450. else
  451. {
  452. open_stack((top-stack)-CLS_current.base);
  453. stack[CLS_current.base] = *Address(function);
  454. return do_protectedrun (MULT_RET);
  455. }
  456. }
  457. int lua_call (char *funcname)
  458. {
  459. Word n = luaI_findsymbolbyname(funcname);
  460. open_stack((top-stack)-CLS_current.base);
  461. stack[CLS_current.base] = s_object(n);
  462. return do_protectedrun(MULT_RET);
  463. }
  464. /*
  465. ** Open file, generate opcode and execute global statement. Return 0 on
  466. ** success or non 0 on error.
  467. */
  468. int lua_dofile (char *filename)
  469. {
  470. int status;
  471. int c;
  472. FILE *f = lua_openfile(filename);
  473. if (f == NULL)
  474. return 2;
  475. c = fgetc(f);
  476. ungetc(c, f);
  477. if (c == ID_CHUNK) {
  478. f = freopen(filename, "rb", f); /* set binary mode */
  479. status = luaI_undump(f);
  480. }
  481. else {
  482. if (c == '#')
  483. while ((c=fgetc(f)) != '\n') /* skip first line */;
  484. status = do_protectedmain();
  485. }
  486. lua_closefile();
  487. return status;
  488. }
  489. /*
  490. ** Generate opcode stored on string and execute global statement. Return 0 on
  491. ** success or non 0 on error.
  492. */
  493. int lua_dostring (char *str)
  494. {
  495. int status;
  496. if (str == NULL)
  497. return 1;
  498. lua_openstring(str);
  499. status = do_protectedmain();
  500. lua_closestring();
  501. return status;
  502. }
  503. /*
  504. ** API: set a function as a fallback
  505. */
  506. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  507. {
  508. adjustC(1); /* one slot for the pseudo-function */
  509. stack[CLS_current.base].tag = LUA_T_CFUNCTION;
  510. stack[CLS_current.base].value.f = luaI_setfallback;
  511. lua_pushstring(name);
  512. lua_pushcfunction(fallback);
  513. if (do_protectedrun(1) == 0)
  514. return (Ref(top-1));
  515. else
  516. return LUA_NOOBJECT;
  517. }
  518. /*
  519. ** API: receives on the stack the table and the index.
  520. ** returns the value.
  521. */
  522. lua_Object lua_getsubscript (void)
  523. {
  524. adjustC(2);
  525. pushsubscript();
  526. CLS_current.base++; /* incorporate object in the stack */
  527. return (Ref(top-1));
  528. }
  529. #define MAX_C_BLOCKS 10
  530. static int numCblocks = 0;
  531. static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
  532. /*
  533. ** API: starts a new block
  534. */
  535. void lua_beginblock (void)
  536. {
  537. if (numCblocks >= MAX_C_BLOCKS)
  538. lua_error("`lua_beginblock': too many nested blocks");
  539. Cblocks[numCblocks] = CLS_current;
  540. numCblocks++;
  541. }
  542. /*
  543. ** API: ends a block
  544. */
  545. void lua_endblock (void)
  546. {
  547. --numCblocks;
  548. CLS_current = Cblocks[numCblocks];
  549. adjustC(0);
  550. }
  551. /*
  552. ** API: receives on the stack the table, the index, and the new value.
  553. */
  554. void lua_storesubscript (void)
  555. {
  556. adjustC(3);
  557. storesubscript();
  558. }
  559. /*
  560. ** API: creates a new table
  561. */
  562. lua_Object lua_createtable (void)
  563. {
  564. adjustC(0);
  565. avalue(top) = lua_createarray(0);
  566. tag(top) = LUA_T_ARRAY;
  567. incr_top;
  568. CLS_current.base++; /* incorporate object in the stack */
  569. return Ref(top-1);
  570. }
  571. /*
  572. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  573. ** 'number' must be 1 to get the first parameter.
  574. */
  575. lua_Object lua_getparam (int number)
  576. {
  577. if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
  578. /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
  579. stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
  580. return CLS_current.base-CLS_current.num+number;
  581. }
  582. int lua_isnumber (lua_Object object)
  583. {
  584. return (object != LUA_NOOBJECT) && (tonumber(Address(object)) == 0);
  585. }
  586. int lua_isstring (lua_Object object)
  587. {
  588. int t = lua_type(object);
  589. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  590. }
  591. int lua_isfunction (lua_Object object)
  592. {
  593. int t = lua_type(object);
  594. return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
  595. (t == LUA_T_MARK) || (t == LUA_T_CMARK);
  596. }
  597. /*
  598. ** Given an object handle, return its number value. On error, return 0.0.
  599. */
  600. real lua_getnumber (lua_Object object)
  601. {
  602. if (object == LUA_NOOBJECT) return 0.0;
  603. if (tonumber (Address(object))) return 0.0;
  604. else return (nvalue(Address(object)));
  605. }
  606. /*
  607. ** Given an object handle, return its string pointer. On error, return NULL.
  608. */
  609. char *lua_getstring (lua_Object object)
  610. {
  611. if (object == LUA_NOOBJECT) return NULL;
  612. if (tostring (Address(object))) return NULL;
  613. else return (svalue(Address(object)));
  614. }
  615. /*
  616. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  617. */
  618. lua_CFunction lua_getcfunction (lua_Object object)
  619. {
  620. if (object == LUA_NOOBJECT || ((tag(Address(object)) != LUA_T_CFUNCTION) &&
  621. (tag(Address(object)) != LUA_T_CMARK)))
  622. return NULL;
  623. else return (fvalue(Address(object)));
  624. }
  625. /*
  626. ** Given an object handle, return its user data. On error, return NULL.
  627. */
  628. void *lua_getuserdata (lua_Object object)
  629. {
  630. if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)
  631. return NULL;
  632. else return (uvalue(Address(object)));
  633. }
  634. lua_Object lua_getref (int ref)
  635. {
  636. Object *o = luaI_getref(ref);
  637. if (o == NULL)
  638. return LUA_NOOBJECT;
  639. adjustC(0);
  640. luaI_pushobject(o);
  641. CLS_current.base++; /* incorporate object in the stack */
  642. return Ref(top-1);
  643. }
  644. void lua_pushref (int ref)
  645. {
  646. Object *o = luaI_getref(ref);
  647. if (o == NULL)
  648. lua_error("access to invalid (possibly garbage collected) reference");
  649. luaI_pushobject(o);
  650. }
  651. int lua_ref (int lock)
  652. {
  653. adjustC(1);
  654. return luaI_ref(--top, lock);
  655. }
  656. /*
  657. ** Get a global object.
  658. */
  659. lua_Object lua_getglobal (char *name)
  660. {
  661. adjustC(0);
  662. getglobal(luaI_findsymbolbyname(name));
  663. CLS_current.base++; /* incorporate object in the stack */
  664. return Ref(top-1);
  665. }
  666. /*
  667. ** Store top of the stack at a global variable array field.
  668. */
  669. void lua_storeglobal (char *name)
  670. {
  671. Word n = luaI_findsymbolbyname(name);
  672. adjustC(1);
  673. s_object(n) = *(--top);
  674. }
  675. /*
  676. ** Push a nil object
  677. */
  678. void lua_pushnil (void)
  679. {
  680. tag(top) = LUA_T_NIL;
  681. incr_top;
  682. }
  683. /*
  684. ** Push an object (tag=number) to stack.
  685. */
  686. void lua_pushnumber (real n)
  687. {
  688. tag(top) = LUA_T_NUMBER; nvalue(top) = n;
  689. incr_top;
  690. }
  691. /*
  692. ** Push an object (tag=string) to stack.
  693. */
  694. void lua_pushstring (char *s)
  695. {
  696. if (s == NULL)
  697. tag(top) = LUA_T_NIL;
  698. else
  699. {
  700. tsvalue(top) = lua_createstring(s);
  701. tag(top) = LUA_T_STRING;
  702. }
  703. incr_top;
  704. }
  705. /*>>>>>>>>>#undef lua_pushliteral
  706. void lua_pushliteral(char *s) { lua_pushstring(s); }*/
  707. /*
  708. ** Push an object (tag=cfunction) to stack.
  709. */
  710. void lua_pushcfunction (lua_CFunction fn)
  711. {
  712. tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  713. incr_top;
  714. }
  715. /*
  716. ** Push an object (tag=userdata) to stack.
  717. */
  718. void lua_pushusertag (void *u, int tag)
  719. {
  720. if (tag < LUA_T_USERDATA)
  721. lua_error("invalid tag in `lua_pushusertag'");
  722. tag(top) = tag; uvalue(top) = u;
  723. incr_top;
  724. }
  725. /*
  726. ** Push an object on the stack.
  727. */
  728. void luaI_pushobject (Object *o)
  729. {
  730. *top = *o;
  731. incr_top;
  732. }
  733. /*
  734. ** Push a lua_Object on stack.
  735. */
  736. void lua_pushobject (lua_Object o)
  737. {
  738. if (o == LUA_NOOBJECT)
  739. lua_error("attempt to push a NOOBJECT");
  740. *top = *Address(o);
  741. if (tag(top) == LUA_T_MARK) tag(top) = LUA_T_FUNCTION;
  742. else if (tag(top) == LUA_T_CMARK) tag(top) = LUA_T_CFUNCTION;
  743. incr_top;
  744. }
  745. int lua_type (lua_Object o)
  746. {
  747. if (o == LUA_NOOBJECT)
  748. return LUA_T_NIL;
  749. else
  750. return tag(Address(o));
  751. }
  752. void luaI_gcFB (Object *o)
  753. {
  754. *top = *o;
  755. incr_top;
  756. callFB(FB_GC);
  757. }
  758. static void call_arith (char *op)
  759. {
  760. lua_pushstring(op);
  761. callFB(FB_ARITH);
  762. }
  763. static void comparison (lua_Type tag_less, lua_Type tag_equal,
  764. lua_Type tag_great, char *op)
  765. {
  766. Object *l = top-2;
  767. Object *r = top-1;
  768. int result;
  769. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  770. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  771. else if (tostring(l) || tostring(r))
  772. {
  773. lua_pushstring(op);
  774. callFB(FB_ORDER);
  775. return;
  776. }
  777. else
  778. result = strcmp(svalue(l), svalue(r));
  779. top--;
  780. nvalue(top-1) = 1;
  781. tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
  782. }
  783. static void adjust_varargs (StkId first_extra_arg)
  784. {
  785. Object arg;
  786. Object *firstelem = stack+first_extra_arg;
  787. int nvararg = top-firstelem;
  788. int i;
  789. if (nvararg < 0) nvararg = 0;
  790. avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
  791. tag(&arg) = LUA_T_ARRAY;
  792. for (i=0; i<nvararg; i++) {
  793. Object index;
  794. tag(&index) = LUA_T_NUMBER;
  795. nvalue(&index) = i+1;
  796. *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
  797. }
  798. /* store counter in field "n" */ {
  799. Object index, extra;
  800. tag(&index) = LUA_T_STRING;
  801. tsvalue(&index) = lua_createstring("n");
  802. tag(&extra) = LUA_T_NUMBER;
  803. nvalue(&extra) = nvararg;
  804. *(lua_hashdefine(avalue(&arg), &index)) = extra;
  805. }
  806. adjust_top(first_extra_arg);
  807. *top = arg; incr_top;
  808. }
  809. /*
  810. ** Execute the given opcode, until a RET. Parameters are between
  811. ** [stack+base,top). Returns n such that the the results are between
  812. ** [stack+n,top).
  813. */
  814. static StkId lua_execute (Byte *pc, StkId base)
  815. {
  816. if (lua_callhook)
  817. callHook (base, LUA_T_MARK, 0);
  818. while (1)
  819. {
  820. OpCode opcode;
  821. switch (opcode = (OpCode)*pc++)
  822. {
  823. case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
  824. case PUSH0: case PUSH1: case PUSH2:
  825. tag(top) = LUA_T_NUMBER;
  826. nvalue(top) = opcode-PUSH0;
  827. incr_top;
  828. break;
  829. case PUSHBYTE:
  830. tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  831. case PUSHWORD:
  832. {
  833. Word w;
  834. get_word(w,pc);
  835. tag(top) = LUA_T_NUMBER; nvalue(top) = w;
  836. incr_top;
  837. }
  838. break;
  839. case PUSHFLOAT:
  840. {
  841. real num;
  842. get_float(num,pc);
  843. tag(top) = LUA_T_NUMBER; nvalue(top) = num;
  844. incr_top;
  845. }
  846. break;
  847. case PUSHSTRING:
  848. {
  849. Word w;
  850. get_word(w,pc);
  851. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  852. incr_top;
  853. }
  854. break;
  855. case PUSHFUNCTION:
  856. {
  857. TFunc *f;
  858. get_code(f,pc);
  859. luaI_insertfunction(f); /* may take part in GC */
  860. top->tag = LUA_T_FUNCTION;
  861. top->value.tf = f;
  862. incr_top;
  863. }
  864. break;
  865. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  866. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  867. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  868. case PUSHLOCAL9:
  869. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  870. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  871. case PUSHGLOBAL:
  872. {
  873. Word w;
  874. get_word(w,pc);
  875. getglobal(w);
  876. }
  877. break;
  878. case PUSHINDEXED:
  879. pushsubscript();
  880. break;
  881. case PUSHSELF:
  882. {
  883. Object receiver = *(top-1);
  884. Word w;
  885. get_word(w,pc);
  886. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  887. incr_top;
  888. pushsubscript();
  889. *top = receiver;
  890. incr_top;
  891. break;
  892. }
  893. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  894. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  895. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  896. case STORELOCAL9:
  897. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  898. break;
  899. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  900. case STOREGLOBAL:
  901. {
  902. Word w;
  903. get_word(w,pc);
  904. s_object(w) = *(--top);
  905. }
  906. break;
  907. case STOREINDEXED0:
  908. storesubscript();
  909. break;
  910. case STOREINDEXED:
  911. {
  912. int n = *pc++;
  913. if (tag(top-3-n) != LUA_T_ARRAY)
  914. {
  915. lua_checkstack(top+2);
  916. *(top+1) = *(top-1);
  917. *(top) = *(top-2-n);
  918. *(top-1) = *(top-3-n);
  919. top += 2;
  920. callFB(FB_SETTABLE);
  921. }
  922. else
  923. {
  924. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  925. *h = *(top-1);
  926. top--;
  927. }
  928. }
  929. break;
  930. case STORELIST0:
  931. case STORELIST:
  932. {
  933. int m, n;
  934. Object *arr;
  935. if (opcode == STORELIST0) m = 0;
  936. else m = *(pc++) * FIELDS_PER_FLUSH;
  937. n = *(pc++);
  938. arr = top-n-1;
  939. while (n)
  940. {
  941. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  942. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  943. top--;
  944. n--;
  945. }
  946. }
  947. break;
  948. case STORERECORD:
  949. {
  950. int n = *(pc++);
  951. Object *arr = top-n-1;
  952. while (n)
  953. {
  954. Word w;
  955. get_word(w,pc);
  956. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  957. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  958. top--;
  959. n--;
  960. }
  961. }
  962. break;
  963. case ADJUST0:
  964. adjust_top(base);
  965. break;
  966. case ADJUST:
  967. adjust_top(base + *(pc++));
  968. break;
  969. case VARARGS:
  970. adjust_varargs(base + *(pc++));
  971. break;
  972. case CREATEARRAY:
  973. {
  974. Word size;
  975. get_word(size,pc);
  976. avalue(top) = lua_createarray(size);
  977. tag(top) = LUA_T_ARRAY;
  978. incr_top;
  979. }
  980. break;
  981. case EQOP:
  982. {
  983. int res = lua_equalObj(top-2, top-1);
  984. --top;
  985. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  986. nvalue(top-1) = 1;
  987. }
  988. break;
  989. case LTOP:
  990. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
  991. break;
  992. case LEOP:
  993. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
  994. break;
  995. case GTOP:
  996. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
  997. break;
  998. case GEOP:
  999. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
  1000. break;
  1001. case ADDOP:
  1002. {
  1003. Object *l = top-2;
  1004. Object *r = top-1;
  1005. if (tonumber(r) || tonumber(l))
  1006. call_arith("add");
  1007. else
  1008. {
  1009. nvalue(l) += nvalue(r);
  1010. --top;
  1011. }
  1012. }
  1013. break;
  1014. case SUBOP:
  1015. {
  1016. Object *l = top-2;
  1017. Object *r = top-1;
  1018. if (tonumber(r) || tonumber(l))
  1019. call_arith("sub");
  1020. else
  1021. {
  1022. nvalue(l) -= nvalue(r);
  1023. --top;
  1024. }
  1025. }
  1026. break;
  1027. case MULTOP:
  1028. {
  1029. Object *l = top-2;
  1030. Object *r = top-1;
  1031. if (tonumber(r) || tonumber(l))
  1032. call_arith("mul");
  1033. else
  1034. {
  1035. nvalue(l) *= nvalue(r);
  1036. --top;
  1037. }
  1038. }
  1039. break;
  1040. case DIVOP:
  1041. {
  1042. Object *l = top-2;
  1043. Object *r = top-1;
  1044. if (tonumber(r) || tonumber(l))
  1045. call_arith("div");
  1046. else
  1047. {
  1048. nvalue(l) /= nvalue(r);
  1049. --top;
  1050. }
  1051. }
  1052. break;
  1053. case POWOP:
  1054. call_arith("pow");
  1055. break;
  1056. case CONCOP:
  1057. {
  1058. Object *l = top-2;
  1059. Object *r = top-1;
  1060. if (tostring(r) || tostring(l))
  1061. callFB(FB_CONCAT);
  1062. else
  1063. {
  1064. tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  1065. --top;
  1066. }
  1067. }
  1068. break;
  1069. case MINUSOP:
  1070. if (tonumber(top-1))
  1071. {
  1072. tag(top) = LUA_T_NIL;
  1073. incr_top;
  1074. call_arith("unm");
  1075. }
  1076. else
  1077. nvalue(top-1) = - nvalue(top-1);
  1078. break;
  1079. case NOTOP:
  1080. tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1081. nvalue(top-1) = 1;
  1082. break;
  1083. case ONTJMP:
  1084. {
  1085. Word w;
  1086. get_word(w,pc);
  1087. if (tag(top-1) != LUA_T_NIL) pc += w;
  1088. }
  1089. break;
  1090. case ONFJMP:
  1091. {
  1092. Word w;
  1093. get_word(w,pc);
  1094. if (tag(top-1) == LUA_T_NIL) pc += w;
  1095. }
  1096. break;
  1097. case JMP:
  1098. {
  1099. Word w;
  1100. get_word(w,pc);
  1101. pc += w;
  1102. }
  1103. break;
  1104. case UPJMP:
  1105. {
  1106. Word w;
  1107. get_word(w,pc);
  1108. pc -= w;
  1109. }
  1110. break;
  1111. case IFFJMP:
  1112. {
  1113. Word w;
  1114. get_word(w,pc);
  1115. top--;
  1116. if (tag(top) == LUA_T_NIL) pc += w;
  1117. }
  1118. break;
  1119. case IFFUPJMP:
  1120. {
  1121. Word w;
  1122. get_word(w,pc);
  1123. top--;
  1124. if (tag(top) == LUA_T_NIL) pc -= w;
  1125. }
  1126. break;
  1127. case POP: --top; break;
  1128. case CALLFUNC:
  1129. {
  1130. int nParams = *(pc++);
  1131. int nResults = *(pc++);
  1132. StkId newBase = (top-stack)-nParams;
  1133. do_call(newBase, nResults);
  1134. }
  1135. break;
  1136. case RETCODE0:
  1137. case RETCODE:
  1138. if (lua_callhook)
  1139. callHook (base, LUA_T_MARK, 1);
  1140. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1141. case SETLINE:
  1142. {
  1143. Word line;
  1144. get_word(line,pc);
  1145. if ((stack+base-1)->tag != LUA_T_LINE)
  1146. {
  1147. /* open space for LINE value */
  1148. open_stack((top-stack)-base);
  1149. base++;
  1150. (stack+base-1)->tag = LUA_T_LINE;
  1151. }
  1152. (stack+base-1)->value.i = line;
  1153. if (lua_linehook)
  1154. lineHook (line);
  1155. break;
  1156. }
  1157. default:
  1158. lua_error ("internal error - opcode doesn't match");
  1159. }
  1160. }
  1161. }