lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. ** $Id: lvm.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "lapi.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "lobject.h"
  17. #include "lopcodes.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lvm.h"
  23. static void luaV_checkGC (lua_State *L, StkId top) {
  24. if (G(L)->nblocks >= G(L)->GCthreshold) {
  25. L->top = top; /* limit for active registers */
  26. luaC_collectgarbage(L);
  27. L->top = L->ci->top; /* restore old top position */
  28. }
  29. }
  30. const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
  31. lua_Number num;
  32. if (ttype(obj) == LUA_TNUMBER) return obj;
  33. if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &num)) {
  34. setnvalue(n, num);
  35. return n;
  36. }
  37. else
  38. return NULL;
  39. }
  40. int luaV_tostring (lua_State *L, TObject *obj) {
  41. if (ttype(obj) != LUA_TNUMBER)
  42. return 1;
  43. else {
  44. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  45. lua_number2str(s, nvalue(obj)); /* convert `s' to number */
  46. setsvalue(obj, luaS_new(L, s));
  47. return 0;
  48. }
  49. }
  50. static void traceexec (lua_State *L, lua_Hook linehook) {
  51. CallInfo *ci = L->ci;
  52. int *lineinfo = ci_func(ci)->l.p->lineinfo;
  53. int pc = cast(int, *ci->pc - ci_func(ci)->l.p->code) - 1;
  54. int newline;
  55. if (ci->line == -1) return; /* no linehooks for this function */
  56. else if (ci->line == 0) { /* first linehook? */
  57. if (pc == 0) { /* function is starting now? */
  58. ci->line = 1;
  59. ci->refi = 0;
  60. ci->lastpc = pc+1; /* make sure it will call linehook */
  61. }
  62. else { /* function started without hooks: */
  63. ci->line = -1; /* keep it that way */
  64. return;
  65. }
  66. }
  67. newline = luaG_getline(lineinfo, pc, ci->line, &ci->refi);
  68. /* calls linehook when enters a new line or jumps back (loop) */
  69. if (newline != ci->line || pc <= ci->lastpc) {
  70. ci->line = newline;
  71. luaD_lineHook(L, newline, linehook);
  72. ci = L->ci; /* previous call may realocate `ci' */
  73. }
  74. ci->lastpc = pc;
  75. }
  76. static void callTMres (lua_State *L, const TObject *f,
  77. const TObject *p1, const TObject *p2, TObject *result ) {
  78. StkId stack = L->stack;
  79. setobj(L->top, f); /* push function */
  80. setobj(L->top+1, p1); /* 1st argument */
  81. setobj(L->top+2, p2); /* 2nd argument */
  82. luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
  83. L->top += 3;
  84. luaD_call(L, L->top - 3, 1);
  85. if (stack != L->stack) /* stack changed? */
  86. result = (result - stack) + L->stack; /* correct pointer */
  87. setobj(result, --L->top); /* get result */
  88. }
  89. static void callTM (lua_State *L, const TObject *f,
  90. const TObject *p1, const TObject *p2, const TObject *p3) {
  91. setobj(L->top, f); /* push function */
  92. setobj(L->top+1, p1); /* 1st argument */
  93. setobj(L->top+2, p2); /* 2nd argument */
  94. setobj(L->top+3, p3); /* 3th argument */
  95. luaD_checkstack(L, 4); /* cannot check before (could invalidate p1...p3) */
  96. L->top += 4;
  97. luaD_call(L, L->top - 4, 0);
  98. }
  99. /*
  100. ** Function to index a table.
  101. ** Receives the table at `t' and the key at `key'.
  102. ** leaves the result at `res'.
  103. */
  104. void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) {
  105. const TObject *tm;
  106. init:
  107. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  108. Table *et = hvalue(t)->metatable;
  109. if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */
  110. const TObject *h = luaH_get(hvalue(t), key); /* do a primitive get */
  111. /* result is no nil or there is no `index' tag method? */
  112. if (ttype(h) != LUA_TNIL || /* no nil? */
  113. (tm = fasttm(L, et, TM_INDEX)) == NULL) { /* or no index TM? */
  114. setobj(res, h); /* default get */
  115. return;
  116. }
  117. }
  118. /* else will call the tag method */
  119. } else { /* not a table; try a `gettable' tag method */
  120. if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) {
  121. luaG_typeerror(L, t, "index");
  122. return; /* to avoid warnings */
  123. }
  124. }
  125. if (ttype(tm) == LUA_TFUNCTION)
  126. callTMres(L, tm, t, key, res);
  127. else {
  128. t = (StkId)tm; /* ?? */
  129. goto init; /* return luaV_gettable(L, tm, key, res); */
  130. }
  131. }
  132. /*
  133. ** Receives table at `t', key at `key' and value at `val'.
  134. */
  135. void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) {
  136. const TObject *tm;
  137. init:
  138. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  139. Table *et = hvalue(t)->metatable;
  140. if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no TM? */
  141. luaH_set(L, hvalue(t), key, val); /* do a primitive set */
  142. return;
  143. }
  144. /* else will call the tag method */
  145. } else { /* not a table; try a `settable' tag method */
  146. if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL) {
  147. luaG_typeerror(L, t, "index");
  148. return; /* to avoid warnings */
  149. }
  150. }
  151. if (ttype(tm) == LUA_TFUNCTION)
  152. callTM(L, tm, t, key, val);
  153. else {
  154. t = (StkId)tm; /* ?? */
  155. goto init; /* luaV_settable(L, tm, key, val); */
  156. }
  157. }
  158. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  159. TObject *res, TMS event) {
  160. const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  161. if (ttype(tm) == LUA_TNIL)
  162. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  163. if (ttype(tm) != LUA_TFUNCTION) return 0;
  164. callTMres(L, tm, p1, p2, res);
  165. return 1;
  166. }
  167. static void call_arith (lua_State *L, StkId p1, TObject *p2,
  168. StkId res, TMS event) {
  169. if (!call_binTM(L, p1, p2, res, event))
  170. luaG_aritherror(L, p1, p2);
  171. }
  172. static int luaV_strlessthan (const TString *ls, const TString *rs) {
  173. const char *l = getstr(ls);
  174. size_t ll = ls->tsv.len;
  175. const char *r = getstr(rs);
  176. size_t lr = rs->tsv.len;
  177. for (;;) {
  178. int temp = strcoll(l, r);
  179. if (temp != 0) return (temp < 0);
  180. else { /* strings are equal up to a `\0' */
  181. size_t len = strlen(l); /* index of first `\0' in both strings */
  182. if (len == lr) /* r is finished? */
  183. return 0; /* l is equal or greater than r */
  184. else if (len == ll) /* l is finished? */
  185. return 1; /* l is smaller than r (because r is not finished) */
  186. /* both strings longer than `len'; go on comparing (after the `\0') */
  187. len++;
  188. l += len; ll -= len; r += len; lr -= len;
  189. }
  190. }
  191. }
  192. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
  193. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
  194. return (nvalue(l) < nvalue(r));
  195. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  196. return luaV_strlessthan(tsvalue(l), tsvalue(r));
  197. else { /* try TM */
  198. if (!call_binTM(L, l, r, L->top, TM_LT))
  199. luaG_ordererror(L, l, r);
  200. return !l_isfalse(L->top);
  201. }
  202. }
  203. void luaV_strconc (lua_State *L, int total, int last) {
  204. do {
  205. StkId top = L->ci->base + last + 1;
  206. int n = 2; /* number of elements handled in this pass (at least 2) */
  207. if (tostring(L, top-2) || tostring(L, top-1)) {
  208. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  209. luaG_concaterror(L, top-2, top-1);
  210. } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
  211. /* at least two string values; get as many as possible */
  212. lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
  213. cast(lu_mem, tsvalue(top-2)->tsv.len);
  214. char *buffer;
  215. int i;
  216. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  217. tl += tsvalue(top-n-1)->tsv.len;
  218. n++;
  219. }
  220. if (tl > MAX_SIZET) luaD_error(L, "string size overflow");
  221. buffer = luaO_openspace(L, tl, char);
  222. tl = 0;
  223. for (i=n; i>0; i--) { /* concat all strings */
  224. size_t l = tsvalue(top-i)->tsv.len;
  225. memcpy(buffer+tl, svalue(top-i), l);
  226. tl += l;
  227. }
  228. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  229. }
  230. total -= n-1; /* got `n' strings to create 1 new */
  231. last -= n-1;
  232. } while (total > 1); /* repeat until only 1 result left */
  233. }
  234. static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
  235. const TObject *b = rb;
  236. const TObject *c = rc;
  237. TObject tempb, tempc;
  238. if ((b = luaV_tonumber(b, &tempb)) != NULL &&
  239. (c = luaV_tonumber(c, &tempc)) != NULL) {
  240. TObject f, o;
  241. setsvalue(&o, luaS_newliteral(L, "pow"));
  242. luaV_gettable(L, gt(L), &o, &f);
  243. if (ttype(&f) != LUA_TFUNCTION)
  244. luaD_error(L, "`pow' (for `^' operator) is not a function");
  245. callTMres(L, &f, b, c, ra);
  246. }
  247. else
  248. call_arith(L, rb, rc, ra, TM_POW);
  249. }
  250. /*
  251. ** some macros for common tasks in `luaV_execute'
  252. */
  253. #define runtime_check(L, c) { if (!(c)) return 0; }
  254. #define RA(i) (base+GETARG_A(i))
  255. #define RB(i) (base+GETARG_B(i))
  256. #define RC(i) (base+GETARG_C(i))
  257. #define RKC(i) ((GETARG_C(i) < MAXSTACK) ? \
  258. base+GETARG_C(i) : \
  259. k+GETARG_C(i)-MAXSTACK)
  260. #define KBc(i) (k+GETARG_Bc(i))
  261. #define Arith(op, optm) { \
  262. const TObject *b = RB(i); const TObject *c = RKC(i); \
  263. TObject tempb, tempc; \
  264. if ((ttype(b) == LUA_TNUMBER || (b = luaV_tonumber(b, &tempb)) != NULL) && \
  265. (ttype(c) == LUA_TNUMBER || (c = luaV_tonumber(c, &tempc)) != NULL)) { \
  266. setnvalue(ra, nvalue(b) op nvalue(c)); \
  267. } else \
  268. call_arith(L, RB(i), RKC(i), ra, optm); \
  269. }
  270. #define dojump(pc, i) ((pc) += GETARG_sBc(i))
  271. /*
  272. ** Executes current Lua function. Parameters are between [base,top).
  273. ** Returns n such that the results are between [n,top).
  274. */
  275. StkId luaV_execute (lua_State *L) {
  276. StkId base;
  277. LClosure *cl;
  278. TObject *k;
  279. const Instruction *pc;
  280. lua_Hook linehook;
  281. reinit:
  282. base = L->ci->base;
  283. cl = &clvalue(base - 1)->l;
  284. k = cl->p->k;
  285. linehook = L->linehook;
  286. L->ci->pc = &pc;
  287. L->ci->pb = &base;
  288. pc = L->ci->savedpc;
  289. /* main loop of interpreter */
  290. for (;;) {
  291. const Instruction i = *pc++;
  292. StkId ra;
  293. if (linehook)
  294. traceexec(L, linehook);
  295. ra = RA(i);
  296. lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
  297. lua_assert(L->top == L->ci->top || GET_OPCODE(i) == OP_CALL ||
  298. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  299. switch (GET_OPCODE(i)) {
  300. case OP_MOVE: {
  301. setobj(ra, RB(i));
  302. break;
  303. }
  304. case OP_LOADK: {
  305. setobj(ra, KBc(i));
  306. break;
  307. }
  308. case OP_LOADBOOL: {
  309. setbvalue(ra, GETARG_B(i));
  310. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  311. break;
  312. }
  313. case OP_LOADNIL: {
  314. TObject *rb = RB(i);
  315. do {
  316. setnilvalue(rb--);
  317. } while (rb >= ra);
  318. break;
  319. }
  320. case OP_GETUPVAL: {
  321. int b = GETARG_B(i);
  322. setobj(ra, cl->upvals[b]->v);
  323. break;
  324. }
  325. case OP_GETGLOBAL: {
  326. lua_assert(ttype(KBc(i)) == LUA_TSTRING);
  327. luaV_gettable(L, gt(L), KBc(i), ra);
  328. break;
  329. }
  330. case OP_GETTABLE: {
  331. luaV_gettable(L, RB(i), RKC(i), ra);
  332. break;
  333. }
  334. case OP_SETGLOBAL: {
  335. lua_assert(ttype(KBc(i)) == LUA_TSTRING);
  336. luaV_settable(L, gt(L), KBc(i), ra);
  337. break;
  338. }
  339. case OP_SETUPVAL: {
  340. int b = GETARG_B(i);
  341. setobj(cl->upvals[b]->v, ra);
  342. break;
  343. }
  344. case OP_SETTABLE: {
  345. luaV_settable(L, RB(i), RKC(i), ra);
  346. break;
  347. }
  348. case OP_NEWTABLE: {
  349. int b = GETARG_B(i);
  350. if (b > 0) b = twoto(b-1);
  351. sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
  352. luaV_checkGC(L, ra+1);
  353. break;
  354. }
  355. case OP_SELF: {
  356. StkId rb = RB(i);
  357. setobj(ra+1, rb);
  358. luaV_gettable(L, rb, RKC(i), ra);
  359. break;
  360. }
  361. case OP_ADD: {
  362. Arith( + , TM_ADD);
  363. break;
  364. }
  365. case OP_SUB: {
  366. Arith( - , TM_SUB);
  367. break;
  368. }
  369. case OP_MUL: {
  370. Arith( * , TM_MUL);
  371. break;
  372. }
  373. case OP_DIV: {
  374. Arith( / , TM_DIV);
  375. break;
  376. }
  377. case OP_POW: {
  378. powOp(L, ra, RB(i), RKC(i));
  379. break;
  380. }
  381. case OP_UNM: {
  382. const TObject *rb = RB(i);
  383. if (ttype(rb) == LUA_TNUMBER || (rb=luaV_tonumber(rb, ra)) != NULL) {
  384. setnvalue(ra, -nvalue(rb));
  385. }
  386. else {
  387. TObject temp;
  388. setnilvalue(&temp);
  389. call_arith(L, RB(i), &temp, ra, TM_UNM);
  390. }
  391. break;
  392. }
  393. case OP_NOT: {
  394. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  395. setbvalue(ra, res);
  396. break;
  397. }
  398. case OP_CONCAT: {
  399. int b = GETARG_B(i);
  400. int c = GETARG_C(i);
  401. luaV_strconc(L, c-b+1, c); /* this call may change `base' (and `ra') */
  402. setobj(base+GETARG_A(i), base+b);
  403. luaV_checkGC(L, base+c+1);
  404. break;
  405. }
  406. case OP_JMP: {
  407. dojump(pc, i);
  408. break;
  409. }
  410. case OP_TESTEQ: { /* skip next instruction if test fails */
  411. if (!luaO_equalObj(ra, RKC(i))) pc++;
  412. break;
  413. }
  414. case OP_TESTNE: {
  415. if (luaO_equalObj(ra, RKC(i))) pc++;
  416. break;
  417. }
  418. case OP_TESTLT: {
  419. if (!luaV_lessthan(L, ra, RKC(i))) pc++;
  420. break;
  421. }
  422. case OP_TESTLE: { /* b <= c === !(c<b) */
  423. if (luaV_lessthan(L, RKC(i), ra)) pc++;
  424. break;
  425. }
  426. case OP_TESTGT: { /* b > c === (c<b) */
  427. if (!luaV_lessthan(L, RKC(i), ra)) pc++;
  428. break;
  429. }
  430. case OP_TESTGE: { /* b >= c === !(b<c) */
  431. if (luaV_lessthan(L, ra, RKC(i))) pc++;
  432. break;
  433. }
  434. case OP_TESTT: {
  435. StkId rb = RB(i);
  436. if (l_isfalse(rb)) pc++;
  437. else setobj(ra, rb);
  438. break;
  439. }
  440. case OP_TESTF: {
  441. StkId rb = RB(i);
  442. if (!l_isfalse(rb)) pc++;
  443. else setobj(ra, rb);
  444. break;
  445. }
  446. case OP_CALL: {
  447. StkId firstResult;
  448. int b = GETARG_B(i);
  449. int nresults;
  450. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  451. nresults = GETARG_C(i) - 1;
  452. firstResult = luaD_precall(L, ra);
  453. if (firstResult) {
  454. if (firstResult > L->top) { /* yield? */
  455. (L->ci-1)->savedpc = pc;
  456. return NULL;
  457. }
  458. /* it was a C function (`precall' called it); adjust results */
  459. luaD_poscall(L, nresults, firstResult);
  460. if (nresults >= 0) L->top = L->ci->top;
  461. }
  462. else { /* it is a Lua function: `call' it */
  463. (L->ci-1)->savedpc = pc;
  464. goto reinit;
  465. }
  466. break;
  467. }
  468. case OP_RETURN: {
  469. CallInfo *ci;
  470. int b;
  471. if (L->openupval) luaF_close(L, base);
  472. b = GETARG_B(i);
  473. if (b != 0) L->top = ra+b-1;
  474. ci = L->ci - 1;
  475. lua_assert((ci+1)->pc == &pc);
  476. if (ci->pc != &pc) /* previous function was running `here'? */
  477. return ra; /* no: return */
  478. else { /* yes: continue its execution */
  479. int nresults;
  480. lua_assert(ttype(ci->base-1) == LUA_TFUNCTION);
  481. base = ci->base; /* restore previous values */
  482. cl = &clvalue(base - 1)->l;
  483. k = cl->p->k;
  484. pc = ci->savedpc;
  485. lua_assert(GET_OPCODE(*(pc-1)) == OP_CALL);
  486. nresults = GETARG_C(*(pc-1)) - 1;
  487. luaD_poscall(L, nresults, ra);
  488. if (nresults >= 0) L->top = L->ci->top;
  489. }
  490. break;
  491. }
  492. case OP_FORPREP: {
  493. if (luaV_tonumber(ra, ra) == NULL)
  494. luaD_error(L, "`for' initial value must be a number");
  495. if (luaV_tonumber(ra+1, ra+1) == NULL)
  496. luaD_error(L, "`for' limit must be a number");
  497. if (luaV_tonumber(ra+2, ra+2) == NULL)
  498. luaD_error(L, "`for' step must be a number");
  499. /* decrement index (to be incremented) */
  500. chgnvalue(ra, nvalue(ra) - nvalue(ra+2));
  501. pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */
  502. /* store in `ra+1' total number of repetitions */
  503. chgnvalue(ra+1, (nvalue(ra+1)-nvalue(ra))/nvalue(ra+2));
  504. /* go through */
  505. }
  506. case OP_FORLOOP: {
  507. runtime_check(L, ttype(ra+1) == LUA_TNUMBER &&
  508. ttype(ra+2) == LUA_TNUMBER);
  509. if (ttype(ra) != LUA_TNUMBER)
  510. luaD_error(L, "`for' index must be a number");
  511. chgnvalue(ra+1, nvalue(ra+1) - 1); /* decrement counter */
  512. if (nvalue(ra+1) >= 0) {
  513. chgnvalue(ra, nvalue(ra) + nvalue(ra+2)); /* increment index */
  514. dojump(pc, i); /* repeat loop */
  515. }
  516. break;
  517. }
  518. case OP_TFORPREP: {
  519. if (ttype(ra) != LUA_TTABLE)
  520. luaD_error(L, "`for' table must be a table");
  521. setnvalue(ra+1, -1); /* initial index */
  522. setnilvalue(ra+2);
  523. setnilvalue(ra+3);
  524. pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */
  525. /* go through */
  526. }
  527. case OP_TFORLOOP: {
  528. Table *t;
  529. int n;
  530. runtime_check(L, ttype(ra) == LUA_TTABLE &&
  531. ttype(ra+1) == LUA_TNUMBER);
  532. t = hvalue(ra);
  533. n = cast(int, nvalue(ra+1));
  534. n = luaH_nexti(t, n, ra+2);
  535. if (n != -1) { /* repeat loop? */
  536. setnvalue(ra+1, n); /* index */
  537. dojump(pc, i); /* repeat loop */
  538. }
  539. break;
  540. }
  541. case OP_SETLIST:
  542. case OP_SETLISTO: {
  543. int bc;
  544. int n;
  545. Table *h;
  546. runtime_check(L, ttype(ra) == LUA_TTABLE);
  547. h = hvalue(ra);
  548. bc = GETARG_Bc(i);
  549. if (GET_OPCODE(i) == OP_SETLIST)
  550. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  551. else {
  552. n = L->top - ra - 1;
  553. L->top = L->ci->top;
  554. }
  555. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  556. for (; n > 0; n--)
  557. luaH_setnum(L, h, bc+n, ra+n);
  558. break;
  559. }
  560. case OP_CLOSE: {
  561. luaF_close(L, ra);
  562. break;
  563. }
  564. case OP_CLOSURE: {
  565. Proto *p;
  566. Closure *ncl;
  567. int nup, j;
  568. p = cl->p->p[GETARG_Bc(i)];
  569. nup = p->nupvalues;
  570. ncl = luaF_newLclosure(L, nup);
  571. ncl->l.p = p;
  572. for (j=0; j<nup; j++, pc++) {
  573. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  574. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  575. else {
  576. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  577. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  578. }
  579. }
  580. setclvalue(ra, ncl);
  581. luaV_checkGC(L, L->top);
  582. break;
  583. }
  584. }
  585. }
  586. }