LUA_API int lua_getmetatable (lua_State *L, int objindex) { const TValue *obj; Table *mt = NULL; int res; lua_lock(L); obj = index2addr(L, objindex); switch (ttypenv(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; break; case LUA_TUSERDATA: mt = uvalue(obj)->metatable; break; default: mt = G(L)->mt[ttypenv(obj)]; break; } if (mt == NULL) res = 0; else { sethvalue(L, L->top, mt); api_incr_top(L); res = 1; } lua_unlock(L); return res; }
LUA_API int lua_getmetatable (lua_State *L, int objindex) { const TObject *obj; Table *mt = NULL; int res; lua_lock(L); obj = luaA_indexAcceptable(L, objindex); if (obj != NULL) { switch (ttype(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; break; case LUA_TUSERDATA: mt = uvalue(obj)->uv.metatable; break; } } if (mt == NULL || mt == hvalue(defaultmeta(L))) res = 0; else { sethvalue(L->top, mt); api_incr_top(L); res = 1; } lua_unlock(L); return res; }
LUA_API void lua_setuservalue (lua_State *L, int idx) { StkId o; lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); api_check(L, ttisuserdata(o), "userdata expected"); if (ttisnil(L->top - 1)) uvalue(o)->env = NULL; else { api_check(L, ttistable(L->top - 1), "table expected"); uvalue(o)->env = hvalue(L->top - 1); luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); } L->top--; lua_unlock(L); }
LUA_API int lua_setmetatable (lua_State *L, int objindex) { TObject *obj, *mt; int res = 1; lua_lock(L); api_checknelems(L, 1); obj = luaA_index(L, objindex); mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L); api_check(L, ttistable(mt)); switch (ttype(obj)) { case LUA_TTABLE: { hvalue(obj)->metatable = hvalue(mt); /* write barrier */ break; } case LUA_TUSERDATA: { uvalue(obj)->uv.metatable = hvalue(mt); /* write barrier */ break; } default: { res = 0; /* cannot set */ break; } } L->top--; lua_unlock(L); return res; }
int debug_getsize(lua_State* L) { TValue* o = L->base; switch (o->tt) { /* Container types */ case LUA_TTABLE: { Table *h = hvalue(o); lua_pushinteger(L, sizeof(Table) + sizeof(TValue) * h->sizearray + sizeof(Node) * sizenode(h)); break; } case LUA_TFUNCTION: { Closure *cl = clvalue(o); lua_pushinteger(L, (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : sizeLclosure(cl->l.nupvalues)); break; } case LUA_TTHREAD: { lua_State *th = thvalue(o); lua_pushinteger(L, sizeof(lua_State) + sizeof(TValue) * th->stacksize + sizeof(CallInfo) * th->size_ci); break; } case LUA_TPROTO: { Proto *p = pvalue(o); lua_pushinteger(L, sizeof(Proto) + sizeof(Instruction) * p->sizecode + sizeof(Proto *) * p->sizep + sizeof(TValue) * p->sizek + sizeof(int) * p->sizelineinfo + sizeof(LocVar) * p->sizelocvars + sizeof(TString *) * p->sizeupvalues); break; } /* Non-containers */ case LUA_TUSERDATA: { lua_pushnumber(L, uvalue(o)->len); break; } case LUA_TLIGHTUSERDATA: { lua_pushnumber(L, sizeof(void*)); break; } case LUA_TSTRING: { TString *s = rawtsvalue(o); lua_pushinteger(L, sizeof(TString) + s->tsv.len + 1); break; } case LUA_TNUMBER: { lua_pushinteger(L, sizeof(lua_Number)); break; } case LUA_TBOOLEAN: { lua_pushinteger(L, sizeof(int)); break; } default: return 0; } return 1; }
LUA_API void *lua_touserdata (lua_State *L, int idx) { StkId o = index2addr(L, idx); switch (ttnov(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } }
static Dtree *addunion(Dtree *t, Node *pat, Node *val, Node ***cap, size_t *ncap) { Node *elt, *tag, *id; int32_t t1, t2; Dtree *sub; Ucon *uc; size_t i; if (t->any) return t->any; uc = finducon(tybase(exprtype(pat)), pat->expr.args[0]); t2 = uc->id; /* if we have the value already... */ sub = NULL; for (i = 0; i < t->nval; i++) { tag = t->val[i]->expr.args[0]; t1 = tag->lit.intval; if (t1 == t2) { if (pat->expr.nargs > 1) { elt = uvalue(val, exprtype(pat->expr.args[1])); return addpat(t->sub[i], pat->expr.args[1], elt, cap, ncap); } else { return t->sub[i]; } } } /* otherwise create a new match */ sub = mkdtree(); sub->patexpr = pat; tag = mkexpr(pat->loc, Outag, val, NULL); tag->expr.type = mktype(pat->loc, Tyint32); id = mkintlit(pat->loc, uc->id); id->expr.type = mktype(pat->loc, Tyint32); lappend(&t->val, &t->nval, id); lappend(&t->sub, &t->nsub, sub); lappend(&t->load, &t->nload, tag); if (pat->expr.nargs == 2) { elt = uvalue(val, exprtype(pat->expr.args[1])); sub = addpat(sub, pat->expr.args[1], elt, cap, ncap); } return sub; }
/* ** The next function tells whether a key or value can be cleared from ** a weak table. Non-collectable objects are never removed from weak ** tables. Strings behave as `values', so are never removed too. for ** other objects: if really collected, cannot keep them; for userdata ** being finalized, keep them in keys, but not in values */ static int iscleared (const TValue *o, int iskey) { if (!iscollectable(o)) return 0; if (ttisstring(o)) { stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ return 0; } return iswhite(gcvalue(o)) || (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); }
LUA_API size_t lua_rawlen (lua_State *L, int idx) { StkId o = index2addr(L, idx); switch (ttypenv(o)) { case LUA_TSTRING: return tsvalue(o)->len; case LUA_TUSERDATA: return uvalue(o)->len; case LUA_TTABLE: return luaH_getn(hvalue(o)); default: return 0; } }
LUA_API void *lua_touserdata (lua_State *L, int idx) { StkId o = luaA_indexAcceptable(L, idx); if (o == NULL) return NULL; switch (ttype(o)) { case LUA_TUSERDATA: return (uvalue(o) + 1); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } }
/* ** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) */ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; if (ttype(t1) != ttype(t2)) { /* not the same variant? */ if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ lua_Integer i1, i2; /* compare them as integers */ return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); } } /* values have same type and same variant */ switch (ttype(t1)) { case LUA_TNIL: return 1; case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TLCF: return fvalue(t1) == fvalue(t2); case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); case LUA_TUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); if (tm == NULL) tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } case LUA_TTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); if (tm == NULL) tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } default: return gcvalue(t1) == gcvalue(t2); } if (tm == NULL) /* no TM? */ return 0; /* objects are different */ luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ return !l_isfalse(L->top); }
/* ** Return the name of the type of an object. For tables and userdata ** with metatable, use their '__name' metafield, if present. */ const char *luaT_objtypename (lua_State *L, const TValue *o) { Table *mt; if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); if (ttisstring(name)) /* is '__name' a string? */ return getstr(tsvalue(name)); /* use it as type name */ } return ttypename(ttnov(o)); /* else use standard type name */ }
LUA_API int lua_getuservalue (lua_State *L, int idx) { StkId o; lua_lock(L); o = index2addr(L, idx); api_check(ttisfulluserdata(o), "full userdata expected"); getuservalue(L, uvalue(o), L->top); api_incr_top(L); lua_unlock(L); return ttnov(L->top - 1); }
LUA_API void lua_setuservalue (lua_State *L, int idx) { StkId o; lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); api_check(ttisfulluserdata(o), "full userdata expected"); setuservalue(L, uvalue(o), L->top - 1); luaC_barrier(L, gcvalue(o), L->top - 1); L->top--; lua_unlock(L); }
const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) { TString *ename = G(L)->tmname[event]; switch (ttype(o)) { case LUA_TTABLE: return luaH_getstr(hvalue(o)->metatable, ename); case LUA_TUSERDATA: return luaH_getstr(uvalue(o)->uv.metatable, ename); default: return &luaO_nilobject; } }
float ExpressionResult::fvalue() const { // Convert any value to float if (size & esDouble) return result.d; else if (size & esFloat) return result.f; else if (size & esUnsigned) return uvalue(); else return value(); }
LUA_API const void *lua_topointer (lua_State *L, int idx) { StkId o = index2addr(L, idx); switch (ttype(o)) { case LUA_TTABLE: return hvalue(o); case LUA_TLCL: return clLvalue(o); case LUA_TCCL: return clCvalue(o); case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); case LUA_TTHREAD: return thvalue(o); case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } }
LUA_API int lua_setmetatable (lua_State *L, int objindex) { TValue *obj; Table *mt; lua_lock(L); api_checknelems(L, 1); obj = index2addr(L, objindex); if (ttisnil(L->top - 1)) mt = NULL; else { api_check(ttistable(L->top - 1), "table expected"); mt = hvalue(L->top - 1); } switch (ttnov(obj)) { case LUA_TTABLE: { hvalue(obj)->metatable = mt; if (mt) { luaC_objbarrier(L, gcvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; } case LUA_TUSERDATA: { uvalue(obj)->metatable = mt; if (mt) { luaC_objbarrier(L, uvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; } default: { G(L)->mt[ttnov(obj)] = mt; break; } } L->top--; lua_unlock(L); return 1; }
LUA_API int lua_setmetatable (lua_State *L, int objindex) { TObject *obj, *mt; int res = 1; lua_lock(L); api_checknelems(L, 1); obj = luaA_index(L, objindex); mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L); api_check(L, ttistable(mt)); switch (ttype(obj)) { case LUA_TTABLE: { #if LUA_REFCOUNT __AddRefDirect(hvalue(mt)); __ReleaseDirect(L, hvalue(obj)->metatable); #endif LUA_REFCOUNT hvalue(obj)->metatable = hvalue(mt); /* write barrier */ break; } case LUA_TUSERDATA: { #if LUA_REFCOUNT __AddRefDirect(hvalue(mt)); __ReleaseDirect(L, &uvalue(obj)->uv.metatable); #endif LUA_REFCOUNT uvalue(obj)->uv.metatable = hvalue(mt); /* write barrier */ break; } default: { res = 0; /* cannot set */ break; } } #if LUA_REFCOUNT setnilvalue(--L->top); #else !LUA_REFCOUNT L->top--; #endif LUA_REFCOUNT lua_unlock(L); return res; }
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { Table *mt; switch (ttypenv(o)) { case LUA_TTABLE: mt = hvalue(o)->metatable; break; case LUA_TUSERDATA: mt = uvalue(o)->metatable; break; default: mt = G(L)->mt[ttypenv(o)]; } return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); }
/* * equality of ktap values. ks == NULL means raw equality */ int kp_equalobjv(ktap_state *ks, const ktap_value *t1, const ktap_value *t2) { switch (ttype(t1)) { case KTAP_TNIL: return 1; case KTAP_TNUMBER: return nvalue(t1) == nvalue(t2); case KTAP_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ case KTAP_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case KTAP_TLCF: return fvalue(t1) == fvalue(t2); case KTAP_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2)); case KTAP_TLNGSTR: return kp_tstring_eqlngstr(rawtsvalue(t1), rawtsvalue(t2)); case KTAP_TUSERDATA: if (uvalue(t1) == uvalue(t2)) return 1; else if (ks == NULL) return 0; case KTAP_TTABLE: if (hvalue(t1) == hvalue(t2)) return 1; else if (ks == NULL) return 0; #ifdef __KERNEL__ case KTAP_TBTRACE: return kp_btrace_equal(btvalue(t1), btvalue(t2)); #endif default: return gcvalue(t1) == gcvalue(t2); } return 0; }
int luaV_equalval(lua_State *L, const TValue *t1, const TValue *t2) { const TValue* tm = 0; lua_assert(ttype(t1) == ttype(t2)); switch (ttype(t1)) { case LUA_TNIL: return 1; case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } case LUA_TTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } default: return gcvalue(t1) == gcvalue(t2); } if (tm == NULL) return 0; /* no TM? */ callTMres(L, L->top, tm, t1, t2); /* call TM */ return !l_isfalse(L->top); }
void kp_showobj(ktap_state *ks, const ktap_value *v) { switch (ttype(v)) { case KTAP_TNIL: kp_puts(ks, "nil"); break; case KTAP_TNUMBER: kp_printf(ks, "%ld", nvalue(v)); break; case KTAP_TBOOLEAN: kp_puts(ks, (bvalue(v) == 1) ? "true" : "false"); break; case KTAP_TLIGHTUSERDATA: kp_printf(ks, "0x%lx", (unsigned long)pvalue(v)); break; case KTAP_TLCF: kp_printf(ks, "0x%lx", (unsigned long)fvalue(v)); break; case KTAP_TSHRSTR: case KTAP_TLNGSTR: kp_puts(ks, svalue(v)); break; case KTAP_TUSERDATA: kp_printf(ks, "0x%lx", (unsigned long)uvalue(v)); break; case KTAP_TTABLE: kp_table_dump(ks, hvalue(v)); break; #ifdef __KERNEL__ case KTAP_TEVENT: kp_transport_event_write(ks, evalue(v)); break; case KTAP_TBTRACE: kp_btrace_dump(ks, btvalue(v)); break; case KTAP_TAGGRTABLE: kp_aggrtable_dump(ks, ahvalue(v)); break; case KTAP_TAGGRACCVAL: kp_aggraccval_dump(ks, aggraccvalue(v)); break; #endif default: kp_error(ks, "print unknown value type: %d\n", ttype(v)); break; } }
static void persistuserdata(PersistInfo *pi) { /* perms reftbl ... udata */ if(persistspecialobject(pi, 0)) { /* perms reftbl ... udata */ return; } else { /* Use literal persistence */ int length = uvalue(getobject(pi->L, -2))->uv.len; pi->writer(pi->L, &length, sizeof(int), pi->ud); pi->writer(pi->L, lua_touserdata(pi->L, -1), length, pi->ud); if(!lua_getmetatable(pi->L, -1)) { /* perms reftbl ... udata */ lua_pushnil(pi->L); /* perms reftbl ... udata mt/nil */ } persist(pi); lua_pop(pi->L, 1); /* perms reftbl ... udata */ } }
void kp_showobj(ktap_State *ks, const Tvalue *v) { switch (ttype(v)) { case KTAP_TNIL: kp_printf(ks, "nil"); break; case KTAP_TNUMBER: kp_printf(ks, "%d", nvalue(v)); break; case KTAP_TBOOLEAN: kp_printf(ks, "%s", (bvalue(v) == 1) ? "true" : "false"); break; case KTAP_TLIGHTUSERDATA: kp_printf(ks, "%d", pvalue(v)); break; case KTAP_TLCF: kp_printf(ks, "0x%x", fvalue(v)); break; case KTAP_TSHRSTR: case KTAP_TLNGSTR: kp_printf(ks, "\"%s\"", getstr(rawtsvalue(v))); break; case KTAP_TUSERDATA: kp_printf(ks, "%d", uvalue(v)); break; case KTAP_TTABLE: kp_table_dump(ks, hvalue(v)); break; #ifdef __KERNEL__ case KTAP_TEVENT: kp_show_event(ks); break; #endif default: kp_printf(ks, "[unknown value type: %d]", ttype(v)); break; } }
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { Table *mt; switch (ttype(o)) { case LUA_TTABLE: mt = hvalue(o)->metatable; break; case LUA_TROTABLE: mt = (Table *)luaR_getmeta(rvalue(o)); break; case LUA_TUSERDATA: mt = uvalue(o)->metatable; break; default: mt = G(L)->mt[ttype(o)]; } if (!mt) return luaO_nilobject; else if (luaR_isrotable(mt)) return luaH_getstr_ro(mt, G(L)->tmname[event]); else return luaH_getstr(mt, G(L)->tmname[event]); }
static int hashindex (Hash *t, Object *ref) /* hash function */ { long int h; switch (tag(ref)) { case LUA_T_NIL: lua_error ("unexpected type to index table"); h = 0; /* UNREACHEABLE */ case LUA_T_NUMBER: h = (long int)nvalue(ref); break; case LUA_T_STRING: h = tsvalue(ref)->hash; break; case LUA_T_FUNCTION: h = (IntPoint)ref->value.tf; break; case LUA_T_CFUNCTION: h = (IntPoint)fvalue(ref); break; case LUA_T_ARRAY: h = (IntPoint)avalue(ref); break; default: /* user data */ h = (IntPoint)uvalue(ref); break; } if (h < 0) h = -h; return h%nhash(t); /* make it a valid index */ }
QString ExpressionResult::toString() const { if (resultType & erUndefined) return "(undefined value)"; else if (resultType & erRuntime) return "(runtime expression)"; switch (size) { case esUndefined: return "(undefined value)"; case esFloat: return QString::number(result.f); case esDouble: return QString::number(result.d); case esInt8: return QString::number(result.i8); case esUInt8: return QString::number(result.ui8); case esInt16: return QString::number(result.i16); case esUInt16: return QString::number(result.ui16); case esInt32: return (result.i32 > 65535 || result.i32 < -65535) ? "0x" + QString::number(result.i32, 16) : QString::number(result.i32); case esUInt32: return (result.ui32 > 65535) ? "0x" + QString::number(result.ui32, 16) : QString::number(result.ui32); case esInt64: return (result.i64 > 65535 || result.i64 < -65535) ? "0x" + QString::number(result.i64, 16) : QString::number(result.i64); case esUInt64: return (result.ui64 > 65535) ? "0x" + QString::number(result.ui64, 16) : QString::number(result.ui64); default: return (size & esUnsigned) ? QString::number(uvalue()) : QString::number(value()); } }
qint64 ExpressionResult::value(ExprResultSizes target) const { qint64 ret; // Is either the target or this value unsigned? if (target && ((target | size) & esUnsigned)) { // If the target is unsigned, return the unsigned value instead. // If this value is unsigned, do not extend the sign! ret = uvalue(target | esUnsigned); } // No target specified, or both target and this value are signed, so // extend this value's sign to 64 bit else { ret = (size & es64Bit) ? result.i64 : (size & es32Bit) ? (qint64) result.i32 : (size & es16Bit) ? (qint64) result.i16 : (qint64) result.i8; } // debugmsg(QString("Returning 0x%1").arg(ret, 16, 16, QChar('0'))); return ret; }
/* ** Given an object handle, return its user data. On error, return NULL. */ void *lua_getuserdata (lua_Object object) { if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA) return NULL; else return (uvalue(Address(object))); }