Clearer distinction between types and tags

LUA_T* represents only types; tags (types + Variants) are represented
by LUA_V* constants.
This commit is contained in:
Roberto Ierusalimschy
2020-01-31 11:09:53 -03:00
parent 69c7139ff8
commit 46c3587a6f
22 changed files with 231 additions and 213 deletions

138
lobject.h
View File

@@ -17,16 +17,16 @@
/*
** Extra tags for collectable non-values
** Extra types for collectable non-values
*/
#define LUA_TUPVAL LUA_NUMTAGS /* upvalues */
#define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */
#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */
#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */
/*
** number of all possible tags (including LUA_TNONE)
** number of all possible types (including LUA_TNONE)
*/
#define LUA_TOTALTAGS (LUA_TPROTO + 2)
#define LUA_TOTALTYPES (LUA_TPROTO + 2)
/*
@@ -154,30 +154,28 @@ typedef StackValue *StkId;
** ===================================================================
*/
/* Standard nil */
#define LUA_VNIL makevariant(LUA_TNIL, 1)
/* Empty slot (which might be different from a slot containing nil) */
#define LUA_VEMPTY makevariant(LUA_TNIL, 2)
/* Value returned for a key not found in a table (absent key) */
#define LUA_VABSTKEY makevariant(LUA_TNIL, 3)
/* macro to test for (any kind of) nil */
#define ttisnil(v) checktype((v), LUA_TNIL)
/* macro to test for a "pure" nil */
#define ttisstrictnil(o) checktag((o), LUA_TNIL)
/* macro to test for a standard nil */
#define ttisstrictnil(o) checktag((o), LUA_VNIL)
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
#define setnilvalue(obj) settt_(obj, LUA_VNIL)
/*
** Variant tag, used only in tables to signal an empty slot
** (which might be different from a slot containing nil)
*/
#define LUA_TEMPTY makevariant(LUA_TNIL, 1)
/*
** Variant used only in the value returned for a key not found in a
** table (absent key).
*/
#define LUA_TABSTKEY makevariant(LUA_TNIL, 2)
#define isabstkey(v) checktag((v), LUA_TABSTKEY)
#define isabstkey(v) checktag((v), LUA_VABSTKEY)
/*
@@ -195,11 +193,11 @@ typedef StackValue *StkId;
/* macro defining a value corresponding to an absent key */
#define ABSTKEYCONSTANT {NULL}, LUA_TABSTKEY
#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY
/* mark an entry as empty */
#define setempty(v) settt_(v, LUA_TEMPTY)
#define setempty(v) settt_(v, LUA_VEMPTY)
@@ -213,19 +211,19 @@ typedef StackValue *StkId;
*/
#define LUA_TFALSE makevariant(LUA_TBOOLEAN, 1)
#define LUA_TTRUE makevariant(LUA_TBOOLEAN, 2)
#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 1)
#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 2)
#define ttisboolean(o) checktype((o), LUA_TBOOLEAN)
#define ttisfalse(o) checktag((o), LUA_TFALSE)
#define ttistrue(o) checktag((o), LUA_TTRUE)
#define ttisfalse(o) checktag((o), LUA_VFALSE)
#define ttistrue(o) checktag((o), LUA_VTRUE)
#define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
#define setbfvalue(obj) settt_(obj, LUA_TFALSE)
#define setbtvalue(obj) settt_(obj, LUA_TTRUE)
#define setbfvalue(obj) settt_(obj, LUA_VFALSE)
#define setbtvalue(obj) settt_(obj, LUA_VTRUE)
/* }================================================================== */
@@ -236,13 +234,15 @@ typedef StackValue *StkId;
** ===================================================================
*/
#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
#define LUA_VTHREAD makevariant(LUA_TTHREAD, 1)
#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))
#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
#define setthvalue(L,obj,x) \
{ TValue *io = (obj); lua_State *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
checkliveness(L,io); }
#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
@@ -295,12 +295,12 @@ typedef struct GCObject {
*/
/* Variant tags for numbers */
#define LUA_TNUMINT makevariant(LUA_TNUMBER, 1) /* integer numbers */
#define LUA_TNUMFLT makevariant(LUA_TNUMBER, 2) /* float numbers */
#define LUA_VNUMINT makevariant(LUA_TNUMBER, 1) /* integer numbers */
#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 2) /* float numbers */
#define ttisnumber(o) checktype((o), LUA_TNUMBER)
#define ttisfloat(o) checktag((o), LUA_TNUMFLT)
#define ttisinteger(o) checktag((o), LUA_TNUMINT)
#define ttisfloat(o) checktag((o), LUA_VNUMFLT)
#define ttisinteger(o) checktag((o), LUA_VNUMINT)
#define nvalue(o) check_exp(ttisnumber(o), \
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
@@ -311,13 +311,13 @@ typedef struct GCObject {
#define ivalueraw(v) ((v).i)
#define setfltvalue(obj,x) \
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
#define chgfltvalue(obj,x) \
{ TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
#define setivalue(obj,x) \
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
#define chgivalue(obj,x) \
{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
@@ -332,12 +332,12 @@ typedef struct GCObject {
*/
/* Variant tags for strings */
#define LUA_TSHRSTR makevariant(LUA_TSTRING, 1) /* short strings */
#define LUA_TLNGSTR makevariant(LUA_TSTRING, 2) /* long strings */
#define LUA_VSHRSTR makevariant(LUA_TSTRING, 1) /* short strings */
#define LUA_VLNGSTR makevariant(LUA_TSTRING, 2) /* long strings */
#define ttisstring(o) checktype((o), LUA_TSTRING)
#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR))
#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR))
#define tsvalueraw(v) (gco2ts((v).gc))
@@ -384,7 +384,7 @@ typedef struct TString {
#define svalue(o) getstr(tsvalue(o))
/* get string length from 'TString *s' */
#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen)
/* get string length from 'TValue *o' */
#define vslen(o) tsslen(tsvalue(o))
@@ -398,8 +398,16 @@ typedef struct TString {
** ===================================================================
*/
#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
#define ttisfulluserdata(o) checktype((o), LUA_TUSERDATA)
/*
** Light userdata should be a variant of userdata, but for compatibility
** reasons they are also different types.
*/
#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 1)
#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 1)
#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)
#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
@@ -407,11 +415,11 @@ typedef struct TString {
#define pvalueraw(v) ((v).p)
#define setpvalue(obj,x) \
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
#define setuvalue(L,obj,x) \
{ TValue *io = (obj); Udata *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
checkliveness(L,io); }
@@ -474,6 +482,9 @@ typedef struct Udata0 {
** ===================================================================
*/
#define LUA_VPROTO makevariant(LUA_TPROTO, 1)
/*
** Description of an upvalue for function prototypes
*/
@@ -548,16 +559,19 @@ typedef struct Proto {
** ===================================================================
*/
#define LUA_VUPVAL makevariant(LUA_TUPVAL, 1)
/* Variant tags for functions */
#define LUA_TLCL makevariant(LUA_TFUNCTION, 1) /* Lua closure */
#define LUA_TLCF makevariant(LUA_TFUNCTION, 2) /* light C function */
#define LUA_TCCL makevariant(LUA_TFUNCTION, 3) /* C closure */
#define LUA_VLCL makevariant(LUA_TFUNCTION, 1) /* Lua closure */
#define LUA_VLCF makevariant(LUA_TFUNCTION, 2) /* light C function */
#define LUA_VCCL makevariant(LUA_TFUNCTION, 3) /* C closure */
#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL)
#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
#define ttislcf(o) checktag((o), LUA_TLCF)
#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL)
#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))
#define ttislcf(o) checktag((o), LUA_VLCF)
#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))
#define isLfunction(o) ttisLclosure(o)
@@ -570,17 +584,17 @@ typedef struct Proto {
#define setclLvalue(L,obj,x) \
{ TValue *io = (obj); LClosure *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
checkliveness(L,io); }
#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
#define setfvalue(obj,x) \
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
#define setclCvalue(L,obj,x) \
{ TValue *io = (obj); CClosure *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
checkliveness(L,io); }
@@ -636,13 +650,15 @@ typedef union Closure {
** ===================================================================
*/
#define ttistable(o) checktag((o), ctb(LUA_TTABLE))
#define LUA_VTABLE makevariant(LUA_TTABLE, 1)
#define ttistable(o) checktag((o), ctb(LUA_VTABLE))
#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
#define sethvalue(L,obj,x) \
{ TValue *io = (obj); Table *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
checkliveness(L,io); }
#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
@@ -713,9 +729,9 @@ typedef struct Table {
#define keyval(node) ((node)->u.key_val)
#define keyisnil(node) (keytt(node) == LUA_TNIL)
#define keyisinteger(node) (keytt(node) == LUA_TNUMINT)
#define keyisinteger(node) (keytt(node) == LUA_VNUMINT)
#define keyival(node) (keyval(node).i)
#define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR))
#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))
#define keystrval(node) (gco2ts(keyval(node).gc))
#define setnilkey(node) (keytt(node) = LUA_TNIL)