rlm@1: /* rlm@1: ** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $ rlm@1: ** Type definitions for Lua objects rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #ifndef lobject_h rlm@1: #define lobject_h rlm@1: rlm@1: rlm@1: #include rlm@1: rlm@1: rlm@1: #include "llimits.h" rlm@1: #include "lua.h" rlm@1: rlm@1: rlm@1: /* tags for values visible from Lua */ rlm@1: #define LAST_TAG LUA_TTHREAD rlm@1: rlm@1: #define NUM_TAGS (LAST_TAG+1) rlm@1: rlm@1: rlm@1: /* rlm@1: ** Extra tags for non-values rlm@1: */ rlm@1: #define LUA_TPROTO (LAST_TAG+1) rlm@1: #define LUA_TUPVAL (LAST_TAG+2) rlm@1: #define LUA_TDEADKEY (LAST_TAG+3) rlm@1: rlm@1: rlm@1: /* rlm@1: ** Union of all collectable objects rlm@1: */ rlm@1: typedef union GCObject GCObject; rlm@1: rlm@1: rlm@1: /* rlm@1: ** Common Header for all collectable objects (in macro form, to be rlm@1: ** included in other objects) rlm@1: */ rlm@1: #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked rlm@1: rlm@1: rlm@1: /* rlm@1: ** Common header in struct form rlm@1: */ rlm@1: typedef struct GCheader { rlm@1: CommonHeader; rlm@1: } GCheader; rlm@1: rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** Union of all Lua values rlm@1: */ rlm@1: typedef union { rlm@1: GCObject *gc; rlm@1: void *p; rlm@1: lua_Number n; rlm@1: int b; rlm@1: } Value; rlm@1: rlm@1: rlm@1: /* rlm@1: ** Tagged Values rlm@1: */ rlm@1: rlm@1: #define TValuefields Value value; int tt rlm@1: rlm@1: typedef struct lua_TValue { rlm@1: TValuefields; rlm@1: } TValue; rlm@1: rlm@1: rlm@1: /* Macros to test type */ rlm@1: #define ttisnil(o) (ttype(o) == LUA_TNIL) rlm@1: #define ttisnumber(o) (ttype(o) == LUA_TNUMBER) rlm@1: #define ttisstring(o) (ttype(o) == LUA_TSTRING) rlm@1: #define ttistable(o) (ttype(o) == LUA_TTABLE) rlm@1: #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) rlm@1: #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) rlm@1: #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) rlm@1: #define ttisthread(o) (ttype(o) == LUA_TTHREAD) rlm@1: #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) rlm@1: rlm@1: /* Macros to access values */ rlm@1: #define ttype(o) ((o)->tt) rlm@1: #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) rlm@1: #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) rlm@1: #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) rlm@1: #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) rlm@1: #define tsvalue(o) (&rawtsvalue(o)->tsv) rlm@1: #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) rlm@1: #define uvalue(o) (&rawuvalue(o)->uv) rlm@1: #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) rlm@1: #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) rlm@1: #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) rlm@1: #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) rlm@1: rlm@1: #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) rlm@1: rlm@1: /* rlm@1: ** for internal debug only rlm@1: */ rlm@1: #define checkconsistency(obj) \ rlm@1: lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) rlm@1: rlm@1: #define checkliveness(g,obj) \ rlm@1: lua_assert(!iscollectable(obj) || \ rlm@1: ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) rlm@1: rlm@1: rlm@1: /* Macros to set values */ rlm@1: #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) rlm@1: rlm@1: #define setnvalue(obj,x) \ rlm@1: { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } rlm@1: rlm@1: #define setpvalue(obj,x) \ rlm@1: { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } rlm@1: rlm@1: #define setbvalue(obj,x) \ rlm@1: { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } rlm@1: rlm@1: #define setsvalue(L,obj,x) \ rlm@1: { TValue *i_o=(obj); \ rlm@1: i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ rlm@1: checkliveness(G(L),i_o); } rlm@1: rlm@1: #define setuvalue(L,obj,x) \ rlm@1: { TValue *i_o=(obj); \ rlm@1: i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ rlm@1: checkliveness(G(L),i_o); } rlm@1: rlm@1: #define setthvalue(L,obj,x) \ rlm@1: { TValue *i_o=(obj); \ rlm@1: i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ rlm@1: checkliveness(G(L),i_o); } rlm@1: rlm@1: #define setclvalue(L,obj,x) \ rlm@1: { TValue *i_o=(obj); \ rlm@1: i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ rlm@1: checkliveness(G(L),i_o); } rlm@1: rlm@1: #define sethvalue(L,obj,x) \ rlm@1: { TValue *i_o=(obj); \ rlm@1: i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ rlm@1: checkliveness(G(L),i_o); } rlm@1: rlm@1: #define setptvalue(L,obj,x) \ rlm@1: { TValue *i_o=(obj); \ rlm@1: i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ rlm@1: checkliveness(G(L),i_o); } rlm@1: rlm@1: rlm@1: rlm@1: rlm@1: #define setobj(L,obj1,obj2) \ rlm@1: { const TValue *o2=(obj2); TValue *o1=(obj1); \ rlm@1: o1->value = o2->value; o1->tt=o2->tt; \ rlm@1: checkliveness(G(L),o1); } rlm@1: rlm@1: rlm@1: /* rlm@1: ** different types of sets, according to destination rlm@1: */ rlm@1: rlm@1: /* from stack to (same) stack */ rlm@1: #define setobjs2s setobj rlm@1: /* to stack (not from same stack) */ rlm@1: #define setobj2s setobj rlm@1: #define setsvalue2s setsvalue rlm@1: #define sethvalue2s sethvalue rlm@1: #define setptvalue2s setptvalue rlm@1: /* from table to same table */ rlm@1: #define setobjt2t setobj rlm@1: /* to table */ rlm@1: #define setobj2t setobj rlm@1: /* to new object */ rlm@1: #define setobj2n setobj rlm@1: #define setsvalue2n setsvalue rlm@1: rlm@1: #define setttype(obj, tt) (ttype(obj) = (tt)) rlm@1: rlm@1: rlm@1: #define iscollectable(o) (ttype(o) >= LUA_TSTRING) rlm@1: rlm@1: rlm@1: rlm@1: typedef TValue *StkId; /* index to stack elements */ rlm@1: rlm@1: rlm@1: /* rlm@1: ** String headers for string table rlm@1: */ rlm@1: typedef union TString { rlm@1: L_Umaxalign dummy; /* ensures maximum alignment for strings */ rlm@1: struct { rlm@1: CommonHeader; rlm@1: lu_byte reserved; rlm@1: unsigned int hash; rlm@1: size_t len; rlm@1: } tsv; rlm@1: } TString; rlm@1: rlm@1: rlm@1: #define getstr(ts) cast(const char *, (ts) + 1) rlm@1: #define svalue(o) getstr(rawtsvalue(o)) rlm@1: rlm@1: rlm@1: rlm@1: typedef union Udata { rlm@1: L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ rlm@1: struct { rlm@1: CommonHeader; rlm@1: struct Table *metatable; rlm@1: struct Table *env; rlm@1: size_t len; rlm@1: } uv; rlm@1: } Udata; rlm@1: rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** Function Prototypes rlm@1: */ rlm@1: typedef struct Proto { rlm@1: CommonHeader; rlm@1: TValue *k; /* constants used by the function */ rlm@1: Instruction *code; rlm@1: struct Proto **p; /* functions defined inside the function */ rlm@1: int *lineinfo; /* map from opcodes to source lines */ rlm@1: struct LocVar *locvars; /* information about local variables */ rlm@1: TString **upvalues; /* upvalue names */ rlm@1: TString *source; rlm@1: int sizeupvalues; rlm@1: int sizek; /* size of `k' */ rlm@1: int sizecode; rlm@1: int sizelineinfo; rlm@1: int sizep; /* size of `p' */ rlm@1: int sizelocvars; rlm@1: int linedefined; rlm@1: int lastlinedefined; rlm@1: GCObject *gclist; rlm@1: lu_byte nups; /* number of upvalues */ rlm@1: lu_byte numparams; rlm@1: lu_byte is_vararg; rlm@1: lu_byte maxstacksize; rlm@1: } Proto; rlm@1: rlm@1: rlm@1: /* masks for new-style vararg */ rlm@1: #define VARARG_HASARG 1 rlm@1: #define VARARG_ISVARARG 2 rlm@1: #define VARARG_NEEDSARG 4 rlm@1: rlm@1: rlm@1: typedef struct LocVar { rlm@1: TString *varname; rlm@1: int startpc; /* first point where variable is active */ rlm@1: int endpc; /* first point where variable is dead */ rlm@1: } LocVar; rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** Upvalues rlm@1: */ rlm@1: rlm@1: typedef struct UpVal { rlm@1: CommonHeader; rlm@1: TValue *v; /* points to stack or to its own value */ rlm@1: union { rlm@1: TValue value; /* the value (when closed) */ rlm@1: struct { /* double linked list (when open) */ rlm@1: struct UpVal *prev; rlm@1: struct UpVal *next; rlm@1: } l; rlm@1: } u; rlm@1: } UpVal; rlm@1: rlm@1: rlm@1: /* rlm@1: ** Closures rlm@1: */ rlm@1: rlm@1: #define ClosureHeader \ rlm@1: CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ rlm@1: struct Table *env rlm@1: rlm@1: typedef struct CClosure { rlm@1: ClosureHeader; rlm@1: lua_CFunction f; rlm@1: TValue upvalue[1]; rlm@1: } CClosure; rlm@1: rlm@1: rlm@1: typedef struct LClosure { rlm@1: ClosureHeader; rlm@1: struct Proto *p; rlm@1: UpVal *upvals[1]; rlm@1: } LClosure; rlm@1: rlm@1: rlm@1: typedef union Closure { rlm@1: CClosure c; rlm@1: LClosure l; rlm@1: } Closure; rlm@1: rlm@1: rlm@1: #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) rlm@1: #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) rlm@1: rlm@1: rlm@1: /* rlm@1: ** Tables rlm@1: */ rlm@1: rlm@1: typedef union TKey { rlm@1: struct { rlm@1: TValuefields; rlm@1: struct Node *next; /* for chaining */ rlm@1: } nk; rlm@1: TValue tvk; rlm@1: } TKey; rlm@1: rlm@1: rlm@1: typedef struct Node { rlm@1: TValue i_val; rlm@1: TKey i_key; rlm@1: } Node; rlm@1: rlm@1: rlm@1: typedef struct Table { rlm@1: CommonHeader; rlm@1: lu_byte flags; /* 1<

lsizenode)) rlm@1: rlm@1: rlm@1: #define luaO_nilobject (&luaO_nilobject_) rlm@1: rlm@1: LUAI_DATA const TValue luaO_nilobject_; rlm@1: rlm@1: #define ceillog2(x) (luaO_log2((x)-1) + 1) rlm@1: rlm@1: LUAI_FUNC int luaO_log2 (unsigned int x); rlm@1: LUAI_FUNC int luaO_int2fb (unsigned int x); rlm@1: LUAI_FUNC int luaO_fb2int (int x); rlm@1: LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); rlm@1: LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); rlm@1: LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, rlm@1: va_list argp); rlm@1: LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); rlm@1: LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); rlm@1: rlm@1: rlm@1: #endif rlm@1: