lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. StkId temp = L->top;
  26. L->top = top;
  27. luaC_collectgarbage(L);
  28. L->top = temp; /* 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 1;
  44. else {
  45. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  46. lua_number2str(s, nvalue(obj)); /* convert `s' to number */
  47. setsvalue(obj, luaS_new(L, s));
  48. return 0;
  49. }
  50. }
  51. static void traceexec (lua_State *L, lua_Hook linehook) {
  52. CallInfo *ci = L->ci;
  53. int *lineinfo = ci_func(ci)->l.p->lineinfo;
  54. int pc = cast(int, *ci->pc - ci_func(ci)->l.p->code) - 1;
  55. int newline;
  56. if (ci->line == -1) return; /* no linehooks for this function */
  57. else if (ci->line == 0) { /* first linehook? */
  58. if (pc == 0) { /* function is starting now? */
  59. ci->line = 1;
  60. ci->refi = 0;
  61. ci->lastpc = pc+1; /* make sure it will call linehook */
  62. }
  63. else { /* function started without hooks: */
  64. ci->line = -1; /* keep it that way */
  65. return;
  66. }
  67. }
  68. newline = luaG_getline(lineinfo, pc, ci->line, &ci->refi);
  69. /* calls linehook when enters a new line or jumps back (loop) */
  70. if (newline != ci->line || pc <= ci->lastpc) {
  71. ci->line = newline;
  72. luaD_lineHook(L, newline, linehook);
  73. }
  74. ci->lastpc = pc;
  75. }
  76. /* maximum stack used by a call to a tag method (func + args) */
  77. #define MAXSTACK_TM 4
  78. static void callTM (lua_State *L, const TObject *f,
  79. const TObject *p1, const TObject *p2, const TObject *p3, TObject *result ) {
  80. StkId base = L->top;
  81. luaD_checkstack(L, MAXSTACK_TM);
  82. setobj(base, f); /* push function */
  83. setobj(base+1, p1); /* 1st argument */
  84. setobj(base+2, p2); /* 2nd argument */
  85. L->top += 3;
  86. if (p3) {
  87. setobj(base+3, p3); /* 3th argument */
  88. L->top++;
  89. }
  90. luaD_call(L, base, 1);
  91. if (result) { /* need a result? */
  92. setobj(result, base); /* get it */
  93. }
  94. L->top = base; /* restore top */
  95. }
  96. /*
  97. ** Function to index a table.
  98. ** Receives the table at `t' and the key at `key'.
  99. ** leaves the result at `res'.
  100. */
  101. void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) {
  102. const TObject *tm;
  103. init:
  104. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  105. Table *et = hvalue(t)->eventtable;
  106. if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */
  107. const TObject *h = luaH_get(hvalue(t), key); /* do a primitive get */
  108. /* result is no nil or there is no `index' tag method? */
  109. if (ttype(h) != LUA_TNIL || /* no nil? */
  110. (tm = fasttm(L, et, TM_INDEX)) == NULL) { /* or no index TM? */
  111. setobj(res, h); /* default get */
  112. return;
  113. }
  114. }
  115. /* else will call the tag method */
  116. } else { /* not a table; try a `gettable' tag method */
  117. if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) {
  118. luaG_typeerror(L, t, "index");
  119. return; /* to avoid warnings */
  120. }
  121. }
  122. if (ttype(tm) == LUA_TFUNCTION)
  123. callTM(L, tm, t, key, NULL, res);
  124. else {
  125. t = (StkId)tm; /* ?? */
  126. goto init; /* return luaV_gettable(L, tm, key, res); */
  127. }
  128. }
  129. /*
  130. ** Receives table at `t', key at `key' and value at `val'.
  131. */
  132. void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) {
  133. const TObject *tm;
  134. init:
  135. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  136. Table *et = hvalue(t)->eventtable;
  137. if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no TM? */
  138. luaH_set(L, hvalue(t), key, val); /* do a primitive set */
  139. return;
  140. }
  141. /* else will call the tag method */
  142. } else { /* not a table; try a `settable' tag method */
  143. if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL) {
  144. luaG_typeerror(L, t, "index");
  145. return; /* to avoid warnings */
  146. }
  147. }
  148. if (ttype(tm) == LUA_TFUNCTION)
  149. callTM(L, tm, t, key, val, NULL);
  150. else {
  151. t = (StkId)tm; /* ?? */
  152. goto init; /* luaV_settable(L, tm, key, val); */
  153. }
  154. }
  155. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  156. TObject *res, TMS event) {
  157. const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  158. if (ttype(tm) == LUA_TNIL)
  159. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  160. if (ttype(tm) != LUA_TFUNCTION) return 0;
  161. callTM(L, tm, p1, p2, NULL, res);
  162. return 1;
  163. }
  164. static void call_arith (lua_State *L, StkId p1, TObject *p2,
  165. StkId res, TMS event) {
  166. if (!call_binTM(L, p1, p2, res, event))
  167. luaG_aritherror(L, p1, p2);
  168. }
  169. static int luaV_strlessthan (const TString *ls, const TString *rs) {
  170. const char *l = getstr(ls);
  171. size_t ll = ls->tsv.len;
  172. const char *r = getstr(rs);
  173. size_t lr = rs->tsv.len;
  174. for (;;) {
  175. int temp = strcoll(l, r);
  176. if (temp != 0) return (temp < 0);
  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 0; /* l is equal or greater than r */
  181. else if (len == ll) /* l is finished? */
  182. return 1; /* 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_lessthan (lua_State *L, const TObject *l, const TObject *r) {
  190. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
  191. return (nvalue(l) < nvalue(r));
  192. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  193. return luaV_strlessthan(tsvalue(l), tsvalue(r));
  194. else { /* try TM */
  195. if (!call_binTM(L, l, r, L->top, TM_LT))
  196. luaG_ordererror(L, l, r);
  197. return !l_isfalse(L->top);
  198. }
  199. }
  200. void luaV_strconc (lua_State *L, int total, int last) {
  201. luaV_checkGC(L, L->ci->base + last + 1);
  202. do {
  203. StkId top = L->ci->base + last + 1;
  204. int n = 2; /* number of elements handled in this pass (at least 2) */
  205. if (tostring(L, top-2) || tostring(L, top-1)) {
  206. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  207. luaG_concaterror(L, top-2, top-1);
  208. } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
  209. /* at least two string values; get as many as possible */
  210. lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
  211. cast(lu_mem, tsvalue(top-2)->tsv.len);
  212. char *buffer;
  213. int i;
  214. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  215. tl += tsvalue(top-n-1)->tsv.len;
  216. n++;
  217. }
  218. if (tl > MAX_SIZET) luaD_error(L, "string size overflow");
  219. buffer = luaO_openspace(L, tl, char);
  220. tl = 0;
  221. for (i=n; i>0; i--) { /* concat all strings */
  222. size_t l = tsvalue(top-i)->tsv.len;
  223. memcpy(buffer+tl, svalue(top-i), l);
  224. tl += l;
  225. }
  226. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  227. }
  228. total -= n-1; /* got `n' strings to create 1 new */
  229. last -= n-1;
  230. } while (total > 1); /* repeat until only 1 result left */
  231. }
  232. static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
  233. const TObject *b = rb;
  234. const TObject *c = rc;
  235. TObject tempb, tempc;
  236. if ((b = luaV_tonumber(b, &tempb)) != NULL &&
  237. (c = luaV_tonumber(c, &tempc)) != NULL) {
  238. TObject f, o;
  239. setsvalue(&o, luaS_newliteral(L, "pow"));
  240. luaV_gettable(L, gt(L), &o, &f);
  241. if (ttype(&f) != LUA_TFUNCTION)
  242. luaD_error(L, "`pow' (for `^' operator) is not a function");
  243. callTM(L, &f, b, c, NULL, ra);
  244. }
  245. else
  246. call_arith(L, rb, rc, ra, TM_POW);
  247. }
  248. /*
  249. ** some macros for common tasks in `luaV_execute'
  250. */
  251. #define runtime_check(L, c) { if (!(c)) return 0; }
  252. #define RA(i) (base+GETARG_A(i))
  253. #define RB(i) (base+GETARG_B(i))
  254. #define RC(i) (base+GETARG_C(i))
  255. #define RKC(i) ((GETARG_C(i) < MAXSTACK) ? \
  256. base+GETARG_C(i) : \
  257. k+GETARG_C(i)-MAXSTACK)
  258. #define KBc(i) (k+GETARG_Bc(i))
  259. #define Arith(op, optm) { \
  260. const TObject *b = RB(i); const TObject *c = RKC(i); \
  261. TObject tempb, tempc; \
  262. if ((ttype(b) == LUA_TNUMBER || (b = luaV_tonumber(b, &tempb)) != NULL) && \
  263. (ttype(c) == LUA_TNUMBER || (c = luaV_tonumber(c, &tempc)) != NULL)) { \
  264. setnvalue(ra, nvalue(b) op nvalue(c)); \
  265. } else \
  266. call_arith(L, RB(i), RKC(i), ra, optm); \
  267. }
  268. #define dojump(pc, i) ((pc) += GETARG_sBc(i))
  269. /*
  270. ** Executes current Lua function. Parameters are between [base,top).
  271. ** Returns n such that the results are between [n,top).
  272. */
  273. StkId luaV_execute (lua_State *L) {
  274. StkId base;
  275. LClosure *cl;
  276. TObject *k;
  277. const Instruction *pc;
  278. lua_Hook linehook;
  279. reinit:
  280. base = L->ci->base;
  281. cl = &clvalue(base - 1)->l;
  282. k = cl->p->k;
  283. linehook = L->linehook;
  284. L->ci->pc = &pc;
  285. pc = L->ci->savedpc;
  286. L->ci->savedpc = NULL;
  287. /* main loop of interpreter */
  288. for (;;) {
  289. const Instruction i = *pc++;
  290. const StkId ra = RA(i);
  291. if (linehook)
  292. traceexec(L, linehook);
  293. lua_assert(L->top == L->ci->top || GET_OPCODE(i) == OP_CALL ||
  294. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  295. lua_assert(L->ci->savedpc == NULL);
  296. switch (GET_OPCODE(i)) {
  297. case OP_MOVE: {
  298. setobj(ra, RB(i));
  299. break;
  300. }
  301. case OP_LOADK: {
  302. setobj(ra, KBc(i));
  303. break;
  304. }
  305. case OP_LOADINT: {
  306. setnvalue(ra, (lua_Number)GETARG_sBc(i));
  307. break;
  308. }
  309. case OP_LOADBOOL: {
  310. setbvalue(ra, GETARG_B(i));
  311. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  312. break;
  313. }
  314. case OP_LOADNIL: {
  315. TObject *rb = RB(i);
  316. do {
  317. setnilvalue(rb--);
  318. } while (rb >= ra);
  319. break;
  320. }
  321. case OP_GETUPVAL: {
  322. int b = GETARG_B(i);
  323. setobj(ra, cl->upvals[b]->v);
  324. break;
  325. }
  326. case OP_GETGLOBAL: {
  327. lua_assert(ttype(KBc(i)) == LUA_TSTRING);
  328. luaV_gettable(L, gt(L), KBc(i), ra);
  329. break;
  330. }
  331. case OP_GETTABLE: {
  332. luaV_gettable(L, RB(i), RKC(i), ra);
  333. break;
  334. }
  335. case OP_SETGLOBAL: {
  336. lua_assert(ttype(KBc(i)) == LUA_TSTRING);
  337. luaV_settable(L, gt(L), KBc(i), ra);
  338. break;
  339. }
  340. case OP_SETUPVAL: {
  341. int b = GETARG_B(i);
  342. setobj(cl->upvals[b]->v, ra);
  343. break;
  344. }
  345. case OP_SETTABLE: {
  346. luaV_settable(L, RB(i), RKC(i), ra);
  347. break;
  348. }
  349. case OP_NEWTABLE: {
  350. int b = GETARG_B(i);
  351. if (b > 0) b = twoto(b-1);
  352. sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
  353. luaV_checkGC(L, ra+1);
  354. break;
  355. }
  356. case OP_SELF: {
  357. StkId rb = RB(i);
  358. setobj(ra+1, rb);
  359. luaV_gettable(L, rb, RKC(i), ra);
  360. break;
  361. }
  362. case OP_ADD: {
  363. Arith( + , TM_ADD);
  364. break;
  365. }
  366. case OP_SUB: {
  367. Arith( - , TM_SUB);
  368. break;
  369. }
  370. case OP_MUL: {
  371. Arith( * , TM_MUL);
  372. break;
  373. }
  374. case OP_DIV: {
  375. Arith( / , TM_DIV);
  376. break;
  377. }
  378. case OP_POW: {
  379. powOp(L, ra, RB(i), RKC(i));
  380. break;
  381. }
  382. case OP_UNM: {
  383. const TObject *rb = RB(i);
  384. if (ttype(rb) == LUA_TNUMBER || (rb=luaV_tonumber(rb, ra)) != NULL) {
  385. setnvalue(ra, -nvalue(rb));
  386. }
  387. else {
  388. TObject temp;
  389. setnilvalue(&temp);
  390. call_arith(L, RB(i), &temp, ra, TM_UNM);
  391. }
  392. break;
  393. }
  394. case OP_NOT: {
  395. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  396. setbvalue(ra, res);
  397. break;
  398. }
  399. case OP_CONCAT: {
  400. int b = GETARG_B(i);
  401. int c = GETARG_C(i);
  402. luaV_strconc(L, c-b+1, c);
  403. setobj(ra, base+b);
  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 == base) { /* 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. if (ci->savedpc == NULL)
  476. return ra;
  477. else { /* previous function is Lua: continue its execution */
  478. int nresults;
  479. lua_assert(ttype(ci->base-1) == LUA_TFUNCTION);
  480. base = ci->base; /* restore previous values */
  481. cl = &clvalue(base - 1)->l;
  482. k = cl->p->k;
  483. pc = ci->savedpc;
  484. ci->savedpc = NULL;
  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. luaV_checkGC(L, L->top);
  569. p = cl->p->p[GETARG_Bc(i)];
  570. nup = p->nupvalues;
  571. ncl = luaF_newLclosure(L, nup);
  572. ncl->l.p = p;
  573. for (j=0; j<nup; j++, pc++) {
  574. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  575. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  576. else {
  577. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  578. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  579. }
  580. }
  581. setclvalue(ra, ncl);
  582. break;
  583. }
  584. }
  585. }
  586. }