opcode.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.92 1997/04/02 22:52:42 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 (ttype(im) != LUA_T_NIL)
  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_getintmethod (int tag, char *event)
  574. {
  575. lua_pushnumber(tag);
  576. lua_pushstring(event);
  577. do_unprotectedrun(luaI_getintmethod, 2, 1);
  578. }
  579. void lua_setintmethod (int tag, char *event, lua_CFunction method)
  580. {
  581. lua_pushnumber(tag);
  582. lua_pushstring(event);
  583. if (method)
  584. lua_pushcfunction (method);
  585. else
  586. lua_pushnil();
  587. do_unprotectedrun(luaI_setintmethod, 3, 0);
  588. }
  589. void lua_seterrormethod (lua_CFunction method)
  590. {
  591. lua_pushcfunction (method);
  592. do_unprotectedrun(luaI_seterrormethod, 1, 0);
  593. }
  594. /*
  595. ** API: receives on the stack the table and the index.
  596. ** returns the value.
  597. */
  598. lua_Object lua_getsubscript (void)
  599. {
  600. adjustC(2);
  601. pushsubscript();
  602. CLS_current.base++; /* incorporate object in the stack */
  603. return (Ref(top-1));
  604. }
  605. #define MAX_C_BLOCKS 10
  606. static int numCblocks = 0;
  607. static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
  608. /*
  609. ** API: starts a new block
  610. */
  611. void lua_beginblock (void)
  612. {
  613. if (numCblocks >= MAX_C_BLOCKS)
  614. lua_error("`lua_beginblock': too many nested blocks");
  615. Cblocks[numCblocks] = CLS_current;
  616. numCblocks++;
  617. }
  618. /*
  619. ** API: ends a block
  620. */
  621. void lua_endblock (void)
  622. {
  623. --numCblocks;
  624. CLS_current = Cblocks[numCblocks];
  625. adjustC(0);
  626. }
  627. void lua_settag (int tag)
  628. {
  629. adjustC(1);
  630. luaI_settag(tag, --top);
  631. }
  632. /*
  633. ** API: receives on the stack the table, the index, and the new value.
  634. */
  635. void lua_storesubscript (void)
  636. {
  637. adjustC(3);
  638. storesubscript(top-3, 1);
  639. }
  640. void lua_basicstoreindex (void)
  641. {
  642. adjustC(3);
  643. storesubscript(top-3, 0);
  644. }
  645. /*
  646. ** API: creates a new table
  647. */
  648. lua_Object lua_createtable (void)
  649. {
  650. adjustC(0);
  651. avalue(top) = lua_createarray(0);
  652. ttype(top) = LUA_T_ARRAY;
  653. incr_top;
  654. CLS_current.base++; /* incorporate object in the stack */
  655. return Ref(top-1);
  656. }
  657. /*
  658. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  659. ** 'number' must be 1 to get the first parameter.
  660. */
  661. lua_Object lua_lua2C (int number)
  662. {
  663. if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
  664. /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
  665. stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
  666. return CLS_current.base-CLS_current.num+number;
  667. }
  668. int lua_isnil (lua_Object o)
  669. {
  670. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
  671. }
  672. int lua_istable (lua_Object o)
  673. {
  674. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
  675. }
  676. int lua_isuserdata (lua_Object o)
  677. {
  678. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
  679. }
  680. int lua_iscfunction (lua_Object o)
  681. {
  682. int t = lua_tag(o);
  683. return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
  684. }
  685. int lua_isnumber (lua_Object o)
  686. {
  687. return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
  688. }
  689. int lua_isstring (lua_Object o)
  690. {
  691. int t = lua_tag(o);
  692. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  693. }
  694. int lua_isfunction (lua_Object o)
  695. {
  696. int t = lua_tag(o);
  697. return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
  698. (t == LUA_T_MARK) || (t == LUA_T_CMARK);
  699. }
  700. /*
  701. ** Given an object handle, return its number value. On error, return 0.0.
  702. */
  703. real lua_getnumber (lua_Object object)
  704. {
  705. if (object == LUA_NOOBJECT) return 0.0;
  706. if (tonumber (Address(object))) return 0.0;
  707. else return (nvalue(Address(object)));
  708. }
  709. /*
  710. ** Given an object handle, return its string pointer. On error, return NULL.
  711. */
  712. char *lua_getstring (lua_Object object)
  713. {
  714. if (object == LUA_NOOBJECT) return NULL;
  715. if (tostring (Address(object))) return NULL;
  716. else return (svalue(Address(object)));
  717. }
  718. void *lua_getbinarydata (lua_Object object)
  719. {
  720. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  721. return NULL;
  722. else return svalue(Address(object));
  723. }
  724. void *lua_getuserdata (lua_Object object)
  725. {
  726. void *add = lua_getbinarydata(object);
  727. if (add == NULL) return NULL;
  728. else return *(void **)add;
  729. }
  730. int lua_getbindatasize (lua_Object object)
  731. {
  732. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  733. return 0;
  734. else return (Address(object))->value.ts->size;
  735. }
  736. /*
  737. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  738. */
  739. lua_CFunction lua_getcfunction (lua_Object object)
  740. {
  741. if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) &&
  742. (ttype(Address(object)) != LUA_T_CMARK)))
  743. return NULL;
  744. else return (fvalue(Address(object)));
  745. }
  746. lua_Object lua_getref (int ref)
  747. {
  748. TObject *o = luaI_getref(ref);
  749. if (o == NULL)
  750. return LUA_NOOBJECT;
  751. adjustC(0);
  752. luaI_pushobject(o);
  753. CLS_current.base++; /* incorporate object in the stack */
  754. return Ref(top-1);
  755. }
  756. void lua_pushref (int ref)
  757. {
  758. TObject *o = luaI_getref(ref);
  759. if (o == NULL)
  760. lua_error("access to invalid (possibly garbage collected) reference");
  761. luaI_pushobject(o);
  762. }
  763. int lua_ref (int lock)
  764. {
  765. adjustC(1);
  766. return luaI_ref(--top, lock);
  767. }
  768. /*
  769. ** Get a global object.
  770. */
  771. lua_Object lua_getglobal (char *name)
  772. {
  773. adjustC(0);
  774. getglobal(luaI_findsymbolbyname(name));
  775. CLS_current.base++; /* incorporate object in the stack */
  776. return Ref(top-1);
  777. }
  778. lua_Object lua_basicgetglobal (char *name)
  779. {
  780. adjustC(0);
  781. *top = lua_table[luaI_findsymbolbyname(name)].object;
  782. incr_top;
  783. CLS_current.base++; /* incorporate object in the stack */
  784. return Ref(top-1);
  785. }
  786. /*
  787. ** Store top of the stack at a global variable array field.
  788. */
  789. static void setglobal (Word n)
  790. {
  791. TObject *oldvalue = &lua_table[n].object;
  792. TObject *im = luaI_getimbyObj(oldvalue, IM_SETGLOBAL);
  793. if (ttype(im) == LUA_T_NIL) /* default behavior */
  794. s_object(n) = *(--top);
  795. else {
  796. TObject newvalue = *(top-1);
  797. ttype(top-1) = LUA_T_STRING;
  798. tsvalue(top-1) = lua_table[n].varname;
  799. *top = *oldvalue;
  800. incr_top;
  801. *top = newvalue;
  802. incr_top;
  803. callIM(im, 3, 0);
  804. }
  805. }
  806. void lua_setglobal (char *name)
  807. {
  808. adjustC(1);
  809. setglobal(luaI_findsymbolbyname(name));
  810. }
  811. void lua_basicsetglobal (char *name)
  812. {
  813. Word n = luaI_findsymbolbyname(name);
  814. adjustC(1);
  815. s_object(n) = *(--top);
  816. }
  817. /*
  818. ** Push a nil object
  819. */
  820. void lua_pushnil (void)
  821. {
  822. ttype(top) = LUA_T_NIL;
  823. incr_top;
  824. }
  825. /*
  826. ** Push an object (ttype=number) to stack.
  827. */
  828. void lua_pushnumber (real n)
  829. {
  830. ttype(top) = LUA_T_NUMBER; nvalue(top) = n;
  831. incr_top;
  832. }
  833. /*
  834. ** Push an object (ttype=string) to stack.
  835. */
  836. void lua_pushstring (char *s)
  837. {
  838. if (s == NULL)
  839. ttype(top) = LUA_T_NIL;
  840. else
  841. {
  842. tsvalue(top) = lua_createstring(s);
  843. ttype(top) = LUA_T_STRING;
  844. }
  845. incr_top;
  846. }
  847. /*>>>>>>>>>#undef lua_pushliteral
  848. void lua_pushliteral(char *s) { lua_pushstring(s); }*/
  849. /*
  850. ** Push an object (ttype=cfunction) to stack.
  851. */
  852. void lua_pushcfunction (lua_CFunction fn)
  853. {
  854. ttype(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  855. incr_top;
  856. }
  857. void lua_pushbinarydata (void *buff, int size, int tag)
  858. {
  859. if (buff == NULL)
  860. ttype(top) = LUA_T_NIL;
  861. else {
  862. if (!luaI_userdatatag(tag))
  863. lua_error("invalid tag for userdata");
  864. tsvalue(top) = luaI_createuserdata(buff, size, tag);
  865. ttype(top) = LUA_T_USERDATA;
  866. }
  867. incr_top;
  868. }
  869. /*
  870. ** Push an object (ttype=userdata) to stack.
  871. */
  872. void lua_pushusertag (void *u, int tag)
  873. {
  874. lua_pushbinarydata(&u, sizeof(void *), tag);
  875. }
  876. /*
  877. ** Push an object on the stack.
  878. */
  879. void luaI_pushobject (TObject *o)
  880. {
  881. *top = *o;
  882. incr_top;
  883. }
  884. /*
  885. ** Push a lua_Object on stack.
  886. */
  887. void lua_pushobject (lua_Object o)
  888. {
  889. if (o == LUA_NOOBJECT)
  890. lua_error("attempt to push a NOOBJECT");
  891. *top = *Address(o);
  892. if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
  893. else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
  894. incr_top;
  895. }
  896. int lua_tag (lua_Object o)
  897. {
  898. return (o == LUA_NOOBJECT) ? LUA_T_NIL : luaI_tag(Address(o));
  899. }
  900. void luaI_gcIM (TObject *o)
  901. {
  902. TObject *im = luaI_getimbyObj(o, IM_GC);
  903. if (ttype(im) != LUA_T_NIL) {
  904. *top = *o;
  905. incr_top;
  906. callIM(im, 1, 0);
  907. }
  908. }
  909. static void call_arith (IMS event)
  910. {
  911. TObject *im = luaI_getimbyObj(top-2, event); /* try first operand */
  912. if (ttype(im) == LUA_T_NIL) {
  913. im = luaI_getimbyObj(top-1, event); /* try second operand */
  914. if (ttype(im) == LUA_T_NIL) {
  915. im = luaI_getim(0, event); /* try a 'global' i.m. */
  916. if (ttype(im) == LUA_T_NIL)
  917. lua_error("unexpected type at conversion to number");
  918. }
  919. }
  920. lua_pushstring(luaI_eventname[event]);
  921. callIM(im, 3, 1);
  922. }
  923. static void concim (TObject *o)
  924. {
  925. TObject *im = luaI_getimbyObj(o, IM_CONCAT);
  926. if (ttype(im) == LUA_T_NIL)
  927. lua_error("unexpected type at conversion to string");
  928. callIM(im, 2, 1);
  929. }
  930. static void ordim (TObject *o, IMS event)
  931. {
  932. TObject *im = luaI_getimbyObj(o, event);
  933. if (ttype(im) == LUA_T_NIL)
  934. lua_error("unexpected type at comparison");
  935. lua_pushstring(luaI_eventname[event]);
  936. callIM(im, 3, 1);
  937. }
  938. static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
  939. lua_Type ttype_great, IMS op)
  940. {
  941. TObject *l = top-2;
  942. TObject *r = top-1;
  943. int result;
  944. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  945. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  946. else if (tostring(l)) {
  947. ordim(l, op);
  948. return;
  949. }
  950. else if (tostring(r)) {
  951. ordim(r, op);
  952. return;
  953. }
  954. else
  955. result = strcmp(svalue(l), svalue(r));
  956. top--;
  957. nvalue(top-1) = 1;
  958. ttype(top-1) = (result < 0) ? ttype_less :
  959. (result == 0) ? ttype_equal : ttype_great;
  960. }
  961. static void adjust_varargs (StkId first_extra_arg)
  962. {
  963. TObject arg;
  964. TObject *firstelem = stack+first_extra_arg;
  965. int nvararg = top-firstelem;
  966. int i;
  967. if (nvararg < 0) nvararg = 0;
  968. avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
  969. ttype(&arg) = LUA_T_ARRAY;
  970. for (i=0; i<nvararg; i++) {
  971. TObject index;
  972. ttype(&index) = LUA_T_NUMBER;
  973. nvalue(&index) = i+1;
  974. *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
  975. }
  976. /* store counter in field "n" */ {
  977. TObject index, extra;
  978. ttype(&index) = LUA_T_STRING;
  979. tsvalue(&index) = lua_createstring("n");
  980. ttype(&extra) = LUA_T_NUMBER;
  981. nvalue(&extra) = nvararg;
  982. *(lua_hashdefine(avalue(&arg), &index)) = extra;
  983. }
  984. adjust_top(first_extra_arg);
  985. *top = arg; incr_top;
  986. }
  987. /*
  988. ** Execute the given opcode, until a RET. Parameters are between
  989. ** [stack+base,top). Returns n such that the the results are between
  990. ** [stack+n,top).
  991. */
  992. static StkId lua_execute (Byte *pc, StkId base)
  993. {
  994. if (lua_callhook)
  995. callHook (base, LUA_T_MARK, 0);
  996. while (1)
  997. {
  998. OpCode opcode;
  999. switch (opcode = (OpCode)*pc++)
  1000. {
  1001. case PUSHNIL: ttype(top) = LUA_T_NIL; incr_top; break;
  1002. case PUSH0: case PUSH1: case PUSH2:
  1003. ttype(top) = LUA_T_NUMBER;
  1004. nvalue(top) = opcode-PUSH0;
  1005. incr_top;
  1006. break;
  1007. case PUSHBYTE:
  1008. ttype(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  1009. case PUSHWORD:
  1010. {
  1011. Word w;
  1012. get_word(w,pc);
  1013. ttype(top) = LUA_T_NUMBER; nvalue(top) = w;
  1014. incr_top;
  1015. }
  1016. break;
  1017. case PUSHFLOAT:
  1018. {
  1019. real num;
  1020. get_float(num,pc);
  1021. ttype(top) = LUA_T_NUMBER; nvalue(top) = num;
  1022. incr_top;
  1023. }
  1024. break;
  1025. case PUSHSTRING:
  1026. {
  1027. Word w;
  1028. get_word(w,pc);
  1029. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1030. incr_top;
  1031. }
  1032. break;
  1033. case PUSHFUNCTION:
  1034. {
  1035. TFunc *f;
  1036. get_code(f,pc);
  1037. luaI_insertfunction(f); /* may take part in GC */
  1038. top->ttype = LUA_T_FUNCTION;
  1039. top->value.tf = f;
  1040. incr_top;
  1041. }
  1042. break;
  1043. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  1044. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  1045. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  1046. case PUSHLOCAL9:
  1047. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  1048. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  1049. case PUSHGLOBAL:
  1050. {
  1051. Word w;
  1052. get_word(w,pc);
  1053. getglobal(w);
  1054. }
  1055. break;
  1056. case PUSHINDEXED:
  1057. pushsubscript();
  1058. break;
  1059. case PUSHSELF:
  1060. {
  1061. TObject receiver = *(top-1);
  1062. Word w;
  1063. get_word(w,pc);
  1064. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1065. incr_top;
  1066. pushsubscript();
  1067. *top = receiver;
  1068. incr_top;
  1069. break;
  1070. }
  1071. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  1072. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  1073. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  1074. case STORELOCAL9:
  1075. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  1076. break;
  1077. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  1078. case STOREGLOBAL:
  1079. {
  1080. Word w;
  1081. get_word(w,pc);
  1082. setglobal(w);
  1083. }
  1084. break;
  1085. case STOREINDEXED0:
  1086. storesubscript(top-3, 1);
  1087. break;
  1088. case STOREINDEXED: {
  1089. int n = *pc++;
  1090. storesubscript(top-3-n, 2);
  1091. break;
  1092. }
  1093. case STORELIST0:
  1094. case STORELIST:
  1095. {
  1096. int m, n;
  1097. TObject *arr;
  1098. if (opcode == STORELIST0) m = 0;
  1099. else m = *(pc++) * FIELDS_PER_FLUSH;
  1100. n = *(pc++);
  1101. arr = top-n-1;
  1102. while (n)
  1103. {
  1104. ttype(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  1105. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1106. top--;
  1107. n--;
  1108. }
  1109. }
  1110. break;
  1111. case STORERECORD: /* opcode obsolete: supersed by STOREMAP */
  1112. {
  1113. int n = *(pc++);
  1114. TObject *arr = top-n-1;
  1115. while (n)
  1116. {
  1117. Word w;
  1118. get_word(w,pc);
  1119. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1120. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1121. top--;
  1122. n--;
  1123. }
  1124. }
  1125. break;
  1126. case STOREMAP: {
  1127. int n = *(pc++);
  1128. TObject *arr = top-(2*n)-1;
  1129. while (n--) {
  1130. *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
  1131. top-=2;
  1132. }
  1133. }
  1134. break;
  1135. case ADJUST0:
  1136. adjust_top(base);
  1137. break;
  1138. case ADJUST:
  1139. adjust_top(base + *(pc++));
  1140. break;
  1141. case VARARGS:
  1142. adjust_varargs(base + *(pc++));
  1143. break;
  1144. case CREATEARRAY:
  1145. {
  1146. Word size;
  1147. get_word(size,pc);
  1148. avalue(top) = lua_createarray(size);
  1149. ttype(top) = LUA_T_ARRAY;
  1150. incr_top;
  1151. }
  1152. break;
  1153. case EQOP:
  1154. {
  1155. int res = lua_equalObj(top-2, top-1);
  1156. --top;
  1157. ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  1158. nvalue(top-1) = 1;
  1159. }
  1160. break;
  1161. case LTOP:
  1162. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  1163. break;
  1164. case LEOP:
  1165. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
  1166. break;
  1167. case GTOP:
  1168. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
  1169. break;
  1170. case GEOP:
  1171. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
  1172. break;
  1173. case ADDOP:
  1174. {
  1175. TObject *l = top-2;
  1176. TObject *r = top-1;
  1177. if (tonumber(r) || tonumber(l))
  1178. call_arith(IM_ADD);
  1179. else
  1180. {
  1181. nvalue(l) += nvalue(r);
  1182. --top;
  1183. }
  1184. }
  1185. break;
  1186. case SUBOP:
  1187. {
  1188. TObject *l = top-2;
  1189. TObject *r = top-1;
  1190. if (tonumber(r) || tonumber(l))
  1191. call_arith(IM_SUB);
  1192. else
  1193. {
  1194. nvalue(l) -= nvalue(r);
  1195. --top;
  1196. }
  1197. }
  1198. break;
  1199. case MULTOP:
  1200. {
  1201. TObject *l = top-2;
  1202. TObject *r = top-1;
  1203. if (tonumber(r) || tonumber(l))
  1204. call_arith(IM_MUL);
  1205. else
  1206. {
  1207. nvalue(l) *= nvalue(r);
  1208. --top;
  1209. }
  1210. }
  1211. break;
  1212. case DIVOP:
  1213. {
  1214. TObject *l = top-2;
  1215. TObject *r = top-1;
  1216. if (tonumber(r) || tonumber(l))
  1217. call_arith(IM_DIV);
  1218. else
  1219. {
  1220. nvalue(l) /= nvalue(r);
  1221. --top;
  1222. }
  1223. }
  1224. break;
  1225. case POWOP:
  1226. call_arith(IM_POW);
  1227. break;
  1228. case CONCOP: {
  1229. TObject *l = top-2;
  1230. TObject *r = top-1;
  1231. if (tostring(l)) /* first argument is not a string */
  1232. concim(l);
  1233. else if (tostring(r)) /* second argument is not a string */
  1234. concim(r);
  1235. else {
  1236. tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
  1237. --top;
  1238. }
  1239. }
  1240. break;
  1241. case MINUSOP:
  1242. if (tonumber(top-1))
  1243. {
  1244. ttype(top) = LUA_T_NIL;
  1245. incr_top;
  1246. call_arith(IM_UNM);
  1247. }
  1248. else
  1249. nvalue(top-1) = - nvalue(top-1);
  1250. break;
  1251. case NOTOP:
  1252. ttype(top-1) = (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1253. nvalue(top-1) = 1;
  1254. break;
  1255. case ONTJMP:
  1256. {
  1257. Word w;
  1258. get_word(w,pc);
  1259. if (ttype(top-1) != LUA_T_NIL) pc += w;
  1260. }
  1261. break;
  1262. case ONFJMP:
  1263. {
  1264. Word w;
  1265. get_word(w,pc);
  1266. if (ttype(top-1) == LUA_T_NIL) pc += w;
  1267. }
  1268. break;
  1269. case JMP:
  1270. {
  1271. Word w;
  1272. get_word(w,pc);
  1273. pc += w;
  1274. }
  1275. break;
  1276. case UPJMP:
  1277. {
  1278. Word w;
  1279. get_word(w,pc);
  1280. pc -= w;
  1281. }
  1282. break;
  1283. case IFFJMP:
  1284. {
  1285. Word w;
  1286. get_word(w,pc);
  1287. top--;
  1288. if (ttype(top) == LUA_T_NIL) pc += w;
  1289. }
  1290. break;
  1291. case IFFUPJMP:
  1292. {
  1293. Word w;
  1294. get_word(w,pc);
  1295. top--;
  1296. if (ttype(top) == LUA_T_NIL) pc -= w;
  1297. }
  1298. break;
  1299. case POP: --top; break;
  1300. case CALLFUNC:
  1301. {
  1302. int nParams = *(pc++);
  1303. int nResults = *(pc++);
  1304. StkId newBase = (top-stack)-nParams;
  1305. do_call(newBase, nResults);
  1306. }
  1307. break;
  1308. case RETCODE0:
  1309. case RETCODE:
  1310. if (lua_callhook)
  1311. callHook (base, LUA_T_MARK, 1);
  1312. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1313. case SETLINE:
  1314. {
  1315. Word line;
  1316. get_word(line,pc);
  1317. if ((stack+base-1)->ttype != LUA_T_LINE)
  1318. {
  1319. /* open space for LINE value */
  1320. open_stack((top-stack)-base);
  1321. base++;
  1322. (stack+base-1)->ttype = LUA_T_LINE;
  1323. }
  1324. (stack+base-1)->value.i = line;
  1325. if (lua_linehook)
  1326. lineHook (line);
  1327. break;
  1328. }
  1329. default:
  1330. lua_error ("internal error - opcode doesn't match");
  1331. }
  1332. }
  1333. }