opcode.c 31 KB

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