opcode.c 31 KB

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