1 /* 2 ** $Id: lobject.h,v 1.1 2006/02/02 13:35:29 cavac Exp $ 3 ** Type definitions for Lua objects 4 ** See Copyright Notice in lua.h 5 */ 6 7 #ifndef lobject_h 8 #define lobject_h 9 10 11 #include "llimits.h" 12 #include "lua.h" 13 14 15 /* tags for values visible from Lua */ 16 #define NUM_TAGS LUA_TTHREAD 17 18 19 /* 20 ** Extra tags for non-values 21 */ 22 #define LUA_TPROTO (NUM_TAGS+1) 23 #define LUA_TUPVAL (NUM_TAGS+2) 24 25 26 /* 27 ** Union of all collectable objects 28 */ 29 typedef union GCObject GCObject; 30 31 32 /* 33 ** Common Header for all collectable objects (in macro form, to be 34 ** included in other objects) 35 */ 36 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked 37 38 39 /* 40 ** Common header in struct form 41 */ 42 typedef struct GCheader { 43 CommonHeader; 44 } GCheader; 45 46 47 48 49 /* 50 ** Union of all Lua values 51 */ 52 typedef union { 53 GCObject *gc; 54 void *p; 55 lua_Number n; 56 int b; 57 } Value; 58 59 60 /* 61 ** Lua values (or `tagged objects') 62 */ 63 typedef struct lua_TObject { 64 int tt; 65 Value value; 66 } TObject; 67 68 69 /* Macros to test type */ 70 #define ttisnil(o) (ttype(o) == LUA_TNIL) 71 #define ttisnumber(o) (ttype(o) == LUA_TNUMBER) 72 #define ttisstring(o) (ttype(o) == LUA_TSTRING) 73 #define ttistable(o) (ttype(o) == LUA_TTABLE) 74 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) 75 #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) 76 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) 77 #define ttisthread(o) (ttype(o) == LUA_TTHREAD) 78 #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) 79 80 /* Macros to access values */ 81 #define ttype(o) ((o)->tt) 82 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) 83 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) 84 #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) 85 #define tsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) 86 #define uvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) 87 #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) 88 #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) 89 #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) 90 #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) 91 92 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) 93 94 /* Macros to set values */ 95 #define setnvalue(obj,x) \ 96 { TObject *i_o=(obj); i_o->tt=LUA_TNUMBER; i_o->value.n=(x); } 97 98 #define chgnvalue(obj,x) \ 99 check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x)) 100 101 #define setpvalue(obj,x) \ 102 { TObject *i_o=(obj); i_o->tt=LUA_TLIGHTUSERDATA; i_o->value.p=(x); } 103 104 #define setbvalue(obj,x) \ 105 { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); } 106 107 #define setsvalue(obj,x) \ 108 { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; \ 109 i_o->value.gc=cast(GCObject *, (x)); \ 110 lua_assert(i_o->value.gc->gch.tt == LUA_TSTRING); } 111 112 #define setuvalue(obj,x) \ 113 { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; \ 114 i_o->value.gc=cast(GCObject *, (x)); \ 115 lua_assert(i_o->value.gc->gch.tt == LUA_TUSERDATA); } 116 117 #define setthvalue(obj,x) \ 118 { TObject *i_o=(obj); i_o->tt=LUA_TTHREAD; \ 119 i_o->value.gc=cast(GCObject *, (x)); \ 120 lua_assert(i_o->value.gc->gch.tt == LUA_TTHREAD); } 121 122 #define setclvalue(obj,x) \ 123 { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; \ 124 i_o->value.gc=cast(GCObject *, (x)); \ 125 lua_assert(i_o->value.gc->gch.tt == LUA_TFUNCTION); } 126 127 #define sethvalue(obj,x) \ 128 { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; \ 129 i_o->value.gc=cast(GCObject *, (x)); \ 130 lua_assert(i_o->value.gc->gch.tt == LUA_TTABLE); } 131 132 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) 133 134 135 136 /* 137 ** for internal debug only 138 */ 139 #define checkconsistency(obj) \ 140 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) 141 142 143 #define setobj(obj1,obj2) \ 144 { const TObject *o2=(obj2); TObject *o1=(obj1); \ 145 checkconsistency(o2); \ 146 o1->tt=o2->tt; o1->value = o2->value; } 147 148 149 /* 150 ** different types of sets, according to destination 151 */ 152 153 /* from stack to (same) stack */ 154 #define setobjs2s setobj 155 /* to stack (not from same stack) */ 156 #define setobj2s setobj 157 #define setsvalue2s setsvalue 158 /* from table to same table */ 159 #define setobjt2t setobj 160 /* to table */ 161 #define setobj2t setobj 162 /* to new object */ 163 #define setobj2n setobj 164 #define setsvalue2n setsvalue 165 166 #define setttype(obj, tt) (ttype(obj) = (tt)) 167 168 169 #define iscollectable(o) (ttype(o) >= LUA_TSTRING) 170 171 172 173 typedef TObject *StkId; /* index to stack elements */ 174 175 176 /* 177 ** String headers for string table 178 */ 179 typedef union TString { 180 L_Umaxalign dummy; /* ensures maximum alignment for strings */ 181 struct { 182 CommonHeader; 183 lu_byte reserved; 184 lu_hash hash; 185 size_t len; 186 } tsv; 187 } TString; 188 189 190 #define getstr(ts) cast(const char *, (ts) + 1) 191 #define svalue(o) getstr(tsvalue(o)) 192 193 194 195 typedef union Udata { 196 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ 197 struct { 198 CommonHeader; 199 struct Table *metatable; 200 size_t len; 201 } uv; 202 } Udata; 203 204 205 206 207 /* 208 ** Function Prototypes 209 */ 210 typedef struct Proto { 211 CommonHeader; 212 TObject *k; /* constants used by the function */ 213 Instruction *code; 214 struct Proto **p; /* functions defined inside the function */ 215 int *lineinfo; /* map from opcodes to source lines */ 216 struct LocVar *locvars; /* information about local variables */ 217 TString **upvalues; /* upvalue names */ 218 TString *source; 219 int sizeupvalues; 220 int sizek; /* size of `k' */ 221 int sizecode; 222 int sizelineinfo; 223 int sizep; /* size of `p' */ 224 int sizelocvars; 225 int lineDefined; 226 GCObject *gclist; 227 lu_byte nups; /* number of upvalues */ 228 lu_byte numparams; 229 lu_byte is_vararg; 230 lu_byte maxstacksize; 231 } Proto; 232 233 234 typedef struct LocVar { 235 TString *varname; 236 int startpc; /* first point where variable is active */ 237 int endpc; /* first point where variable is dead */ 238 } LocVar; 239 240 241 242 /* 243 ** Upvalues 244 */ 245 246 typedef struct UpVal { 247 CommonHeader; 248 TObject *v; /* points to stack or to its own value */ 249 TObject value; /* the value (when closed) */ 250 } UpVal; 251 252 253 /* 254 ** Closures 255 */ 256 257 #define ClosureHeader \ 258 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist 259 260 typedef struct CClosure { 261 ClosureHeader; 262 lua_CFunction f; 263 TObject upvalue[1]; 264 } CClosure; 265 266 267 typedef struct LClosure { 268 ClosureHeader; 269 struct Proto *p; 270 TObject g; /* global table for this closure */ 271 UpVal *upvals[1]; 272 } LClosure; 273 274 275 typedef union Closure { 276 CClosure c; 277 LClosure l; 278 } Closure; 279 280 281 #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) 282 #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) 283 284 285 /* 286 ** Tables 287 */ 288 289 typedef struct Node { 290 TObject i_key; 291 TObject i_val; 292 struct Node *next; /* for chaining */ 293 } Node; 294 295 296 typedef struct Table { 297 CommonHeader; 298 lu_byte flags; /* 1<<p means tagmethod(p) is not present */ 299 lu_byte lsizenode; /* log2 of size of `node' array */ 300 struct Table *metatable; 301 TObject *array; /* array part */ 302 Node *node; 303 Node *firstfree; /* this position is free; all positions after it are full */ 304 GCObject *gclist; 305 int sizearray; /* size of `array' array */ 306 } Table; 307 308 309 310 /* 311 ** `module' operation for hashing (size is always a power of 2) 312 */ 313 #define lmod(s,size) \ 314 check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))) 315 316 317 #define twoto(x) (1<<(x)) 318 #define sizenode(t) (twoto((t)->lsizenode)) 319 320 321 322 extern const TObject luaO_nilobject; 323 324 int luaO_log2 (unsigned int x); 325 int luaO_int2fb (unsigned int x); 326 #define fb2int(x) (((x) & 7) << ((x) >> 3)) 327 328 int luaO_rawequalObj (const TObject *t1, const TObject *t2); 329 int luaO_str2d (const char *s, lua_Number *result); 330 331 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); 332 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 333 void luaO_chunkid (char *out, const char *source, int len); 334 335 336 #endif 337