opcode.c 30 KB

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