2 ** $Id: lvm.c,v 2.63 2006/06/05 15:58:59 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
), &num
)) {
47 int luaV_tostring (lua_State
*L
, StkId obj
) {
51 char s
[LUAI_MAXNUMBER2STR
];
52 lua_Number n
= nvalue(obj
);
54 setsvalue2s(L
, obj
, luaS_new(L
, s
));
60 static void traceexec (lua_State
*L
, const Instruction
*pc
) {
61 lu_byte mask
= L
->hookmask
;
62 const Instruction
*oldpc
= L
->savedpc
;
64 if (mask
> LUA_MASKLINE
) { /* instruction-hook set? */
65 if (L
->hookcount
== 0) {
67 luaD_callhook(L
, LUA_HOOKCOUNT
, -1);
70 if (mask
& LUA_MASKLINE
) {
71 Proto
*p
= ci_func(L
->ci
)->l
.p
;
72 int npc
= pcRel(pc
, p
);
73 int newline
= getline(p
, npc
);
74 /* call linehook when enter a new function, when jump back (loop),
75 or when enter a new line */
76 if (npc
== 0 || pc
<= oldpc
|| newline
!= getline(p
, pcRel(oldpc
, p
)))
77 luaD_callhook(L
, LUA_HOOKLINE
, newline
);
82 static void callTMres (lua_State
*L
, StkId res
, const TValue
*f
,
83 const TValue
*p1
, const TValue
*p2
) {
84 ptrdiff_t result
= savestack(L
, res
);
85 setobj2s(L
, L
->top
, f
); /* push function */
86 setobj2s(L
, L
->top
+1, p1
); /* 1st argument */
87 setobj2s(L
, L
->top
+2, p2
); /* 2nd argument */
88 luaD_checkstack(L
, 3);
90 luaD_call(L
, L
->top
- 3, 1);
91 res
= restorestack(L
, result
);
93 setobjs2s(L
, res
, L
->top
);
98 static void callTM (lua_State
*L
, const TValue
*f
, const TValue
*p1
,
99 const TValue
*p2
, const TValue
*p3
) {
100 setobj2s(L
, L
->top
, f
); /* push function */
101 setobj2s(L
, L
->top
+1, p1
); /* 1st argument */
102 setobj2s(L
, L
->top
+2, p2
); /* 2nd argument */
103 setobj2s(L
, L
->top
+3, p3
); /* 3th argument */
104 luaD_checkstack(L
, 4);
106 luaD_call(L
, L
->top
- 4, 0);
110 void luaV_gettable (lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
112 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
114 if (ttistable(t
)) { /* `t' is a table? */
115 Table
*h
= hvalue(t
);
116 const TValue
*res
= luaH_get(h
, key
); /* do a primitive get */
117 if (!ttisnil(res
) || /* result is no nil? */
118 (tm
= fasttm(L
, h
->metatable
, TM_INDEX
)) == NULL
) { /* or no TM? */
119 setobj2s(L
, val
, res
);
122 /* else will try the tag method */
124 else if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_INDEX
)))
125 luaG_typeerror(L
, t
, "index");
126 if (ttisfunction(tm
)) {
127 callTMres(L
, val
, tm
, t
, key
);
130 t
= tm
; /* else repeat with `tm' */
132 luaG_runerror(L
, "loop in gettable");
136 void luaV_settable (lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
138 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
140 if (ttistable(t
)) { /* `t' is a table? */
141 Table
*h
= hvalue(t
);
142 TValue
*oldval
= luaH_set(L
, h
, key
); /* do a primitive set */
143 if (!ttisnil(oldval
) || /* result is no nil? */
144 (tm
= fasttm(L
, h
->metatable
, TM_NEWINDEX
)) == NULL
) { /* or no TM? */
145 setobj2t(L
, oldval
, val
);
146 luaC_barriert(L
, h
, val
);
149 /* else will try the tag method */
151 else if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_NEWINDEX
)))
152 luaG_typeerror(L
, t
, "index");
153 if (ttisfunction(tm
)) {
154 callTM(L
, tm
, t
, key
, val
);
157 t
= tm
; /* else repeat with `tm' */
159 luaG_runerror(L
, "loop in settable");
163 static int call_binTM (lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
164 StkId res
, TMS event
) {
165 const TValue
*tm
= luaT_gettmbyobj(L
, p1
, event
); /* try first operand */
167 tm
= luaT_gettmbyobj(L
, p2
, event
); /* try second operand */
168 if (!ttisfunction(tm
)) return 0;
169 callTMres(L
, res
, tm
, p1
, p2
);
174 static const TValue
*get_compTM (lua_State
*L
, Table
*mt1
, Table
*mt2
,
176 const TValue
*tm1
= fasttm(L
, mt1
, event
);
178 if (tm1
== NULL
) return NULL
; /* no metamethod */
179 if (mt1
== mt2
) return tm1
; /* same metatables => same metamethods */
180 tm2
= fasttm(L
, mt2
, event
);
181 if (tm2
== NULL
) return NULL
; /* no metamethod */
182 if (luaO_rawequalObj(tm1
, tm2
)) /* same metamethods? */
188 static int call_orderTM (lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
190 const TValue
*tm1
= luaT_gettmbyobj(L
, p1
, event
);
192 if (ttisnil(tm1
)) return -1; /* no metamethod? */
193 tm2
= luaT_gettmbyobj(L
, p2
, event
);
194 if (!luaO_rawequalObj(tm1
, tm2
)) /* different metamethods? */
196 callTMres(L
, L
->top
, tm1
, p1
, p2
);
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 (ttype(l
) != ttype(r
))
226 return luaG_ordererror(L
, l
, r
);
227 else if (ttisnumber(l
))
228 return luai_numlt(nvalue(l
), nvalue(r
));
229 else if (ttisstring(l
))
230 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) < 0;
231 else if ((res
= call_orderTM(L
, l
, r
, TM_LT
)) != -1)
233 return luaG_ordererror(L
, l
, r
);
237 static int lessequal (lua_State
*L
, const TValue
*l
, const TValue
*r
) {
239 if (ttype(l
) != ttype(r
))
240 return luaG_ordererror(L
, l
, r
);
241 else if (ttisnumber(l
))
242 return luai_numle(nvalue(l
), nvalue(r
));
243 else if (ttisstring(l
))
244 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) <= 0;
245 else if ((res
= call_orderTM(L
, l
, r
, TM_LE
)) != -1) /* first try `le' */
247 else if ((res
= call_orderTM(L
, r
, l
, TM_LT
)) != -1) /* else try `lt' */
249 return luaG_ordererror(L
, l
, r
);
253 int luaV_equalval (lua_State
*L
, const TValue
*t1
, const TValue
*t2
) {
255 lua_assert(ttype(t1
) == ttype(t2
));
257 case LUA_TNIL
: return 1;
258 case LUA_TNUMBER
: return luai_numeq(nvalue(t1
), nvalue(t2
));
259 case LUA_TBOOLEAN
: return bvalue(t1
) == bvalue(t2
); /* true must be 1 !! */
260 case LUA_TLIGHTUSERDATA
: return pvalue(t1
) == pvalue(t2
);
261 case LUA_TUSERDATA
: {
262 if (uvalue(t1
) == uvalue(t2
)) return 1;
263 tm
= get_compTM(L
, uvalue(t1
)->metatable
, uvalue(t2
)->metatable
,
265 break; /* will try TM */
268 if (hvalue(t1
) == hvalue(t2
)) return 1;
269 tm
= get_compTM(L
, hvalue(t1
)->metatable
, hvalue(t2
)->metatable
, TM_EQ
);
270 break; /* will try TM */
272 default: return gcvalue(t1
) == gcvalue(t2
);
274 if (tm
== NULL
) return 0; /* no TM? */
275 callTMres(L
, L
->top
, tm
, t1
, t2
); /* call TM */
276 return !l_isfalse(L
->top
);
280 void luaV_concat (lua_State
*L
, int total
, int last
) {
282 StkId top
= L
->base
+ last
+ 1;
283 int n
= 2; /* number of elements handled in this pass (at least 2) */
284 if (!tostring(L
, top
-2) || !tostring(L
, top
-1)) {
285 if (!call_binTM(L
, top
-2, top
-1, top
-2, TM_CONCAT
))
286 luaG_concaterror(L
, top
-2, top
-1);
287 } else if (tsvalue(top
-1)->len
> 0) { /* if len=0, do nothing */
288 /* at least two string values; get as many as possible */
289 size_t tl
= tsvalue(top
-1)->len
;
292 /* collect total length */
293 for (n
= 1; n
< total
&& tostring(L
, top
-n
-1); n
++) {
294 size_t l
= tsvalue(top
-n
-1)->len
;
295 if (l
>= MAX_SIZET
- tl
) luaG_runerror(L
, "string length overflow");
298 buffer
= luaZ_openspace(L
, &G(L
)->buff
, tl
);
300 for (i
=n
; i
>0; i
--) { /* concat all strings */
301 size_t l
= tsvalue(top
-i
)->len
;
302 memcpy(buffer
+tl
, svalue(top
-i
), l
);
305 setsvalue2s(L
, top
-n
, luaS_newlstr(L
, buffer
, tl
));
307 total
-= n
-1; /* got `n' strings to create 1 new */
309 } while (total
> 1); /* repeat until only 1 result left */
313 static void Arith (lua_State
*L
, StkId ra
, const TValue
*rb
,
314 const TValue
*rc
, TMS op
) {
317 if ((b
= luaV_tonumber(rb
, &tempb
)) != NULL
&&
318 (c
= luaV_tonumber(rc
, &tempc
)) != NULL
) {
319 lua_Number nb
= nvalue(b
), nc
= nvalue(c
);
321 case TM_ADD
: setnvalue(ra
, luai_numadd(nb
, nc
)); break;
322 case TM_SUB
: setnvalue(ra
, luai_numsub(nb
, nc
)); break;
323 case TM_MUL
: setnvalue(ra
, luai_nummul(nb
, nc
)); break;
324 case TM_DIV
: setnvalue(ra
, luai_numdiv(nb
, nc
)); break;
325 case TM_MOD
: setnvalue(ra
, luai_nummod(nb
, nc
)); break;
326 case TM_POW
: setnvalue(ra
, luai_numpow(nb
, nc
)); break;
327 case TM_UNM
: setnvalue(ra
, luai_numunm(nb
)); break;
328 default: lua_assert(0); break;
331 else if (!call_binTM(L
, rb
, rc
, ra
, op
))
332 luaG_aritherror(L
, rb
, rc
);
338 ** some macros for common tasks in `luaV_execute'
341 #define runtime_check(L, c) { if (!(c)) break; }
343 #define RA(i) (base+GETARG_A(i))
344 /* to be used after possible stack reallocation */
345 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
346 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
347 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
348 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
349 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
350 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
351 #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
354 #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
357 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
360 #define arith_op(op,tm) { \
361 TValue *rb = RKB(i); \
362 TValue *rc = RKC(i); \
363 if (ttisnumber(rb) && ttisnumber(rc)) { \
364 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
365 setnvalue(ra, op(nb, nc)); \
368 Protect(Arith(L, ra, rb, rc, tm)); \
373 void luaV_execute (lua_State
*L
, int nexeccalls
) {
377 const Instruction
*pc
;
378 reentry
: /* entry point */
379 lua_assert(isLua(L
->ci
));
381 cl
= &clvalue(L
->ci
->func
)->l
;
384 /* main loop of interpreter */
386 const Instruction i
= *pc
++;
388 if ((L
->hookmask
& (LUA_MASKLINE
| LUA_MASKCOUNT
)) &&
389 (--L
->hookcount
== 0 || L
->hookmask
& LUA_MASKLINE
)) {
391 if (L
->status
== LUA_YIELD
) { /* did hook yield? */
397 /* warning!! several calls may realloc the stack and invalidate `ra' */
399 lua_assert(base
== L
->base
&& L
->base
== L
->ci
->base
);
400 lua_assert(base
<= L
->top
&& L
->top
<= L
->stack
+ L
->stacksize
);
401 lua_assert(L
->top
== L
->ci
->top
|| luaG_checkopenop(i
));
402 switch (GET_OPCODE(i
)) {
404 setobjs2s(L
, ra
, RB(i
));
408 setobj2s(L
, ra
, KBx(i
));
412 setbvalue(ra
, GETARG_B(i
));
413 if (GETARG_C(i
)) pc
++; /* skip next instruction (if C) */
425 setobj2s(L
, ra
, cl
->upvals
[b
]->v
);
431 sethvalue(L
, &g
, cl
->env
);
432 lua_assert(ttisstring(rb
));
433 Protect(luaV_gettable(L
, &g
, rb
, ra
));
437 Protect(luaV_gettable(L
, RB(i
), RKC(i
), ra
));
442 sethvalue(L
, &g
, cl
->env
);
443 lua_assert(ttisstring(KBx(i
)));
444 Protect(luaV_settable(L
, &g
, KBx(i
), ra
));
448 UpVal
*uv
= cl
->upvals
[GETARG_B(i
)];
449 setobj(L
, uv
->v
, ra
);
450 luaC_barrier(L
, uv
, ra
);
454 Protect(luaV_settable(L
, ra
, RKB(i
), RKC(i
)));
460 sethvalue(L
, ra
, luaH_new(L
, luaO_fb2int(b
), luaO_fb2int(c
)));
461 Protect(luaC_checkGC(L
));
466 setobjs2s(L
, ra
+1, rb
);
467 Protect(luaV_gettable(L
, rb
, RKC(i
), ra
));
471 arith_op(luai_numadd
, TM_ADD
);
475 arith_op(luai_numsub
, TM_SUB
);
479 arith_op(luai_nummul
, TM_MUL
);
483 arith_op(luai_numdiv
, TM_DIV
);
487 arith_op(luai_nummod
, TM_MOD
);
491 arith_op(luai_numpow
, TM_POW
);
496 if (ttisnumber(rb
)) {
497 lua_Number nb
= nvalue(rb
);
498 setnvalue(ra
, luai_numunm(nb
));
501 Protect(Arith(L
, ra
, rb
, rb
, TM_UNM
));
506 int res
= l_isfalse(RB(i
)); /* next assignment may change this value */
511 const TValue
*rb
= RB(i
);
514 setnvalue(ra
, cast_num(luaH_getn(hvalue(rb
))));
518 setnvalue(ra
, cast_num(tsvalue(rb
)->len
));
521 default: { /* try metamethod */
523 if (!call_binTM(L
, rb
, luaO_nilobject
, ra
, TM_LEN
))
524 luaG_typeerror(L
, rb
, "get length of");
533 Protect(luaV_concat(L
, c
-b
+1, c
); luaC_checkGC(L
));
534 setobjs2s(L
, RA(i
), base
+b
);
538 dojump(L
, pc
, GETARG_sBx(i
));
545 if (equalobj(L
, rb
, rc
) == GETARG_A(i
))
546 dojump(L
, pc
, GETARG_sBx(*pc
));
553 if (luaV_lessthan(L
, RKB(i
), RKC(i
)) == GETARG_A(i
))
554 dojump(L
, pc
, GETARG_sBx(*pc
));
561 if (lessequal(L
, RKB(i
), RKC(i
)) == GETARG_A(i
))
562 dojump(L
, pc
, GETARG_sBx(*pc
));
568 if (l_isfalse(ra
) != GETARG_C(i
))
569 dojump(L
, pc
, GETARG_sBx(*pc
));
575 if (l_isfalse(rb
) != GETARG_C(i
)) {
576 setobjs2s(L
, ra
, rb
);
577 dojump(L
, pc
, GETARG_sBx(*pc
));
584 int nresults
= GETARG_C(i
) - 1;
585 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
587 switch (luaD_precall(L
, ra
, nresults
)) {
590 goto reentry
; /* restart luaV_execute over new Lua function */
593 /* it was a C function (`precall' called it); adjust results */
594 if (nresults
>= 0) L
->top
= L
->ci
->top
;
605 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
607 lua_assert(GETARG_C(i
) - 1 == LUA_MULTRET
);
608 switch (luaD_precall(L
, ra
, LUA_MULTRET
)) {
610 /* tail call: put new frame in place of previous one */
611 CallInfo
*ci
= L
->ci
- 1; /* previous frame */
613 StkId func
= ci
->func
;
614 StkId pfunc
= (ci
+1)->func
; /* previous function index */
615 if (L
->openupval
) luaF_close(L
, ci
->base
);
616 L
->base
= ci
->base
= ci
->func
+ ((ci
+1)->base
- pfunc
);
617 for (aux
= 0; pfunc
+aux
< L
->top
; aux
++) /* move frame down */
618 setobjs2s(L
, func
+aux
, pfunc
+aux
);
619 ci
->top
= L
->top
= func
+aux
; /* correct top */
620 lua_assert(L
->top
== L
->base
+ clvalue(func
)->l
.p
->maxstacksize
);
621 ci
->savedpc
= L
->savedpc
;
622 ci
->tailcalls
++; /* one more call lost */
623 L
->ci
--; /* remove new frame */
626 case PCRC
: { /* it was a C function (`precall' called it) */
637 if (b
!= 0) L
->top
= ra
+b
-1;
638 if (L
->openupval
) luaF_close(L
, base
);
640 b
= luaD_poscall(L
, ra
);
641 if (--nexeccalls
== 0) /* was previous function running `here'? */
642 return; /* no: return */
643 else { /* yes: continue its execution */
644 if (b
) L
->top
= L
->ci
->top
;
645 lua_assert(isLua(L
->ci
));
646 lua_assert(GET_OPCODE(*((L
->ci
)->savedpc
- 1)) == OP_CALL
);
651 lua_Number step
= nvalue(ra
+2);
652 lua_Number idx
= luai_numadd(nvalue(ra
), step
); /* increment index */
653 lua_Number limit
= nvalue(ra
+1);
654 if (luai_numlt(0, step
) ? luai_numle(idx
, limit
)
655 : luai_numle(limit
, idx
)) {
656 dojump(L
, pc
, GETARG_sBx(i
)); /* jump back */
657 setnvalue(ra
, idx
); /* update internal index... */
658 setnvalue(ra
+3, idx
); /* ...and external index */
663 const TValue
*init
= ra
;
664 const TValue
*plimit
= ra
+1;
665 const TValue
*pstep
= ra
+2;
666 L
->savedpc
= pc
; /* next steps may throw errors */
667 if (!tonumber(init
, ra
))
668 luaG_runerror(L
, LUA_QL("for") " initial value must be a number");
669 else if (!tonumber(plimit
, ra
+1))
670 luaG_runerror(L
, LUA_QL("for") " limit must be a number");
671 else if (!tonumber(pstep
, ra
+2))
672 luaG_runerror(L
, LUA_QL("for") " step must be a number");
673 setnvalue(ra
, luai_numsub(nvalue(ra
), nvalue(pstep
)));
674 dojump(L
, pc
, GETARG_sBx(i
));
678 StkId cb
= ra
+ 3; /* call base */
679 setobjs2s(L
, cb
+2, ra
+2);
680 setobjs2s(L
, cb
+1, ra
+1);
681 setobjs2s(L
, cb
, ra
);
682 L
->top
= cb
+3; /* func. + 2 args (state and index) */
683 Protect(luaD_call(L
, cb
, GETARG_C(i
)));
685 cb
= RA(i
) + 3; /* previous call may change the stack */
686 if (!ttisnil(cb
)) { /* continue loop? */
687 setobjs2s(L
, cb
-1, cb
); /* save control variable */
688 dojump(L
, pc
, GETARG_sBx(*pc
)); /* jump back */
699 n
= cast_int(L
->top
- ra
) - 1;
702 if (c
== 0) c
= cast_int(*pc
++);
703 runtime_check(L
, ttistable(ra
));
705 last
= ((c
-1)*LFIELDS_PER_FLUSH
) + n
;
706 if (last
> h
->sizearray
) /* needs more space? */
707 luaH_resizearray(L
, h
, last
); /* pre-alloc it at once */
710 setobj2t(L
, luaH_setnum(L
, h
, last
--), val
);
711 luaC_barriert(L
, h
, val
);
723 p
= cl
->p
->p
[GETARG_Bx(i
)];
725 ncl
= luaF_newLclosure(L
, nup
, cl
->env
);
727 for (j
=0; j
<nup
; j
++, pc
++) {
728 if (GET_OPCODE(*pc
) == OP_GETUPVAL
)
729 ncl
->l
.upvals
[j
] = cl
->upvals
[GETARG_B(*pc
)];
731 lua_assert(GET_OPCODE(*pc
) == OP_MOVE
);
732 ncl
->l
.upvals
[j
] = luaF_findupval(L
, base
+ GETARG_B(*pc
));
735 setclvalue(L
, ra
, ncl
);
736 Protect(luaC_checkGC(L
));
740 int b
= GETARG_B(i
) - 1;
742 CallInfo
*ci
= L
->ci
;
743 int n
= cast_int(ci
->base
- ci
->func
) - cl
->p
->numparams
- 1;
744 if (b
== LUA_MULTRET
) {
745 Protect(luaD_checkstack(L
, n
));
746 ra
= RA(i
); /* previous call may change the stack */
750 for (j
= 0; j
< b
; j
++) {
752 setobjs2s(L
, ra
+ j
, ci
->base
- n
+ j
);