opcode.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.89 1997/03/31 20:59:09 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 "luamem.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) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
  20. #define tostring(o) ((ttype(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 TObject initial_stack;
  27. static TObject *stackLimit = &initial_stack+1;
  28. static TObject *stack = &initial_stack;
  29. static TObject *top = &initial_stack;
  30. /* macros to convert from lua_Object to (TObject *) 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. TObject *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, TObject);
  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, TObject, 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 (TObject *obj)
  104. {
  105. float t;
  106. char c;
  107. if (ttype(obj) != LUA_T_STRING)
  108. return 1;
  109. else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
  110. {
  111. nvalue(obj) = t;
  112. ttype(obj) = LUA_T_NUMBER;
  113. return 0;
  114. }
  115. else
  116. return 2;
  117. }
  118. /*
  119. ** Convert, if possible, to a string ttype
  120. ** Return 0 in success or not 0 on error.
  121. */
  122. static int lua_tostring (TObject *obj)
  123. {
  124. if (ttype(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. ttype(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. TObject *nt;
  145. lua_checkstack(stack+newtop);
  146. nt = stack+newtop; /* warning: previous call may change stack */
  147. while (top < nt) ttype(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. TObject *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. static void callIM (TObject *f, int nParams, int nResults)
  217. {
  218. open_stack(nParams);
  219. *(top-nParams-1) = *f;
  220. do_call((top-stack)-nParams, nResults);
  221. }
  222. /*
  223. ** Call a function (C or Lua). The parameters must be on the stack,
  224. ** between [stack+base,top). The function to be called is at stack+base-1.
  225. ** When returns, the results are on the stack, between [stack+base-1,top).
  226. ** The number of results is nResults, unless nResults=MULT_RET.
  227. */
  228. static void do_call (StkId base, int nResults)
  229. {
  230. StkId firstResult;
  231. TObject *func = stack+base-1;
  232. int i;
  233. if (ttype(func) == LUA_T_CFUNCTION) {
  234. ttype(func) = LUA_T_CMARK;
  235. firstResult = callC(fvalue(func), base);
  236. }
  237. else if (ttype(func) == LUA_T_FUNCTION) {
  238. ttype(func) = LUA_T_MARK;
  239. firstResult = lua_execute(func->value.tf->code, base);
  240. }
  241. else { /* func is not a function */
  242. /* Check the fallback for invalid functions */
  243. TObject *im = luaI_getimbyObj(func, IM_FUNCTION);
  244. if (ttype(im) == LUA_T_NIL)
  245. lua_error("call expression not a function");
  246. open_stack((top-stack)-(base-1));
  247. stack[base-1] = *im;
  248. do_call(base, nResults);
  249. return;
  250. }
  251. /* adjust the number of results */
  252. if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
  253. adjust_top(firstResult+nResults);
  254. /* move results to base-1 (to erase parameters and function) */
  255. base--;
  256. nResults = top - (stack+firstResult); /* actual number of results */
  257. for (i=0; i<nResults; i++)
  258. *(stack+base+i) = *(stack+firstResult+i);
  259. top -= firstResult-base;
  260. }
  261. /*
  262. ** Function to index a table. Receives the table at top-2 and the index
  263. ** at top-1.
  264. */
  265. static void pushsubscript (void)
  266. {
  267. int tg = luaI_tag(top-2);
  268. TObject *im = luaI_getim(tg, IM_GETTABLE);
  269. if (ttype(top-2) == LUA_T_ARRAY && ttype(im) == LUA_T_NIL) {
  270. TObject *h = lua_hashget(avalue(top-2), top-1);
  271. if (h != NULL && ttype(h) != LUA_T_NIL) {
  272. --top;
  273. *(top-1) = *h;
  274. }
  275. else if (ttype(im=luaI_getim(tg, IM_INDEX)) != LUA_T_NIL)
  276. callIM(im, 2, 1);
  277. else {
  278. --top;
  279. ttype(top-1) = LUA_T_NIL;
  280. }
  281. }
  282. else { /* object is not a table, and/or has a specific "gettable" method */
  283. if (im)
  284. callIM(im, 2, 1);
  285. else
  286. lua_error("indexed expression not a table");
  287. }
  288. }
  289. lua_Object lua_basicindex (void)
  290. {
  291. adjustC(2);
  292. if (ttype(top-2) != LUA_T_ARRAY)
  293. lua_error("indexed expression not a table in basic indexing");
  294. else {
  295. TObject *h = lua_hashget(avalue(top-2), top-1);
  296. --top;
  297. if (h != NULL)
  298. *(top-1) = *h;
  299. else
  300. ttype(top-1) = LUA_T_NIL;
  301. }
  302. CLS_current.base++; /* incorporate object in the stack */
  303. return (Ref(top-1));
  304. }
  305. /*
  306. ** Function to store indexed based on values at the top
  307. ** mode = 0: basic store (without internal methods)
  308. ** mode = 1: normal store (with internal methods)
  309. ** mode = 2: "deep stack" store (with internal methods)
  310. */
  311. static void storesubscript (TObject *t, int mode)
  312. {
  313. TObject *im = (mode == 0) ? NULL : luaI_getimbyObj(t, IM_SETTABLE);
  314. if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
  315. TObject *h = lua_hashdefine(avalue(t), t+1);
  316. *h = *(top-1);
  317. top -= (mode == 2) ? 1 : 3;
  318. }
  319. else { /* object is not a table, and/or has a specific "settable" method */
  320. if (im && ttype(im) != LUA_T_NIL) {
  321. if (mode == 2) {
  322. lua_checkstack(top+2);
  323. *(top+1) = *(top-1);
  324. *(top) = *(t+1);
  325. *(top-1) = *t;
  326. top += 2;
  327. }
  328. callIM(im, 3, 0);
  329. }
  330. else
  331. lua_error("indexed expression not a table");
  332. }
  333. }
  334. static void getglobal (Word n)
  335. {
  336. TObject *value = &lua_table[n].object;
  337. TObject *im = luaI_getimbyObj(value, IM_GETGLOBAL);
  338. if (ttype(im) == LUA_T_NIL) { /* default behavior */
  339. *top = *value;
  340. incr_top;
  341. }
  342. else {
  343. ttype(top) = LUA_T_STRING;
  344. tsvalue(top) = lua_table[n].varname;
  345. incr_top;
  346. *top = *value;
  347. incr_top;
  348. callIM(im, 2, 1);
  349. }
  350. }
  351. /*
  352. ** Traverse all objects on stack
  353. */
  354. void lua_travstack (int (*fn)(TObject *))
  355. {
  356. TObject *o;
  357. for (o = top-1; o >= stack; o--)
  358. fn (o);
  359. }
  360. /*
  361. ** Error messages and debug functions
  362. */
  363. static void lua_message (char *s)
  364. {
  365. TObject *im = luaI_geterrorim();
  366. if (ttype(im) == LUA_T_NIL)
  367. fprintf(stderr, "lua: %s\n", s);
  368. else {
  369. lua_pushstring(s);
  370. callIM(im, 1, 0);
  371. }
  372. }
  373. /*
  374. ** Reports an error, and jumps up to the available recover label
  375. */
  376. void lua_error (char *s)
  377. {
  378. if (s) lua_message(s);
  379. if (errorJmp)
  380. longjmp(*errorJmp, 1);
  381. else
  382. {
  383. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  384. exit(1);
  385. }
  386. }
  387. lua_Function lua_stackedfunction (int level)
  388. {
  389. StkId i;
  390. for (i = (top-1)-stack; i>=0; i--)
  391. if (stack[i].ttype == LUA_T_MARK || stack[i].ttype == LUA_T_CMARK)
  392. if (level-- == 0)
  393. return Ref(stack+i);
  394. return LUA_NOOBJECT;
  395. }
  396. int lua_currentline (lua_Function func)
  397. {
  398. TObject *f = Address(func);
  399. return (f+1 < top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
  400. }
  401. lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
  402. {
  403. TObject *f = luaI_Address(func);
  404. *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  405. if (*name)
  406. {
  407. /* if "*name", there must be a LUA_T_LINE */
  408. /* therefore, f+2 points to function base */
  409. return Ref((f+2)+(local_number-1));
  410. }
  411. else
  412. return LUA_NOOBJECT;
  413. }
  414. int lua_setlocal (lua_Function func, int local_number)
  415. {
  416. TObject *f = Address(func);
  417. char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  418. adjustC(1);
  419. --top;
  420. if (name)
  421. {
  422. /* if "name", there must be a LUA_T_LINE */
  423. /* therefore, f+2 points to function base */
  424. *((f+2)+(local_number-1)) = *top;
  425. return 1;
  426. }
  427. else
  428. return 0;
  429. }
  430. /*
  431. ** Call the function at CLS_current.base, and incorporate results on
  432. ** the Lua2C structure.
  433. */
  434. static void do_callinc (int nResults)
  435. {
  436. do_call(CLS_current.base+1, nResults);
  437. CLS_current.num = (top-stack) - CLS_current.base; /* number of results */
  438. CLS_current.base += CLS_current.num; /* incorporate results on the stack */
  439. }
  440. static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
  441. {
  442. adjustC(nParams);
  443. open_stack((top-stack)-CLS_current.base);
  444. stack[CLS_current.base].ttype = LUA_T_CFUNCTION;
  445. stack[CLS_current.base].value.f = f;
  446. do_callinc(nResults);
  447. }
  448. /*
  449. ** Execute a protected call. Assumes that function is at CLS_current.base and
  450. ** parameters are on top of it. Leave nResults on the stack.
  451. */
  452. static int do_protectedrun (int nResults)
  453. {
  454. jmp_buf myErrorJmp;
  455. int status;
  456. struct C_Lua_Stack oldCLS = CLS_current;
  457. jmp_buf *oldErr = errorJmp;
  458. errorJmp = &myErrorJmp;
  459. if (setjmp(myErrorJmp) == 0) {
  460. do_callinc(nResults);
  461. status = 0;
  462. }
  463. else { /* an error occurred: restore CLS_current and top */
  464. CLS_current = oldCLS;
  465. top = stack+CLS_current.base;
  466. status = 1;
  467. }
  468. errorJmp = oldErr;
  469. return status;
  470. }
  471. int luaI_dorun (TFunc *tf)
  472. {
  473. int status;
  474. adjustC(1); /* one slot for the pseudo-function */
  475. stack[CLS_current.base].ttype = LUA_T_FUNCTION;
  476. stack[CLS_current.base].value.tf = tf;
  477. status = do_protectedrun(MULT_RET);
  478. return status;
  479. }
  480. static int do_protectedmain (void)
  481. {
  482. TFunc tf;
  483. int status;
  484. jmp_buf myErrorJmp;
  485. jmp_buf *oldErr = errorJmp;
  486. errorJmp = &myErrorJmp;
  487. luaI_initTFunc(&tf);
  488. tf.fileName = lua_parsedfile;
  489. if (setjmp(myErrorJmp) == 0)
  490. {
  491. lua_parse(&tf);
  492. status = luaI_dorun(&tf);
  493. }
  494. else
  495. {
  496. status = 1;
  497. adjustC(0); /* erase extra slot */
  498. }
  499. errorJmp = oldErr;
  500. luaI_free(tf.code);
  501. return status;
  502. }
  503. /*
  504. ** Execute the given lua function. Return 0 on success or 1 on error.
  505. */
  506. int lua_callfunction (lua_Object function)
  507. {
  508. if (function == LUA_NOOBJECT)
  509. return 1;
  510. else
  511. {
  512. open_stack((top-stack)-CLS_current.base);
  513. stack[CLS_current.base] = *Address(function);
  514. return do_protectedrun (MULT_RET);
  515. }
  516. }
  517. int lua_call (char *funcname)
  518. {
  519. Word n = luaI_findsymbolbyname(funcname);
  520. open_stack((top-stack)-CLS_current.base);
  521. stack[CLS_current.base] = s_object(n);
  522. return do_protectedrun(MULT_RET);
  523. }
  524. /*
  525. ** Open file, generate opcode and execute global statement. Return 0 on
  526. ** success or non 0 on error.
  527. */
  528. int lua_dofile (char *filename)
  529. {
  530. int status;
  531. int c;
  532. FILE *f = lua_openfile(filename);
  533. if (f == NULL)
  534. return 2;
  535. c = fgetc(f);
  536. ungetc(c, f);
  537. if (c == ID_CHUNK) {
  538. f = freopen(filename, "rb", f); /* set binary mode */
  539. status = luaI_undump(f);
  540. }
  541. else {
  542. if (c == '#')
  543. while ((c=fgetc(f)) != '\n') /* skip first line */;
  544. status = do_protectedmain();
  545. }
  546. lua_closefile();
  547. return status;
  548. }
  549. /*
  550. ** Generate opcode stored on string and execute global statement. Return 0 on
  551. ** success or non 0 on error.
  552. */
  553. int lua_dostring (char *str)
  554. {
  555. int status;
  556. if (str == NULL)
  557. return 1;
  558. lua_openstring(str);
  559. status = do_protectedmain();
  560. lua_closestring();
  561. return status;
  562. }
  563. /*
  564. ** API: set a function as a fallback
  565. */
  566. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  567. {
  568. lua_pushstring(name);
  569. lua_pushcfunction(fallback);
  570. do_unprotectedrun(luaI_setfallback, 2, 1);
  571. return (Ref(top-1));
  572. }
  573. void lua_setintmethod (int tag, char *event, lua_CFunction method)
  574. {
  575. lua_pushnumber(tag);
  576. lua_pushstring(event);
  577. if (method)
  578. lua_pushcfunction (method);
  579. else
  580. lua_pushnil();
  581. do_unprotectedrun(luaI_setintmethod, 3, 0);
  582. }
  583. void lua_seterrormethod (lua_CFunction method)
  584. {
  585. lua_pushcfunction (method);
  586. do_unprotectedrun(luaI_seterrormethod, 1, 0);
  587. }
  588. /*
  589. ** API: receives on the stack the table and the index.
  590. ** returns the value.
  591. */
  592. lua_Object lua_getsubscript (void)
  593. {
  594. adjustC(2);
  595. pushsubscript();
  596. CLS_current.base++; /* incorporate object in the stack */
  597. return (Ref(top-1));
  598. }
  599. #define MAX_C_BLOCKS 10
  600. static int numCblocks = 0;
  601. static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
  602. /*
  603. ** API: starts a new block
  604. */
  605. void lua_beginblock (void)
  606. {
  607. if (numCblocks >= MAX_C_BLOCKS)
  608. lua_error("`lua_beginblock': too many nested blocks");
  609. Cblocks[numCblocks] = CLS_current;
  610. numCblocks++;
  611. }
  612. /*
  613. ** API: ends a block
  614. */
  615. void lua_endblock (void)
  616. {
  617. --numCblocks;
  618. CLS_current = Cblocks[numCblocks];
  619. adjustC(0);
  620. }
  621. void lua_settag (int tag)
  622. {
  623. adjustC(1);
  624. luaI_settag(tag, --top);
  625. }
  626. /*
  627. ** API: receives on the stack the table, the index, and the new value.
  628. */
  629. void lua_storesubscript (void)
  630. {
  631. adjustC(3);
  632. storesubscript(top-3, 1);
  633. }
  634. void lua_basicstoreindex (void)
  635. {
  636. adjustC(3);
  637. storesubscript(top-3, 0);
  638. }
  639. /*
  640. ** API: creates a new table
  641. */
  642. lua_Object lua_createtable (void)
  643. {
  644. adjustC(0);
  645. avalue(top) = lua_createarray(0);
  646. ttype(top) = LUA_T_ARRAY;
  647. incr_top;
  648. CLS_current.base++; /* incorporate object in the stack */
  649. return Ref(top-1);
  650. }
  651. /*
  652. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  653. ** 'number' must be 1 to get the first parameter.
  654. */
  655. lua_Object lua_lua2C (int number)
  656. {
  657. if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
  658. /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
  659. stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
  660. return CLS_current.base-CLS_current.num+number;
  661. }
  662. int lua_isnil (lua_Object o)
  663. {
  664. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
  665. }
  666. int lua_istable (lua_Object o)
  667. {
  668. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
  669. }
  670. int lua_isuserdata (lua_Object o)
  671. {
  672. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
  673. }
  674. int lua_iscfunction (lua_Object o)
  675. {
  676. int t = lua_tag(o);
  677. return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
  678. }
  679. int lua_isnumber (lua_Object o)
  680. {
  681. return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
  682. }
  683. int lua_isstring (lua_Object o)
  684. {
  685. int t = lua_tag(o);
  686. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  687. }
  688. int lua_isfunction (lua_Object o)
  689. {
  690. int t = lua_tag(o);
  691. return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
  692. (t == LUA_T_MARK) || (t == LUA_T_CMARK);
  693. }
  694. /*
  695. ** Given an object handle, return its number value. On error, return 0.0.
  696. */
  697. real lua_getnumber (lua_Object object)
  698. {
  699. if (object == LUA_NOOBJECT) return 0.0;
  700. if (tonumber (Address(object))) return 0.0;
  701. else return (nvalue(Address(object)));
  702. }
  703. /*
  704. ** Given an object handle, return its string pointer. On error, return NULL.
  705. */
  706. char *lua_getstring (lua_Object object)
  707. {
  708. if (object == LUA_NOOBJECT) return NULL;
  709. if (tostring (Address(object))) return NULL;
  710. else return (svalue(Address(object)));
  711. }
  712. void *lua_getbinarydata (lua_Object object)
  713. {
  714. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  715. lua_error("getbinarydata: object is not binary data");
  716. return svalue(Address(object));
  717. }
  718. int lua_getbindatasize (lua_Object object)
  719. {
  720. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  721. return 0;
  722. else return (Address(object))->value.ts->size;
  723. }
  724. /*
  725. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  726. */
  727. lua_CFunction lua_getcfunction (lua_Object object)
  728. {
  729. if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) &&
  730. (ttype(Address(object)) != LUA_T_CMARK)))
  731. return NULL;
  732. else return (fvalue(Address(object)));
  733. }
  734. lua_Object lua_getref (int ref)
  735. {
  736. TObject *o = luaI_getref(ref);
  737. if (o == NULL)
  738. return LUA_NOOBJECT;
  739. adjustC(0);
  740. luaI_pushobject(o);
  741. CLS_current.base++; /* incorporate object in the stack */
  742. return Ref(top-1);
  743. }
  744. void lua_pushref (int ref)
  745. {
  746. TObject *o = luaI_getref(ref);
  747. if (o == NULL)
  748. lua_error("access to invalid (possibly garbage collected) reference");
  749. luaI_pushobject(o);
  750. }
  751. int lua_ref (int lock)
  752. {
  753. adjustC(1);
  754. return luaI_ref(--top, lock);
  755. }
  756. /*
  757. ** Get a global object.
  758. */
  759. lua_Object lua_getglobal (char *name)
  760. {
  761. adjustC(0);
  762. getglobal(luaI_findsymbolbyname(name));
  763. CLS_current.base++; /* incorporate object in the stack */
  764. return Ref(top-1);
  765. }
  766. lua_Object lua_basicgetglobal (char *name)
  767. {
  768. adjustC(0);
  769. *top = lua_table[luaI_findsymbolbyname(name)].object;
  770. incr_top;
  771. CLS_current.base++; /* incorporate object in the stack */
  772. return Ref(top-1);
  773. }
  774. /*
  775. ** Store top of the stack at a global variable array field.
  776. */
  777. static void setglobal (Word n)
  778. {
  779. TObject *oldvalue = &lua_table[n].object;
  780. TObject *im = luaI_getimbyObj(oldvalue, IM_SETGLOBAL);
  781. if (ttype(im) == LUA_T_NIL) /* default behavior */
  782. s_object(n) = *(--top);
  783. else {
  784. TObject newvalue = *(top-1);
  785. ttype(top-1) = LUA_T_STRING;
  786. tsvalue(top-1) = lua_table[n].varname;
  787. *top = *oldvalue;
  788. incr_top;
  789. *top = newvalue;
  790. incr_top;
  791. callIM(im, 3, 0);
  792. }
  793. }
  794. void lua_setglobal (char *name)
  795. {
  796. adjustC(1);
  797. setglobal(luaI_findsymbolbyname(name));
  798. }
  799. void lua_basicsetglobal (char *name)
  800. {
  801. Word n = luaI_findsymbolbyname(name);
  802. adjustC(1);
  803. s_object(n) = *(--top);
  804. }
  805. /*
  806. ** Push a nil object
  807. */
  808. void lua_pushnil (void)
  809. {
  810. ttype(top) = LUA_T_NIL;
  811. incr_top;
  812. }
  813. /*
  814. ** Push an object (ttype=number) to stack.
  815. */
  816. void lua_pushnumber (real n)
  817. {
  818. ttype(top) = LUA_T_NUMBER; nvalue(top) = n;
  819. incr_top;
  820. }
  821. /*
  822. ** Push an object (ttype=string) to stack.
  823. */
  824. void lua_pushstring (char *s)
  825. {
  826. if (s == NULL)
  827. ttype(top) = LUA_T_NIL;
  828. else
  829. {
  830. tsvalue(top) = lua_createstring(s);
  831. ttype(top) = LUA_T_STRING;
  832. }
  833. incr_top;
  834. }
  835. /*>>>>>>>>>#undef lua_pushliteral
  836. void lua_pushliteral(char *s) { lua_pushstring(s); }*/
  837. /*
  838. ** Push an object (ttype=cfunction) to stack.
  839. */
  840. void lua_pushcfunction (lua_CFunction fn)
  841. {
  842. ttype(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  843. incr_top;
  844. }
  845. void lua_pushbinarydata (void *buff, int size, int tag)
  846. {
  847. if (buff == NULL)
  848. ttype(top) = LUA_T_NIL;
  849. else {
  850. tsvalue(top) = luaI_createuserdata(buff, size, tag);
  851. ttype(top) = LUA_T_USERDATA;
  852. }
  853. incr_top;
  854. }
  855. /*
  856. ** Push an object (ttype=userdata) to stack.
  857. */
  858. void lua_pushusertag (void *u, int tag)
  859. {
  860. if (luaI_typetag(tag) != LUA_T_USERDATA)
  861. lua_error("invalid tag in `lua_pushusertag'");
  862. lua_pushbinarydata(&u, sizeof(void *), tag);
  863. }
  864. /*
  865. ** Push an object on the stack.
  866. */
  867. void luaI_pushobject (TObject *o)
  868. {
  869. *top = *o;
  870. incr_top;
  871. }
  872. /*
  873. ** Push a lua_Object on stack.
  874. */
  875. void lua_pushobject (lua_Object o)
  876. {
  877. if (o == LUA_NOOBJECT)
  878. lua_error("attempt to push a NOOBJECT");
  879. *top = *Address(o);
  880. if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
  881. else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
  882. incr_top;
  883. }
  884. int lua_tag (lua_Object o)
  885. {
  886. return (o == LUA_NOOBJECT) ? LUA_T_NIL : luaI_tag(Address(o));
  887. }
  888. void luaI_gcIM (TObject *o)
  889. {
  890. TObject *im = luaI_getimbyObj(o, IM_GC);
  891. if (ttype(im) != LUA_T_NIL) {
  892. *top = *o;
  893. incr_top;
  894. callIM(im, 1, 0);
  895. }
  896. }
  897. static void call_arith (IMS event)
  898. {
  899. TObject *im = luaI_getimbyObj(top-2, event); /* try first operand */
  900. if (ttype(im) == LUA_T_NIL) {
  901. im = luaI_getimbyObj(top-1, event); /* try second operand */
  902. if (ttype(im) == LUA_T_NIL) {
  903. im = luaI_getim(0, event); /* try a 'global' i.m. */
  904. if (ttype(im) == LUA_T_NIL)
  905. lua_error("unexpected type at conversion to number");
  906. }
  907. }
  908. lua_pushstring(luaI_eventname[event]);
  909. callIM(im, 3, 1);
  910. }
  911. static void concim (TObject *o)
  912. {
  913. TObject *im = luaI_getimbyObj(o, IM_CONCAT);
  914. if (ttype(im) == LUA_T_NIL)
  915. lua_error("unexpected type at conversion to string");
  916. callIM(im, 2, 1);
  917. }
  918. static void ordim (TObject *o, IMS event)
  919. {
  920. TObject *im = luaI_getimbyObj(o, event);
  921. if (ttype(im) == LUA_T_NIL)
  922. lua_error("unexpected type at comparison");
  923. lua_pushstring(luaI_eventname[event]);
  924. callIM(im, 3, 1);
  925. }
  926. static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
  927. lua_Type ttype_great, IMS op)
  928. {
  929. TObject *l = top-2;
  930. TObject *r = top-1;
  931. int result;
  932. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  933. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  934. else if (tostring(l)) {
  935. ordim(l, op);
  936. return;
  937. }
  938. else if (tostring(r)) {
  939. ordim(r, op);
  940. return;
  941. }
  942. else
  943. result = strcmp(svalue(l), svalue(r));
  944. top--;
  945. nvalue(top-1) = 1;
  946. ttype(top-1) = (result < 0) ? ttype_less :
  947. (result == 0) ? ttype_equal : ttype_great;
  948. }
  949. static void adjust_varargs (StkId first_extra_arg)
  950. {
  951. TObject arg;
  952. TObject *firstelem = stack+first_extra_arg;
  953. int nvararg = top-firstelem;
  954. int i;
  955. if (nvararg < 0) nvararg = 0;
  956. avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
  957. ttype(&arg) = LUA_T_ARRAY;
  958. for (i=0; i<nvararg; i++) {
  959. TObject index;
  960. ttype(&index) = LUA_T_NUMBER;
  961. nvalue(&index) = i+1;
  962. *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
  963. }
  964. /* store counter in field "n" */ {
  965. TObject index, extra;
  966. ttype(&index) = LUA_T_STRING;
  967. tsvalue(&index) = lua_createstring("n");
  968. ttype(&extra) = LUA_T_NUMBER;
  969. nvalue(&extra) = nvararg;
  970. *(lua_hashdefine(avalue(&arg), &index)) = extra;
  971. }
  972. adjust_top(first_extra_arg);
  973. *top = arg; incr_top;
  974. }
  975. /*
  976. ** Execute the given opcode, until a RET. Parameters are between
  977. ** [stack+base,top). Returns n such that the the results are between
  978. ** [stack+n,top).
  979. */
  980. static StkId lua_execute (Byte *pc, StkId base)
  981. {
  982. if (lua_callhook)
  983. callHook (base, LUA_T_MARK, 0);
  984. while (1)
  985. {
  986. OpCode opcode;
  987. switch (opcode = (OpCode)*pc++)
  988. {
  989. case PUSHNIL: ttype(top) = LUA_T_NIL; incr_top; break;
  990. case PUSH0: case PUSH1: case PUSH2:
  991. ttype(top) = LUA_T_NUMBER;
  992. nvalue(top) = opcode-PUSH0;
  993. incr_top;
  994. break;
  995. case PUSHBYTE:
  996. ttype(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  997. case PUSHWORD:
  998. {
  999. Word w;
  1000. get_word(w,pc);
  1001. ttype(top) = LUA_T_NUMBER; nvalue(top) = w;
  1002. incr_top;
  1003. }
  1004. break;
  1005. case PUSHFLOAT:
  1006. {
  1007. real num;
  1008. get_float(num,pc);
  1009. ttype(top) = LUA_T_NUMBER; nvalue(top) = num;
  1010. incr_top;
  1011. }
  1012. break;
  1013. case PUSHSTRING:
  1014. {
  1015. Word w;
  1016. get_word(w,pc);
  1017. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1018. incr_top;
  1019. }
  1020. break;
  1021. case PUSHFUNCTION:
  1022. {
  1023. TFunc *f;
  1024. get_code(f,pc);
  1025. luaI_insertfunction(f); /* may take part in GC */
  1026. top->ttype = LUA_T_FUNCTION;
  1027. top->value.tf = f;
  1028. incr_top;
  1029. }
  1030. break;
  1031. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  1032. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  1033. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  1034. case PUSHLOCAL9:
  1035. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  1036. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  1037. case PUSHGLOBAL:
  1038. {
  1039. Word w;
  1040. get_word(w,pc);
  1041. getglobal(w);
  1042. }
  1043. break;
  1044. case PUSHINDEXED:
  1045. pushsubscript();
  1046. break;
  1047. case PUSHSELF:
  1048. {
  1049. TObject receiver = *(top-1);
  1050. Word w;
  1051. get_word(w,pc);
  1052. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1053. incr_top;
  1054. pushsubscript();
  1055. *top = receiver;
  1056. incr_top;
  1057. break;
  1058. }
  1059. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  1060. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  1061. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  1062. case STORELOCAL9:
  1063. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  1064. break;
  1065. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  1066. case STOREGLOBAL:
  1067. {
  1068. Word w;
  1069. get_word(w,pc);
  1070. setglobal(w);
  1071. }
  1072. break;
  1073. case STOREINDEXED0:
  1074. storesubscript(top-3, 1);
  1075. break;
  1076. case STOREINDEXED: {
  1077. int n = *pc++;
  1078. storesubscript(top-3-n, 2);
  1079. break;
  1080. }
  1081. case STORELIST0:
  1082. case STORELIST:
  1083. {
  1084. int m, n;
  1085. TObject *arr;
  1086. if (opcode == STORELIST0) m = 0;
  1087. else m = *(pc++) * FIELDS_PER_FLUSH;
  1088. n = *(pc++);
  1089. arr = top-n-1;
  1090. while (n)
  1091. {
  1092. ttype(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  1093. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1094. top--;
  1095. n--;
  1096. }
  1097. }
  1098. break;
  1099. case STORERECORD: /* opcode obsolete: supersed by STOREMAP */
  1100. {
  1101. int n = *(pc++);
  1102. TObject *arr = top-n-1;
  1103. while (n)
  1104. {
  1105. Word w;
  1106. get_word(w,pc);
  1107. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1108. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1109. top--;
  1110. n--;
  1111. }
  1112. }
  1113. break;
  1114. case STOREMAP: {
  1115. int n = *(pc++);
  1116. TObject *arr = top-(2*n)-1;
  1117. while (n--) {
  1118. *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
  1119. top-=2;
  1120. }
  1121. }
  1122. break;
  1123. case ADJUST0:
  1124. adjust_top(base);
  1125. break;
  1126. case ADJUST:
  1127. adjust_top(base + *(pc++));
  1128. break;
  1129. case VARARGS:
  1130. adjust_varargs(base + *(pc++));
  1131. break;
  1132. case CREATEARRAY:
  1133. {
  1134. Word size;
  1135. get_word(size,pc);
  1136. avalue(top) = lua_createarray(size);
  1137. ttype(top) = LUA_T_ARRAY;
  1138. incr_top;
  1139. }
  1140. break;
  1141. case EQOP:
  1142. {
  1143. int res = lua_equalObj(top-2, top-1);
  1144. --top;
  1145. ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  1146. nvalue(top-1) = 1;
  1147. }
  1148. break;
  1149. case LTOP:
  1150. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  1151. break;
  1152. case LEOP:
  1153. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
  1154. break;
  1155. case GTOP:
  1156. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
  1157. break;
  1158. case GEOP:
  1159. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
  1160. break;
  1161. case ADDOP:
  1162. {
  1163. TObject *l = top-2;
  1164. TObject *r = top-1;
  1165. if (tonumber(r) || tonumber(l))
  1166. call_arith(IM_ADD);
  1167. else
  1168. {
  1169. nvalue(l) += nvalue(r);
  1170. --top;
  1171. }
  1172. }
  1173. break;
  1174. case SUBOP:
  1175. {
  1176. TObject *l = top-2;
  1177. TObject *r = top-1;
  1178. if (tonumber(r) || tonumber(l))
  1179. call_arith(IM_SUB);
  1180. else
  1181. {
  1182. nvalue(l) -= nvalue(r);
  1183. --top;
  1184. }
  1185. }
  1186. break;
  1187. case MULTOP:
  1188. {
  1189. TObject *l = top-2;
  1190. TObject *r = top-1;
  1191. if (tonumber(r) || tonumber(l))
  1192. call_arith(IM_MUL);
  1193. else
  1194. {
  1195. nvalue(l) *= nvalue(r);
  1196. --top;
  1197. }
  1198. }
  1199. break;
  1200. case DIVOP:
  1201. {
  1202. TObject *l = top-2;
  1203. TObject *r = top-1;
  1204. if (tonumber(r) || tonumber(l))
  1205. call_arith(IM_DIV);
  1206. else
  1207. {
  1208. nvalue(l) /= nvalue(r);
  1209. --top;
  1210. }
  1211. }
  1212. break;
  1213. case POWOP:
  1214. call_arith(IM_POW);
  1215. break;
  1216. case CONCOP: {
  1217. TObject *l = top-2;
  1218. TObject *r = top-1;
  1219. if (tostring(l)) /* first argument is not a string */
  1220. concim(l);
  1221. else if (tostring(r)) /* second argument is not a string */
  1222. concim(r);
  1223. else {
  1224. tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
  1225. --top;
  1226. }
  1227. }
  1228. break;
  1229. case MINUSOP:
  1230. if (tonumber(top-1))
  1231. {
  1232. ttype(top) = LUA_T_NIL;
  1233. incr_top;
  1234. call_arith(IM_UNM);
  1235. }
  1236. else
  1237. nvalue(top-1) = - nvalue(top-1);
  1238. break;
  1239. case NOTOP:
  1240. ttype(top-1) = (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1241. nvalue(top-1) = 1;
  1242. break;
  1243. case ONTJMP:
  1244. {
  1245. Word w;
  1246. get_word(w,pc);
  1247. if (ttype(top-1) != LUA_T_NIL) pc += w;
  1248. }
  1249. break;
  1250. case ONFJMP:
  1251. {
  1252. Word w;
  1253. get_word(w,pc);
  1254. if (ttype(top-1) == LUA_T_NIL) pc += w;
  1255. }
  1256. break;
  1257. case JMP:
  1258. {
  1259. Word w;
  1260. get_word(w,pc);
  1261. pc += w;
  1262. }
  1263. break;
  1264. case UPJMP:
  1265. {
  1266. Word w;
  1267. get_word(w,pc);
  1268. pc -= w;
  1269. }
  1270. break;
  1271. case IFFJMP:
  1272. {
  1273. Word w;
  1274. get_word(w,pc);
  1275. top--;
  1276. if (ttype(top) == LUA_T_NIL) pc += w;
  1277. }
  1278. break;
  1279. case IFFUPJMP:
  1280. {
  1281. Word w;
  1282. get_word(w,pc);
  1283. top--;
  1284. if (ttype(top) == LUA_T_NIL) pc -= w;
  1285. }
  1286. break;
  1287. case POP: --top; break;
  1288. case CALLFUNC:
  1289. {
  1290. int nParams = *(pc++);
  1291. int nResults = *(pc++);
  1292. StkId newBase = (top-stack)-nParams;
  1293. do_call(newBase, nResults);
  1294. }
  1295. break;
  1296. case RETCODE0:
  1297. case RETCODE:
  1298. if (lua_callhook)
  1299. callHook (base, LUA_T_MARK, 1);
  1300. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1301. case SETLINE:
  1302. {
  1303. Word line;
  1304. get_word(line,pc);
  1305. if ((stack+base-1)->ttype != LUA_T_LINE)
  1306. {
  1307. /* open space for LINE value */
  1308. open_stack((top-stack)-base);
  1309. base++;
  1310. (stack+base-1)->ttype = LUA_T_LINE;
  1311. }
  1312. (stack+base-1)->value.i = line;
  1313. if (lua_linehook)
  1314. lineHook (line);
  1315. break;
  1316. }
  1317. default:
  1318. lua_error ("internal error - opcode doesn't match");
  1319. }
  1320. }
  1321. }