lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. ** $Id: lvm.c,v 1.233 2002/05/27 20:35:40 roberto Exp roberto $
  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 "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "lobject.h"
  16. #include "lopcodes.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #include "lvm.h"
  22. /* limit for table tag-method chains (to avoid loops) */
  23. #define MAXTAGLOOP 10000
  24. static void luaV_checkGC (lua_State *L, StkId top) {
  25. if (G(L)->nblocks >= G(L)->GCthreshold) {
  26. L->top = top; /* limit for active registers */
  27. luaC_collectgarbage(L);
  28. L->top = L->ci->top; /* restore old top position */
  29. }
  30. }
  31. const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
  32. lua_Number num;
  33. if (ttype(obj) == LUA_TNUMBER) return obj;
  34. if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &num)) {
  35. setnvalue(n, num);
  36. return n;
  37. }
  38. else
  39. return NULL;
  40. }
  41. int luaV_tostring (lua_State *L, TObject *obj) {
  42. if (ttype(obj) != LUA_TNUMBER)
  43. return 0;
  44. else {
  45. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  46. lua_number2str(s, nvalue(obj));
  47. setsvalue(obj, luaS_new(L, s));
  48. return 1;
  49. }
  50. }
  51. static void traceexec (lua_State *L) {
  52. CallInfo *ci = L->ci;
  53. Proto *p = ci_func(ci)->l.p;
  54. int newline = getline(p, pcRel(*ci->pc, p));
  55. if (pcRel(*ci->pc, p) == 0) /* tracing may be starting now? */
  56. ci->savedpc = *ci->pc; /* initialize `savedpc' */
  57. /* calls linehook when enters a new line or jumps back (loop) */
  58. if (*ci->pc <= ci->savedpc || newline != getline(p, pcRel(ci->savedpc, p))) {
  59. luaD_lineHook(L, newline);
  60. ci = L->ci; /* previous call may reallocate `ci' */
  61. }
  62. ci->savedpc = *ci->pc;
  63. }
  64. static void callTMres (lua_State *L, const TObject *f,
  65. const TObject *p1, const TObject *p2, TObject *result ) {
  66. ptrdiff_t res = savestack(L, result);
  67. setobj(L->top, f); /* push function */
  68. setobj(L->top+1, p1); /* 1st argument */
  69. setobj(L->top+2, p2); /* 2nd argument */
  70. luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
  71. L->top += 3;
  72. luaD_call(L, L->top - 3, 1);
  73. result = restorestack(L, res); /* previous call may change stack */
  74. setobj(result, --L->top); /* get result */
  75. }
  76. static void callTM (lua_State *L, const TObject *f,
  77. const TObject *p1, const TObject *p2, const TObject *p3) {
  78. setobj(L->top, f); /* push function */
  79. setobj(L->top+1, p1); /* 1st argument */
  80. setobj(L->top+2, p2); /* 2nd argument */
  81. setobj(L->top+3, p3); /* 3th argument */
  82. luaD_checkstack(L, 4); /* cannot check before (could invalidate p1...p3) */
  83. L->top += 4;
  84. luaD_call(L, L->top - 4, 0);
  85. }
  86. /*
  87. ** Function to index a table.
  88. ** Receives the table at `t' and the key at `key'.
  89. ** leaves the result at `res'.
  90. */
  91. void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) {
  92. const TObject *tm;
  93. int loop = 0;
  94. init:
  95. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  96. Table *h = hvalue(t);
  97. Table *et = h->metatable;
  98. if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */
  99. const TObject *v = luaH_get(h, key); /* do a primitive get */
  100. if (ttype(v) != LUA_TNIL || /* result is no nil ... */
  101. (tm = fasttm(L, et, TM_INDEX)) == NULL) { /* ... or no index TM? */
  102. setobj(res, v); /* default get */
  103. return;
  104. }
  105. }
  106. /* else will try the tag method */
  107. } else { /* not a table; try a `gettable' tag method */
  108. if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) {
  109. luaG_typeerror(L, t, "index");
  110. return; /* to avoid warnings */
  111. }
  112. }
  113. if (ttype(tm) == LUA_TFUNCTION)
  114. callTMres(L, tm, t, key, res);
  115. else {
  116. if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in gettable");
  117. t = tm;
  118. goto init; /* return luaV_gettable(L, tm, key, res); */
  119. }
  120. }
  121. /*
  122. ** Receives table at `t', key at `key' and value at `val'.
  123. */
  124. void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
  125. const TObject *tm;
  126. int loop = 0;
  127. init:
  128. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  129. Table *h = hvalue(t);
  130. Table *et = h->metatable;
  131. if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no settable TM? */
  132. TObject *oldval = luaH_set(L, h, key); /* do a primitive set */
  133. if (ttype(oldval) != LUA_TNIL || /* result is no nil ... */
  134. (tm = fasttm(L, et, TM_NEWINDEX)) == NULL) { /* ... or no TM? */
  135. setobj(oldval, val);
  136. return;
  137. }
  138. }
  139. /* else will try the tag method */
  140. } else { /* `t' is not a table; try a `settable' tag method */
  141. if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL) {
  142. luaG_typeerror(L, t, "index");
  143. return; /* to avoid warnings */
  144. }
  145. }
  146. if (ttype(tm) == LUA_TFUNCTION)
  147. callTM(L, tm, t, key, val);
  148. else {
  149. if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in settable");
  150. t = tm;
  151. goto init; /* luaV_settable(L, tm, key, val); */
  152. }
  153. }
  154. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  155. TObject *res, TMS event) {
  156. const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  157. if (ttype(tm) == LUA_TNIL)
  158. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  159. if (ttype(tm) != LUA_TFUNCTION) return 0;
  160. callTMres(L, tm, p1, p2, res);
  161. return 1;
  162. }
  163. static void call_arith (lua_State *L, StkId p1, const TObject *p2,
  164. StkId res, TMS event) {
  165. if (!call_binTM(L, p1, p2, res, event))
  166. luaG_aritherror(L, p1, p2);
  167. }
  168. static int luaV_strcmp (const TString *ls, const TString *rs) {
  169. const char *l = getstr(ls);
  170. size_t ll = ls->tsv.len;
  171. const char *r = getstr(rs);
  172. size_t lr = rs->tsv.len;
  173. for (;;) {
  174. int temp = strcoll(l, r);
  175. if (temp < 0) return CMP_LT;
  176. else if (temp > 0) return CMP_GT;
  177. else { /* strings are equal up to a `\0' */
  178. size_t len = strlen(l); /* index of first `\0' in both strings */
  179. if (len == lr) /* r is finished? */
  180. return (len == ll) ? CMP_EQ : CMP_GT; /* l is eq. or gt. than r */
  181. else if (len == ll) /* l is finished? */
  182. return CMP_LT; /* l is smaller than r (because r is not finished) */
  183. /* both strings longer than `len'; go on comparing (after the `\0') */
  184. len++;
  185. l += len; ll -= len; r += len; lr -= len;
  186. }
  187. }
  188. }
  189. int luaV_cmp (lua_State *L, const TObject *l, const TObject *r, int cond) {
  190. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER) {
  191. lua_Number n1 = nvalue(l);
  192. lua_Number n2 = nvalue(r);
  193. if (n1 < n2) return (cond & CMP_LT);
  194. else if (n1 > n2) return (cond & CMP_GT);
  195. else if (n1 == n2) return (cond & CMP_EQ);
  196. else return (cond & CMP_N);
  197. }
  198. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  199. return luaV_strcmp(tsvalue(l), tsvalue(r)) & cond;
  200. else { /* try TM */
  201. if (cond & CMP_EQ ? cond & CMP_LT : cond & CMP_GT) { /* `<=' or `>' ? */
  202. const TObject *temp = l; l = r; r = temp; /* exchange terms */
  203. }
  204. if (!call_binTM(L, l, r, L->top, TM_LT))
  205. luaG_ordererror(L, l, r);
  206. return (cond & CMP_EQ) ? l_isfalse(L->top) : !l_isfalse(L->top);
  207. }
  208. }
  209. void luaV_concat (lua_State *L, int total, int last) {
  210. do {
  211. StkId top = L->ci->base + last + 1;
  212. int n = 2; /* number of elements handled in this pass (at least 2) */
  213. if (!tostring(L, top-2) || !tostring(L, top-1)) {
  214. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  215. luaG_concaterror(L, top-2, top-1);
  216. } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
  217. /* at least two string values; get as many as possible */
  218. lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
  219. cast(lu_mem, tsvalue(top-2)->tsv.len);
  220. char *buffer;
  221. int i;
  222. while (n < total && tostring(L, top-n-1)) { /* collect total length */
  223. tl += tsvalue(top-n-1)->tsv.len;
  224. n++;
  225. }
  226. if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
  227. buffer = luaO_openspace(L, tl, char);
  228. tl = 0;
  229. for (i=n; i>0; i--) { /* concat all strings */
  230. size_t l = tsvalue(top-i)->tsv.len;
  231. memcpy(buffer+tl, svalue(top-i), l);
  232. tl += l;
  233. }
  234. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  235. }
  236. total -= n-1; /* got `n' strings to create 1 new */
  237. last -= n-1;
  238. } while (total > 1); /* repeat until only 1 result left */
  239. }
  240. static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
  241. const TObject *b = rb;
  242. const TObject *c = rc;
  243. TObject tempb, tempc;
  244. if (tonumber(b, &tempb) && tonumber(c, &tempc)) {
  245. TObject f, o;
  246. setsvalue(&o, luaS_newliteral(L, "pow"));
  247. luaV_gettable(L, gt(L), &o, &f);
  248. if (ttype(&f) != LUA_TFUNCTION)
  249. luaG_runerror(L, "`pow' (for `^' operator) is not a function");
  250. callTMres(L, &f, b, c, ra);
  251. }
  252. else
  253. call_arith(L, rb, rc, ra, TM_POW);
  254. }
  255. /*
  256. ** some macros for common tasks in `luaV_execute'
  257. */
  258. #define runtime_check(L, c) { if (!(c)) return 0; }
  259. #define RA(i) (base+GETARG_A(i))
  260. #define RB(i) (base+GETARG_B(i))
  261. #define RC(i) (base+GETARG_C(i))
  262. #define RKC(i) ((GETARG_C(i) < MAXSTACK) ? \
  263. base+GETARG_C(i) : \
  264. k+GETARG_C(i)-MAXSTACK)
  265. #define KBx(i) (k+GETARG_Bx(i))
  266. #define Arith(op, optm) { \
  267. const TObject *b = RB(i); const TObject *c = RKC(i); \
  268. TObject tempb, tempc; \
  269. if (tonumber(b, &tempb) && tonumber(c, &tempc)) { \
  270. setnvalue(ra, nvalue(b) op nvalue(c)); \
  271. } else \
  272. call_arith(L, RB(i), RKC(i), ra, optm); \
  273. }
  274. #define dojump(pc, i) ((pc) += (i))
  275. StkId luaV_execute (lua_State *L) {
  276. StkId base;
  277. LClosure *cl;
  278. TObject *k;
  279. const Instruction *pc;
  280. callentry: /* entry point when calling new functions */
  281. L->ci->pc = &pc;
  282. L->ci->pb = &base;
  283. pc = L->ci->savedpc;
  284. retentry: /* entry point when returning to old functions */
  285. base = L->ci->base;
  286. cl = &clvalue(base - 1)->l;
  287. k = cl->p->k;
  288. /* main loop of interpreter */
  289. for (;;) {
  290. const Instruction i = *pc++;
  291. StkId ra;
  292. if (L->linehook)
  293. traceexec(L);
  294. ra = RA(i);
  295. lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
  296. lua_assert(L->top == L->ci->top ||
  297. GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  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, KBx(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(KBx(i)) == LUA_TSTRING);
  327. luaV_gettable(L, gt(L), KBx(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(KBx(i)) == LUA_TSTRING);
  336. luaV_settable(L, gt(L), KBx(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 (tonumber(rb, ra)) {
  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_concat(L, c-b+1, c); /* 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, GETARG_sBx(i));
  408. break;
  409. }
  410. case OP_EQ: { /* skip next instruction if test fails */
  411. if (luaO_equalObj(ra, RKC(i)) != GETARG_B(i)) pc++;
  412. else dojump(pc, GETARG_sBx(*pc) + 1);
  413. break;
  414. }
  415. case OP_CMP: {
  416. if (!(luaV_cmp(L, ra, RKC(i), GETARG_B(i)))) pc++;
  417. else dojump(pc, GETARG_sBx(*pc) + 1);
  418. break;
  419. }
  420. case OP_TEST: {
  421. StkId rc = RKC(i);
  422. if (l_isfalse(rc) == GETARG_B(i)) pc++;
  423. else {
  424. setobj(ra, rc);
  425. dojump(pc, GETARG_sBx(*pc) + 1);
  426. }
  427. break;
  428. }
  429. case OP_CALL: {
  430. StkId firstResult;
  431. int b = GETARG_B(i);
  432. int nresults;
  433. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  434. nresults = GETARG_C(i) - 1;
  435. firstResult = luaD_precall(L, ra);
  436. if (firstResult) {
  437. if (firstResult > L->top) { /* yield? */
  438. (L->ci-1)->savedpc = pc;
  439. return NULL;
  440. }
  441. /* it was a C function (`precall' called it); adjust results */
  442. luaD_poscall(L, nresults, firstResult);
  443. if (nresults >= 0) L->top = L->ci->top;
  444. }
  445. else { /* it is a Lua function: `call' it */
  446. (L->ci-1)->savedpc = pc;
  447. goto callentry;
  448. }
  449. break;
  450. }
  451. case OP_TAILCALL: {
  452. int b = GETARG_B(i);
  453. if (L->openupval) luaF_close(L, base);
  454. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  455. luaD_poscall(L, LUA_MULTRET, ra); /* move down function and args. */
  456. ra = luaD_precall(L, base-1);
  457. if (ra == NULL) goto callentry; /* it is a Lua function */
  458. else if (ra > L->top) return NULL; /* yield??? */
  459. else goto ret;
  460. }
  461. case OP_RETURN: {
  462. int b;
  463. if (L->openupval) luaF_close(L, base);
  464. b = GETARG_B(i);
  465. if (b != 0) L->top = ra+b-1;
  466. lua_assert(L->ci->pc == &pc);
  467. }
  468. ret: {
  469. CallInfo *ci;
  470. ci = L->ci - 1;
  471. if (ci->pc != &pc) /* previous function was running `here'? */
  472. return ra; /* no: return */
  473. else { /* yes: continue its execution */
  474. int nresults;
  475. lua_assert(ttype(ci->base-1) == LUA_TFUNCTION);
  476. pc = ci->savedpc;
  477. lua_assert(GET_OPCODE(*(pc-1)) == OP_CALL);
  478. nresults = GETARG_C(*(pc-1)) - 1;
  479. luaD_poscall(L, nresults, ra);
  480. if (nresults >= 0) L->top = L->ci->top;
  481. goto retentry;
  482. }
  483. break;
  484. }
  485. case OP_FORLOOP: {
  486. lua_Number step, index, limit;
  487. const TObject *plimit = ra+1;
  488. const TObject *pstep = ra+2;
  489. if (ttype(ra) != LUA_TNUMBER)
  490. luaG_runerror(L, "`for' initial value must be a number");
  491. if (!tonumber(plimit, ra+1))
  492. luaG_runerror(L, "`for' limit must be a number");
  493. if (!tonumber(pstep, ra+2))
  494. luaG_runerror(L, "`for' step must be a number");
  495. step = nvalue(pstep);
  496. index = nvalue(ra) + step; /* increment index */
  497. limit = nvalue(plimit);
  498. if (step > 0 ? index <= limit : index >= limit) {
  499. dojump(pc, GETARG_sBx(i)); /* jump back */
  500. chgnvalue(ra, index); /* update index */
  501. }
  502. break;
  503. }
  504. case OP_TFORLOOP: {
  505. setobj(ra+4, ra+2);
  506. setobj(ra+3, ra+1);
  507. setobj(ra+2, ra);
  508. L->top = ra+5;
  509. luaD_call(L, ra+2, GETARG_C(i) + 1);
  510. L->top = L->ci->top;
  511. if (ttype(ra+2) == LUA_TNIL) pc++; /* skip jump (break loop) */
  512. else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
  513. break;
  514. }
  515. case OP_TFORPREP: {
  516. if (ttype(ra) == LUA_TTABLE) {
  517. setobj(ra+1, ra);
  518. setsvalue(ra, luaS_new(L, "next"));
  519. luaV_gettable(L, gt(L), ra, ra);
  520. }
  521. dojump(pc, GETARG_sBx(i));
  522. break;
  523. }
  524. case OP_SETLIST:
  525. case OP_SETLISTO: {
  526. int bc;
  527. int n;
  528. Table *h;
  529. runtime_check(L, ttype(ra) == LUA_TTABLE);
  530. h = hvalue(ra);
  531. bc = GETARG_Bx(i);
  532. if (GET_OPCODE(i) == OP_SETLIST)
  533. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  534. else {
  535. n = L->top - ra - 1;
  536. L->top = L->ci->top;
  537. }
  538. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  539. for (; n > 0; n--)
  540. setobj(luaH_setnum(L, h, bc+n), ra+n);
  541. break;
  542. }
  543. case OP_CLOSE: {
  544. luaF_close(L, ra);
  545. break;
  546. }
  547. case OP_CLOSURE: {
  548. Proto *p;
  549. Closure *ncl;
  550. int nup, j;
  551. p = cl->p->p[GETARG_Bx(i)];
  552. nup = p->nupvalues;
  553. ncl = luaF_newLclosure(L, nup);
  554. ncl->l.p = p;
  555. for (j=0; j<nup; j++, pc++) {
  556. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  557. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  558. else {
  559. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  560. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  561. }
  562. }
  563. setclvalue(ra, ncl);
  564. luaV_checkGC(L, L->top);
  565. break;
  566. }
  567. }
  568. }
  569. }