]>
git.proxmox.com Git - ceph.git/blob - ceph/src/lua/src/lvm.c
2 ** $Id: lvm.c,v 2.265 2015/11/23 11:30:45 roberto Exp $
4 ** See Copyright Notice in lua.h
34 /* limit for table tag-method chains (to avoid loops) */
35 #define MAXTAGLOOP 2000
40 ** 'l_intfitsf' checks whether a given integer can be converted to a
41 ** float without rounding. Used in comparisons. Left undefined if
42 ** all integers fit in a float precisely.
44 #if !defined(l_intfitsf)
46 /* number of bits in the mantissa of a float */
47 #define NBM (l_mathlim(MANT_DIG))
50 ** Check whether some integers may not fit in a float, that is, whether
51 ** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger).
52 ** (The shifts are done in parts to avoid shifting by more than the size
53 ** of an integer. In a worst case, NBM == 113 for long double and
54 ** sizeof(integer) == 32.)
56 #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
57 >> (NBM - (3 * (NBM / 4)))) > 0
59 #define l_intfitsf(i) \
60 (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM))
69 ** Try to convert a value to a float. The float case is already handled
70 ** by the macro 'tonumber'.
72 int luaV_tonumber_ (const TValue
*obj
, lua_Number
*n
) {
74 if (ttisinteger(obj
)) {
75 *n
= cast_num(ivalue(obj
));
78 else if (cvt2num(obj
) && /* string convertible to number? */
79 luaO_str2num(svalue(obj
), &v
) == vslen(obj
) + 1) {
80 *n
= nvalue(&v
); /* convert result of 'luaO_str2num' to a float */
84 return 0; /* conversion failed */
89 ** try to convert a value to an integer, rounding according to 'mode':
90 ** mode == 0: accepts only integral values
91 ** mode == 1: takes the floor of the number
92 ** mode == 2: takes the ceil of the number
94 int luaV_tointeger (const TValue
*obj
, lua_Integer
*p
, int mode
) {
98 lua_Number n
= fltvalue(obj
);
99 lua_Number f
= l_floor(n
);
100 if (n
!= f
) { /* not an integral value? */
101 if (mode
== 0) return 0; /* fails if mode demands integral value */
102 else if (mode
> 1) /* needs ceil? */
103 f
+= 1; /* convert floor to ceil (remember: n != f) */
105 return lua_numbertointeger(f
, p
);
107 else if (ttisinteger(obj
)) {
111 else if (cvt2num(obj
) &&
112 luaO_str2num(svalue(obj
), &v
) == vslen(obj
) + 1) {
114 goto again
; /* convert result from 'luaO_str2num' to an integer */
116 return 0; /* conversion failed */
121 ** Try to convert a 'for' limit to an integer, preserving the
122 ** semantics of the loop.
123 ** (The following explanation assumes a non-negative step; it is valid
124 ** for negative steps mutatis mutandis.)
125 ** If the limit can be converted to an integer, rounding down, that is
127 ** Otherwise, check whether the limit can be converted to a number. If
128 ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER,
129 ** which means no limit. If the number is too negative, the loop
130 ** should not run, because any initial integer value is larger than the
131 ** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects
132 ** the extreme case when the initial value is LUA_MININTEGER, in which
133 ** case the LUA_MININTEGER limit would still run the loop once.
135 static int forlimit (const TValue
*obj
, lua_Integer
*p
, lua_Integer step
,
137 *stopnow
= 0; /* usually, let loops run */
138 if (!luaV_tointeger(obj
, p
, (step
< 0 ? 2 : 1))) { /* not fit in integer? */
139 lua_Number n
; /* try to convert to float */
140 if (!tonumber(obj
, &n
)) /* cannot convert to float? */
141 return 0; /* not a number */
142 if (luai_numlt(0, n
)) { /* if true, float is larger than max integer */
144 if (step
< 0) *stopnow
= 1;
146 else { /* float is smaller than min integer */
148 if (step
>= 0) *stopnow
= 1;
156 ** Complete a table access: if 't' is a table, 'tm' has its metamethod;
157 ** otherwise, 'tm' is NULL.
159 void luaV_finishget (lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
,
161 int loop
; /* counter to avoid infinite loops */
162 lua_assert(tm
!= NULL
|| !ttistable(t
));
163 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
164 if (tm
== NULL
) { /* no metamethod (from a table)? */
165 if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_INDEX
)))
166 luaG_typeerror(L
, t
, "index"); /* no metamethod */
168 if (ttisfunction(tm
)) { /* metamethod is a function */
169 luaT_callTM(L
, tm
, t
, key
, val
, 1); /* call it */
172 t
= tm
; /* else repeat access over 'tm' */
173 if (luaV_fastget(L
,t
,key
,tm
,luaH_get
)) { /* try fast track */
174 setobj2s(L
, val
, tm
); /* done */
179 luaG_runerror(L
, "gettable chain too long; possible loop");
184 ** Main function for table assignment (invoking metamethods if needed).
185 ** Compute 't[key] = val'
187 void luaV_finishset (lua_State
*L
, const TValue
*t
, TValue
*key
,
188 StkId val
, const TValue
*oldval
) {
189 int loop
; /* counter to avoid infinite loops */
190 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
192 if (oldval
!= NULL
) {
193 lua_assert(ttistable(t
) && ttisnil(oldval
));
194 /* must check the metamethod */
195 if ((tm
= fasttm(L
, hvalue(t
)->metatable
, TM_NEWINDEX
)) == NULL
&&
196 /* no metamethod; is there a previous entry in the table? */
197 (oldval
!= luaO_nilobject
||
198 /* no previous entry; must create one. (The next test is
199 always true; we only need the assignment.) */
200 (oldval
= luaH_newkey(L
, hvalue(t
), key
), 1))) {
201 /* no metamethod and (now) there is an entry with given key */
202 setobj2t(L
, cast(TValue
*, oldval
), val
);
203 invalidateTMcache(hvalue(t
));
204 luaC_barrierback(L
, hvalue(t
), val
);
207 /* else will try the metamethod */
209 else { /* not a table; check metamethod */
210 if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_NEWINDEX
)))
211 luaG_typeerror(L
, t
, "index");
213 /* try the metamethod */
214 if (ttisfunction(tm
)) {
215 luaT_callTM(L
, tm
, t
, key
, val
, 0);
218 t
= tm
; /* else repeat assignment over 'tm' */
219 if (luaV_fastset(L
, t
, key
, oldval
, luaH_get
, val
))
223 luaG_runerror(L
, "settable chain too long; possible loop");
228 ** Compare two strings 'ls' x 'rs', returning an integer smaller-equal-
229 ** -larger than zero if 'ls' is smaller-equal-larger than 'rs'.
230 ** The code is a little tricky because it allows '\0' in the strings
231 ** and it uses 'strcoll' (to respect locales) for each segments
234 static int l_strcmp (const TString
*ls
, const TString
*rs
) {
235 const char *l
= getstr(ls
);
236 size_t ll
= tsslen(ls
);
237 const char *r
= getstr(rs
);
238 size_t lr
= tsslen(rs
);
239 for (;;) { /* for each segment */
240 int temp
= strcoll(l
, r
);
241 if (temp
!= 0) /* not equal? */
242 return temp
; /* done */
243 else { /* strings are equal up to a '\0' */
244 size_t len
= strlen(l
); /* index of first '\0' in both strings */
245 if (len
== lr
) /* 'rs' is finished? */
246 return (len
== ll
) ? 0 : 1; /* check 'ls' */
247 else if (len
== ll
) /* 'ls' is finished? */
248 return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */
249 /* both strings longer than 'len'; go on comparing after the '\0' */
251 l
+= len
; ll
-= len
; r
+= len
; lr
-= len
;
258 ** Check whether integer 'i' is less than float 'f'. If 'i' has an
259 ** exact representation as a float ('l_intfitsf'), compare numbers as
260 ** floats. Otherwise, if 'f' is outside the range for integers, result
261 ** is trivial. Otherwise, compare them as integers. (When 'i' has no
262 ** float representation, either 'f' is "far away" from 'i' or 'f' has
263 ** no precision left for a fractional part; either way, how 'f' is
264 ** truncated is irrelevant.) When 'f' is NaN, comparisons must result
267 static int LTintfloat (lua_Integer i
, lua_Number f
) {
268 #if defined(l_intfitsf)
269 if (!l_intfitsf(i
)) {
270 if (f
>= -cast_num(LUA_MININTEGER
)) /* -minint == maxint + 1 */
271 return 1; /* f >= maxint + 1 > i */
272 else if (f
> cast_num(LUA_MININTEGER
)) /* minint < f <= maxint ? */
273 return (i
< cast(lua_Integer
, f
)); /* compare them as integers */
274 else /* f <= minint <= i (or 'f' is NaN) --> not(i < f) */
278 return luai_numlt(cast_num(i
), f
); /* compare them as floats */
283 ** Check whether integer 'i' is less than or equal to float 'f'.
284 ** See comments on previous function.
286 static int LEintfloat (lua_Integer i
, lua_Number f
) {
287 #if defined(l_intfitsf)
288 if (!l_intfitsf(i
)) {
289 if (f
>= -cast_num(LUA_MININTEGER
)) /* -minint == maxint + 1 */
290 return 1; /* f >= maxint + 1 > i */
291 else if (f
>= cast_num(LUA_MININTEGER
)) /* minint <= f <= maxint ? */
292 return (i
<= cast(lua_Integer
, f
)); /* compare them as integers */
293 else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */
297 return luai_numle(cast_num(i
), f
); /* compare them as floats */
302 ** Return 'l < r', for numbers.
304 static int LTnum (const TValue
*l
, const TValue
*r
) {
305 if (ttisinteger(l
)) {
306 lua_Integer li
= ivalue(l
);
308 return li
< ivalue(r
); /* both are integers */
309 else /* 'l' is int and 'r' is float */
310 return LTintfloat(li
, fltvalue(r
)); /* l < r ? */
313 lua_Number lf
= fltvalue(l
); /* 'l' must be float */
315 return luai_numlt(lf
, fltvalue(r
)); /* both are float */
316 else if (luai_numisnan(lf
)) /* 'r' is int and 'l' is float */
317 return 0; /* NaN < i is always false */
318 else /* without NaN, (l < r) <--> not(r <= l) */
319 return !LEintfloat(ivalue(r
), lf
); /* not (r <= l) ? */
325 ** Return 'l <= r', for numbers.
327 static int LEnum (const TValue
*l
, const TValue
*r
) {
328 if (ttisinteger(l
)) {
329 lua_Integer li
= ivalue(l
);
331 return li
<= ivalue(r
); /* both are integers */
332 else /* 'l' is int and 'r' is float */
333 return LEintfloat(li
, fltvalue(r
)); /* l <= r ? */
336 lua_Number lf
= fltvalue(l
); /* 'l' must be float */
338 return luai_numle(lf
, fltvalue(r
)); /* both are float */
339 else if (luai_numisnan(lf
)) /* 'r' is int and 'l' is float */
340 return 0; /* NaN <= i is always false */
341 else /* without NaN, (l <= r) <--> not(r < l) */
342 return !LTintfloat(ivalue(r
), lf
); /* not (r < l) ? */
348 ** Main operation less than; return 'l < r'.
350 int luaV_lessthan (lua_State
*L
, const TValue
*l
, const TValue
*r
) {
352 if (ttisnumber(l
) && ttisnumber(r
)) /* both operands are numbers? */
354 else if (ttisstring(l
) && ttisstring(r
)) /* both are strings? */
355 return l_strcmp(tsvalue(l
), tsvalue(r
)) < 0;
356 else if ((res
= luaT_callorderTM(L
, l
, r
, TM_LT
)) < 0) /* no metamethod? */
357 luaG_ordererror(L
, l
, r
); /* error */
363 ** Main operation less than or equal to; return 'l <= r'. If it needs
364 ** a metamethod and there is no '__le', try '__lt', based on
365 ** l <= r iff !(r < l) (assuming a total order). If the metamethod
366 ** yields during this substitution, the continuation has to know
367 ** about it (to negate the result of r<l); bit CIST_LEQ in the call
368 ** status keeps that information.
370 int luaV_lessequal (lua_State
*L
, const TValue
*l
, const TValue
*r
) {
372 if (ttisnumber(l
) && ttisnumber(r
)) /* both operands are numbers? */
374 else if (ttisstring(l
) && ttisstring(r
)) /* both are strings? */
375 return l_strcmp(tsvalue(l
), tsvalue(r
)) <= 0;
376 else if ((res
= luaT_callorderTM(L
, l
, r
, TM_LE
)) >= 0) /* try 'le' */
378 else { /* try 'lt': */
379 L
->ci
->callstatus
|= CIST_LEQ
; /* mark it is doing 'lt' for 'le' */
380 res
= luaT_callorderTM(L
, r
, l
, TM_LT
);
381 L
->ci
->callstatus
^= CIST_LEQ
; /* clear mark */
383 luaG_ordererror(L
, l
, r
);
384 return !res
; /* result is negated */
390 ** Main operation for equality of Lua values; return 't1 == t2'.
391 ** L == NULL means raw equality (no metamethods)
393 int luaV_equalobj (lua_State
*L
, const TValue
*t1
, const TValue
*t2
) {
395 if (ttype(t1
) != ttype(t2
)) { /* not the same variant? */
396 if (ttnov(t1
) != ttnov(t2
) || ttnov(t1
) != LUA_TNUMBER
)
397 return 0; /* only numbers can be equal with different variants */
398 else { /* two numbers with different variants */
399 lua_Integer i1
, i2
; /* compare them as integers */
400 return (tointeger(t1
, &i1
) && tointeger(t2
, &i2
) && i1
== i2
);
403 /* values have same type and same variant */
405 case LUA_TNIL
: return 1;
406 case LUA_TNUMINT
: return (ivalue(t1
) == ivalue(t2
));
407 case LUA_TNUMFLT
: return luai_numeq(fltvalue(t1
), fltvalue(t2
));
408 case LUA_TBOOLEAN
: return bvalue(t1
) == bvalue(t2
); /* true must be 1 !! */
409 case LUA_TLIGHTUSERDATA
: return pvalue(t1
) == pvalue(t2
);
410 case LUA_TLCF
: return fvalue(t1
) == fvalue(t2
);
411 case LUA_TSHRSTR
: return eqshrstr(tsvalue(t1
), tsvalue(t2
));
412 case LUA_TLNGSTR
: return luaS_eqlngstr(tsvalue(t1
), tsvalue(t2
));
413 case LUA_TUSERDATA
: {
414 if (uvalue(t1
) == uvalue(t2
)) return 1;
415 else if (L
== NULL
) return 0;
416 tm
= fasttm(L
, uvalue(t1
)->metatable
, TM_EQ
);
418 tm
= fasttm(L
, uvalue(t2
)->metatable
, TM_EQ
);
419 break; /* will try TM */
422 if (hvalue(t1
) == hvalue(t2
)) return 1;
423 else if (L
== NULL
) return 0;
424 tm
= fasttm(L
, hvalue(t1
)->metatable
, TM_EQ
);
426 tm
= fasttm(L
, hvalue(t2
)->metatable
, TM_EQ
);
427 break; /* will try TM */
430 return gcvalue(t1
) == gcvalue(t2
);
432 if (tm
== NULL
) /* no TM? */
433 return 0; /* objects are different */
434 luaT_callTM(L
, tm
, t1
, t2
, L
->top
, 1); /* call TM */
435 return !l_isfalse(L
->top
);
439 /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
440 #define tostring(L,o) \
441 (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
443 #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
445 /* copy strings in stack from top - n up to top - 1 to buffer */
446 static void copy2buff (StkId top
, int n
, char *buff
) {
447 size_t tl
= 0; /* size already copied */
449 size_t l
= vslen(top
- n
); /* length of string being copied */
450 memcpy(buff
+ tl
, svalue(top
- n
), l
* sizeof(char));
457 ** Main operation for concatenation: concat 'total' values in the stack,
458 ** from 'L->top - total' up to 'L->top - 1'.
460 void luaV_concat (lua_State
*L
, int total
) {
461 lua_assert(total
>= 2);
464 int n
= 2; /* number of elements handled in this pass (at least 2) */
465 if (!(ttisstring(top
-2) || cvt2str(top
-2)) || !tostring(L
, top
-1))
466 luaT_trybinTM(L
, top
-2, top
-1, top
-2, TM_CONCAT
);
467 else if (isemptystr(top
- 1)) /* second operand is empty? */
468 cast_void(tostring(L
, top
- 2)); /* result is first operand */
469 else if (isemptystr(top
- 2)) { /* first operand is an empty string? */
470 setobjs2s(L
, top
- 2, top
- 1); /* result is second op. */
473 /* at least two non-empty string values; get as many as possible */
474 size_t tl
= vslen(top
- 1);
476 /* collect total length and number of strings */
477 for (n
= 1; n
< total
&& tostring(L
, top
- n
- 1); n
++) {
478 size_t l
= vslen(top
- n
- 1);
479 if (l
>= (MAX_SIZE
/sizeof(char)) - tl
)
480 luaG_runerror(L
, "string length overflow");
483 if (tl
<= LUAI_MAXSHORTLEN
) { /* is result a short string? */
484 char buff
[LUAI_MAXSHORTLEN
];
485 copy2buff(top
, n
, buff
); /* copy strings to buffer */
486 ts
= luaS_newlstr(L
, buff
, tl
);
488 else { /* long string; copy strings directly to final result */
489 ts
= luaS_createlngstrobj(L
, tl
);
490 copy2buff(top
, n
, getstr(ts
));
492 setsvalue2s(L
, top
- n
, ts
); /* create result */
494 total
-= n
-1; /* got 'n' strings to create 1 new */
495 L
->top
-= n
-1; /* popped 'n' strings and pushed one */
496 } while (total
> 1); /* repeat until only 1 result left */
501 ** Main operation 'ra' = #rb'.
503 void luaV_objlen (lua_State
*L
, StkId ra
, const TValue
*rb
) {
507 Table
*h
= hvalue(rb
);
508 tm
= fasttm(L
, h
->metatable
, TM_LEN
);
509 if (tm
) break; /* metamethod? break switch to call it */
510 setivalue(ra
, luaH_getn(h
)); /* else primitive len */
514 setivalue(ra
, tsvalue(rb
)->shrlen
);
518 setivalue(ra
, tsvalue(rb
)->u
.lnglen
);
521 default: { /* try metamethod */
522 tm
= luaT_gettmbyobj(L
, rb
, TM_LEN
);
523 if (ttisnil(tm
)) /* no metamethod? */
524 luaG_typeerror(L
, rb
, "get length of");
528 luaT_callTM(L
, tm
, rb
, rb
, ra
, 1);
533 ** Integer division; return 'm // n', that is, floor(m/n).
534 ** C division truncates its result (rounds towards zero).
535 ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
536 ** otherwise 'floor(q) == trunc(q) - 1'.
538 lua_Integer
luaV_div (lua_State
*L
, lua_Integer m
, lua_Integer n
) {
539 if (l_castS2U(n
) + 1u <= 1u) { /* special cases: -1 or 0 */
541 luaG_runerror(L
, "attempt to divide by zero");
542 return intop(-, 0, m
); /* n==-1; avoid overflow with 0x80000...//-1 */
545 lua_Integer q
= m
/ n
; /* perform C division */
546 if ((m
^ n
) < 0 && m
% n
!= 0) /* 'm/n' would be negative non-integer? */
547 q
-= 1; /* correct result for different rounding */
554 ** Integer modulus; return 'm % n'. (Assume that C '%' with
555 ** negative operands follows C99 behavior. See previous comment
558 lua_Integer
luaV_mod (lua_State
*L
, lua_Integer m
, lua_Integer n
) {
559 if (l_castS2U(n
) + 1u <= 1u) { /* special cases: -1 or 0 */
561 luaG_runerror(L
, "attempt to perform 'n%%0'");
562 return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
565 lua_Integer r
= m
% n
;
566 if (r
!= 0 && (m
^ n
) < 0) /* 'm/n' would be non-integer negative? */
567 r
+= n
; /* correct result for different rounding */
573 /* number of bits in an integer */
574 #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
577 ** Shift left operation. (Shift right just negates 'y'.)
579 lua_Integer
luaV_shiftl (lua_Integer x
, lua_Integer y
) {
580 if (y
< 0) { /* shift right? */
581 if (y
<= -NBITS
) return 0;
582 else return intop(>>, x
, -y
);
584 else { /* shift left */
585 if (y
>= NBITS
) return 0;
586 else return intop(<<, x
, y
);
592 ** check whether cached closure in prototype 'p' may be reused, that is,
593 ** whether there is a cached closure with the same upvalues needed by
594 ** new closure to be created.
596 static LClosure
*getcached (Proto
*p
, UpVal
**encup
, StkId base
) {
597 LClosure
*c
= p
->cache
;
598 if (c
!= NULL
) { /* is there a cached closure? */
599 int nup
= p
->sizeupvalues
;
600 Upvaldesc
*uv
= p
->upvalues
;
602 for (i
= 0; i
< nup
; i
++) { /* check whether it has right upvalues */
603 TValue
*v
= uv
[i
].instack
? base
+ uv
[i
].idx
: encup
[uv
[i
].idx
]->v
;
604 if (c
->upvals
[i
]->v
!= v
)
605 return NULL
; /* wrong upvalue; cannot reuse closure */
608 return c
; /* return cached closure (or NULL if no cached closure) */
613 ** create a new Lua closure, push it in the stack, and initialize
614 ** its upvalues. Note that the closure is not cached if prototype is
615 ** already black (which means that 'cache' was already cleared by the
618 static void pushclosure (lua_State
*L
, Proto
*p
, UpVal
**encup
, StkId base
,
620 int nup
= p
->sizeupvalues
;
621 Upvaldesc
*uv
= p
->upvalues
;
623 LClosure
*ncl
= luaF_newLclosure(L
, nup
);
625 setclLvalue(L
, ra
, ncl
); /* anchor new closure in stack */
626 for (i
= 0; i
< nup
; i
++) { /* fill in its upvalues */
627 if (uv
[i
].instack
) /* upvalue refers to local variable? */
628 ncl
->upvals
[i
] = luaF_findupval(L
, base
+ uv
[i
].idx
);
629 else /* get upvalue from enclosing function */
630 ncl
->upvals
[i
] = encup
[uv
[i
].idx
];
631 ncl
->upvals
[i
]->refcount
++;
632 /* new closure is white, so we do not need a barrier here */
634 if (!isblack(p
)) /* cache will not break GC invariant? */
635 p
->cache
= ncl
; /* save it on cache for reuse */
640 ** finish execution of an opcode interrupted by an yield
642 void luaV_finishOp (lua_State
*L
) {
643 CallInfo
*ci
= L
->ci
;
644 StkId base
= ci
->u
.l
.base
;
645 Instruction inst
= *(ci
->u
.l
.savedpc
- 1); /* interrupted instruction */
646 OpCode op
= GET_OPCODE(inst
);
647 switch (op
) { /* finish its execution */
648 case OP_ADD
: case OP_SUB
: case OP_MUL
: case OP_DIV
: case OP_IDIV
:
649 case OP_BAND
: case OP_BOR
: case OP_BXOR
: case OP_SHL
: case OP_SHR
:
650 case OP_MOD
: case OP_POW
:
651 case OP_UNM
: case OP_BNOT
: case OP_LEN
:
652 case OP_GETTABUP
: case OP_GETTABLE
: case OP_SELF
: {
653 setobjs2s(L
, base
+ GETARG_A(inst
), --L
->top
);
656 case OP_LE
: case OP_LT
: case OP_EQ
: {
657 int res
= !l_isfalse(L
->top
- 1);
659 if (ci
->callstatus
& CIST_LEQ
) { /* "<=" using "<" instead? */
660 lua_assert(op
== OP_LE
);
661 ci
->callstatus
^= CIST_LEQ
; /* clear mark */
662 res
= !res
; /* negate result */
664 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_JMP
);
665 if (res
!= GETARG_A(inst
)) /* condition failed? */
666 ci
->u
.l
.savedpc
++; /* skip jump instruction */
670 StkId top
= L
->top
- 1; /* top when 'luaT_trybinTM' was called */
671 int b
= GETARG_B(inst
); /* first element to concatenate */
672 int total
= cast_int(top
- 1 - (base
+ b
)); /* yet to concatenate */
673 setobj2s(L
, top
- 2, top
); /* put TM result in proper position */
674 if (total
> 1) { /* are there elements to concat? */
675 L
->top
= top
- 1; /* top is one after last element (at top-2) */
676 luaV_concat(L
, total
); /* concat them (may yield again) */
678 /* move final result to final position */
679 setobj2s(L
, ci
->u
.l
.base
+ GETARG_A(inst
), L
->top
- 1);
680 L
->top
= ci
->top
; /* restore top */
684 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_TFORLOOP
);
685 L
->top
= ci
->top
; /* correct top */
689 if (GETARG_C(inst
) - 1 >= 0) /* nresults >= 0? */
690 L
->top
= ci
->top
; /* adjust results */
693 case OP_TAILCALL
: case OP_SETTABUP
: case OP_SETTABLE
:
695 default: lua_assert(0);
703 ** {==================================================================
704 ** Function 'luaV_execute': main interpreter loop
705 ** ===================================================================
710 ** some macros for common tasks in 'luaV_execute'
714 #define RA(i) (base+GETARG_A(i))
715 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
716 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
717 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
718 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
719 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
720 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
723 /* execute a jump instruction */
724 #define dojump(ci,i,e) \
725 { int a = GETARG_A(i); \
726 if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \
727 ci->u.l.savedpc += GETARG_sBx(i) + e; }
729 /* for test instructions, execute the jump instruction that follows it */
730 #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
733 #define Protect(x) { {x;}; base = ci->u.l.base; }
735 #define checkGC(L,c) \
736 { luaC_condGC(L, L->top = (c), /* limit of live values */ \
737 Protect(L->top = ci->top)); /* restore top */ \
738 luai_threadyield(L); }
741 #define vmdispatch(o) switch(o)
742 #define vmcase(l) case l:
743 #define vmbreak break
747 ** copy of 'luaV_gettable', but protecting call to potential metamethod
748 ** (which can reallocate the stack)
750 #define gettableProtected(L,t,k,v) { const TValue *aux; \
751 if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \
752 else Protect(luaV_finishget(L,t,k,v,aux)); }
755 /* same for 'luaV_settable' */
756 #define settableProtected(L,t,k,v) { const TValue *slot; \
757 if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
758 Protect(luaV_finishset(L,t,k,v,slot)); }
762 void luaV_execute (lua_State
*L
) {
763 CallInfo
*ci
= L
->ci
;
767 ci
->callstatus
|= CIST_FRESH
; /* fresh invocation of 'luaV_execute" */
768 newframe
: /* reentry point when frame changes (call/return) */
769 lua_assert(ci
== L
->ci
);
770 cl
= clLvalue(ci
->func
); /* local reference to function's closure */
771 k
= cl
->p
->k
; /* local reference to function's constant table */
772 base
= ci
->u
.l
.base
; /* local copy of function's base */
773 /* main loop of interpreter */
775 Instruction i
= *(ci
->u
.l
.savedpc
++);
777 if (L
->hookmask
& (LUA_MASKLINE
| LUA_MASKCOUNT
))
778 Protect(luaG_traceexec(L
));
779 /* WARNING: several calls may realloc the stack and invalidate 'ra' */
781 lua_assert(base
== ci
->u
.l
.base
);
782 lua_assert(base
<= L
->top
&& L
->top
< L
->stack
+ L
->stacksize
);
783 vmdispatch (GET_OPCODE(i
)) {
785 setobjs2s(L
, ra
, RB(i
));
789 TValue
*rb
= k
+ GETARG_Bx(i
);
795 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
796 rb
= k
+ GETARG_Ax(*ci
->u
.l
.savedpc
++);
800 vmcase(OP_LOADBOOL
) {
801 setbvalue(ra
, GETARG_B(i
));
802 if (GETARG_C(i
)) ci
->u
.l
.savedpc
++; /* skip next instruction (if C) */
812 vmcase(OP_GETUPVAL
) {
814 setobj2s(L
, ra
, cl
->upvals
[b
]->v
);
817 vmcase(OP_GETTABUP
) {
818 TValue
*upval
= cl
->upvals
[GETARG_B(i
)]->v
;
820 gettableProtected(L
, upval
, rc
, ra
);
823 vmcase(OP_GETTABLE
) {
826 gettableProtected(L
, rb
, rc
, ra
);
829 vmcase(OP_SETTABUP
) {
830 TValue
*upval
= cl
->upvals
[GETARG_A(i
)]->v
;
833 settableProtected(L
, upval
, rb
, rc
);
836 vmcase(OP_SETUPVAL
) {
837 UpVal
*uv
= cl
->upvals
[GETARG_B(i
)];
838 setobj(L
, uv
->v
, ra
);
839 luaC_upvalbarrier(L
, uv
);
842 vmcase(OP_SETTABLE
) {
845 settableProtected(L
, ra
, rb
, rc
);
848 vmcase(OP_NEWTABLE
) {
851 Table
*t
= luaH_new(L
);
853 if (b
!= 0 || c
!= 0)
854 luaH_resize(L
, t
, luaO_fb2int(b
), luaO_fb2int(c
));
862 TString
*key
= tsvalue(rc
); /* key must be a string */
863 setobjs2s(L
, ra
+ 1, rb
);
864 if (luaV_fastget(L
, rb
, key
, aux
, luaH_getstr
)) {
865 setobj2s(L
, ra
, aux
);
867 else Protect(luaV_finishget(L
, rb
, rc
, ra
, aux
));
873 lua_Number nb
; lua_Number nc
;
874 if (ttisinteger(rb
) && ttisinteger(rc
)) {
875 lua_Integer ib
= ivalue(rb
); lua_Integer ic
= ivalue(rc
);
876 setivalue(ra
, intop(+, ib
, ic
));
878 else if (tonumber(rb
, &nb
) && tonumber(rc
, &nc
)) {
879 setfltvalue(ra
, luai_numadd(L
, nb
, nc
));
881 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_ADD
)); }
887 lua_Number nb
; lua_Number nc
;
888 if (ttisinteger(rb
) && ttisinteger(rc
)) {
889 lua_Integer ib
= ivalue(rb
); lua_Integer ic
= ivalue(rc
);
890 setivalue(ra
, intop(-, ib
, ic
));
892 else if (tonumber(rb
, &nb
) && tonumber(rc
, &nc
)) {
893 setfltvalue(ra
, luai_numsub(L
, nb
, nc
));
895 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_SUB
)); }
901 lua_Number nb
; lua_Number nc
;
902 if (ttisinteger(rb
) && ttisinteger(rc
)) {
903 lua_Integer ib
= ivalue(rb
); lua_Integer ic
= ivalue(rc
);
904 setivalue(ra
, intop(*, ib
, ic
));
906 else if (tonumber(rb
, &nb
) && tonumber(rc
, &nc
)) {
907 setfltvalue(ra
, luai_nummul(L
, nb
, nc
));
909 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_MUL
)); }
912 vmcase(OP_DIV
) { /* float division (always with floats) */
915 lua_Number nb
; lua_Number nc
;
916 if (tonumber(rb
, &nb
) && tonumber(rc
, &nc
)) {
917 setfltvalue(ra
, luai_numdiv(L
, nb
, nc
));
919 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_DIV
)); }
925 lua_Integer ib
; lua_Integer ic
;
926 if (tointeger(rb
, &ib
) && tointeger(rc
, &ic
)) {
927 setivalue(ra
, intop(&, ib
, ic
));
929 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_BAND
)); }
935 lua_Integer ib
; lua_Integer ic
;
936 if (tointeger(rb
, &ib
) && tointeger(rc
, &ic
)) {
937 setivalue(ra
, intop(|, ib
, ic
));
939 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_BOR
)); }
945 lua_Integer ib
; lua_Integer ic
;
946 if (tointeger(rb
, &ib
) && tointeger(rc
, &ic
)) {
947 setivalue(ra
, intop(^, ib
, ic
));
949 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_BXOR
)); }
955 lua_Integer ib
; lua_Integer ic
;
956 if (tointeger(rb
, &ib
) && tointeger(rc
, &ic
)) {
957 setivalue(ra
, luaV_shiftl(ib
, ic
));
959 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_SHL
)); }
965 lua_Integer ib
; lua_Integer ic
;
966 if (tointeger(rb
, &ib
) && tointeger(rc
, &ic
)) {
967 setivalue(ra
, luaV_shiftl(ib
, -ic
));
969 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_SHR
)); }
975 lua_Number nb
; lua_Number nc
;
976 if (ttisinteger(rb
) && ttisinteger(rc
)) {
977 lua_Integer ib
= ivalue(rb
); lua_Integer ic
= ivalue(rc
);
978 setivalue(ra
, luaV_mod(L
, ib
, ic
));
980 else if (tonumber(rb
, &nb
) && tonumber(rc
, &nc
)) {
982 luai_nummod(L
, nb
, nc
, m
);
985 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_MOD
)); }
988 vmcase(OP_IDIV
) { /* floor division */
991 lua_Number nb
; lua_Number nc
;
992 if (ttisinteger(rb
) && ttisinteger(rc
)) {
993 lua_Integer ib
= ivalue(rb
); lua_Integer ic
= ivalue(rc
);
994 setivalue(ra
, luaV_div(L
, ib
, ic
));
996 else if (tonumber(rb
, &nb
) && tonumber(rc
, &nc
)) {
997 setfltvalue(ra
, luai_numidiv(L
, nb
, nc
));
999 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_IDIV
)); }
1003 TValue
*rb
= RKB(i
);
1004 TValue
*rc
= RKC(i
);
1005 lua_Number nb
; lua_Number nc
;
1006 if (tonumber(rb
, &nb
) && tonumber(rc
, &nc
)) {
1007 setfltvalue(ra
, luai_numpow(L
, nb
, nc
));
1009 else { Protect(luaT_trybinTM(L
, rb
, rc
, ra
, TM_POW
)); }
1015 if (ttisinteger(rb
)) {
1016 lua_Integer ib
= ivalue(rb
);
1017 setivalue(ra
, intop(-, 0, ib
));
1019 else if (tonumber(rb
, &nb
)) {
1020 setfltvalue(ra
, luai_numunm(L
, nb
));
1023 Protect(luaT_trybinTM(L
, rb
, rb
, ra
, TM_UNM
));
1030 if (tointeger(rb
, &ib
)) {
1031 setivalue(ra
, intop(^, ~l_castS2U(0), ib
));
1034 Protect(luaT_trybinTM(L
, rb
, rb
, ra
, TM_BNOT
));
1040 int res
= l_isfalse(rb
); /* next assignment may change this value */
1045 Protect(luaV_objlen(L
, ra
, RB(i
)));
1049 int b
= GETARG_B(i
);
1050 int c
= GETARG_C(i
);
1052 L
->top
= base
+ c
+ 1; /* mark the end of concat operands */
1053 Protect(luaV_concat(L
, c
- b
+ 1));
1054 ra
= RA(i
); /* 'luaV_concat' may invoke TMs and move the stack */
1056 setobjs2s(L
, ra
, rb
);
1057 checkGC(L
, (ra
>= rb
? ra
+ 1 : rb
));
1058 L
->top
= ci
->top
; /* restore top */
1066 TValue
*rb
= RKB(i
);
1067 TValue
*rc
= RKC(i
);
1069 if (luaV_equalobj(L
, rb
, rc
) != GETARG_A(i
))
1078 if (luaV_lessthan(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
1087 if (luaV_lessequal(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
1095 if (GETARG_C(i
) ? l_isfalse(ra
) : !l_isfalse(ra
))
1101 vmcase(OP_TESTSET
) {
1103 if (GETARG_C(i
) ? l_isfalse(rb
) : !l_isfalse(rb
))
1106 setobjs2s(L
, ra
, rb
);
1112 int b
= GETARG_B(i
);
1113 int nresults
= GETARG_C(i
) - 1;
1114 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
1115 if (luaD_precall(L
, ra
, nresults
)) { /* C function? */
1117 L
->top
= ci
->top
; /* adjust results */
1118 Protect((void)0); /* update 'base' */
1120 else { /* Lua function */
1122 goto newframe
; /* restart luaV_execute over new Lua function */
1126 vmcase(OP_TAILCALL
) {
1127 int b
= GETARG_B(i
);
1128 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
1129 lua_assert(GETARG_C(i
) - 1 == LUA_MULTRET
);
1130 if (luaD_precall(L
, ra
, LUA_MULTRET
)) { /* C function? */
1131 Protect((void)0); /* update 'base' */
1134 /* tail call: put called frame (n) in place of caller one (o) */
1135 CallInfo
*nci
= L
->ci
; /* called frame */
1136 CallInfo
*oci
= nci
->previous
; /* caller frame */
1137 StkId nfunc
= nci
->func
; /* called function */
1138 StkId ofunc
= oci
->func
; /* caller function */
1139 /* last stack slot filled by 'precall' */
1140 StkId lim
= nci
->u
.l
.base
+ getproto(nfunc
)->numparams
;
1142 /* close all upvalues from previous call */
1143 if (cl
->p
->sizep
> 0) luaF_close(L
, oci
->u
.l
.base
);
1144 /* move new frame into old one */
1145 for (aux
= 0; nfunc
+ aux
< lim
; aux
++)
1146 setobjs2s(L
, ofunc
+ aux
, nfunc
+ aux
);
1147 oci
->u
.l
.base
= ofunc
+ (nci
->u
.l
.base
- nfunc
); /* correct base */
1148 oci
->top
= L
->top
= ofunc
+ (L
->top
- nfunc
); /* correct top */
1149 oci
->u
.l
.savedpc
= nci
->u
.l
.savedpc
;
1150 oci
->callstatus
|= CIST_TAIL
; /* function was tail called */
1151 ci
= L
->ci
= oci
; /* remove new frame */
1152 lua_assert(L
->top
== oci
->u
.l
.base
+ getproto(ofunc
)->maxstacksize
);
1153 goto newframe
; /* restart luaV_execute over new Lua function */
1158 int b
= GETARG_B(i
);
1159 if (cl
->p
->sizep
> 0) luaF_close(L
, base
);
1160 b
= luaD_poscall(L
, ci
, ra
, (b
!= 0 ? b
- 1 : cast_int(L
->top
- ra
)));
1161 if (ci
->callstatus
& CIST_FRESH
) /* local 'ci' still from callee */
1162 return; /* external invocation: return */
1163 else { /* invocation via reentry: continue execution */
1165 if (b
) L
->top
= ci
->top
;
1166 lua_assert(isLua(ci
));
1167 lua_assert(GET_OPCODE(*((ci
)->u
.l
.savedpc
- 1)) == OP_CALL
);
1168 goto newframe
; /* restart luaV_execute over new Lua function */
1171 vmcase(OP_FORLOOP
) {
1172 if (ttisinteger(ra
)) { /* integer loop? */
1173 lua_Integer step
= ivalue(ra
+ 2);
1174 lua_Integer idx
= intop(+, ivalue(ra
), step
); /* increment index */
1175 lua_Integer limit
= ivalue(ra
+ 1);
1176 if ((0 < step
) ? (idx
<= limit
) : (limit
<= idx
)) {
1177 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
1178 chgivalue(ra
, idx
); /* update internal index... */
1179 setivalue(ra
+ 3, idx
); /* ...and external index */
1182 else { /* floating loop */
1183 lua_Number step
= fltvalue(ra
+ 2);
1184 lua_Number idx
= luai_numadd(L
, fltvalue(ra
), step
); /* inc. index */
1185 lua_Number limit
= fltvalue(ra
+ 1);
1186 if (luai_numlt(0, step
) ? luai_numle(idx
, limit
)
1187 : luai_numle(limit
, idx
)) {
1188 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
1189 chgfltvalue(ra
, idx
); /* update internal index... */
1190 setfltvalue(ra
+ 3, idx
); /* ...and external index */
1195 vmcase(OP_FORPREP
) {
1197 TValue
*plimit
= ra
+ 1;
1198 TValue
*pstep
= ra
+ 2;
1201 if (ttisinteger(init
) && ttisinteger(pstep
) &&
1202 forlimit(plimit
, &ilimit
, ivalue(pstep
), &stopnow
)) {
1203 /* all values are integer */
1204 lua_Integer initv
= (stopnow
? 0 : ivalue(init
));
1205 setivalue(plimit
, ilimit
);
1206 setivalue(init
, intop(-, initv
, ivalue(pstep
)));
1208 else { /* try making all values floats */
1209 lua_Number ninit
; lua_Number nlimit
; lua_Number nstep
;
1210 if (!tonumber(plimit
, &nlimit
))
1211 luaG_runerror(L
, "'for' limit must be a number");
1212 setfltvalue(plimit
, nlimit
);
1213 if (!tonumber(pstep
, &nstep
))
1214 luaG_runerror(L
, "'for' step must be a number");
1215 setfltvalue(pstep
, nstep
);
1216 if (!tonumber(init
, &ninit
))
1217 luaG_runerror(L
, "'for' initial value must be a number");
1218 setfltvalue(init
, luai_numsub(L
, ninit
, nstep
));
1220 ci
->u
.l
.savedpc
+= GETARG_sBx(i
);
1223 vmcase(OP_TFORCALL
) {
1224 StkId cb
= ra
+ 3; /* call base */
1225 setobjs2s(L
, cb
+2, ra
+2);
1226 setobjs2s(L
, cb
+1, ra
+1);
1227 setobjs2s(L
, cb
, ra
);
1228 L
->top
= cb
+ 3; /* func. + 2 args (state and index) */
1229 Protect(luaD_call(L
, cb
, GETARG_C(i
)));
1231 i
= *(ci
->u
.l
.savedpc
++); /* go to next instruction */
1233 lua_assert(GET_OPCODE(i
) == OP_TFORLOOP
);
1236 vmcase(OP_TFORLOOP
) {
1238 if (!ttisnil(ra
+ 1)) { /* continue loop? */
1239 setobjs2s(L
, ra
, ra
+ 1); /* save control variable */
1240 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
1244 vmcase(OP_SETLIST
) {
1245 int n
= GETARG_B(i
);
1246 int c
= GETARG_C(i
);
1249 if (n
== 0) n
= cast_int(L
->top
- ra
) - 1;
1251 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
1252 c
= GETARG_Ax(*ci
->u
.l
.savedpc
++);
1255 last
= ((c
-1)*LFIELDS_PER_FLUSH
) + n
;
1256 if (last
> h
->sizearray
) /* needs more space? */
1257 luaH_resizearray(L
, h
, last
); /* preallocate it at once */
1258 for (; n
> 0; n
--) {
1260 luaH_setint(L
, h
, last
--, val
);
1261 luaC_barrierback(L
, h
, val
);
1263 L
->top
= ci
->top
; /* correct top (in case of previous open call) */
1266 vmcase(OP_CLOSURE
) {
1267 Proto
*p
= cl
->p
->p
[GETARG_Bx(i
)];
1268 LClosure
*ncl
= getcached(p
, cl
->upvals
, base
); /* cached closure */
1269 if (ncl
== NULL
) /* no match? */
1270 pushclosure(L
, p
, cl
->upvals
, base
, ra
); /* create a new one */
1272 setclLvalue(L
, ra
, ncl
); /* push cashed closure */
1277 int b
= GETARG_B(i
) - 1; /* required results */
1279 int n
= cast_int(base
- ci
->func
) - cl
->p
->numparams
- 1;
1280 if (n
< 0) /* less arguments than parameters? */
1281 n
= 0; /* no vararg arguments */
1282 if (b
< 0) { /* B == 0? */
1283 b
= n
; /* get all var. arguments */
1284 Protect(luaD_checkstack(L
, n
));
1285 ra
= RA(i
); /* previous call may change the stack */
1288 for (j
= 0; j
< b
&& j
< n
; j
++)
1289 setobjs2s(L
, ra
+ j
, base
- n
+ j
);
1290 for (; j
< b
; j
++) /* complete required results with nil */
1291 setnilvalue(ra
+ j
);
1294 vmcase(OP_EXTRAARG
) {
1302 /* }================================================================== */