1*088332b5SXin Li /* 2*088332b5SXin Li ** $Id: lvm.h $ 3*088332b5SXin Li ** Lua virtual machine 4*088332b5SXin Li ** See Copyright Notice in lua.h 5*088332b5SXin Li */ 6*088332b5SXin Li 7*088332b5SXin Li #ifndef lvm_h 8*088332b5SXin Li #define lvm_h 9*088332b5SXin Li 10*088332b5SXin Li 11*088332b5SXin Li #include "ldo.h" 12*088332b5SXin Li #include "lobject.h" 13*088332b5SXin Li #include "ltm.h" 14*088332b5SXin Li 15*088332b5SXin Li 16*088332b5SXin Li #if !defined(LUA_NOCVTN2S) 17*088332b5SXin Li #define cvt2str(o) ttisnumber(o) 18*088332b5SXin Li #else 19*088332b5SXin Li #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20*088332b5SXin Li #endif 21*088332b5SXin Li 22*088332b5SXin Li 23*088332b5SXin Li #if !defined(LUA_NOCVTS2N) 24*088332b5SXin Li #define cvt2num(o) ttisstring(o) 25*088332b5SXin Li #else 26*088332b5SXin Li #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27*088332b5SXin Li #endif 28*088332b5SXin Li 29*088332b5SXin Li 30*088332b5SXin Li /* 31*088332b5SXin Li ** You can define LUA_FLOORN2I if you want to convert floats to integers 32*088332b5SXin Li ** by flooring them (instead of raising an error if they are not 33*088332b5SXin Li ** integral values) 34*088332b5SXin Li */ 35*088332b5SXin Li #if !defined(LUA_FLOORN2I) 36*088332b5SXin Li #define LUA_FLOORN2I F2Ieq 37*088332b5SXin Li #endif 38*088332b5SXin Li 39*088332b5SXin Li 40*088332b5SXin Li /* 41*088332b5SXin Li ** Rounding modes for float->integer coercion 42*088332b5SXin Li */ 43*088332b5SXin Li typedef enum { 44*088332b5SXin Li F2Ieq, /* no rounding; accepts only integral values */ 45*088332b5SXin Li F2Ifloor, /* takes the floor of the number */ 46*088332b5SXin Li F2Iceil /* takes the ceil of the number */ 47*088332b5SXin Li } F2Imod; 48*088332b5SXin Li 49*088332b5SXin Li 50*088332b5SXin Li /* convert an object to a float (including string coercion) */ 51*088332b5SXin Li #define tonumber(o,n) \ 52*088332b5SXin Li (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 53*088332b5SXin Li 54*088332b5SXin Li 55*088332b5SXin Li /* convert an object to a float (without string coercion) */ 56*088332b5SXin Li #define tonumberns(o,n) \ 57*088332b5SXin Li (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ 58*088332b5SXin Li (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) 59*088332b5SXin Li 60*088332b5SXin Li 61*088332b5SXin Li /* convert an object to an integer (including string coercion) */ 62*088332b5SXin Li #define tointeger(o,i) \ 63*088332b5SXin Li (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 64*088332b5SXin Li 65*088332b5SXin Li 66*088332b5SXin Li /* convert an object to an integer (without string coercion) */ 67*088332b5SXin Li #define tointegerns(o,i) \ 68*088332b5SXin Li (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) 69*088332b5SXin Li 70*088332b5SXin Li 71*088332b5SXin Li #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 72*088332b5SXin Li 73*088332b5SXin Li #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 74*088332b5SXin Li 75*088332b5SXin Li 76*088332b5SXin Li /* 77*088332b5SXin Li ** fast track for 'gettable': if 't' is a table and 't[k]' is present, 78*088332b5SXin Li ** return 1 with 'slot' pointing to 't[k]' (position of final result). 79*088332b5SXin Li ** Otherwise, return 0 (meaning it will have to check metamethod) 80*088332b5SXin Li ** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL 81*088332b5SXin Li ** (otherwise). 'f' is the raw get function to use. 82*088332b5SXin Li */ 83*088332b5SXin Li #define luaV_fastget(L,t,k,slot,f) \ 84*088332b5SXin Li (!ttistable(t) \ 85*088332b5SXin Li ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 86*088332b5SXin Li : (slot = f(hvalue(t), k), /* else, do raw access */ \ 87*088332b5SXin Li !isempty(slot))) /* result not empty? */ 88*088332b5SXin Li 89*088332b5SXin Li 90*088332b5SXin Li /* 91*088332b5SXin Li ** Special case of 'luaV_fastget' for integers, inlining the fast case 92*088332b5SXin Li ** of 'luaH_getint'. 93*088332b5SXin Li */ 94*088332b5SXin Li #define luaV_fastgeti(L,t,k,slot) \ 95*088332b5SXin Li (!ttistable(t) \ 96*088332b5SXin Li ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 97*088332b5SXin Li : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ 98*088332b5SXin Li ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ 99*088332b5SXin Li !isempty(slot))) /* result not empty? */ 100*088332b5SXin Li 101*088332b5SXin Li 102*088332b5SXin Li /* 103*088332b5SXin Li ** Finish a fast set operation (when fast get succeeds). In that case, 104*088332b5SXin Li ** 'slot' points to the place to put the value. 105*088332b5SXin Li */ 106*088332b5SXin Li #define luaV_finishfastset(L,t,slot,v) \ 107*088332b5SXin Li { setobj2t(L, cast(TValue *,slot), v); \ 108*088332b5SXin Li luaC_barrierback(L, gcvalue(t), v); } 109*088332b5SXin Li 110*088332b5SXin Li 111*088332b5SXin Li 112*088332b5SXin Li 113*088332b5SXin Li LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 114*088332b5SXin Li LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 115*088332b5SXin Li LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 116*088332b5SXin Li LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 117*088332b5SXin Li LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); 118*088332b5SXin Li LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, 119*088332b5SXin Li F2Imod mode); 120*088332b5SXin Li LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); 121*088332b5SXin Li LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 122*088332b5SXin Li StkId val, const TValue *slot); 123*088332b5SXin Li LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 124*088332b5SXin Li TValue *val, const TValue *slot); 125*088332b5SXin Li LUAI_FUNC void luaV_finishOp (lua_State *L); 126*088332b5SXin Li LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); 127*088332b5SXin Li LUAI_FUNC void luaV_concat (lua_State *L, int total); 128*088332b5SXin Li LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y); 129*088332b5SXin Li LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 130*088332b5SXin Li LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); 131*088332b5SXin Li LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 132*088332b5SXin Li LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 133*088332b5SXin Li 134*088332b5SXin Li #endif 135