opcode.c 31 KB

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