/* ** Similar to 'tonumber', but does not attempt to convert strings and ** ensure correct precision (no extra bits). Used in comparisons. */ static int tofloat (const TValue *obj, lua_Number *n) { if (ttisfloat(obj)) *n = fltvalue(obj); else if (ttisinteger(obj)) { volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */ *n = x; } else { *n = 0; /* to avoid warnings */ return 0; } return 1; }
/* ** Main operation less than or equal to; return 'l <= r'. If it needs ** a metamethod and there is no '__le', try '__lt', based on ** l <= r iff !(r < l) (assuming a total order). If the metamethod ** yields during this substitution, the continuation has to know ** about it (to negate the result of r<l); bit CIST_LEQ in the call ** status keeps that information. */ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { int res; #ifndef _KERNEL if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ return LEnum(l, r); #else /* _KERNEL */ if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ return (ivalue(l) <= ivalue(r)); #endif /* _KERNEL */ else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ return res; else { /* try 'lt': */ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ res = luaT_callorderTM(L, r, l, TM_LT); L->ci->callstatus ^= CIST_LEQ; /* clear mark */ if (res < 0) luaG_ordererror(L, l, r); return !res; /* result is negated */ } }
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res) { switch (op) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* operate only on integers */ lua_Integer i1; lua_Integer i2; if (tointeger(p1, &i1) && tointeger(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); return; } else break; /* go to the end */ } case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ lua_Number n1; lua_Number n2; if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return; } else break; /* go to the end */ } default: { /* other operations */ lua_Number n1; lua_Number n2; if (ttisinteger(p1) && ttisinteger(p2)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); return; } else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return; } else break; /* go to the end */ } } /* could not perform raw operation; try metamethod */ lua_assert(L != NULL); /* should not fail when folding (compile time) */ luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD)); }
/* ** Try to convert a value to a float. The float case is already handled ** by the macro 'tonumber'. */ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { TValue v; if (ttisinteger(obj)) { *n = cast_num(ivalue(obj)); return 1; } else if (cvt2num(obj) && /* string convertible to number? */ luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ return 1; } else return 0; /* conversion failed */ }
static int addk (FuncState *fs, TValue *k, TValue *v) { lua_State *L = fs->L; TValue *idx = luaH_set(L, fs->h, k); Proto *f = fs->f; int oldsize = f->sizek; if (ttype(idx)==LUA_TNUMBER) { luai_normalize(idx); lua_assert( ttype(idx)==LUA_TINT ); /* had no fraction */ } if (ttisint(idx)) { lua_assert(luaO_rawequalObj(&fs->f->k[ivalue(idx)], v)); return cast_int(ivalue(idx)); } else { /* constant not found; create a new entry */ setivalue(idx, fs->nk); luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, MAXARG_Bx, "constant table overflow"); while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[fs->nk], v); luaC_barrier(L, f, v); return fs->nk++; } }
/* ** returns the index for `key' if `key' is an appropriate key to live in ** the array part of the table, -1 otherwise. ** ** Anything <=0 is taken as not being in the array part. */ static int arrayindex (const TValue *key, int max) { lua_Integer i; switch( ttype(key) ) { #ifdef LUA_TINT case LUA_TINT: i= ivalue(key); break; #endif case LUA_TNUMBER: if (tt_integer_valued(key,&i)) break; default: return -1; /* not to be used as array index */ } return (i <= max) ? cast_int(i) : -1; }
/*=============================+ * evaluate -- Generic evaluator *============================*/ PVALUE evaluate (PNODE node, SYMTAB stab, BOOLEAN *eflg) { if (prog_trace) { trace_out("%d: ", iline(node)+1); trace_pnode(node); trace_endl(); } if (iistype(node, IIDENT)) return evaluate_iden(node, stab, eflg); if (iistype(node, IBCALL)) return evaluate_func(node, stab, eflg); if (iistype(node, IFCALL)) return evaluate_ufunc(node, stab, eflg); *eflg = FALSE; if (iistype(node, IICONS)) return copy_pvalue(ivalue(node)); if (iistype(node, ISCONS)) return copy_pvalue(ivalue(node)); if (iistype(node, IFCONS)) return copy_pvalue(ivalue(node)); *eflg = TRUE; return NULL; }
/* ** returns the index of a 'key' for table traversals. First goes all ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */ static unsigned int findindex (lua_State *L, Table *t, TValue *key, unsigned int asize) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; if (i != 0 && i <= asize) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key); if (unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ return (i + 1) + asize; } }
/* ** Try to "constant-fold" an operation; return 1 iff successful */ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { TValue v1, v2, res; if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) return 0; /* non-numeric operands or not safe to fold */ luaO_arith(fs->ls->L, op, &v1, &v2, &res); if (ttisinteger(&res)) { e1->k = VKINT; e1->u.ival = ivalue(&res); } else { lua_Number n = fltvalue(&res); if (luai_numisnan(n) || isminuszero(n)) return 0; /* folds neither NaN nor -0 */ e1->k = VKFLT; e1->u.nval = n; } return 1; }
/* ** return false if folding can raise an error */ static int validop (int op, TValue *v1, TValue *v2) { lua_Number a, b; lua_Integer i; cast_void(a); cast_void(b); /* macro may not use its arguments */ if (luai_numinvalidop(op, (cast_void(tonumber(v1, &a)), a), (cast_void(tonumber(v2, &b)), b))) return 0; switch (op) { case LUA_OPIDIV: /* division by 0 and conversion errors */ return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0); case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */ return (tointeger(v1, &i) && tointeger(v2, &i)); case LUA_OPMOD: /* integer module by 0 */ return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0); default: return 1; /* everything else is valid */ } }
/* ** Try to convert a value to a float. Check 'isinteger' first, because ** in general the float case is already handled by the macro 'tonumber'. */ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { TValue v; again: if (ttisinteger(obj)) { *n = cast_num(ivalue(obj)); return 1; } else if (ttisfloat(obj)) { *n = fltvalue(obj); return 1; } else if (cvt2num(obj) && /* string convertible to number? */ luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { obj = &v; goto again; /* convert result from 'luaO_str2num' to a float */ } return 0; /* conversion failed */ }
static int read_numeral (LexState *ls, SemInfo *seminfo) { TValue obj; int first = ls->current; lua_assert(lisdigit(ls->current)); save_and_next(ls); if (first == '0') check_next2(ls, "xX"); /* hexadecimal? */ for (;;) { if (lisxdigit(ls->current)) save_and_next(ls); else break; } save(ls, '\0'); if (!buff2num(ls->buff, &obj)) /* format error? */ lexerror(ls, "malformed number", TK_INT); lua_assert(ttisinteger(&obj)); seminfo->i = ivalue(&obj); return TK_INT; }
int luaV_tostring (lua_State *L, StkId obj) { if (!ttisnumber(obj)) return 0; else { char buff[MAXNUMBER2STR]; size_t len; if (ttisinteger(obj)) len = lua_integer2str(buff, ivalue(obj)); else { len = lua_number2str(buff, fltvalue(obj)); if (strspn(buff, "-0123456789") == len) { /* look like an integer? */ buff[len++] = '.'; /* add a '.0' */ buff[len++] = '0'; buff[len] = '\0'; } } setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); return 1; } }
/* ** Check whether key 'k1' is equal to the key in node 'n2'. ** This equality is raw, so there are no metamethods. Floats ** with integer values have been normalized, so integers cannot ** be equal to floats. It is assumed that 'eqshrstr' is simply ** pointer equality, so that short strings are handled in the ** default case. */ static int equalkey (const TValue *k1, const Node *n2) { if (rawtt(k1) != keytt(n2)) /* not the same variants? */ return 0; /* cannot be same key */ switch (ttypetag(k1)) { case LUA_TNIL: return 1; case LUA_TNUMINT: return (ivalue(k1) == keyival(n2)); case LUA_TNUMFLT: return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); case LUA_TBOOLEAN: return bvalue(k1) == bvalueraw(keyval(n2)); case LUA_TLIGHTUSERDATA: return pvalue(k1) == pvalueraw(keyval(n2)); case LUA_TLCF: return fvalue(k1) == fvalueraw(keyval(n2)); case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); default: return gcvalue(k1) == gcvalueraw(keyval(n2)); } }
static void DumpConstants(const Proto* f, DumpState* D) { int i,n=f->sizek; DumpInt(n,D); for (i=0; i<n; i++) { const TValue* o=&f->k[i]; DumpChar(ttype(o),D); switch (ttype(o)) { case LUA_TNIL: break; case LUA_TBOOLEAN: DumpChar(bvalue(o),D); break; #ifdef LUA_TINT case LUA_TINT: DumpInteger(ivalue(o),D); break; case LUA_TNUMBER: DumpNumber(nvalue_fast(o),D); break; #else case LUA_TNUMBER: DumpNumber(nvalue(o),D); break; #endif case LUA_TSTRING: DumpString(rawtsvalue(o),D); break; default: lua_assert(0); /* cannot happen */ break; } } n=f->sizep; DumpInt(n,D); for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); }
static pointer plugin_function(scheme *sc, pointer args) { printf("this is a plugin!\n"); /* first argument is an integer */ long i = ivalue(car(args)); printf("the first argument is %d\n", i); /* pop first argument using cdr */ args = cdr(args); /* second argument is a string */ char *str = string_value(car(args)); printf("the second argument is %s\n", str); /* pop the argument */ args = cdr(args); /* second argument is a float*/ SPFLOAT flt = rvalue(car(args)); printf("the third argument is %g\n", flt); /* return a float */ return mk_real(sc, 0.2468); }
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { int res; int tl= ttype(l); if (tl == ttype(r)) { switch(tl) { #ifdef LUA_TINT case LUA_TINT: return ivalue(l) < ivalue(r); #endif case LUA_TNUMBER: #ifdef LNUM_COMPLEX if ( (nvalue_img_fast(l)!=0) || (nvalue_img_fast(r)!=0) ) error_complex( L, l, r ); #endif return luai_numlt(nvalue_fast(l), nvalue_fast(r)); case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; } if ((res = call_orderTM(L, l, r, TM_LT)) != -1) return res; /* fall through to 'luaG_ordererror()' */ } #ifdef LUA_TINT else if (ttype_ext(l) == ttype_ext(r)) { lua_Integer tmp; /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons * in integer realm. Only otherwise cast 'l' to FP (which might change its * value). */ # ifdef LNUM_COMPLEX if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) ) error_complex( L, l, r ); # endif if (tl==LUA_TINT) { /* l:int, r:num */ return tt_integer_valued(r,&tmp) ? (ivalue(l) < tmp) : luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) ); } else { /* l:num, r:int */ return tt_integer_valued(l,&tmp) ? (tmp < ivalue(r)) : luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) ); } } #endif return luaG_ordererror(L, l, r); }
static int lessequal (lua_State *L, const TValue *l, const TValue *r) { int res; int tl= ttype(l); if (tl == ttype(r)) { switch(tl) { #ifdef LUA_TINT case LUA_TINT: return ivalue(l) <= ivalue(r); #endif case LUA_TNUMBER: #ifdef LNUM_COMPLEX if ( (nvalue_img_fast(l)!=0) || (nvalue_img_fast(r)!=0) ) error_complex( L, l, r ); #endif return luai_numle(nvalue_fast(l), nvalue_fast(r)); case LUA_TSTRING: return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; } if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ return res; else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */ return !res; /* fall through to 'luaG_ordererror()' */ } #ifdef LUA_TINT else if (ttype_ext(l) == ttype_ext(r)) { lua_Integer tmp; # ifdef LNUM_COMPLEX if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) ) error_complex( L, l, r ); # endif if (tl==LUA_TINT) { /* l:int, r:num */ return tt_integer_valued(r,&tmp) ? (ivalue(l) <= tmp) : luai_numle( cast_num(ivalue(l)), nvalue_fast(r) ); } else { /* l:num, r:int */ return tt_integer_valued(l,&tmp) ? (tmp <= ivalue(r)) : luai_numle( nvalue_fast(l), cast_num(ivalue(r)) ); } } #endif return luaG_ordererror(L, l, r); }
/* ** try to convert a value to an integer, rounding according to 'mode': ** mode == 0: accepts only integral values ** mode == 1: takes the floor of the number ** mode == 2: takes the ceil of the number */ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { TValue v; again: if (ttisfloat(obj)) { lua_Number n = fltvalue(obj); lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ if (mode == 0) return 0; /* fails if mode demands integral value */ else if (mode > 1) /* needs ceil? */ f += 1; /* convert floor to ceil (remember: n != f) */ } return lua_numbertointeger(f, p); } else if (ttisinteger(obj)) { *p = ivalue(obj); return 1; } else if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { obj = &v; goto again; /* convert result from 'luaO_str2num' to an integer */ } return 0; /* conversion failed */ }
Term::operator const List&() const { ASSERT(type(f_LIST)); return *m_lists[ivalue()]; }
/* ** try to convert a value to an integer, rounding according to 'mode': ** mode == 0: accepts only integral values ** mode == 1: takes the floor of the number ** mode == 2: takes the ceil of the number */ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { TValue v; again: #ifndef _KERNEL if (ttisfloat(obj)) { lua_Number n = fltvalue(obj); lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ if (mode == 0) return 0; /* fails if mode demands integral value */ else if (mode > 1) /* needs ceil? */ f += 1; /* convert floor to ceil (remember: n != f) */ } return lua_numbertointeger(f, p); } else if (ttisinteger(obj)) { #else /* _KERNEL */ if (ttisinteger(obj)) { UNUSED(mode); #endif *p = ivalue(obj); return 1; } else if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { obj = &v; goto again; /* convert result from 'luaO_str2num' to an integer */ } return 0; /* conversion failed */ } #ifndef _KERNEL /* ** Try to convert a 'for' limit to an integer, preserving the ** semantics of the loop. ** (The following explanation assumes a non-negative step; it is valid ** for negative steps mutatis mutandis.) ** If the limit can be converted to an integer, rounding down, that is ** it. ** Otherwise, check whether the limit can be converted to a number. If ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, ** which means no limit. If the number is too negative, the loop ** should not run, because any initial integer value is larger than the ** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects ** the extreme case when the initial value is LUA_MININTEGER, in which ** case the LUA_MININTEGER limit would still run the loop once. */ static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, int *stopnow) { *stopnow = 0; /* usually, let loops run */ if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ lua_Number n; /* try to convert to float */ if (!tonumber(obj, &n)) /* cannot convert to float? */ return 0; /* not a number */ if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ *p = LUA_MAXINTEGER; if (step < 0) *stopnow = 1; } else { /* float is smaller than min integer */ *p = LUA_MININTEGER; if (step >= 0) *stopnow = 1; } } return 1; }
Term::operator const Double&() const { ASSERT(type(f_DOUBLE)); return *m_doubles[ivalue()]; }
/* Note: if called for unary operations, 'rc'=='rb'. */ static void Arith (lua_State *L, StkId ra, const TValue *rb, const TValue *rc, TMS op) { TValue tempb, tempc; const TValue *b, *c; lua_Number nb,nc; if ((b = luaV_tonumber(rb, &tempb)) != NULL && (c = luaV_tonumber(rc, &tempc)) != NULL) { /* Keep integer arithmetics in the integer realm, if possible. */ #ifdef LUA_TINT if (ttisint(b) && ttisint(c)) { lua_Integer ib = ivalue(b), ic = ivalue(c); lua_Integer *ri = &ra->value.i; ra->tt= LUA_TINT; /* part of 'setivalue(ra)' */ switch (op) { case TM_ADD: if (try_addint( ri, ib, ic)) return; break; case TM_SUB: if (try_subint( ri, ib, ic)) return; break; case TM_MUL: if (try_mulint( ri, ib, ic)) return; break; case TM_DIV: if (try_divint( ri, ib, ic)) return; break; case TM_MOD: if (try_modint( ri, ib, ic)) return; break; case TM_POW: if (try_powint( ri, ib, ic)) return; break; case TM_UNM: if (try_unmint( ri, ib)) return; break; default: lua_assert(0); } } #endif /* Fallback to floating point, when leaving range. */ #ifdef LNUM_COMPLEX if ((nvalue_img(b)!=0) || (nvalue_img(c)!=0)) { lua_Complex r; if (op==TM_UNM) { r= -nvalue_complex_fast(b); /* never an integer (or scalar) */ setnvalue_complex_fast( ra, r ); } else { lua_Complex bb= nvalue_complex(b), cc= nvalue_complex(c); switch (op) { case TM_ADD: r= bb + cc; break; case TM_SUB: r= bb - cc; break; case TM_MUL: r= bb * cc; break; case TM_DIV: r= bb / cc; break; case TM_MOD: luaG_runerror(L, "attempt to use %% on complex numbers"); /* no return */ case TM_POW: r= luai_vectpow( bb, cc ); break; default: lua_assert(0); r=0; } setnvalue_complex( ra, r ); } return; } #endif nb = nvalue(b); nc = nvalue(c); switch (op) { case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); return; case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); return; case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); return; case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); return; case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); return; case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); return; case TM_UNM: setnvalue(ra, luai_numunm(nb)); return; default: lua_assert(0); } } /* Either operand not a number */ if (!call_binTM(L, rb, rc, ra, op)) luaG_aritherror(L, rb, rc); }
Term::operator SysData*() const { ASSERT(type(f_SYSDATA)); return *m_sysd[ivalue()]; }
inline Term::operator Var() const { ASSERT(type(f_VAR)); return Var(ivalue()); }
inline Term::operator Int() const { ASSERT(type(f_INT)); return Int(ivalue()); }
/* inline Term::operator kstring() const { ASSERT(type(f_ATOM)); return kstring(ivalue()); } */ inline Term::operator CCP() const { ASSERT(type(f_ATOM)); return kstring(ivalue()); }
inline kstring Term::kstr() const { ASSERT(type(f_ATOM)); return kstring(ivalue()); }
long Cell::toInteger() const { return ivalue(m_value); }