2 ** $Id: lvm.c,v 2.155 2013/03/16 21:10:18 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
)) {
46 int luaV_tostring(lua_State
*L
, StkId obj
) {
50 char s
[LUAI_MAXNUMBER2STR
];
51 lua_Number n
= nvalue(obj
);
52 int l
= lua_number2str(s
, n
);
53 setsvalue2s(L
, obj
, luaS_newlstr(L
, s
, l
));
59 static void traceexec(lua_State
*L
) {
61 lu_byte mask
= L
->hookmask
;
62 int counthook
= ((mask
& LUA_MASKCOUNT
) && L
->hookcount
== 0);
64 resethookcount(L
); /* reset count */
65 if (ci
->callstatus
& CIST_HOOKYIELD
) { /* called hook last time? */
66 ci
->callstatus
&= ~CIST_HOOKYIELD
; /* erase mark */
67 return; /* do not call hook again (VM yielded, so it did not move) */
70 luaD_hook(L
, LUA_HOOKCOUNT
, -1); /* call count hook */
71 if (mask
& LUA_MASKLINE
) {
72 Proto
*p
= ci_func(ci
)->p
;
73 int npc
= pcRel(ci
->u
.l
.savedpc
, p
);
74 int newline
= getfuncline(p
, npc
);
75 if (npc
== 0 || /* call linehook when enter a new function, */
76 ci
->u
.l
.savedpc
<= L
->oldpc
|| /* when jump back (loop), or when */
77 newline
!= getfuncline(p
, pcRel(L
->oldpc
, p
))) /* enter a new line */
78 luaD_hook(L
, LUA_HOOKLINE
, newline
); /* call line hook */
80 L
->oldpc
= ci
->u
.l
.savedpc
;
81 if (L
->status
== LUA_YIELD
) { /* did hook yield? */
83 L
->hookcount
= 1; /* undo decrement to zero */
84 ci
->u
.l
.savedpc
--; /* undo increment (resume will increment it again) */
85 ci
->callstatus
|= CIST_HOOKYIELD
; /* mark that it yielded */
86 ci
->func
= L
->top
- 1; /* protect stack below results */
87 luaD_throw(L
, LUA_YIELD
);
92 static void callTM(lua_State
*L
, const TValue
*f
, const TValue
*p1
,
93 const TValue
*p2
, TValue
*p3
, int hasres
) {
94 ptrdiff_t result
= savestack(L
, p3
);
95 setobj2s(L
, L
->top
++, f
); /* push function */
96 setobj2s(L
, L
->top
++, p1
); /* 1st argument */
97 setobj2s(L
, L
->top
++, p2
); /* 2nd argument */
98 if (!hasres
) /* no result? 'p3' is third argument */
99 setobj2s(L
, L
->top
++, p3
); /* 3rd argument */
100 /* metamethod may yield only when called from Lua code */
101 luaD_call(L
, L
->top
- (4 - hasres
), hasres
, isLua(L
->ci
));
102 if (hasres
) { /* if has result, move it to its place */
103 p3
= restorestack(L
, result
);
104 setobjs2s(L
, p3
, --L
->top
);
109 void luaV_gettable(lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
111 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
113 if (ttistable(t
)) { /* `t' is a table? */
114 Table
*h
= hvalue(t
);
115 const TValue
*res
= luaH_get(h
, key
); /* do a primitive get */
116 if (!ttisnil(res
) || /* result is not nil? */
117 (tm
= fasttm(L
, h
->metatable
, TM_INDEX
)) == NULL
) { /* or no TM? */
118 setobj2s(L
, val
, res
);
121 /* else will try the tag method */
122 } else if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_INDEX
)))
123 luaG_typeerror(L
, t
, "index");
124 if (ttisfunction(tm
)) {
125 callTM(L
, tm
, t
, key
, val
, 1);
128 t
= tm
; /* else repeat with 'tm' */
130 luaG_runerror(L
, "loop in gettable");
134 void luaV_settable(lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
136 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
138 if (ttistable(t
)) { /* `t' is a table? */
139 Table
*h
= hvalue(t
);
140 TValue
*oldval
= cast(TValue
*, luaH_get(h
, key
));
141 /* if previous value is not nil, there must be a previous entry
142 in the table; moreover, a metamethod has no relevance */
143 if (!ttisnil(oldval
) ||
144 /* previous value is nil; must check the metamethod */
145 ((tm
= fasttm(L
, h
->metatable
, TM_NEWINDEX
)) == NULL
&&
146 /* no metamethod; is there a previous entry in the table? */
147 (oldval
!= luaO_nilobject
||
148 /* no previous entry; must create one. (The next test is
149 always true; we only need the assignment.) */
150 (oldval
= luaH_newkey(L
, h
, key
), 1)))) {
151 /* no metamethod and (now) there is an entry with given key */
152 setobj2t(L
, oldval
, val
); /* assign new value to that entry */
153 invalidateTMcache(h
);
154 luaC_barrierback(L
, obj2gco(h
), val
);
157 /* else will try the metamethod */
158 } else /* not a table; check metamethod */
159 if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_NEWINDEX
)))
160 luaG_typeerror(L
, t
, "index");
161 /* there is a metamethod */
162 if (ttisfunction(tm
)) {
163 callTM(L
, tm
, t
, key
, val
, 0);
166 t
= tm
; /* else repeat with 'tm' */
168 luaG_runerror(L
, "loop in settable");
172 static int call_binTM(lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
173 StkId res
, TMS event
) {
174 const TValue
*tm
= luaT_gettmbyobj(L
, p1
, event
); /* try first operand */
176 tm
= luaT_gettmbyobj(L
, p2
, event
); /* try second operand */
177 if (ttisnil(tm
)) return 0;
178 callTM(L
, tm
, p1
, p2
, res
, 1);
183 static const TValue
*get_equalTM(lua_State
*L
, Table
*mt1
, Table
*mt2
,
185 const TValue
*tm1
= fasttm(L
, mt1
, event
);
187 if (tm1
== NULL
) return NULL
; /* no metamethod */
188 if (mt1
== mt2
) return tm1
; /* same metatables => same metamethods */
189 tm2
= fasttm(L
, mt2
, event
);
190 if (tm2
== NULL
) return NULL
; /* no metamethod */
191 if (luaV_rawequalobj(tm1
, tm2
)) /* same metamethods? */
197 static int call_orderTM(lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
199 if (!call_binTM(L
, p1
, p2
, L
->top
, event
))
200 return -1; /* no metamethod */
202 return !l_isfalse(L
->top
);
206 static int l_strcmp(const TString
*ls
, const TString
*rs
) {
207 const char *l
= getstr(ls
);
208 size_t ll
= ls
->tsv
.len
;
209 const char *r
= getstr(rs
);
210 size_t lr
= rs
->tsv
.len
;
212 int temp
= strcoll(l
, r
);
213 if (temp
!= 0) return temp
;
214 else { /* strings are equal up to a `\0' */
215 size_t len
= strlen(l
); /* index of first `\0' in both strings */
216 if (len
== lr
) /* r is finished? */
217 return (len
== ll
) ? 0 : 1;
218 else if (len
== ll
) /* l is finished? */
219 return -1; /* l is smaller than r (because r is not finished) */
220 /* both strings longer than `len'; go on comparing (after the `\0') */
231 int luaV_lessthan(lua_State
*L
, const TValue
*l
, const TValue
*r
) {
233 if (ttisnumber(l
) && ttisnumber(r
))
234 return luai_numlt(L
, nvalue(l
), nvalue(r
));
235 else if (ttisstring(l
) && ttisstring(r
))
236 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) < 0;
237 else if ((res
= call_orderTM(L
, l
, r
, TM_LT
)) < 0)
238 luaG_ordererror(L
, l
, r
);
243 int luaV_lessequal(lua_State
*L
, const TValue
*l
, const TValue
*r
) {
245 if (ttisnumber(l
) && ttisnumber(r
))
246 return luai_numle(L
, nvalue(l
), nvalue(r
));
247 else if (ttisstring(l
) && ttisstring(r
))
248 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) <= 0;
249 else if ((res
= call_orderTM(L
, l
, r
, TM_LE
)) >= 0) /* first try `le' */
251 else if ((res
= call_orderTM(L
, r
, l
, TM_LT
)) < 0) /* else try `lt' */
252 luaG_ordererror(L
, l
, r
);
258 ** equality of Lua values. L == NULL means raw equality (no metamethods)
260 int luaV_equalobj_(lua_State
*L
, const TValue
*t1
, const TValue
*t2
) {
262 lua_assert(ttisequal(t1
, t2
));
267 return luai_numeq(nvalue(t1
), nvalue(t2
));
269 return bvalue(t1
) == bvalue(t2
); /* true must be 1 !! */
270 case LUA_TLIGHTUSERDATA
:
271 return pvalue(t1
) == pvalue(t2
);
273 return fvalue(t1
) == fvalue(t2
);
275 return eqshrstr(rawtsvalue(t1
), rawtsvalue(t2
));
277 return luaS_eqlngstr(rawtsvalue(t1
), rawtsvalue(t2
));
278 case LUA_TUSERDATA
: {
279 if (uvalue(t1
) == uvalue(t2
)) return 1;
280 else if (L
== NULL
) return 0;
281 tm
= get_equalTM(L
, uvalue(t1
)->metatable
, uvalue(t2
)->metatable
, TM_EQ
);
282 break; /* will try TM */
285 if (hvalue(t1
) == hvalue(t2
)) return 1;
286 else if (L
== NULL
) return 0;
287 tm
= get_equalTM(L
, hvalue(t1
)->metatable
, hvalue(t2
)->metatable
, TM_EQ
);
288 break; /* will try TM */
291 lua_assert(iscollectable(t1
));
292 return gcvalue(t1
) == gcvalue(t2
);
294 if (tm
== NULL
) return 0; /* no TM? */
295 callTM(L
, tm
, t1
, t2
, L
->top
, 1); /* call TM */
296 return !l_isfalse(L
->top
);
300 void luaV_concat(lua_State
*L
, int total
) {
301 lua_assert(total
>= 2);
304 int n
= 2; /* number of elements handled in this pass (at least 2) */
305 if (!(ttisstring(top
- 2) || ttisnumber(top
- 2)) || !tostring(L
, top
- 1)) {
306 if (!call_binTM(L
, top
- 2, top
- 1, top
- 2, TM_CONCAT
))
307 luaG_concaterror(L
, top
- 2, top
- 1);
308 } else if (tsvalue(top
- 1)->len
== 0) /* second operand is empty? */
309 (void)tostring(L
, top
- 2); /* result is first operand */
310 else if (ttisstring(top
- 2) && tsvalue(top
- 2)->len
== 0) {
311 setobjs2s(L
, top
- 2, top
- 1); /* result is second op. */
313 /* at least two non-empty string values; get as many as possible */
314 size_t tl
= tsvalue(top
- 1)->len
;
317 /* collect total length */
318 for (i
= 1; i
< total
&& tostring(L
, top
- i
- 1); i
++) {
319 size_t l
= tsvalue(top
- i
- 1)->len
;
320 if (l
>= (MAX_SIZET
/ sizeof(char)) - tl
)
321 luaG_runerror(L
, "string length overflow");
324 buffer
= luaZ_openspace(L
, &G(L
)->buff
, tl
);
327 do { /* concat all strings */
328 size_t l
= tsvalue(top
- i
)->len
;
329 memcpy(buffer
+ tl
, svalue(top
- i
), l
* sizeof(char));
332 setsvalue2s(L
, top
- n
, luaS_newlstr(L
, buffer
, tl
));
334 total
-= n
- 1; /* got 'n' strings to create 1 new */
335 L
->top
-= n
- 1; /* popped 'n' strings and pushed one */
336 } while (total
> 1); /* repeat until only 1 result left */
340 void luaV_objlen(lua_State
*L
, StkId ra
, const TValue
*rb
) {
342 switch (ttypenv(rb
)) {
344 Table
*h
= hvalue(rb
);
345 tm
= fasttm(L
, h
->metatable
, TM_LEN
);
346 if (tm
) break; /* metamethod? break switch to call it */
347 setnvalue(ra
, cast_num(luaH_getn(h
))); /* else primitive len */
351 setnvalue(ra
, cast_num(tsvalue(rb
)->len
));
354 default: { /* try metamethod */
355 tm
= luaT_gettmbyobj(L
, rb
, TM_LEN
);
356 if (ttisnil(tm
)) /* no metamethod? */
357 luaG_typeerror(L
, rb
, "get length of");
361 callTM(L
, tm
, rb
, rb
, ra
, 1);
365 void luaV_arith(lua_State
*L
, StkId ra
, const TValue
*rb
,
366 const TValue
*rc
, TMS op
) {
369 if ((b
= luaV_tonumber(rb
, &tempb
)) != NULL
&&
370 (c
= luaV_tonumber(rc
, &tempc
)) != NULL
) {
371 lua_Number res
= luaO_arith(op
- TM_ADD
+ LUA_OPADD
, nvalue(b
), nvalue(c
));
373 } else if (!call_binTM(L
, rb
, rc
, ra
, op
))
374 luaG_aritherror(L
, rb
, rc
);
379 ** check whether cached closure in prototype 'p' may be reused, that is,
380 ** whether there is a cached closure with the same upvalues needed by
381 ** new closure to be created.
383 static Closure
*getcached(Proto
*p
, UpVal
**encup
, StkId base
) {
384 Closure
*c
= p
->cache
;
385 if (c
!= NULL
) { /* is there a cached closure? */
386 int nup
= p
->sizeupvalues
;
387 Upvaldesc
*uv
= p
->upvalues
;
389 for (i
= 0; i
< nup
; i
++) { /* check whether it has right upvalues */
390 TValue
*v
= uv
[i
].instack
? base
+ uv
[i
].idx
: encup
[uv
[i
].idx
]->v
;
391 if (c
->l
.upvals
[i
]->v
!= v
)
392 return NULL
; /* wrong upvalue; cannot reuse closure */
395 return c
; /* return cached closure (or NULL if no cached closure) */
400 ** create a new Lua closure, push it in the stack, and initialize
401 ** its upvalues. Note that the call to 'luaC_barrierproto' must come
402 ** before the assignment to 'p->cache', as the function needs the
403 ** original value of that field.
405 static void pushclosure(lua_State
*L
, Proto
*p
, UpVal
**encup
, StkId base
,
407 int nup
= p
->sizeupvalues
;
408 Upvaldesc
*uv
= p
->upvalues
;
410 Closure
*ncl
= luaF_newLclosure(L
, nup
);
412 setclLvalue(L
, ra
, ncl
); /* anchor new closure in stack */
413 for (i
= 0; i
< nup
; i
++) { /* fill in its upvalues */
414 if (uv
[i
].instack
) /* upvalue refers to local variable? */
415 ncl
->l
.upvals
[i
] = luaF_findupval(L
, base
+ uv
[i
].idx
);
416 else /* get upvalue from enclosing function */
417 ncl
->l
.upvals
[i
] = encup
[uv
[i
].idx
];
419 luaC_barrierproto(L
, p
, ncl
);
420 p
->cache
= ncl
; /* save it on cache for reuse */
425 ** finish execution of an opcode interrupted by an yield
427 void luaV_finishOp(lua_State
*L
) {
428 CallInfo
*ci
= L
->ci
;
429 StkId base
= ci
->u
.l
.base
;
430 Instruction inst
= *(ci
->u
.l
.savedpc
- 1); /* interrupted instruction */
431 OpCode op
= GET_OPCODE(inst
);
432 switch (op
) { /* finish its execution */
444 setobjs2s(L
, base
+ GETARG_A(inst
), --L
->top
);
450 int res
= !l_isfalse(L
->top
- 1);
452 /* metamethod should not be called when operand is K */
453 lua_assert(!ISK(GETARG_B(inst
)));
454 if (op
== OP_LE
&& /* "<=" using "<" instead? */
455 ttisnil(luaT_gettmbyobj(L
, base
+ GETARG_B(inst
), TM_LE
)))
456 res
= !res
; /* invert result */
457 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_JMP
);
458 if (res
!= GETARG_A(inst
)) /* condition failed? */
459 ci
->u
.l
.savedpc
++; /* skip jump instruction */
463 StkId top
= L
->top
- 1; /* top when 'call_binTM' was called */
464 int b
= GETARG_B(inst
); /* first element to concatenate */
465 int total
= cast_int(top
- 1 - (base
+ b
)); /* yet to concatenate */
466 setobj2s(L
, top
- 2, top
); /* put TM result in proper position */
467 if (total
> 1) { /* are there elements to concat? */
468 L
->top
= top
- 1; /* top is one after last element (at top-2) */
469 luaV_concat(L
, total
); /* concat them (may yield again) */
471 /* move final result to final position */
472 setobj2s(L
, ci
->u
.l
.base
+ GETARG_A(inst
), L
->top
- 1);
473 L
->top
= ci
->top
; /* restore top */
477 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_TFORLOOP
);
478 L
->top
= ci
->top
; /* correct top */
482 if (GETARG_C(inst
) - 1 >= 0) /* nresults >= 0? */
483 L
->top
= ci
->top
; /* adjust results */
498 ** some macros for common tasks in `luaV_execute'
501 #if !defined luai_runtimecheck
502 #define luai_runtimecheck(L, c) /* void */
506 #define RA(i) (base+GETARG_A(i))
507 /* to be used after possible stack reallocation */
508 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
509 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
510 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
511 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
512 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
513 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
515 (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
518 /* execute a jump instruction */
519 #define dojump(ci,i,e) \
520 { int a = GETARG_A(i); \
521 if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
522 ci->u.l.savedpc += GETARG_sBx(i) + e; }
524 /* for test instructions, execute the jump instruction that follows it */
525 #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
528 #define Protect(x) { {x;}; base = ci->u.l.base; }
530 #define checkGC(L,c) \
531 Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
533 L->top = ci->top;}) /* restore top */ \
534 luai_threadyield(L); )
537 #define arith_op(op,tm) { \
538 TValue *rb = RKB(i); \
539 TValue *rc = RKC(i); \
540 if (ttisnumber(rb) && ttisnumber(rc)) { \
541 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
542 setnvalue(ra, op(L, nb, nc)); \
544 else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
547 #define vmdispatch(o) switch(o)
548 #define vmcase(l,b) case l: {b} break;
549 #define vmcasenb(l,b) case l: {b} /* nb = no break */
551 void luaV_execute(lua_State
*L
) {
552 CallInfo
*ci
= L
->ci
;
556 newframe
: /* reentry point when frame changes (call/return) */
557 lua_assert(ci
== L
->ci
);
558 cl
= clLvalue(ci
->func
);
561 /* main loop of interpreter */
563 Instruction i
= *(ci
->u
.l
.savedpc
++);
565 if ((L
->hookmask
& (LUA_MASKLINE
| LUA_MASKCOUNT
)) &&
566 (--L
->hookcount
== 0 || L
->hookmask
& LUA_MASKLINE
)) {
567 Protect(traceexec(L
));
569 /* WARNING: several calls may realloc the stack and invalidate `ra' */
571 lua_assert(base
== ci
->u
.l
.base
);
572 lua_assert(base
<= L
->top
&& L
->top
< L
->stack
+ L
->stacksize
);
573 vmdispatch(GET_OPCODE(i
)) {
575 setobjs2s(L
, ra
, RB(i
));
578 TValue
*rb
= k
+ GETARG_Bx(i
);
583 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
584 rb
= k
+ GETARG_Ax(*ci
->u
.l
.savedpc
++);
588 setbvalue(ra
, GETARG_B(i
));
589 if (GETARG_C(i
)) ci
->u
.l
.savedpc
++; /* skip next instruction (if C) */
599 setobj2s(L
, ra
, cl
->upvals
[b
]->v
);
603 Protect(luaV_gettable(L
, cl
->upvals
[b
]->v
, RKC(i
), ra
));
606 Protect(luaV_gettable(L
, RB(i
), RKC(i
), ra
));
610 Protect(luaV_settable(L
, cl
->upvals
[a
]->v
, RKB(i
), RKC(i
)));
613 UpVal
*uv
= cl
->upvals
[GETARG_B(i
)];
614 setobj(L
, uv
->v
, ra
);
615 luaC_barrier(L
, uv
, ra
);
618 Protect(luaV_settable(L
, ra
, RKB(i
), RKC(i
)));
623 Table
*t
= luaH_new(L
);
625 if (b
!= 0 || c
!= 0)
626 luaH_resize(L
, t
, luaO_fb2int(b
), luaO_fb2int(c
));
631 setobjs2s(L
, ra
+ 1, rb
);
632 Protect(luaV_gettable(L
, rb
, RKC(i
), ra
));
635 arith_op(luai_numadd
, TM_ADD
);
638 arith_op(luai_numsub
, TM_SUB
);
641 arith_op(luai_nummul
, TM_MUL
);
644 arith_op(luai_numdiv
, TM_DIV
);
647 arith_op(luai_nummod
, TM_MOD
);
650 arith_op(luai_numpow
, TM_POW
);
654 if (ttisnumber(rb
)) {
655 lua_Number nb
= nvalue(rb
);
656 setnvalue(ra
, luai_numunm(L
, nb
));
658 Protect(luaV_arith(L
, ra
, rb
, rb
, TM_UNM
));
663 int res
= l_isfalse(rb
); /* next assignment may change this value */
667 Protect(luaV_objlen(L
, ra
, RB(i
)));
673 L
->top
= base
+ c
+ 1; /* mark the end of concat operands */
674 Protect(luaV_concat(L
, c
- b
+ 1));
675 ra
= RA(i
); /* 'luav_concat' may invoke TMs and move the stack */
677 setobjs2s(L
, ra
, rb
);
678 checkGC(L
, (ra
>= rb
? ra
+ 1 : rb
));
679 L
->top
= ci
->top
; /* restore top */
688 if (cast_int(equalobj(L
, rb
, rc
)) != GETARG_A(i
))
696 if (luaV_lessthan(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
704 if (luaV_lessequal(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
711 if (GETARG_C(i
) ? l_isfalse(ra
) : !l_isfalse(ra
))
718 if (GETARG_C(i
) ? l_isfalse(rb
) : !l_isfalse(rb
))
721 setobjs2s(L
, ra
, rb
);
727 int nresults
= GETARG_C(i
) - 1;
728 if (b
!= 0) L
->top
= ra
+ b
; /* else previous instruction set top */
729 if (luaD_precall(L
, ra
, nresults
)) { /* C function? */
730 if (nresults
>= 0) L
->top
= ci
->top
; /* adjust results */
732 } else { /* Lua function */
734 ci
->callstatus
|= CIST_REENTRY
;
735 goto newframe
; /* restart luaV_execute over new Lua function */
740 if (b
!= 0) L
->top
= ra
+ b
; /* else previous instruction set top */
741 lua_assert(GETARG_C(i
) - 1 == LUA_MULTRET
);
742 if (luaD_precall(L
, ra
, LUA_MULTRET
)) /* C function? */
745 /* tail call: put called frame (n) in place of caller one (o) */
746 CallInfo
*nci
= L
->ci
; /* called frame */
747 CallInfo
*oci
= nci
->previous
; /* caller frame */
748 StkId nfunc
= nci
->func
; /* called function */
749 StkId ofunc
= oci
->func
; /* caller function */
750 /* last stack slot filled by 'precall' */
751 StkId lim
= nci
->u
.l
.base
+ getproto(nfunc
)->numparams
;
753 /* close all upvalues from previous call */
754 if (cl
->p
->sizep
> 0) luaF_close(L
, oci
->u
.l
.base
);
755 /* move new frame into old one */
756 for (aux
= 0; nfunc
+ aux
< lim
; aux
++)
757 setobjs2s(L
, ofunc
+ aux
, nfunc
+ aux
);
758 oci
->u
.l
.base
= ofunc
+ (nci
->u
.l
.base
- nfunc
); /* correct base */
759 oci
->top
= L
->top
= ofunc
+ (L
->top
- nfunc
); /* correct top */
760 oci
->u
.l
.savedpc
= nci
->u
.l
.savedpc
;
761 oci
->callstatus
|= CIST_TAIL
; /* function was tail called */
762 ci
= L
->ci
= oci
; /* remove new frame */
763 lua_assert(L
->top
== oci
->u
.l
.base
+ getproto(ofunc
)->maxstacksize
);
764 goto newframe
; /* restart luaV_execute over new Lua function */
769 if (b
!= 0) L
->top
= ra
+ b
- 1;
770 if (cl
->p
->sizep
> 0) luaF_close(L
, base
);
771 b
= luaD_poscall(L
, ra
);
772 if (!(ci
->callstatus
& CIST_REENTRY
)) /* 'ci' still the called one */
773 return; /* external invocation: return */
774 else { /* invocation via reentry: continue execution */
776 if (b
) L
->top
= ci
->top
;
777 lua_assert(isLua(ci
));
778 lua_assert(GET_OPCODE(*((ci
)->u
.l
.savedpc
- 1)) == OP_CALL
);
779 goto newframe
; /* restart luaV_execute over new Lua function */
783 lua_Number step
= nvalue(ra
+ 2);
784 lua_Number idx
= luai_numadd(L
, nvalue(ra
), step
); /* increment index */
785 lua_Number limit
= nvalue(ra
+ 1);
786 if (luai_numlt(L
, 0, step
) ? luai_numle(L
, idx
, limit
)
787 : luai_numle(L
, limit
, idx
)) {
788 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
789 setnvalue(ra
, idx
); /* update internal index... */
790 setnvalue(ra
+ 3, idx
); /* ...and external index */
794 const TValue
*init
= ra
;
795 const TValue
*plimit
= ra
+ 1;
796 const TValue
*pstep
= ra
+ 2;
797 if (!tonumber(init
, ra
))
798 luaG_runerror(L
, LUA_QL("for") " initial value must be a number");
799 else if (!tonumber(plimit
, ra
+ 1))
800 luaG_runerror(L
, LUA_QL("for") " limit must be a number");
801 else if (!tonumber(pstep
, ra
+ 2))
802 luaG_runerror(L
, LUA_QL("for") " step must be a number");
803 setnvalue(ra
, luai_numsub(L
, nvalue(ra
), nvalue(pstep
)));
804 ci
->u
.l
.savedpc
+= GETARG_sBx(i
);
806 vmcasenb(OP_TFORCALL
,
807 StkId cb
= ra
+ 3; /* call base */
808 setobjs2s(L
, cb
+ 2, ra
+ 2);
809 setobjs2s(L
, cb
+ 1, ra
+ 1);
810 setobjs2s(L
, cb
, ra
);
811 L
->top
= cb
+ 3; /* func. + 2 args (state and index) */
812 Protect(luaD_call(L
, cb
, GETARG_C(i
), 1));
814 i
= *(ci
->u
.l
.savedpc
++); /* go to next instruction */
816 lua_assert(GET_OPCODE(i
) == OP_TFORLOOP
);
821 if (!ttisnil(ra
+ 1)) { /* continue loop? */
822 setobjs2s(L
, ra
, ra
+ 1); /* save control variable */
823 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
831 if (n
== 0) n
= cast_int(L
->top
- ra
) - 1;
833 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
834 c
= GETARG_Ax(*ci
->u
.l
.savedpc
++);
836 luai_runtimecheck(L
, ttistable(ra
));
838 last
= ((c
- 1) * LFIELDS_PER_FLUSH
) + n
;
839 if (last
> h
->sizearray
) /* needs more space? */
840 luaH_resizearray(L
, h
, last
); /* pre-allocate it at once */
842 TValue
*val
= ra
+ n
;
843 luaH_setint(L
, h
, last
--, val
);
844 luaC_barrierback(L
, obj2gco(h
), val
);
846 L
->top
= ci
->top
; /* correct top (in case of previous open call) */
849 Proto
*p
= cl
->p
->p
[GETARG_Bx(i
)];
850 Closure
*ncl
= getcached(p
, cl
->upvals
, base
); /* cached closure */
851 if (ncl
== NULL
) /* no match? */
852 pushclosure(L
, p
, cl
->upvals
, base
, ra
); /* create a new one */
854 setclLvalue(L
, ra
, ncl
); /* push cashed closure */
858 int b
= GETARG_B(i
) - 1;
860 int n
= cast_int(base
- ci
->func
) - cl
->p
->numparams
- 1;
861 if (b
< 0) { /* B == 0? */
862 b
= n
; /* get all var. arguments */
863 Protect(luaD_checkstack(L
, n
));
864 ra
= RA(i
); /* previous call may change the stack */
867 for (j
= 0; j
< b
; j
++) {
869 setobjs2s(L
, ra
+ j
, base
- n
+ j
);