opcode.c 32 KB

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