misc/liblua/lobject.h
changeset 10017 de822cd3df3a
parent 3697 d5b30d6373fc
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    15 #include "llimits.h"
    15 #include "llimits.h"
    16 #include "lua.h"
    16 #include "lua.h"
    17 
    17 
    18 
    18 
    19 /* tags for values visible from Lua */
    19 /* tags for values visible from Lua */
    20 #define LAST_TAG	LUA_TTHREAD
    20 #define LAST_TAG    LUA_TTHREAD
    21 
    21 
    22 #define NUM_TAGS	(LAST_TAG+1)
    22 #define NUM_TAGS    (LAST_TAG+1)
    23 
    23 
    24 
    24 
    25 /*
    25 /*
    26 ** Extra tags for non-values
    26 ** Extra tags for non-values
    27 */
    27 */
    28 #define LUA_TPROTO	(LAST_TAG+1)
    28 #define LUA_TPROTO  (LAST_TAG+1)
    29 #define LUA_TUPVAL	(LAST_TAG+2)
    29 #define LUA_TUPVAL  (LAST_TAG+2)
    30 #define LUA_TDEADKEY	(LAST_TAG+3)
    30 #define LUA_TDEADKEY    (LAST_TAG+3)
    31 
    31 
    32 
    32 
    33 /*
    33 /*
    34 ** Union of all collectable objects
    34 ** Union of all collectable objects
    35 */
    35 */
    38 
    38 
    39 /*
    39 /*
    40 ** Common Header for all collectable objects (in macro form, to be
    40 ** Common Header for all collectable objects (in macro form, to be
    41 ** included in other objects)
    41 ** included in other objects)
    42 */
    42 */
    43 #define CommonHeader	GCObject *next; lu_byte tt; lu_byte marked
    43 #define CommonHeader    GCObject *next; lu_byte tt; lu_byte marked
    44 
    44 
    45 
    45 
    46 /*
    46 /*
    47 ** Common header in struct form
    47 ** Common header in struct form
    48 */
    48 */
    66 
    66 
    67 /*
    67 /*
    68 ** Tagged Values
    68 ** Tagged Values
    69 */
    69 */
    70 
    70 
    71 #define TValuefields	Value value; int tt
    71 #define TValuefields    Value value; int tt
    72 
    72 
    73 typedef struct lua_TValue {
    73 typedef struct lua_TValue {
    74   TValuefields;
    74   TValuefields;
    75 } TValue;
    75 } TValue;
    76 
    76 
    77 
    77 
    78 /* Macros to test type */
    78 /* Macros to test type */
    79 #define ttisnil(o)	(ttype(o) == LUA_TNIL)
    79 #define ttisnil(o)  (ttype(o) == LUA_TNIL)
    80 #define ttisnumber(o)	(ttype(o) == LUA_TNUMBER)
    80 #define ttisnumber(o)   (ttype(o) == LUA_TNUMBER)
    81 #define ttisstring(o)	(ttype(o) == LUA_TSTRING)
    81 #define ttisstring(o)   (ttype(o) == LUA_TSTRING)
    82 #define ttistable(o)	(ttype(o) == LUA_TTABLE)
    82 #define ttistable(o)    (ttype(o) == LUA_TTABLE)
    83 #define ttisfunction(o)	(ttype(o) == LUA_TFUNCTION)
    83 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
    84 #define ttisboolean(o)	(ttype(o) == LUA_TBOOLEAN)
    84 #define ttisboolean(o)  (ttype(o) == LUA_TBOOLEAN)
    85 #define ttisuserdata(o)	(ttype(o) == LUA_TUSERDATA)
    85 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
    86 #define ttisthread(o)	(ttype(o) == LUA_TTHREAD)
    86 #define ttisthread(o)   (ttype(o) == LUA_TTHREAD)
    87 #define ttislightuserdata(o)	(ttype(o) == LUA_TLIGHTUSERDATA)
    87 #define ttislightuserdata(o)    (ttype(o) == LUA_TLIGHTUSERDATA)
    88 
    88 
    89 /* Macros to access values */
    89 /* Macros to access values */
    90 #define ttype(o)	((o)->tt)
    90 #define ttype(o)    ((o)->tt)
    91 #define gcvalue(o)	check_exp(iscollectable(o), (o)->value.gc)
    91 #define gcvalue(o)  check_exp(iscollectable(o), (o)->value.gc)
    92 #define pvalue(o)	check_exp(ttislightuserdata(o), (o)->value.p)
    92 #define pvalue(o)   check_exp(ttislightuserdata(o), (o)->value.p)
    93 #define nvalue(o)	check_exp(ttisnumber(o), (o)->value.n)
    93 #define nvalue(o)   check_exp(ttisnumber(o), (o)->value.n)
    94 #define rawtsvalue(o)	check_exp(ttisstring(o), &(o)->value.gc->ts)
    94 #define rawtsvalue(o)   check_exp(ttisstring(o), &(o)->value.gc->ts)
    95 #define tsvalue(o)	(&rawtsvalue(o)->tsv)
    95 #define tsvalue(o)  (&rawtsvalue(o)->tsv)
    96 #define rawuvalue(o)	check_exp(ttisuserdata(o), &(o)->value.gc->u)
    96 #define rawuvalue(o)    check_exp(ttisuserdata(o), &(o)->value.gc->u)
    97 #define uvalue(o)	(&rawuvalue(o)->uv)
    97 #define uvalue(o)   (&rawuvalue(o)->uv)
    98 #define clvalue(o)	check_exp(ttisfunction(o), &(o)->value.gc->cl)
    98 #define clvalue(o)  check_exp(ttisfunction(o), &(o)->value.gc->cl)
    99 #define hvalue(o)	check_exp(ttistable(o), &(o)->value.gc->h)
    99 #define hvalue(o)   check_exp(ttistable(o), &(o)->value.gc->h)
   100 #define bvalue(o)	check_exp(ttisboolean(o), (o)->value.b)
   100 #define bvalue(o)   check_exp(ttisboolean(o), (o)->value.b)
   101 #define thvalue(o)	check_exp(ttisthread(o), &(o)->value.gc->th)
   101 #define thvalue(o)  check_exp(ttisthread(o), &(o)->value.gc->th)
   102 
   102 
   103 #define l_isfalse(o)	(ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
   103 #define l_isfalse(o)    (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
   104 
   104 
   105 /*
   105 /*
   106 ** for internal debug only
   106 ** for internal debug only
   107 */
   107 */
   108 #define checkconsistency(obj) \
   108 #define checkconsistency(obj) \
   167 /*
   167 /*
   168 ** different types of sets, according to destination
   168 ** different types of sets, according to destination
   169 */
   169 */
   170 
   170 
   171 /* from stack to (same) stack */
   171 /* from stack to (same) stack */
   172 #define setobjs2s	setobj
   172 #define setobjs2s   setobj
   173 /* to stack (not from same stack) */
   173 /* to stack (not from same stack) */
   174 #define setobj2s	setobj
   174 #define setobj2s    setobj
   175 #define setsvalue2s	setsvalue
   175 #define setsvalue2s setsvalue
   176 #define sethvalue2s	sethvalue
   176 #define sethvalue2s sethvalue
   177 #define setptvalue2s	setptvalue
   177 #define setptvalue2s    setptvalue
   178 /* from table to same table */
   178 /* from table to same table */
   179 #define setobjt2t	setobj
   179 #define setobjt2t   setobj
   180 /* to table */
   180 /* to table */
   181 #define setobj2t	setobj
   181 #define setobj2t    setobj
   182 /* to new object */
   182 /* to new object */
   183 #define setobj2n	setobj
   183 #define setobj2n    setobj
   184 #define setsvalue2n	setsvalue
   184 #define setsvalue2n setsvalue
   185 
   185 
   186 #define setttype(obj, tt) (ttype(obj) = (tt))
   186 #define setttype(obj, tt) (ttype(obj) = (tt))
   187 
   187 
   188 
   188 
   189 #define iscollectable(o)	(ttype(o) >= LUA_TSTRING)
   189 #define iscollectable(o)    (ttype(o) >= LUA_TSTRING)
   190 
   190 
   191 
   191 
   192 
   192 
   193 typedef TValue *StkId;  /* index to stack elements */
   193 typedef TValue *StkId;  /* index to stack elements */
   194 
   194 
   205     size_t len;
   205     size_t len;
   206   } tsv;
   206   } tsv;
   207 } TString;
   207 } TString;
   208 
   208 
   209 
   209 
   210 #define getstr(ts)	cast(const char *, (ts) + 1)
   210 #define getstr(ts)  cast(const char *, (ts) + 1)
   211 #define svalue(o)       getstr(rawtsvalue(o))
   211 #define svalue(o)       getstr(rawtsvalue(o))
   212 
   212 
   213 
   213 
   214 
   214 
   215 typedef union Udata {
   215 typedef union Udata {
   252   lu_byte maxstacksize;
   252   lu_byte maxstacksize;
   253 } Proto;
   253 } Proto;
   254 
   254 
   255 
   255 
   256 /* masks for new-style vararg */
   256 /* masks for new-style vararg */
   257 #define VARARG_HASARG		1
   257 #define VARARG_HASARG       1
   258 #define VARARG_ISVARARG		2
   258 #define VARARG_ISVARARG     2
   259 #define VARARG_NEEDSARG		4
   259 #define VARARG_NEEDSARG     4
   260 
   260 
   261 
   261 
   262 typedef struct LocVar {
   262 typedef struct LocVar {
   263   TString *varname;
   263   TString *varname;
   264   int startpc;  /* first point where variable is active */
   264   int startpc;  /* first point where variable is active */
   287 /*
   287 /*
   288 ** Closures
   288 ** Closures
   289 */
   289 */
   290 
   290 
   291 #define ClosureHeader \
   291 #define ClosureHeader \
   292 	CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
   292     CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
   293 	struct Table *env
   293     struct Table *env
   294 
   294 
   295 typedef struct CClosure {
   295 typedef struct CClosure {
   296   ClosureHeader;
   296   ClosureHeader;
   297   lua_CFunction f;
   297   lua_CFunction f;
   298   TValue upvalue[1];
   298   TValue upvalue[1];
   310   CClosure c;
   310   CClosure c;
   311   LClosure l;
   311   LClosure l;
   312 } Closure;
   312 } Closure;
   313 
   313 
   314 
   314 
   315 #define iscfunction(o)	(ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
   315 #define iscfunction(o)  (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
   316 #define isLfunction(o)	(ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
   316 #define isLfunction(o)  (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
   317 
   317 
   318 
   318 
   319 /*
   319 /*
   320 ** Tables
   320 ** Tables
   321 */
   321 */
   351 
   351 
   352 /*
   352 /*
   353 ** `module' operation for hashing (size is always a power of 2)
   353 ** `module' operation for hashing (size is always a power of 2)
   354 */
   354 */
   355 #define lmod(s,size) \
   355 #define lmod(s,size) \
   356 	(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
   356     (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
   357 
   357 
   358 
   358 
   359 #define twoto(x)	(1<<(x))
   359 #define twoto(x)    (1<<(x))
   360 #define sizenode(t)	(twoto((t)->lsizenode))
   360 #define sizenode(t) (twoto((t)->lsizenode))
   361 
   361 
   362 
   362 
   363 #define luaO_nilobject		(&luaO_nilobject_)
   363 #define luaO_nilobject      (&luaO_nilobject_)
   364 
   364 
   365 LUAI_DATA const TValue luaO_nilobject_;
   365 LUAI_DATA const TValue luaO_nilobject_;
   366 
   366 
   367 #define ceillog2(x)	(luaO_log2((x)-1) + 1)
   367 #define ceillog2(x) (luaO_log2((x)-1) + 1)
   368 
   368 
   369 LUAI_FUNC int luaO_log2 (unsigned int x);
   369 LUAI_FUNC int luaO_log2 (unsigned int x);
   370 LUAI_FUNC int luaO_int2fb (unsigned int x);
   370 LUAI_FUNC int luaO_int2fb (unsigned int x);
   371 LUAI_FUNC int luaO_fb2int (int x);
   371 LUAI_FUNC int luaO_fb2int (int x);
   372 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
   372 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);