00001
00002
00003
00004
00005
00006
00007
00008 #ifndef lobject_h
00009 #define lobject_h
00010
00011
00012 #include <stdarg.h>
00013
00014
00015 #include <lua/llimits.h>
00016 #include <lua/lua.h>
00017
00018
00019
00020 #define LAST_TAG LUA_TTHREAD
00021
00022 #define NUM_TAGS (LAST_TAG+1)
00023
00024
00025
00026
00027
00028 #define LUA_TPROTO (LAST_TAG+1)
00029 #define LUA_TUPVAL (LAST_TAG+2)
00030 #define LUA_TDEADKEY (LAST_TAG+3)
00031
00032
00033
00034
00035
00036 typedef union GCObject GCObject;
00037
00038
00039
00040
00041
00042
00043 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
00044
00045
00046
00047
00048
00049 typedef struct GCheader {
00050 CommonHeader;
00051 } GCheader;
00052
00053
00054
00055
00056
00057
00058
00059 typedef union {
00060 GCObject *gc;
00061 void *p;
00062 lua_Number n;
00063 int b;
00064 } Value;
00065
00066
00067
00068
00069
00070
00071 #define TValuefields Value value; int tt
00072
00073 typedef struct lua_TValue {
00074 TValuefields;
00075 } TValue;
00076
00077
00078
00079 #define ttisnil(o) (ttype(o) == LUA_TNIL)
00080 #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
00081 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
00082 #define ttistable(o) (ttype(o) == LUA_TTABLE)
00083 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
00084 #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
00085 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
00086 #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
00087 #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
00088 #define ttisrotable(o) (ttype(o) == LUA_TROTABLE)
00089 #define ttislightfunction(o) (ttype(o) == LUA_TLIGHTFUNCTION)
00090
00091
00092 #define ttype(o) ((o)->tt)
00093 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
00094 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
00095 #define rvalue(o) check_exp(ttisrotable(o), (o)->value.p)
00096 #define fvalue(o) check_exp(ttislightfunction(o), (o)->value.p)
00097 #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
00098 #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
00099 #define tsvalue(o) (&rawtsvalue(o)->tsv)
00100 #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
00101 #define uvalue(o) (&rawuvalue(o)->uv)
00102 #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
00103 #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
00104 #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
00105 #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
00106
00107 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
00108
00109
00110
00111
00112 #define checkconsistency(obj) \
00113 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
00114
00115 #define checkliveness(g,obj) \
00116 lua_assert(!iscollectable(obj) || \
00117 ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
00118
00119
00120
00121 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
00122
00123 #define setnvalue(obj,x) \
00124 { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
00125
00126 #define setpvalue(obj,x) \
00127 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
00128
00129 #define setrvalue(obj,x) \
00130 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TROTABLE; }
00131
00132 #define setfvalue(obj,x) \
00133 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTFUNCTION; }
00134
00135 #define setbvalue(obj,x) \
00136 { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
00137
00138 #define setsvalue(L,obj,x) \
00139 { TValue *i_o=(obj); \
00140 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
00141 checkliveness(G(L),i_o); }
00142
00143 #define setuvalue(L,obj,x) \
00144 { TValue *i_o=(obj); \
00145 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
00146 checkliveness(G(L),i_o); }
00147
00148 #define setthvalue(L,obj,x) \
00149 { TValue *i_o=(obj); \
00150 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
00151 checkliveness(G(L),i_o); }
00152
00153 #define setclvalue(L,obj,x) \
00154 { TValue *i_o=(obj); \
00155 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
00156 checkliveness(G(L),i_o); }
00157
00158 #define sethvalue(L,obj,x) \
00159 { TValue *i_o=(obj); \
00160 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
00161 checkliveness(G(L),i_o); }
00162
00163 #define setptvalue(L,obj,x) \
00164 { TValue *i_o=(obj); \
00165 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
00166 checkliveness(G(L),i_o); }
00167
00168
00169
00170
00171 #define setobj(L,obj1,obj2) \
00172 { const TValue *o2=(obj2); TValue *o1=(obj1); \
00173 o1->value = o2->value; o1->tt=o2->tt; \
00174 checkliveness(G(L),o1); }
00175
00176
00177
00178
00179
00180
00181
00182 #define setobjs2s setobj
00183
00184 #define setobj2s setobj
00185 #define setsvalue2s setsvalue
00186 #define sethvalue2s sethvalue
00187 #define setptvalue2s setptvalue
00188
00189 #define setobjt2t setobj
00190
00191 #define setobj2t setobj
00192
00193 #define setobj2n setobj
00194 #define setsvalue2n setsvalue
00195
00196 #define setttype(obj, tt) (ttype(obj) = (tt))
00197
00198
00199 #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
00200
00201
00202
00203 typedef TValue *StkId;
00204
00205
00206
00207
00208
00209 typedef union TString {
00210 L_Umaxalign dummy;
00211 struct {
00212 CommonHeader;
00213 lu_byte reserved;
00214 unsigned int hash;
00215 size_t len;
00216 } tsv;
00217 } TString;
00218
00219
00220 #define getstr(ts) cast(const char *, (ts) + 1)
00221 #define svalue(o) getstr(rawtsvalue(o))
00222
00223
00224
00225 typedef union Udata {
00226 L_Umaxalign dummy;
00227 struct {
00228 CommonHeader;
00229 struct Table *metatable;
00230 struct Table *env;
00231 size_t len;
00232 } uv;
00233 } Udata;
00234
00235
00236
00237
00238
00239
00240
00241 typedef struct Proto {
00242 CommonHeader;
00243 TValue *k;
00244 Instruction *code;
00245 struct Proto **p;
00246 int *lineinfo;
00247 struct LocVar *locvars;
00248 TString **upvalues;
00249 TString *source;
00250 int sizeupvalues;
00251 int sizek;
00252 int sizecode;
00253 int sizelineinfo;
00254 int sizep;
00255 int sizelocvars;
00256 int linedefined;
00257 int lastlinedefined;
00258 GCObject *gclist;
00259 lu_byte nups;
00260 lu_byte numparams;
00261 lu_byte is_vararg;
00262 lu_byte maxstacksize;
00263 } Proto;
00264
00265
00266
00267 #define VARARG_HASARG 1
00268 #define VARARG_ISVARARG 2
00269 #define VARARG_NEEDSARG 4
00270
00271
00272 typedef struct LocVar {
00273 TString *varname;
00274 int startpc;
00275 int endpc;
00276 } LocVar;
00277
00278
00279
00280
00281
00282
00283
00284 typedef struct UpVal {
00285 CommonHeader;
00286 TValue *v;
00287 union {
00288 TValue value;
00289 struct {
00290 struct UpVal *prev;
00291 struct UpVal *next;
00292 } l;
00293 } u;
00294 } UpVal;
00295
00296
00297
00298
00299
00300
00301 #define ClosureHeader \
00302 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
00303 struct Table *env
00304
00305 typedef struct CClosure {
00306 ClosureHeader;
00307 lua_CFunction f;
00308 TValue upvalue[1];
00309 } CClosure;
00310
00311
00312 typedef struct LClosure {
00313 ClosureHeader;
00314 struct Proto *p;
00315 UpVal *upvals[1];
00316 } LClosure;
00317
00318
00319 typedef union Closure {
00320 CClosure c;
00321 LClosure l;
00322 } Closure;
00323
00324
00325 #define iscfunction(o) ((ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)||(ttype(o)==LUA_TLIGHTFUNCTION))
00326 #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
00327
00328
00329
00330
00331
00332
00333 typedef union TKey {
00334 struct {
00335 TValuefields;
00336 struct Node *next;
00337 } nk;
00338 TValue tvk;
00339 } TKey;
00340
00341
00342 typedef struct Node {
00343 TValue i_val;
00344 TKey i_key;
00345 } Node;
00346
00347
00348 typedef struct Table {
00349 CommonHeader;
00350 lu_byte flags;
00351 lu_byte lsizenode;
00352 struct Table *metatable;
00353 TValue *array;
00354 Node *node;
00355 Node *lastfree;
00356 GCObject *gclist;
00357 int sizearray;
00358 } Table;
00359
00360
00361
00362
00363
00364
00365 #define lmod(s,size) \
00366 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
00367
00368
00369 #define twoto(x) (1<<(x))
00370 #define sizenode(t) (twoto((t)->lsizenode))
00371
00372
00373 #define luaO_nilobject (&luaO_nilobject_)
00374
00375 LUAI_DATA const TValue luaO_nilobject_;
00376
00377 #define ceillog2(x) (luaO_log2((x)-1) + 1)
00378
00379 LUAI_FUNC int luaO_log2 (unsigned int x);
00380 LUAI_FUNC int luaO_int2fb (unsigned int x);
00381 LUAI_FUNC int luaO_fb2int (int x);
00382 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
00383 LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
00384 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
00385 va_list argp);
00386 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
00387 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
00388
00389
00390 #endif
00391