opcode.c 31 KB

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