2 ** $Id: lvm.c,v 2.147 2011/12/07 14:43:55 roberto Exp $
4 ** See Copyright Notice in lua.h
31 /* limit for table tag-method chains (to avoid loops) */
32 #define MAXTAGLOOP 100
35 const TValue
*luaV_tonumber (const TValue
*obj
, TValue
*n
) {
37 if (ttisnumber(obj
)) return obj
;
38 if (ttisstring(obj
) && luaO_str2d(svalue(obj
), tsvalue(obj
)->len
, &num
)) {
47 int luaV_tostring (lua_State
*L
, StkId obj
) {
51 char s
[LUAI_MAXNUMBER2STR
];
52 lua_Number n
= nvalue(obj
);
53 int l
= lua_number2str(s
, n
);
54 setsvalue2s(L
, obj
, luaS_newlstr(L
, s
, l
));
60 static void traceexec (lua_State
*L
) {
62 lu_byte mask
= L
->hookmask
;
63 if ((mask
& LUA_MASKCOUNT
) && L
->hookcount
== 0) {
65 luaD_hook(L
, LUA_HOOKCOUNT
, -1);
67 if (mask
& LUA_MASKLINE
) {
68 Proto
*p
= ci_func(ci
)->p
;
69 int npc
= pcRel(ci
->u
.l
.savedpc
, p
);
70 int newline
= getfuncline(p
, npc
);
71 if (npc
== 0 || /* call linehook when enter a new function, */
72 ci
->u
.l
.savedpc
<= L
->oldpc
|| /* when jump back (loop), or when */
73 newline
!= getfuncline(p
, pcRel(L
->oldpc
, p
))) /* enter a new line */
74 luaD_hook(L
, LUA_HOOKLINE
, newline
);
76 L
->oldpc
= ci
->u
.l
.savedpc
;
77 if (L
->status
== LUA_YIELD
) { /* did hook yield? */
78 ci
->u
.l
.savedpc
--; /* undo increment (resume will increment it again) */
79 luaD_throw(L
, LUA_YIELD
);
84 static void callTM (lua_State
*L
, const TValue
*f
, const TValue
*p1
,
85 const TValue
*p2
, TValue
*p3
, int hasres
) {
86 ptrdiff_t result
= savestack(L
, p3
);
87 setobj2s(L
, L
->top
++, f
); /* push function */
88 setobj2s(L
, L
->top
++, p1
); /* 1st argument */
89 setobj2s(L
, L
->top
++, p2
); /* 2nd argument */
90 if (!hasres
) /* no result? 'p3' is third argument */
91 setobj2s(L
, L
->top
++, p3
); /* 3rd argument */
92 luaD_checkstack(L
, 0);
93 /* metamethod may yield only when called from Lua code */
94 luaD_call(L
, L
->top
- (4 - hasres
), hasres
, isLua(L
->ci
));
95 if (hasres
) { /* if has result, move it to its place */
96 p3
= restorestack(L
, result
);
97 setobjs2s(L
, p3
, --L
->top
);
102 void luaV_gettable (lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
104 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
106 if (ttistable(t
)) { /* `t' is a table? */
107 Table
*h
= hvalue(t
);
108 const TValue
*res
= luaH_get(h
, key
); /* do a primitive get */
109 if (!ttisnil(res
) || /* result is not nil? */
110 (tm
= fasttm(L
, h
->metatable
, TM_INDEX
)) == NULL
) { /* or no TM? */
111 setobj2s(L
, val
, res
);
114 /* else will try the tag method */
116 else if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_INDEX
)))
117 luaG_typeerror(L
, t
, "index");
118 if (ttisfunction(tm
)) {
119 callTM(L
, tm
, t
, key
, val
, 1);
122 t
= tm
; /* else repeat with 'tm' */
124 luaG_runerror(L
, "loop in gettable");
128 void luaV_settable (lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
130 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
132 if (ttistable(t
)) { /* `t' is a table? */
133 Table
*h
= hvalue(t
);
134 TValue
*oldval
= cast(TValue
*, luaH_get(h
, key
));
135 /* if previous value is not nil, there must be a previous entry
136 in the table; moreover, a metamethod has no relevance */
137 if (!ttisnil(oldval
) ||
138 /* previous value is nil; must check the metamethod */
139 ((tm
= fasttm(L
, h
->metatable
, TM_NEWINDEX
)) == NULL
&&
140 /* no metamethod; is there a previous entry in the table? */
141 (oldval
!= luaO_nilobject
||
142 /* no previous entry; must create one. (The next test is
143 always true; we only need the assignment.) */
144 (oldval
= luaH_newkey(L
, h
, key
), 1)))) {
145 /* no metamethod and (now) there is an entry with given key */
146 setobj2t(L
, oldval
, val
); /* assign new value to that entry */
147 invalidateTMcache(h
);
148 luaC_barrierback(L
, obj2gco(h
), val
);
151 /* else will try the metamethod */
153 else /* not a table; check metamethod */
154 if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_NEWINDEX
)))
155 luaG_typeerror(L
, t
, "index");
156 /* there is a metamethod */
157 if (ttisfunction(tm
)) {
158 callTM(L
, tm
, t
, key
, val
, 0);
161 t
= tm
; /* else repeat with 'tm' */
163 luaG_runerror(L
, "loop in settable");
167 static int call_binTM (lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
168 StkId res
, TMS event
) {
169 const TValue
*tm
= luaT_gettmbyobj(L
, p1
, event
); /* try first operand */
171 tm
= luaT_gettmbyobj(L
, p2
, event
); /* try second operand */
172 if (ttisnil(tm
)) return 0;
173 callTM(L
, tm
, p1
, p2
, res
, 1);
178 static const TValue
*get_equalTM (lua_State
*L
, Table
*mt1
, Table
*mt2
,
180 const TValue
*tm1
= fasttm(L
, mt1
, event
);
182 if (tm1
== NULL
) return NULL
; /* no metamethod */
183 if (mt1
== mt2
) return tm1
; /* same metatables => same metamethods */
184 tm2
= fasttm(L
, mt2
, event
);
185 if (tm2
== NULL
) return NULL
; /* no metamethod */
186 if (luaV_rawequalobj(tm1
, tm2
)) /* same metamethods? */
192 static int call_orderTM (lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
194 if (!call_binTM(L
, p1
, p2
, L
->top
, event
))
195 return -1; /* no metamethod */
197 return !l_isfalse(L
->top
);
201 static int l_strcmp (const TString
*ls
, const TString
*rs
) {
202 const char *l
= getstr(ls
);
203 size_t ll
= ls
->tsv
.len
;
204 const char *r
= getstr(rs
);
205 size_t lr
= rs
->tsv
.len
;
207 int temp
= strcoll(l
, r
);
208 if (temp
!= 0) return temp
;
209 else { /* strings are equal up to a `\0' */
210 size_t len
= strlen(l
); /* index of first `\0' in both strings */
211 if (len
== lr
) /* r is finished? */
212 return (len
== ll
) ? 0 : 1;
213 else if (len
== ll
) /* l is finished? */
214 return -1; /* l is smaller than r (because r is not finished) */
215 /* both strings longer than `len'; go on comparing (after the `\0') */
217 l
+= len
; ll
-= len
; r
+= len
; lr
-= len
;
223 int luaV_lessthan (lua_State
*L
, const TValue
*l
, const TValue
*r
) {
225 if (ttisnumber(l
) && ttisnumber(r
))
226 return luai_numlt(L
, nvalue(l
), nvalue(r
));
227 else if (ttisstring(l
) && ttisstring(r
))
228 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) < 0;
229 else if ((res
= call_orderTM(L
, l
, r
, TM_LT
)) < 0)
230 luaG_ordererror(L
, l
, r
);
235 int luaV_lessequal (lua_State
*L
, const TValue
*l
, const TValue
*r
) {
237 if (ttisnumber(l
) && ttisnumber(r
))
238 return luai_numle(L
, nvalue(l
), nvalue(r
));
239 else if (ttisstring(l
) && ttisstring(r
))
240 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) <= 0;
241 else if ((res
= call_orderTM(L
, l
, r
, TM_LE
)) >= 0) /* first try `le' */
243 else if ((res
= call_orderTM(L
, r
, l
, TM_LT
)) < 0) /* else try `lt' */
244 luaG_ordererror(L
, l
, r
);
250 ** equality of Lua values. L == NULL means raw equality (no metamethods)
252 int luaV_equalobj_ (lua_State
*L
, const TValue
*t1
, const TValue
*t2
) {
254 lua_assert(ttisequal(t1
, t2
));
256 case LUA_TNIL
: return 1;
257 case LUA_TNUMBER
: return luai_numeq(nvalue(t1
), nvalue(t2
));
258 case LUA_TBOOLEAN
: return bvalue(t1
) == bvalue(t2
); /* true must be 1 !! */
259 case LUA_TLIGHTUSERDATA
: return pvalue(t1
) == pvalue(t2
);
260 case LUA_TLCF
: return fvalue(t1
) == fvalue(t2
);
261 case LUA_TSTRING
: return eqstr(rawtsvalue(t1
), rawtsvalue(t2
));
262 case LUA_TUSERDATA
: {
263 if (uvalue(t1
) == uvalue(t2
)) return 1;
264 else if (L
== NULL
) return 0;
265 tm
= get_equalTM(L
, uvalue(t1
)->metatable
, uvalue(t2
)->metatable
, TM_EQ
);
266 break; /* will try TM */
269 if (hvalue(t1
) == hvalue(t2
)) return 1;
270 else if (L
== NULL
) return 0;
271 tm
= get_equalTM(L
, hvalue(t1
)->metatable
, hvalue(t2
)->metatable
, TM_EQ
);
272 break; /* will try TM */
275 lua_assert(iscollectable(t1
));
276 return gcvalue(t1
) == gcvalue(t2
);
278 if (tm
== NULL
) return 0; /* no TM? */
279 callTM(L
, tm
, t1
, t2
, L
->top
, 1); /* call TM */
280 return !l_isfalse(L
->top
);
284 void luaV_concat (lua_State
*L
, int total
) {
285 lua_assert(total
>= 2);
288 int n
= 2; /* number of elements handled in this pass (at least 2) */
289 if (!(ttisstring(top
-2) || ttisnumber(top
-2)) || !tostring(L
, top
-1)) {
290 if (!call_binTM(L
, top
-2, top
-1, top
-2, TM_CONCAT
))
291 luaG_concaterror(L
, top
-2, top
-1);
293 else if (tsvalue(top
-1)->len
== 0) /* second operand is empty? */
294 (void)tostring(L
, top
- 2); /* result is first operand */
295 else if (ttisstring(top
-2) && tsvalue(top
-2)->len
== 0) {
296 setsvalue2s(L
, top
-2, rawtsvalue(top
-1)); /* result is second op. */
299 /* at least two non-empty string values; get as many as possible */
300 size_t tl
= tsvalue(top
-1)->len
;
303 /* collect total length */
304 for (i
= 1; i
< total
&& tostring(L
, top
-i
-1); i
++) {
305 size_t l
= tsvalue(top
-i
-1)->len
;
306 if (l
>= (MAX_SIZET
/sizeof(char)) - tl
)
307 luaG_runerror(L
, "string length overflow");
310 buffer
= luaZ_openspace(L
, &G(L
)->buff
, tl
);
313 do { /* concat all strings */
314 size_t l
= tsvalue(top
-i
)->len
;
315 memcpy(buffer
+tl
, svalue(top
-i
), l
* sizeof(char));
318 setsvalue2s(L
, top
-n
, luaS_newlstr(L
, buffer
, tl
));
320 total
-= n
-1; /* got 'n' strings to create 1 new */
321 L
->top
-= n
-1; /* popped 'n' strings and pushed one */
322 } while (total
> 1); /* repeat until only 1 result left */
326 void luaV_objlen (lua_State
*L
, StkId ra
, const TValue
*rb
) {
328 switch (ttypenv(rb
)) {
330 Table
*h
= hvalue(rb
);
331 tm
= fasttm(L
, h
->metatable
, TM_LEN
);
332 if (tm
) break; /* metamethod? break switch to call it */
333 setnvalue(ra
, cast_num(luaH_getn(h
))); /* else primitive len */
337 setnvalue(ra
, cast_num(tsvalue(rb
)->len
));
340 default: { /* try metamethod */
341 tm
= luaT_gettmbyobj(L
, rb
, TM_LEN
);
342 if (ttisnil(tm
)) /* no metamethod? */
343 luaG_typeerror(L
, rb
, "get length of");
347 callTM(L
, tm
, rb
, rb
, ra
, 1);
351 void luaV_arith (lua_State
*L
, StkId ra
, const TValue
*rb
,
352 const TValue
*rc
, TMS op
) {
355 if ((b
= luaV_tonumber(rb
, &tempb
)) != NULL
&&
356 (c
= luaV_tonumber(rc
, &tempc
)) != NULL
) {
357 lua_Number res
= luaO_arith(op
- TM_ADD
+ LUA_OPADD
, nvalue(b
), nvalue(c
));
360 else if (!call_binTM(L
, rb
, rc
, ra
, op
))
361 luaG_aritherror(L
, rb
, rc
);
366 ** check whether cached closure in prototype 'p' may be reused, that is,
367 ** whether there is a cached closure with the same upvalues needed by
368 ** new closure to be created.
370 static Closure
*getcached (Proto
*p
, UpVal
**encup
, StkId base
) {
371 Closure
*c
= p
->cache
;
372 if (c
!= NULL
) { /* is there a cached closure? */
373 int nup
= p
->sizeupvalues
;
374 Upvaldesc
*uv
= p
->upvalues
;
376 for (i
= 0; i
< nup
; i
++) { /* check whether it has right upvalues */
377 TValue
*v
= uv
[i
].instack
? base
+ uv
[i
].idx
: encup
[uv
[i
].idx
]->v
;
378 if (c
->l
.upvals
[i
]->v
!= v
)
379 return NULL
; /* wrong upvalue; cannot reuse closure */
382 return c
; /* return cached closure (or NULL if no cached closure) */
387 ** create a new Lua closure, push it in the stack, and initialize
388 ** its upvalues. Note that the call to 'luaC_barrierproto' must come
389 ** before the assignment to 'p->cache', as the function needs the
390 ** original value of that field.
392 static void pushclosure (lua_State
*L
, Proto
*p
, UpVal
**encup
, StkId base
,
394 int nup
= p
->sizeupvalues
;
395 Upvaldesc
*uv
= p
->upvalues
;
397 Closure
*ncl
= luaF_newLclosure(L
, p
);
398 setclLvalue(L
, ra
, ncl
); /* anchor new closure in stack */
399 for (i
= 0; i
< nup
; i
++) { /* fill in its upvalues */
400 if (uv
[i
].instack
) /* upvalue refers to local variable? */
401 ncl
->l
.upvals
[i
] = luaF_findupval(L
, base
+ uv
[i
].idx
);
402 else /* get upvalue from enclosing function */
403 ncl
->l
.upvals
[i
] = encup
[uv
[i
].idx
];
405 luaC_barrierproto(L
, p
, ncl
);
406 p
->cache
= ncl
; /* save it on cache for reuse */
411 ** finish execution of an opcode interrupted by an yield
413 void luaV_finishOp (lua_State
*L
) {
414 CallInfo
*ci
= L
->ci
;
415 StkId base
= ci
->u
.l
.base
;
416 Instruction inst
= *(ci
->u
.l
.savedpc
- 1); /* interrupted instruction */
417 OpCode op
= GET_OPCODE(inst
);
418 switch (op
) { /* finish its execution */
419 case OP_ADD
: case OP_SUB
: case OP_MUL
: case OP_DIV
:
420 case OP_MOD
: case OP_POW
: case OP_UNM
: case OP_LEN
:
421 case OP_GETTABUP
: case OP_GETTABLE
: case OP_SELF
: {
422 setobjs2s(L
, base
+ GETARG_A(inst
), --L
->top
);
425 case OP_LE
: case OP_LT
: case OP_EQ
: {
426 int res
= !l_isfalse(L
->top
- 1);
428 /* metamethod should not be called when operand is K */
429 lua_assert(!ISK(GETARG_B(inst
)));
430 if (op
== OP_LE
&& /* "<=" using "<" instead? */
431 ttisnil(luaT_gettmbyobj(L
, base
+ GETARG_B(inst
), TM_LE
)))
432 res
= !res
; /* invert result */
433 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_JMP
);
434 if (res
!= GETARG_A(inst
)) /* condition failed? */
435 ci
->u
.l
.savedpc
++; /* skip jump instruction */
439 StkId top
= L
->top
- 1; /* top when 'call_binTM' was called */
440 int b
= GETARG_B(inst
); /* first element to concatenate */
441 int total
= cast_int(top
- 1 - (base
+ b
)); /* yet to concatenate */
442 setobj2s(L
, top
- 2, top
); /* put TM result in proper position */
443 if (total
> 1) { /* are there elements to concat? */
444 L
->top
= top
- 1; /* top is one after last element (at top-2) */
445 luaV_concat(L
, total
); /* concat them (may yield again) */
447 /* move final result to final position */
448 setobj2s(L
, ci
->u
.l
.base
+ GETARG_A(inst
), L
->top
- 1);
449 L
->top
= ci
->top
; /* restore top */
453 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_TFORLOOP
);
454 L
->top
= ci
->top
; /* correct top */
458 if (GETARG_C(inst
) - 1 >= 0) /* nresults >= 0? */
459 L
->top
= ci
->top
; /* adjust results */
462 case OP_TAILCALL
: case OP_SETTABUP
: case OP_SETTABLE
:
464 default: lua_assert(0);
471 ** some macros for common tasks in `luaV_execute'
474 #if !defined luai_runtimecheck
475 #define luai_runtimecheck(L, c) /* void */
479 #define RA(i) (base+GETARG_A(i))
480 /* to be used after possible stack reallocation */
481 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
482 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
483 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
484 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
485 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
486 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
488 (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
491 /* execute a jump instruction */
492 #define dojump(ci,i,e) \
493 { int a = GETARG_A(i); \
494 if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
495 ci->u.l.savedpc += GETARG_sBx(i) + e; }
497 /* for test instructions, execute the jump instruction that follows it */
498 #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
501 #define Protect(x) { {x;}; base = ci->u.l.base; }
503 #define checkGC(L,c) Protect(luaC_condGC(L, c); luai_threadyield(L);)
506 #define arith_op(op,tm) { \
507 TValue *rb = RKB(i); \
508 TValue *rc = RKC(i); \
509 if (ttisnumber(rb) && ttisnumber(rc)) { \
510 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
511 setnvalue(ra, op(L, nb, nc)); \
513 else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
516 #define vmdispatch(o) switch(o)
517 #define vmcase(l,b) case l: {b} break;
518 #define vmcasenb(l,b) case l: {b} /* nb = no break */
520 void luaV_execute (lua_State
*L
) {
521 CallInfo
*ci
= L
->ci
;
525 newframe
: /* reentry point when frame changes (call/return) */
526 lua_assert(ci
== L
->ci
);
527 cl
= clLvalue(ci
->func
);
530 /* main loop of interpreter */
532 Instruction i
= *(ci
->u
.l
.savedpc
++);
534 if ((L
->hookmask
& (LUA_MASKLINE
| LUA_MASKCOUNT
)) &&
535 (--L
->hookcount
== 0 || L
->hookmask
& LUA_MASKLINE
)) {
536 Protect(traceexec(L
));
538 /* WARNING: several calls may realloc the stack and invalidate `ra' */
540 lua_assert(base
== ci
->u
.l
.base
);
541 lua_assert(base
<= L
->top
&& L
->top
< L
->stack
+ L
->stacksize
);
542 vmdispatch (GET_OPCODE(i
)) {
544 setobjs2s(L
, ra
, RB(i
));
547 TValue
*rb
= k
+ GETARG_Bx(i
);
552 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
553 rb
= k
+ GETARG_Ax(*ci
->u
.l
.savedpc
++);
557 setbvalue(ra
, GETARG_B(i
));
558 if (GETARG_C(i
)) ci
->u
.l
.savedpc
++; /* skip next instruction (if C) */
568 setobj2s(L
, ra
, cl
->upvals
[b
]->v
);
572 Protect(luaV_gettable(L
, cl
->upvals
[b
]->v
, RKC(i
), ra
));
575 Protect(luaV_gettable(L
, RB(i
), RKC(i
), ra
));
579 Protect(luaV_settable(L
, cl
->upvals
[a
]->v
, RKB(i
), RKC(i
)));
582 UpVal
*uv
= cl
->upvals
[GETARG_B(i
)];
583 setobj(L
, uv
->v
, ra
);
584 luaC_barrier(L
, uv
, ra
);
587 Protect(luaV_settable(L
, ra
, RKB(i
), RKC(i
)));
592 Table
*t
= luaH_new(L
);
594 if (b
!= 0 || c
!= 0)
595 luaH_resize(L
, t
, luaO_fb2int(b
), luaO_fb2int(c
));
597 L
->top
= ra
+ 1; /* limit of live values */
599 L
->top
= ci
->top
; /* restore top */
604 setobjs2s(L
, ra
+1, rb
);
605 Protect(luaV_gettable(L
, rb
, RKC(i
), ra
));
608 arith_op(luai_numadd
, TM_ADD
);
611 arith_op(luai_numsub
, TM_SUB
);
614 arith_op(luai_nummul
, TM_MUL
);
617 arith_op(luai_numdiv
, TM_DIV
);
620 arith_op(luai_nummod
, TM_MOD
);
623 arith_op(luai_numpow
, TM_POW
);
627 if (ttisnumber(rb
)) {
628 lua_Number nb
= nvalue(rb
);
629 setnvalue(ra
, luai_numunm(L
, nb
));
632 Protect(luaV_arith(L
, ra
, rb
, rb
, TM_UNM
));
637 int res
= l_isfalse(rb
); /* next assignment may change this value */
641 Protect(luaV_objlen(L
, ra
, RB(i
)));
647 L
->top
= base
+ c
+ 1; /* mark the end of concat operands */
648 Protect(luaV_concat(L
, c
- b
+ 1));
649 ra
= RA(i
); /* 'luav_concat' may invoke TMs and move the stack */
651 setobjs2s(L
, ra
, rb
);
653 L
->top
= (ra
>= rb
? ra
+ 1 : rb
); /* limit of live values */
656 L
->top
= ci
->top
; /* restore top */
665 if (cast_int(equalobj(L
, rb
, rc
)) != GETARG_A(i
))
673 if (luaV_lessthan(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
681 if (luaV_lessequal(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
688 if (GETARG_C(i
) ? l_isfalse(ra
) : !l_isfalse(ra
))
695 if (GETARG_C(i
) ? l_isfalse(rb
) : !l_isfalse(rb
))
698 setobjs2s(L
, ra
, rb
);
704 int nresults
= GETARG_C(i
) - 1;
705 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
706 if (luaD_precall(L
, ra
, nresults
)) { /* C function? */
707 if (nresults
>= 0) L
->top
= ci
->top
; /* adjust results */
710 else { /* Lua function */
712 ci
->callstatus
|= CIST_REENTRY
;
713 goto newframe
; /* restart luaV_execute over new Lua function */
718 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
719 lua_assert(GETARG_C(i
) - 1 == LUA_MULTRET
);
720 if (luaD_precall(L
, ra
, LUA_MULTRET
)) /* C function? */
723 /* tail call: put called frame (n) in place of caller one (o) */
724 CallInfo
*nci
= L
->ci
; /* called frame */
725 CallInfo
*oci
= nci
->previous
; /* caller frame */
726 StkId nfunc
= nci
->func
; /* called function */
727 StkId ofunc
= oci
->func
; /* caller function */
728 /* last stack slot filled by 'precall' */
729 StkId lim
= nci
->u
.l
.base
+ getproto(nfunc
)->numparams
;
731 /* close all upvalues from previous call */
732 if (cl
->p
->sizep
> 0) luaF_close(L
, oci
->u
.l
.base
);
733 /* move new frame into old one */
734 for (aux
= 0; nfunc
+ aux
< lim
; aux
++)
735 setobjs2s(L
, ofunc
+ aux
, nfunc
+ aux
);
736 oci
->u
.l
.base
= ofunc
+ (nci
->u
.l
.base
- nfunc
); /* correct base */
737 oci
->top
= L
->top
= ofunc
+ (L
->top
- nfunc
); /* correct top */
738 oci
->u
.l
.savedpc
= nci
->u
.l
.savedpc
;
739 oci
->callstatus
|= CIST_TAIL
; /* function was tail called */
740 ci
= L
->ci
= oci
; /* remove new frame */
741 lua_assert(L
->top
== oci
->u
.l
.base
+ getproto(ofunc
)->maxstacksize
);
742 goto newframe
; /* restart luaV_execute over new Lua function */
747 if (b
!= 0) L
->top
= ra
+b
-1;
748 if (cl
->p
->sizep
> 0) luaF_close(L
, base
);
749 b
= luaD_poscall(L
, ra
);
750 if (!(ci
->callstatus
& CIST_REENTRY
)) /* 'ci' still the called one */
751 return; /* external invocation: return */
752 else { /* invocation via reentry: continue execution */
754 if (b
) L
->top
= ci
->top
;
755 lua_assert(isLua(ci
));
756 lua_assert(GET_OPCODE(*((ci
)->u
.l
.savedpc
- 1)) == OP_CALL
);
757 goto newframe
; /* restart luaV_execute over new Lua function */
761 lua_Number step
= nvalue(ra
+2);
762 lua_Number idx
= luai_numadd(L
, nvalue(ra
), step
); /* increment index */
763 lua_Number limit
= nvalue(ra
+1);
764 if (luai_numlt(L
, 0, step
) ? luai_numle(L
, idx
, limit
)
765 : luai_numle(L
, limit
, idx
)) {
766 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
767 setnvalue(ra
, idx
); /* update internal index... */
768 setnvalue(ra
+3, idx
); /* ...and external index */
772 const TValue
*init
= ra
;
773 const TValue
*plimit
= ra
+1;
774 const TValue
*pstep
= ra
+2;
775 if (!tonumber(init
, ra
))
776 luaG_runerror(L
, LUA_QL("for") " initial value must be a number");
777 else if (!tonumber(plimit
, ra
+1))
778 luaG_runerror(L
, LUA_QL("for") " limit must be a number");
779 else if (!tonumber(pstep
, ra
+2))
780 luaG_runerror(L
, LUA_QL("for") " step must be a number");
781 setnvalue(ra
, luai_numsub(L
, nvalue(ra
), nvalue(pstep
)));
782 ci
->u
.l
.savedpc
+= GETARG_sBx(i
);
784 vmcasenb(OP_TFORCALL
,
785 StkId cb
= ra
+ 3; /* call base */
786 setobjs2s(L
, cb
+2, ra
+2);
787 setobjs2s(L
, cb
+1, ra
+1);
788 setobjs2s(L
, cb
, ra
);
789 L
->top
= cb
+ 3; /* func. + 2 args (state and index) */
790 Protect(luaD_call(L
, cb
, GETARG_C(i
), 1));
792 i
= *(ci
->u
.l
.savedpc
++); /* go to next instruction */
794 lua_assert(GET_OPCODE(i
) == OP_TFORLOOP
);
799 if (!ttisnil(ra
+ 1)) { /* continue loop? */
800 setobjs2s(L
, ra
, ra
+ 1); /* save control variable */
801 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
809 if (n
== 0) n
= cast_int(L
->top
- ra
) - 1;
811 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
812 c
= GETARG_Ax(*ci
->u
.l
.savedpc
++);
814 luai_runtimecheck(L
, ttistable(ra
));
816 last
= ((c
-1)*LFIELDS_PER_FLUSH
) + n
;
817 if (last
> h
->sizearray
) /* needs more space? */
818 luaH_resizearray(L
, h
, last
); /* pre-allocate it at once */
821 luaH_setint(L
, h
, last
--, val
);
822 luaC_barrierback(L
, obj2gco(h
), val
);
824 L
->top
= ci
->top
; /* correct top (in case of previous open call) */
827 Proto
*p
= cl
->p
->p
[GETARG_Bx(i
)];
828 Closure
*ncl
= getcached(p
, cl
->upvals
, base
); /* cached closure */
829 if (ncl
== NULL
) /* no match? */
830 pushclosure(L
, p
, cl
->upvals
, base
, ra
); /* create a new one */
832 setclLvalue(L
, ra
, ncl
); /* push cashed closure */
834 L
->top
= ra
+ 1; /* limit of live values */
836 L
->top
= ci
->top
; /* restore top */
840 int b
= GETARG_B(i
) - 1;
842 int n
= cast_int(base
- ci
->func
) - cl
->p
->numparams
- 1;
843 if (b
< 0) { /* B == 0? */
844 b
= n
; /* get all var. arguments */
845 Protect(luaD_checkstack(L
, n
));
846 ra
= RA(i
); /* previous call may change the stack */
849 for (j
= 0; j
< b
; j
++) {
851 setobjs2s(L
, ra
+ j
, base
- n
+ j
);